From 29b871bd8a801395ebb435167851d5ecff86546f Mon Sep 17 00:00:00 2001 From: Michael Gerbig Date: Mon, 30 Sep 2019 17:20:32 +0200 Subject: [PATCH] feat: add env var configuration property --- .releaserc.yml | 3 ++- .../semrel/SemanticReleaseConfig.groovy | 2 ++ .../illjut/gradle/semrel/SemrelPlugin.groovy | 3 ++- .../de/illjut/gradle/semrel/ExecConfig.java | 21 +++++++++++++++---- .../de/illjut/gradle/semrel/NodeExec.java | 11 +++++++++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.releaserc.yml b/.releaserc.yml index 077956f..4611ef2 100644 --- a/.releaserc.yml +++ b/.releaserc.yml @@ -18,4 +18,5 @@ gradle: - '@semantic-release/release-notes-generator' - '@semantic-release/github' config: - registry: https://registry.npmjs.org/ \ No newline at end of file + registry: https://registry.npmjs.org/ + env: \ No newline at end of file diff --git a/src/main/groovy/de/illjut/gradle/semrel/SemanticReleaseConfig.groovy b/src/main/groovy/de/illjut/gradle/semrel/SemanticReleaseConfig.groovy index 5a2caac..31ef9d7 100644 --- a/src/main/groovy/de/illjut/gradle/semrel/SemanticReleaseConfig.groovy +++ b/src/main/groovy/de/illjut/gradle/semrel/SemanticReleaseConfig.groovy @@ -12,6 +12,7 @@ class SemanticReleaseConfig { private final boolean downloadNode private final boolean autoDetectNode private final List packages + private final envVars private final distUrl private final npmConfig @@ -30,6 +31,7 @@ class SemanticReleaseConfig { this.autoDetectNode = config.gradle?.node?.detect ?: false this.nodeVersion = config.gradle?.node?.version ?: '10.16.3' this.npmConfig = config.gradle?.config; + this.envVars = config.gradle?.env; if (config.gradle?.node?.packages != null) { this.packages = config.gradle.node.packages diff --git a/src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy b/src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy index 8efe830..e89d559 100644 --- a/src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy +++ b/src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy @@ -28,7 +28,8 @@ class SemrelPlugin implements Plugin { def config = new SemanticReleaseConfig(project.rootProject.file(".releaserc.yml")) def execConfig = ExecConfig.instance() .registry(config.npmConfig?.registry) - .strictSsl(config.npmConfig?.strictSsl); + .strictSsl(config.npmConfig?.strictSsl) + .envVars(config.envVars); def nodeVersion = config.nodeVersion; diff --git a/src/main/java/de/illjut/gradle/semrel/ExecConfig.java b/src/main/java/de/illjut/gradle/semrel/ExecConfig.java index a79e0bd..5697bfc 100644 --- a/src/main/java/de/illjut/gradle/semrel/ExecConfig.java +++ b/src/main/java/de/illjut/gradle/semrel/ExecConfig.java @@ -7,6 +7,7 @@ public class ExecConfig { private Optional registry = Optional.empty(); private Optional strictSsl = Optional.empty(); + private Optional> envVars = Optional.empty(); public static ExecConfig instance() { return new ExecConfig(); @@ -30,6 +31,12 @@ public ExecConfig strictSsl(Boolean strictSsl) { return this; } + public ExecConfig envVars(Map envVars) { + this.envVars = Optional.ofNullable(envVars); + + return this; + } + private void putToMap(Map map, String key, Optional value) { if (value.isPresent()) { map.put(key, value.get()); @@ -37,12 +44,18 @@ private void putToMap(Map map, String key, Optional valu } public Map buildEnvVarMap() { - Map envVars = new HashMap(); + Map result = new HashMap(); + + this.putToMap(result, "npm_config_registry", this.registry); + this.putToMap(result, "npm_config_strict_ssl", this.strictSsl); - this.putToMap(envVars, "npm_config_registry", this.registry); - this.putToMap(envVars, "npm_config_strict_ssl", this.strictSsl); + if (this.envVars.isPresent()) { + this.envVars.get().forEach((key, value) -> { + result.put(key, String.valueOf(value)); + }); + } - return envVars; + return result; } } \ No newline at end of file diff --git a/src/main/java/de/illjut/gradle/semrel/NodeExec.java b/src/main/java/de/illjut/gradle/semrel/NodeExec.java index 99d8bc2..9e22085 100644 --- a/src/main/java/de/illjut/gradle/semrel/NodeExec.java +++ b/src/main/java/de/illjut/gradle/semrel/NodeExec.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -76,7 +77,15 @@ private ProcessResult exec(List command, File workDir, File nodePathOver if (pathVar != null) processBuilder.environment().put(pathVar, pathValue); if(this.execConfig != null) { - processBuilder.environment().putAll(this.execConfig.buildEnvVarMap()); + Map envMap = this.execConfig.buildEnvVarMap(); + if (this.logger.isInfoEnabled()) { + this.logger.info("running node invocation with env vars:"); + envMap.forEach((value, key) -> { + this.logger.info(" " + value + ": " + key); + }); + } + + processBuilder.environment().putAll(envMap); } Process proc = processBuilder.start();