Skip to content

Commit

Permalink
Allow additional environment variables to be set when running an app. (
Browse files Browse the repository at this point in the history
…#67)

This is typed as a non-null get-only property so users can use collection initialization syntax:

new AppRunnerSettings
{
	EnvironmentVariables =
	{
		{ "name", "value" },
	},
}
  • Loading branch information
bgrainger authored Jul 12, 2024
1 parent 063d3ab commit c7dca80
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
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;
}
}

0 comments on commit c7dca80

Please sign in to comment.