Fixes and move to jvmDemo (#286)

This commit is contained in:
Vechro
2023-01-15 16:27:19 +02:00
committed by GitHub
parent e27f7eb4cb
commit 47d4293a57
117 changed files with 75 additions and 310 deletions

View File

@@ -7,17 +7,17 @@ import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.register
import org.gradle.process.ExecOperations
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import java.io.File
import java.net.URLClassLoader
import javax.inject.Inject
abstract class CollectScreenshotsTask @Inject constructor() : DefaultTask() {
@get:Incremental
@get:PathSensitive(PathSensitivity.NAME_ONLY)
@get:InputDirectory
@get:PathSensitive(PathSensitivity.NAME_ONLY)
@get:SkipWhenEmpty
abstract val inputDir: DirectoryProperty
@get:Input
@@ -27,54 +27,55 @@ abstract class CollectScreenshotsTask @Inject constructor() : DefaultTask() {
abstract val outputDir: DirectoryProperty
@get:Input
@get:Optional
abstract val ignore: ListProperty<String>
@get:Inject
abstract val execOperations: ExecOperations
init {
ignore.set(emptyList())
}
@TaskAction
fun execute(inputChanges: InputChanges) {
val preloadClass = File(project.rootProject.projectDir, "buildSrc/build/classes/kotlin/preload")
require(preloadClass.exists()) {
"preload class not found: '${preloadClass.absolutePath}'"
}
inputChanges.getFileChanges(inputDir).forEach { change ->
println(change)
if (change.fileType == FileType.DIRECTORY) return@forEach
if (change.file.extension == "class") {
val klassName = change.file.nameWithoutExtension
if (klassName.dropLast(2) in ignore.get())
if (klassName.dropLast(2) in ignore.get()) {
return@forEach
val cp = (runtimeDependencies.get().map { it.toURI().toURL() } +
inputDir.get().asFile.toURI().toURL()
)
.toTypedArray()
val ucl = URLClassLoader(cp)
val klass = ucl.loadClass(klassName)
println("Collecting screenshot for ${klassName} ${klass}")
}
try {
val mainMethod = klass.getMethod("main")
project.javaexec {
this.classpath += project.files(inputDir.get().asFile, preloadClass)
this.classpath += runtimeDependencies.get()
this.mainClass.set(klassName)
this.workingDir(project.rootProject.projectDir)
jvmArgs("-DtakeScreenshot=true", "-DscreenshotPath=${outputDir.get().asFile}/$klassName.png", "-Dorg.openrndr.exceptions=JVM")
}
val cp = (runtimeDependencies.get().map { it.toURI().toURL() } + inputDir.get().asFile.toURI()
.toURL()).toTypedArray()
val ucl = URLClassLoader(cp)
val klass = ucl.loadClass(klassName)
klass.getMethod("main")
} catch (e: NoSuchMethodException) {
// silently ignore
return@forEach
}
println("Collecting screenshot for ${klassName}")
execOperations.javaexec {
this.classpath += project.files(inputDir.get().asFile, preloadClass)
this.classpath += runtimeDependencies.get()
this.mainClass.set(klassName)
this.workingDir(project.rootProject.projectDir)
this.jvmArgs(
"-DtakeScreenshot=true",
"-DscreenshotPath=${outputDir.get().asFile}/$klassName.png",
"-Dorg.openrndr.exceptions=JVM"
)
}
}
}
// this is only executed if there are changes in the inputDir
val runDemos = outputDir.get().asFile.listFiles { file: File ->
file.extension == "png"
}.map { it.nameWithoutExtension }.sorted()
}!!.map { it.nameWithoutExtension }.sorted()
val readme = File(project.projectDir, "README.md")
if (readme.exists()) {
var lines = readme.readLines().toMutableList()
@@ -84,10 +85,18 @@ abstract class CollectScreenshotsTask @Inject constructor() : DefaultTask() {
}
lines.add("<!-- __demos__ -->")
lines.add("## Demos")
// Find out if current project is MPP
val demoModuleName = if (project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")) {
"jvmDemo"
} else {
"demo"
}
for (demo in runDemos) {
val projectPath = project.projectDir.relativeTo(project.rootDir)
lines.add("### ${demo.dropLast(2)}")
lines.add("[source code](src/demo/kotlin/${demo.dropLast(2)}.kt)")
lines.add("[source code](src/${demoModuleName}/kotlin/${demo.dropLast(2)}.kt)")
lines.add("")
lines.add("![${demo}](https://raw.githubusercontent.com/openrndr/orx/media/$projectPath/images/${demo}.png)")
lines.add("")
@@ -99,18 +108,11 @@ abstract class CollectScreenshotsTask @Inject constructor() : DefaultTask() {
}
object ScreenshotsHelper {
fun KotlinJvmCompilation.collectScreenshots(config: CollectScreenshotsTask.() -> Unit): CollectScreenshotsTask {
val task = this.project.tasks.register<CollectScreenshotsTask>("collectScreenshots").get()
task.outputDir.set(project.file(project.projectDir.toString() + "/images"))
task.inputDir.set(output.classesDirs.first())
task.runtimeDependencies.set(runtimeDependencyFiles)
task.config()
task.dependsOn(this.compileKotlinTask)
return task
}
fun collectScreenshots(project: Project, sourceSet: SourceSet, config: CollectScreenshotsTask.() -> Unit): CollectScreenshotsTask {
fun collectScreenshots(
project: Project,
sourceSet: SourceSet,
config: CollectScreenshotsTask.() -> Unit
): CollectScreenshotsTask {
val task = project.tasks.register<CollectScreenshotsTask>("collectScreenshots").get()
task.outputDir.set(project.file(project.projectDir.toString() + "/images"))
task.inputDir.set(File(project.buildDir, "classes/kotlin/${sourceSet.name}"))

View File

@@ -17,10 +17,6 @@ plugins {
repositories {
mavenCentral()
maven {
// This is needed to resolve `com.github.ricardomatias:delaunator`
url = URI("https://maven.openrndr.org")
}
mavenLocal()
}
@@ -39,6 +35,7 @@ dependencies {
"demoImplementation"(libs.openrndr.application)
"demoImplementation"(libs.openrndr.extensions)
"demoRuntimeOnly"(libs.openrndr.gl3.core)
"demoRuntimeOnly"(libs.slf4j.simple)
}
kotlin {

View File

@@ -1,5 +1,6 @@
package org.openrndr.extra.convention
import CollectScreenshotsTask
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
@@ -16,10 +17,6 @@ plugins {
repositories {
mavenCentral()
maven {
// This is needed to resolve `com.github.ricardomatias:delaunator`
url = URI("https://maven.openrndr.org")
}
mavenLocal()
}
@@ -34,13 +31,15 @@ kotlin {
jvm {
jvmToolchain(libs.versions.jvmTarget.get().toInt())
compilations {
val main by getting
val main by getting
@Suppress("UNUSED_VARIABLE")
val demo by creating {
defaultSourceSet {
dependencies {
implementation(main.output.allOutputs)
}
associateWith(main)
tasks.register<CollectScreenshotsTask>("collectScreenshots") {
inputDir.set(output.classesDirs.singleFile)
runtimeDependencies.set(runtimeDependencyFiles)
outputDir.set(project.file(project.projectDir.toString() + "/images"))
dependsOn(compileKotlinTask)
}
}
}
@@ -84,6 +83,7 @@ kotlin {
implementation(libs.openrndr.application)
implementation(libs.openrndr.extensions)
runtimeOnly(libs.openrndr.gl3.core)
runtimeOnly(libs.slf4j.simple)
}
}
}