Skip to content

Commit

Permalink
feat: add env var configuration property
Browse files Browse the repository at this point in the history
  • Loading branch information
error418 committed Sep 30, 2019
1 parent e7c6aea commit 29b871b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .releaserc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ gradle:
- '@semantic-release/release-notes-generator'
- '@semantic-release/github'
config:
registry: https://registry.npmjs.org/
registry: https://registry.npmjs.org/
env:
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class SemanticReleaseConfig {
private final boolean downloadNode
private final boolean autoDetectNode
private final List<String> packages
private final envVars
private final distUrl
private final npmConfig

Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/main/groovy/de/illjut/gradle/semrel/SemrelPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class SemrelPlugin implements Plugin<Project> {
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;

Expand Down
21 changes: 17 additions & 4 deletions src/main/java/de/illjut/gradle/semrel/ExecConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class ExecConfig {
private Optional<String> registry = Optional.empty();
private Optional<String> strictSsl = Optional.empty();
private Optional<Map<String, Object>> envVars = Optional.empty();

public static ExecConfig instance() {
return new ExecConfig();
Expand All @@ -30,19 +31,31 @@ public ExecConfig strictSsl(Boolean strictSsl) {
return this;
}

public ExecConfig envVars(Map<String, Object> envVars) {
this.envVars = Optional.ofNullable(envVars);

return this;
}

private void putToMap(Map<String, String> map, String key, Optional<String> value) {
if (value.isPresent()) {
map.put(key, value.get());
}
}

public Map<String, String> buildEnvVarMap() {
Map<String, String> envVars = new HashMap<String, String>();
Map<String, String> result = new HashMap<String, String>();

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;
}

}
11 changes: 10 additions & 1 deletion src/main/java/de/illjut/gradle/semrel/NodeExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,7 +77,15 @@ private ProcessResult exec(List<String> command, File workDir, File nodePathOver
if (pathVar != null) processBuilder.environment().put(pathVar, pathValue);

if(this.execConfig != null) {
processBuilder.environment().putAll(this.execConfig.buildEnvVarMap());
Map<String, String> 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();
Expand Down

0 comments on commit 29b871b

Please sign in to comment.