diff --git a/DecSm.Atom.Analyzers.Tests/DecSm.Atom.Analyzers.Tests.csproj b/DecSm.Atom.Analyzers.Tests/DecSm.Atom.Analyzers.Tests.csproj index 8bda884b..7b8bb2ed 100644 --- a/DecSm.Atom.Analyzers.Tests/DecSm.Atom.Analyzers.Tests.csproj +++ b/DecSm.Atom.Analyzers.Tests/DecSm.Atom.Analyzers.Tests.csproj @@ -13,7 +13,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/DecSm.Atom.Module.AzureStorage/DecSm.Atom.Module.AzureStorage.csproj b/DecSm.Atom.Module.AzureStorage/DecSm.Atom.Module.AzureStorage.csproj index 4ea74368..246f82af 100644 --- a/DecSm.Atom.Module.AzureStorage/DecSm.Atom.Module.AzureStorage.csproj +++ b/DecSm.Atom.Module.AzureStorage/DecSm.Atom.Module.AzureStorage.csproj @@ -6,7 +6,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/DecSm.Atom.Module.GithubWorkflows.Tests/DecSm.Atom.Module.GithubWorkflows.Tests.csproj b/DecSm.Atom.Module.GithubWorkflows.Tests/DecSm.Atom.Module.GithubWorkflows.Tests.csproj index d54b04e2..f5434f36 100644 --- a/DecSm.Atom.Module.GithubWorkflows.Tests/DecSm.Atom.Module.GithubWorkflows.Tests.csproj +++ b/DecSm.Atom.Module.GithubWorkflows.Tests/DecSm.Atom.Module.GithubWorkflows.Tests.csproj @@ -19,7 +19,7 @@ - + diff --git a/DecSm.Atom.SourceGenerators.Tests/DecSm.Atom.SourceGenerators.Tests.csproj b/DecSm.Atom.SourceGenerators.Tests/DecSm.Atom.SourceGenerators.Tests.csproj index 83f2548a..96080fe6 100644 --- a/DecSm.Atom.SourceGenerators.Tests/DecSm.Atom.SourceGenerators.Tests.csproj +++ b/DecSm.Atom.SourceGenerators.Tests/DecSm.Atom.SourceGenerators.Tests.csproj @@ -18,7 +18,7 @@ - + diff --git a/DecSm.Atom.Tests/DecSm.Atom.Tests.csproj b/DecSm.Atom.Tests/DecSm.Atom.Tests.csproj index b2f7ada2..b9b97da7 100644 --- a/DecSm.Atom.Tests/DecSm.Atom.Tests.csproj +++ b/DecSm.Atom.Tests/DecSm.Atom.Tests.csproj @@ -19,7 +19,7 @@ - + diff --git a/DecSm.Atom/Process/ProcessRunOptions.cs b/DecSm.Atom/Process/ProcessRunOptions.cs index ee171fd3..90e170c5 100644 --- a/DecSm.Atom/Process/ProcessRunOptions.cs +++ b/DecSm.Atom/Process/ProcessRunOptions.cs @@ -200,4 +200,17 @@ public ProcessRunOptions(string Name, string[] Args) : this(Name, string.Join(" /// /// public bool AllowFailedResult { get; init; } + + /// + /// Gets or sets the environment variables to be used by the process during execution. + /// + /// + /// A dictionary where keys represent the names of the environment variables and values represent their corresponding values. + /// A value of null for any variable indicates that it should be removed from the environment. + /// + /// + /// These environment variables are passed to the process when it is executed, potentially overriding inherited variables + /// from the current environment. If not set, the process will inherit the current environment's variables by default. + /// + public Dictionary EnvironmentVariables { get; init; } = []; } diff --git a/DecSm.Atom/Process/ProcessRunner.cs b/DecSm.Atom/Process/ProcessRunner.cs index d566e1e3..7a9d484f 100644 --- a/DecSm.Atom/Process/ProcessRunner.cs +++ b/DecSm.Atom/Process/ProcessRunner.cs @@ -150,14 +150,38 @@ public sealed class ProcessRunner(ILogger logger) : IProcessRunne /// public ProcessRunResult Run(ProcessRunOptions options) { - if (options.WorkingDirectory is { Length: > 0 }) - logger.Log(options.InvocationLogLevel, - "Run: {Name} {Args} in {WorkingDirectory}", - options.Name, - options.Args, - options.WorkingDirectory); - else - logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args); + switch (options) + { + case { WorkingDirectory.Length: > 0, EnvironmentVariables.Count: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} in {WorkingDirectory} with env {EnvironmentVariables}", + options.Name, + options.Args, + options.WorkingDirectory, + string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}"))); + + break; + case { WorkingDirectory.Length: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} in {WorkingDirectory}", + options.Name, + options.Args, + options.WorkingDirectory); + + break; + case { EnvironmentVariables.Count: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} with env {EnvironmentVariables}", + options.Name, + options.Args, + string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}"))); + + break; + default: + logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args); + + break; + } using var process = new System.Diagnostics.Process(); @@ -170,6 +194,9 @@ public ProcessRunResult Run(ProcessRunOptions options) RedirectStandardError = true, }; + foreach (var environmentVariable in options.EnvironmentVariables) + process.StartInfo.Environment[environmentVariable.Key] = environmentVariable.Value; + var outputBuilder = new StringBuilder(); var errorBuilder = new StringBuilder(); @@ -278,14 +305,38 @@ void OnProcessOnOutputDataReceived(object _, DataReceivedEventArgs e) /// public async Task RunAsync(ProcessRunOptions options, CancellationToken cancellationToken = default) { - if (options.WorkingDirectory is { Length: > 0 }) - logger.Log(options.InvocationLogLevel, - "Run: {Name} {Args} in {WorkingDirectory}", - options.Name, - options.Args, - options.WorkingDirectory); - else - logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args); + switch (options) + { + case { WorkingDirectory.Length: > 0, EnvironmentVariables.Count: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} in {WorkingDirectory} with env {EnvironmentVariables}", + options.Name, + options.Args, + options.WorkingDirectory, + string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}"))); + + break; + case { WorkingDirectory.Length: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} in {WorkingDirectory}", + options.Name, + options.Args, + options.WorkingDirectory); + + break; + case { EnvironmentVariables.Count: > 0 }: + logger.Log(options.InvocationLogLevel, + "Run: {Name} {Args} with env {EnvironmentVariables}", + options.Name, + options.Args, + string.Join(", ", options.EnvironmentVariables.Select(kv => $"{kv.Key}={kv.Value}"))); + + break; + default: + logger.Log(options.InvocationLogLevel, "Run: {Name} {Args}", options.Name, options.Args); + + break; + } using var process = new System.Diagnostics.Process(); @@ -298,6 +349,9 @@ public async Task RunAsync(ProcessRunOptions options, Cancella RedirectStandardError = true, }; + foreach (var environmentVariable in options.EnvironmentVariables) + process.StartInfo.Environment[environmentVariable.Key] = environmentVariable.Value; + var outputBuilder = new StringBuilder(); var errorBuilder = new StringBuilder();