Change GitArchiver to use the new requestAsset event

This commit is contained in:
Edwin Jakobs
2021-08-28 00:05:42 +02:00
parent bf4b016039
commit 7640246627
4 changed files with 71 additions and 22 deletions

View 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.

View File

@@ -1,11 +1,17 @@
import org.openrndr.applicationSynchronous
import org.openrndr.extensions.Screenshots
import org.openrndr.extensions.SingleScreenshot
fun main() = applicationSynchronous {
program {
if (System.getProperty("takeScreenshot") == "true") {
extend(SingleScreenshot()) {
this.outputFile = System.getProperty("screenshotPath")
}
}
val ga = extend(GitArchiver()) {
commitOnRun = false
commitOnProduceAssets = false
commitOnRun = true
commitOnRequestAssets = false
}
extend(Screenshots())
extend {

View File

@@ -1,27 +1,36 @@
package org.openrndr.extra.gitarchiver
import mu.KotlinLogging
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.lib.Constants
import org.openrndr.AssetMetadata
import org.openrndr.Extension
import org.openrndr.Program
import java.io.File
val logger = KotlinLogging.logger { }
class GitArchiver : Extension {
override var enabled: Boolean = true
var commitOnRun = false
var commitOnProduceAssets = true
var commitOnRequestAssets = true
var autoCommitMessage = "auto commit"
private val repo = FileRepository(".git")
private val git = Git(repo)
private val git = Git.open(File("."))
fun commitChanges() {
git.add().addFilepattern("src").call()
git.commit().setMessage(autoCommitMessage).call()
try {
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 {
@@ -46,15 +55,18 @@ class GitArchiver : Extension {
val oldMetadata = oldMetadataFunction()
val commitHash = this.commitHash.take(7)
program.assetProperties["git-commit-hash"] = commitHash
logger.info { "current commit hash '$commitHash'" }
AssetMetadata(oldMetadata.programName, "${oldMetadata.assetBaseName}$commitHash", program.assetProperties.mapValues { it.value })
AssetMetadata(
oldMetadata.programName,
"${oldMetadata.assetBaseName}-$commitHash",
program.assetProperties.mapValues { it.value })
}
program.produceAssets.listen {
if (commitOnProduceAssets) {
program.requestAssets.listeners.add(0, {
if (commitOnRequestAssets) {
commitChanges()
}
}
})
if (commitOnRun) {
commitChanges()

View File

@@ -2,6 +2,7 @@ package org.openrndr.extra.gui
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import mu.KotlinLogging
import org.openrndr.*
import org.openrndr.color.ColorRGBa
import org.openrndr.dialogs.getDefaultPathForContext
@@ -67,11 +68,15 @@ private fun <T : Any> setAndPersist(compartmentLabel: String, property: KMutable
state.parameterValues[property.name] = value
}
private val logger = KotlinLogging.logger { }
@Suppress("unused", "UNCHECKED_CAST")
class GUI : Extension {
private var onChangeListener: ((name: String, value: Any?) -> Unit)? = null
override var enabled = true
var listenToProduceAssetsEvent = true
var visible = true
set(value) {
if (field != value) {
@@ -101,13 +106,19 @@ class GUI : Extension {
val collapsed = ElementClass("collapsed")
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 {
if (it.key == KEY_F11) {
println("f11 pressed")
visible = !visible
}
if (it.key == KEY_LEFT_SHIFT) {