Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow additional environment variables to be set when running an app #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/Faithlife.Build/AppRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,27 @@ private static int DoRunApp(string path, AppRunnerSettings settings)
var handleOutputLine = settings.HandleOutputLine;
var handleErrorLine = settings.HandleErrorLine;

var startInfo = new ProcessStartInfo
{
FileName = commandPath,
Arguments = argsString,
WorkingDirectory = settings.WorkingDirectory,
UseShellExecute = false,
RedirectStandardOutput = handleOutputLine is not null,
RedirectStandardError = handleErrorLine is not null,
CreateNoWindow = false,
};

// add environment variables only if they're specified (to avoid touching the lazy ProcessStartInfo.Environment property)
if (settings.EnvironmentVariables.Count != 0)
{
foreach (var (name, value) in settings.EnvironmentVariables)
startInfo.Environment.Add(name, value);
}

using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = commandPath,
Arguments = argsString,
WorkingDirectory = settings.WorkingDirectory,
UseShellExecute = false,
RedirectStandardOutput = handleOutputLine is not null,
RedirectStandardError = handleErrorLine is not null,
CreateNoWindow = false,
},
StartInfo = startInfo,
};

if (!settings.NoEcho)
Expand Down
6 changes: 6 additions & 0 deletions src/Faithlife.Build/AppRunnerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public sealed class AppRunnerSettings
/// </summary>
public string? WorkingDirectory { get; set; }

/// <summary>
/// Additional environment variables to set when running the app.
/// </summary>
public IDictionary<string, string?> EnvironmentVariables { get; private set; } = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// True if the process information should not be written to standard error.
/// </summary>
Expand Down Expand Up @@ -52,6 +57,7 @@ public AppRunnerSettings Clone()
{
var clone = (AppRunnerSettings) MemberwiseClone();
clone.Arguments = clone.Arguments?.ToList();
clone.EnvironmentVariables = new Dictionary<string, string?>(clone.EnvironmentVariables, StringComparer.OrdinalIgnoreCase);
return clone;
}
}