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.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 {
|
||||
|
||||
@@ -1,30 +1,39 @@
|
||||
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 { }
|
||||
|
||||
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 {
|
||||
fun tag(name: String): Boolean {
|
||||
val existing = git.tagList().call().find { it.name == name }
|
||||
if (existing != null) {
|
||||
git.tag().setName(name).call()
|
||||
@@ -34,11 +43,11 @@ class GitArchiver : Extension {
|
||||
return existing != null
|
||||
}
|
||||
|
||||
val commitHash:String
|
||||
get() {
|
||||
val id = repo.resolve(Constants.HEAD)
|
||||
return id.name
|
||||
}
|
||||
val commitHash: String
|
||||
get() {
|
||||
val id = repo.resolve(Constants.HEAD)
|
||||
return id.name
|
||||
}
|
||||
|
||||
override fun setup(program: Program) {
|
||||
val oldMetadataFunction = program.assetMetadata
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user