Change GitArchiver to use the new requestAsset event
This commit is contained in:
20
orx-jvm/orx-git-archiver/README.md
Normal file
20
orx-jvm/orx-git-archiver/README.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# orx-git-archiver
|
||||||
|
|
||||||
|
An extension that hooks into `Program.requestAssets` to commit
|
||||||
|
changed code to Git and provide filenames based on the commit hash.
|
||||||
|
|
||||||
|
## How do I use it?
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
application {
|
||||||
|
program {
|
||||||
|
extend(GitArchiver()) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
extend(Screenshots())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
Now when a screenshot is taken, first all uncommitted code is committed to git.
|
||||||
|
The screenshot is saved with the first 7 characters of the commit hash in the filename.
|
||||||
@@ -1,11 +1,17 @@
|
|||||||
import org.openrndr.applicationSynchronous
|
import org.openrndr.applicationSynchronous
|
||||||
import org.openrndr.extensions.Screenshots
|
import org.openrndr.extensions.Screenshots
|
||||||
|
import org.openrndr.extensions.SingleScreenshot
|
||||||
|
|
||||||
fun main() = applicationSynchronous {
|
fun main() = applicationSynchronous {
|
||||||
program {
|
program {
|
||||||
|
if (System.getProperty("takeScreenshot") == "true") {
|
||||||
|
extend(SingleScreenshot()) {
|
||||||
|
this.outputFile = System.getProperty("screenshotPath")
|
||||||
|
}
|
||||||
|
}
|
||||||
val ga = extend(GitArchiver()) {
|
val ga = extend(GitArchiver()) {
|
||||||
commitOnRun = false
|
commitOnRun = true
|
||||||
commitOnProduceAssets = false
|
commitOnRequestAssets = false
|
||||||
}
|
}
|
||||||
extend(Screenshots())
|
extend(Screenshots())
|
||||||
extend {
|
extend {
|
||||||
|
|||||||
@@ -1,30 +1,39 @@
|
|||||||
|
package org.openrndr.extra.gitarchiver
|
||||||
|
|
||||||
import mu.KotlinLogging
|
import mu.KotlinLogging
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
|
import org.eclipse.jgit.api.errors.EmptyCommitException
|
||||||
import org.eclipse.jgit.internal.storage.file.FileRepository
|
import org.eclipse.jgit.internal.storage.file.FileRepository
|
||||||
import org.eclipse.jgit.lib.Constants
|
import org.eclipse.jgit.lib.Constants
|
||||||
import org.openrndr.AssetMetadata
|
import org.openrndr.AssetMetadata
|
||||||
import org.openrndr.Extension
|
import org.openrndr.Extension
|
||||||
import org.openrndr.Program
|
import org.openrndr.Program
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
val logger = KotlinLogging.logger { }
|
||||||
|
|
||||||
val logger = KotlinLogging.logger { }
|
|
||||||
class GitArchiver : Extension {
|
class GitArchiver : Extension {
|
||||||
override var enabled: Boolean = true
|
override var enabled: Boolean = true
|
||||||
|
|
||||||
var commitOnRun = false
|
var commitOnRun = false
|
||||||
var commitOnProduceAssets = true
|
var commitOnRequestAssets = true
|
||||||
|
|
||||||
var autoCommitMessage = "auto commit"
|
var autoCommitMessage = "auto commit"
|
||||||
|
|
||||||
private val repo = FileRepository(".git")
|
private val repo = FileRepository(".git")
|
||||||
private val git = Git(repo)
|
private val git = Git.open(File("."))
|
||||||
|
|
||||||
|
|
||||||
fun commitChanges() {
|
fun commitChanges() {
|
||||||
git.add().addFilepattern("src").call()
|
try {
|
||||||
git.commit().setMessage(autoCommitMessage).call()
|
git.commit().setAll(true).setAllowEmpty(false).setMessage(autoCommitMessage).call()
|
||||||
|
logger.info { "git repository is now at ${commitHash.take(7)}" }
|
||||||
|
} catch (e: EmptyCommitException) {
|
||||||
|
logger.info { "no changes" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tag(name: String) : Boolean {
|
fun tag(name: String): Boolean {
|
||||||
val existing = git.tagList().call().find { it.name == name }
|
val existing = git.tagList().call().find { it.name == name }
|
||||||
if (existing != null) {
|
if (existing != null) {
|
||||||
git.tag().setName(name).call()
|
git.tag().setName(name).call()
|
||||||
@@ -34,11 +43,11 @@ class GitArchiver : Extension {
|
|||||||
return existing != null
|
return existing != null
|
||||||
}
|
}
|
||||||
|
|
||||||
val commitHash:String
|
val commitHash: String
|
||||||
get() {
|
get() {
|
||||||
val id = repo.resolve(Constants.HEAD)
|
val id = repo.resolve(Constants.HEAD)
|
||||||
return id.name
|
return id.name
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setup(program: Program) {
|
override fun setup(program: Program) {
|
||||||
val oldMetadataFunction = program.assetMetadata
|
val oldMetadataFunction = program.assetMetadata
|
||||||
@@ -46,15 +55,18 @@ class GitArchiver : Extension {
|
|||||||
val oldMetadata = oldMetadataFunction()
|
val oldMetadata = oldMetadataFunction()
|
||||||
val commitHash = this.commitHash.take(7)
|
val commitHash = this.commitHash.take(7)
|
||||||
program.assetProperties["git-commit-hash"] = commitHash
|
program.assetProperties["git-commit-hash"] = commitHash
|
||||||
logger.info { "current commit hash '$commitHash'" }
|
AssetMetadata(
|
||||||
AssetMetadata(oldMetadata.programName, "${oldMetadata.assetBaseName}$commitHash", program.assetProperties.mapValues { it.value })
|
oldMetadata.programName,
|
||||||
|
"${oldMetadata.assetBaseName}-$commitHash",
|
||||||
|
program.assetProperties.mapValues { it.value })
|
||||||
}
|
}
|
||||||
|
|
||||||
program.produceAssets.listen {
|
program.requestAssets.listeners.add(0, {
|
||||||
if (commitOnProduceAssets) {
|
if (commitOnRequestAssets) {
|
||||||
commitChanges()
|
commitChanges()
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
|
|
||||||
if (commitOnRun) {
|
if (commitOnRun) {
|
||||||
commitChanges()
|
commitChanges()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.openrndr.extra.gui
|
|||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.google.gson.reflect.TypeToken
|
import com.google.gson.reflect.TypeToken
|
||||||
|
import mu.KotlinLogging
|
||||||
import org.openrndr.*
|
import org.openrndr.*
|
||||||
import org.openrndr.color.ColorRGBa
|
import org.openrndr.color.ColorRGBa
|
||||||
import org.openrndr.dialogs.getDefaultPathForContext
|
import org.openrndr.dialogs.getDefaultPathForContext
|
||||||
@@ -67,11 +68,15 @@ private fun <T : Any> setAndPersist(compartmentLabel: String, property: KMutable
|
|||||||
state.parameterValues[property.name] = value
|
state.parameterValues[property.name] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val logger = KotlinLogging.logger { }
|
||||||
|
|
||||||
@Suppress("unused", "UNCHECKED_CAST")
|
@Suppress("unused", "UNCHECKED_CAST")
|
||||||
class GUI : Extension {
|
class GUI : Extension {
|
||||||
private var onChangeListener: ((name: String, value: Any?) -> Unit)? = null
|
private var onChangeListener: ((name: String, value: Any?) -> Unit)? = null
|
||||||
override var enabled = true
|
override var enabled = true
|
||||||
|
|
||||||
|
var listenToProduceAssetsEvent = true
|
||||||
|
|
||||||
var visible = true
|
var visible = true
|
||||||
set(value) {
|
set(value) {
|
||||||
if (field != value) {
|
if (field != value) {
|
||||||
@@ -101,13 +106,19 @@ class GUI : Extension {
|
|||||||
val collapsed = ElementClass("collapsed")
|
val collapsed = ElementClass("collapsed")
|
||||||
|
|
||||||
override fun setup(program: Program) {
|
override fun setup(program: Program) {
|
||||||
|
program.produceAssets.listen {
|
||||||
|
if (listenToProduceAssetsEvent) {
|
||||||
|
val targetDir = File("gui-parameters")
|
||||||
|
if (!targetDir.exists()) { targetDir.mkdir() }
|
||||||
|
val targetFile = File(targetDir, "${it.assetMetadata.assetBaseName}.json")
|
||||||
|
logger.info("Saving parameters to '${targetFile.absolutePath}")
|
||||||
|
saveParameters(targetFile)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
program.keyboard.keyDown.listen {
|
program.keyboard.keyDown.listen {
|
||||||
if (it.key == KEY_F11) {
|
if (it.key == KEY_F11) {
|
||||||
println("f11 pressed")
|
|
||||||
visible = !visible
|
visible = !visible
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (it.key == KEY_LEFT_SHIFT) {
|
if (it.key == KEY_LEFT_SHIFT) {
|
||||||
|
|||||||
Reference in New Issue
Block a user