Skip to content

Commit

Permalink
Implements append logic for env-vars
Browse files Browse the repository at this point in the history
Get value from shell, as this will validate if the variable is being
overwritten in some place. Making the information more concise with what
the user is expecting.

- 'AppendToUserEnv' append to the start to give the new value the
highest priority.
  • Loading branch information
edassis committed Dec 14, 2023
1 parent 8f45dd5 commit 66a665d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 29 deletions.
49 changes: 23 additions & 26 deletions GodotEnv/src/common/clients/EnvironmentVariableClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task AppendToUserEnv(string name, string value) {
var tokens = currentValue.Split(';').ToList();
tokens = tokens.Where(t => !t.Contains(value, StringComparison.OrdinalIgnoreCase)).ToList();

tokens.Add(value);
tokens.Insert(0, value);
Environment.SetEnvironmentVariable(
name, filter(tokens), EnvironmentVariableTarget.User
);
Expand All @@ -79,18 +79,17 @@ public async Task AppendToUserEnv(string name, string value) {
name, EnvironmentVariableTarget.User
) ?? "";
break;
// TODO: Treat case where exported var is a composite value
// In case the path assigned to variable changes, the previous one will remain in the file but with lower priority.
case OSType.MacOS:
var zshRcPath = FileClient.Combine(FileClient.UserDirectory, ".zshrc");
FileClient.AddLinesToFileIfNotPresent(
zshRcPath, $"export {name}=\"{value}\""
zshRcPath, $"export {name}=\"{value}:${name}\""
);
break;
case OSType.Linux:
var bashRcPath =
FileClient.Combine(FileClient.UserDirectory, ".bashrc");
var bashRcPath = FileClient.Combine(FileClient.UserDirectory, ".bashrc");
FileClient.AddLinesToFileIfNotPresent(
bashRcPath, $"export {name}=\"{value}\""
bashRcPath, $"export {name}=\"{value}:${name}\""
);
break;
case OSType.Unknown:
Expand All @@ -100,34 +99,32 @@ public async Task AppendToUserEnv(string name, string value) {
}

public string GetUserEnv(string name) {
var shell = Computer.CreateShell(FileClient.AppDataDirectory);

switch (FileClient.OS) {
case OSType.Windows:
return Environment.GetEnvironmentVariable(
name, EnvironmentVariableTarget.User
) ?? "";
case OSType.MacOS:
var zshRcPath = FileClient.Combine(FileClient.UserDirectory, ".zshrc");
return ExtractBashEnvVarFromLine(
FileClient.FindLineBeginningWithPrefix(
zshRcPath, $"export {name}="
),
name
);
case OSType.Linux:
var bashRcPath =
FileClient.Combine(FileClient.UserDirectory, ".bashrc");
return ExtractBashEnvVarFromLine(
FileClient.FindLineBeginningWithPrefix(
bashRcPath, $"export {name}="
),
name
);
// It's important to use the user's default shell to get the env-var value here.
// Note the use of the '-i' flag to initialize an interactive shell. Properly loading '<shell>'rc file.
case OSType.MacOS: {
var task = shell.Run(
"zsh", ["-ic", $"echo ${name}"]
);
task.Wait();
return task.Result.StandardOutput;
}
case OSType.Linux: {
var task = shell.Run(
"bash", ["-ic", $"echo ${name}"]
);
task.Wait();
return task.Result.StandardOutput;
}
case OSType.Unknown:
default:
return "";
}
}

private static string ExtractBashEnvVarFromLine(string line, string name) =>
line.Replace($"export {name}=", "").Replace("\"", "").Trim();
}
17 changes: 14 additions & 3 deletions GodotEnv/src/features/godot/domain/GodotRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public IEnvironmentVariableClient EnvironmentVariableClient {
FileClient.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_CACHE_PATH
);

public string GodotBinPath => FileClient.Combine(
FileClient.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH
);

public string GodotSymlinkPath => FileClient.Combine(
FileClient.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH, Defaults.GODOT_BIN_NAME
);
Expand All @@ -158,7 +162,7 @@ public IEnvironmentVariableClient EnvironmentVariableClient {
);

public string GodotSharpSymlinkPath => FileClient.Combine(
FileClient.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_SHARP_PATH
FileClient.AppDataDirectory, Defaults.GODOT_PATH, Defaults.GODOT_BIN_PATH, Defaults.GODOT_SHARP_PATH
);

public string GodotSymlinkTarget => FileClient.FileSymlinkTarget(
Expand Down Expand Up @@ -360,6 +364,14 @@ await ZipClient.ExtractToDirectory(
public async Task UpdateGodotSymlink(
GodotInstallation installation, ILog log
) {
if (FileClient.IsFileSymlink(GodotBinPath)) { // Removes old 'bin' file-symlink.
await FileClient.DeleteFile(GodotBinPath);
}

if (!FileClient.DirectoryExists(GodotBinPath)) {
FileClient.CreateDirectory(GodotBinPath);
}

// Create or update the symlink to the new version of Godot.
await FileClient.CreateSymlink(GodotSymlinkPath, installation.ExecutionPath);
await CreateShortcuts(installation);
Expand Down Expand Up @@ -499,8 +511,7 @@ public async Task AddOrUpdateGodotEnvVariable(ILog log) {
log.Print("");
}

public string GetGodotEnvVariable() =>
EnvironmentVariableClient.GetUserEnv(Defaults.GODOT_ENV_VAR_NAME);
public string GetGodotEnvVariable() => EnvironmentVariableClient.GetUserEnv(Defaults.GODOT_ENV_VAR_NAME);

public List<GodotInstallation> GetInstallationsList() {
var installations = new List<GodotInstallation>();
Expand Down

0 comments on commit 66a665d

Please sign in to comment.