From 0b6164885996e06198c96f7ce5ff5d3888ecff7d Mon Sep 17 00:00:00 2001 From: LexManos Date: Sat, 4 Nov 2023 14:34:09 -0700 Subject: [PATCH] Add support for detecting values from project properties, and global prefix properties. --- README.md | 20 ++++++++--- .../GradleJarSignerExtension.java | 35 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 821177f..c34d43c 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,23 @@ You can also configure the task itself to specify any of the information set in ### Github Secrets A large motivation for this was wanting to use Github Actions and still be able to sign my built files. Github does not allow you to have files as [secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets) just strings and the workarounds I found involved committing a encrypted form of your keystore to your repo and then decrypting it during an Action. Instead I decided to allow you to specify the keystore file as a base64 encoded string which can be used as a Secret. -You can either manually configure the information by pulling the secrets yourself, or I added a simple helper `jarSigner.fromEnvironmentVariables()` which does the following: +You can either manually configure the information by pulling the secrets yourself, or I added a simple helper `jarSigner.autoDetect()` which which search the following locations in order: + + if (prefix != null) { + project.findProperty(prefix + '.' + prop) + System.getenv(prefix + '.' + prop) + } + project.findProperty(prop) + System.getenv(prop) +`prefix` defaults to `project.name` you can override by calling `jarSigner.autoDetect('prefix')` + +For the following properties: jarSigner { - alias = System.env('SIGN_KEY_ALIAS') - keyPass = System.env('SIGN_KEY_PASSWORD') - storePass = System.env('SIGN_KEYSTORE_PASSWORD') - keyStoreData = System.env('SIGN_KEYSTORE_DATA') + alias = 'SIGN_KEY_ALIAS' + keyPass = 'SIGN_KEY_PASSWORD' + storePass = 'SIGN_KEYSTORE_PASSWORD' + keyStoreData = 'SIGN_KEYSTORE_DATA' } ### Conclusion diff --git a/src/main/java/net/minecraftforge/gradlejarsigner/GradleJarSignerExtension.java b/src/main/java/net/minecraftforge/gradlejarsigner/GradleJarSignerExtension.java index 53e5826..cf57ebf 100644 --- a/src/main/java/net/minecraftforge/gradlejarsigner/GradleJarSignerExtension.java +++ b/src/main/java/net/minecraftforge/gradlejarsigner/GradleJarSignerExtension.java @@ -33,10 +33,22 @@ public SignTask sign(Zip task, Closure cfg) { } public void fromEnvironmentVariables() { - set("SIGN_KEY_ALIAS", this::setAlias); - set("SIGN_KEY_PASSWORD", this::setKeyPass); - set("SIGN_KEYSTORE_PASSWORD", this::setStorePass); - set("SIGN_KEYSTORE_DATA", this::setKeyStoreData); + fromEnvironmentVariables(project.getName()); + } + + public void fromEnvironmentVariables(String prefix) { + autoDetect(prefix); + } + + public void autoDetect() { + autoDetect(project.getName()); + } + + public void autoDetect(String prefix) { + set(prefix, "SIGN_KEY_ALIAS", this::setAlias); + set(prefix, "SIGN_KEY_PASSWORD", this::setKeyPass); + set(prefix, "SIGN_KEYSTORE_PASSWORD", this::setStorePass); + set(prefix, "SIGN_KEYSTORE_DATA", this::setKeyStoreData); } public void setAlias(String value) { @@ -77,8 +89,19 @@ void fill(SignTask task) { task.setKeyStoreFile(this.keyStoreFile); } - private void set(String key, Consumer prop) { - String data = System.getenv(key); + private void set(String prefix, String key, Consumer prop) { + String data = null; + if (prefix != null) { + data = (String)project.findProperty(prefix + '.' + key); + if (data == null) + data = System.getenv(prefix + '.' + key); + } + + if (data == null) + data = (String)project.findProperty(key); + if (data == null) + data = System.getenv(key); + if (data != null) prop.accept(data); }