Skip to content

Commit

Permalink
Substitute parent process env vars in IDEA - wasn't working before
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
ashald committed Apr 25, 2019
1 parent 4f3f0dd commit 6cf3cb0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning].

## Unreleased

### Fixed

- Substitute parent process env vars in IDEA - wasn't working before ([#81])

## 3.1.0 - 2018-11-28

### Added
Expand Down Expand Up @@ -92,7 +98,8 @@ This project adheres to [Semantic Versioning].
[#68]: https://github.com/Ashald/EnvFile/issues/68
[#70]: https://github.com/Ashald/EnvFile/issues/70
[#72]: https://github.com/Ashald/EnvFile/issues/72
[#81]: https://github.com/Ashald/EnvFile/issues/81

[Keep a CHANGELOG]: http://keepachangelog.com
[Semantic Versioning]: http://semver.org/
[StringSubstitutor]: https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringSubstitutor.html
[StringSubstitutor]: https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringSubstitutor.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.commons.text.StringSubstitutor;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -47,4 +48,9 @@ private String renderValue(String template, @NotNull Map<String, String> context

return stage2;
}

@Override
public boolean isFileLocationValid(File file) {
return file != null && file.exists();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Map;

Expand All @@ -11,4 +12,6 @@ public interface EnvVarsProvider {
@NotNull Map<String, String> process(@NotNull Map<String, String> runConfigEnv, String path, @NotNull Map<String, String> aggregatedEnv) throws EnvFileErrorException, IOException;

boolean isEditable();

boolean isFileLocationValid(File file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.ashald.envfile.AbstractEnvVarsProvider;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -23,4 +24,8 @@ public boolean isEditable() {
return false;
}

@Override
public boolean isFileLocationValid(File file) {
return true; // no file needed
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Map<String, String> process(Map<String, String> runConfigEnv, Map<String,

if (isEnabled() && parser != null) {
File file = getFile();
if (ignoreMissing && (file == null || !file.exists())) {
if (!parser.isFileLocationValid(file) && ignoreMissing) {
return aggregatedEnv;
} else {
return parser.process(runConfigEnv, file == null ? null : file.getPath(), aggregatedEnv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.intellij.execution.ExecutionException;
import com.intellij.execution.RunConfigurationExtension;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.RunConfigurationBase;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.projectRoots.JdkUtil;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import net.ashald.envfile.platform.ui.EnvFileConfigurationEditor;
Expand Down Expand Up @@ -58,7 +60,15 @@ protected void validateConfiguration(@NotNull RunConfigurationBase configuration
*/
@Override
public <T extends RunConfigurationBase> void updateJavaParameters(T configuration, JavaParameters params, RunnerSettings runnerSettings) throws ExecutionException {
Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, new HashMap<>(params.getEnv()));
// Borrowed from com.intellij.openapi.projectRoots.JdkUtil
Map<String, String> sourceEnv = new GeneralCommandLine()
.withEnvironment(params.getEnv())
.withParentEnvironmentType(
params.isPassParentEnvs() ? GeneralCommandLine.ParentEnvironmentType.CONSOLE : GeneralCommandLine.ParentEnvironmentType.NONE
)
.getEffectiveEnvironment();

Map<String, String> newEnv = EnvFileConfigurationEditor.collectEnv(configuration, new HashMap<>(sourceEnv));
// there is a chance that env is an immutable map,
// that is why it is safer to replace it instead of updating it
params.setEnv(newEnv);
Expand Down

0 comments on commit 6cf3cb0

Please sign in to comment.