From 1acc02fc2a8b6af5d897ccf3b72e094018899618 Mon Sep 17 00:00:00 2001 From: Mattias Karlsson Date: Thu, 21 Jul 2016 23:00:00 +0200 Subject: [PATCH] Add environment variable support for StartProcess & Tools * Add EnvironmentVariables to ProcessSettings * Add EnvironmentVariables to ToolSettings * Fixes #1092 --- src/Cake.Core/CakeRuntime.cs | 6 ++++++ src/Cake.Core/ICakeRuntime.cs | 8 ++++++++ src/Cake.Core/IO/ProcessRunner.cs | 14 ++++++++++++++ src/Cake.Core/IO/ProcessSettings.cs | 19 +++++++++++++++++++ src/Cake.Core/Tooling/Tool.cs | 14 ++++++++++++++ src/Cake.Core/Tooling/ToolSettings.cs | 14 ++++++++++++++ src/Cake.Testing/FakeRuntime.cs | 9 ++++++++- 7 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/Cake.Core/CakeRuntime.cs b/src/Cake.Core/CakeRuntime.cs index 607f5ab18d..d26bb63f0d 100644 --- a/src/Cake.Core/CakeRuntime.cs +++ b/src/Cake.Core/CakeRuntime.cs @@ -16,6 +16,11 @@ public sealed class CakeRuntime : ICakeRuntime /// public FrameworkName TargetFramework { get; private set; } + /// + /// Gets the version of Cake executing the script. + /// + public Version CakeVersion { get; private set; } + /// /// Initializes a new instance of the class. /// @@ -26,6 +31,7 @@ public CakeRuntime() // that this actually is what happens on Mono. var frameworkName = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName; TargetFramework = new FrameworkName(frameworkName ?? ".NETFramework,Version=v4.5"); + CakeVersion = typeof(ICakeRuntime).Assembly.GetName().Version; } } } diff --git a/src/Cake.Core/ICakeRuntime.cs b/src/Cake.Core/ICakeRuntime.cs index 0f373a21fe..ff0ba84865 100644 --- a/src/Cake.Core/ICakeRuntime.cs +++ b/src/Cake.Core/ICakeRuntime.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. + +using System; using System.Runtime.Versioning; namespace Cake.Core @@ -15,5 +17,11 @@ public interface ICakeRuntime /// /// The target framework. FrameworkName TargetFramework { get; } + + /// + /// Gets the version of Cake executing the script. + /// + /// The Cake.exe version. + Version CakeVersion { get; } } } \ No newline at end of file diff --git a/src/Cake.Core/IO/ProcessRunner.cs b/src/Cake.Core/IO/ProcessRunner.cs index 989231a274..831a20ec42 100644 --- a/src/Cake.Core/IO/ProcessRunner.cs +++ b/src/Cake.Core/IO/ProcessRunner.cs @@ -3,7 +3,10 @@ // See the LICENSE file in the project root for more information. using System; using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.Specialized; using System.Diagnostics; +using System.Linq; using Cake.Core.Diagnostics; namespace Cake.Core.IO @@ -78,6 +81,17 @@ public IProcess Start(FilePath filePath, ProcessSettings settings) RedirectStandardOutput = settings.RedirectStandardOutput }; + // Add environment variables + info.EnvironmentVariables["CAKE"] = "True"; + info.EnvironmentVariables["CAKE_VERSION"] = _environment.Runtime.CakeVersion.ToString(3); + if (settings.EnvironmentVariables != null) + { + foreach (var environmentVariable in settings.EnvironmentVariables) + { + info.EnvironmentVariables[environmentVariable.Key] = environmentVariable.Value; + } + } + // Start and return the process. var process = Process.Start(info); diff --git a/src/Cake.Core/IO/ProcessSettings.cs b/src/Cake.Core/IO/ProcessSettings.cs index e587149918..26e649b850 100644 --- a/src/Cake.Core/IO/ProcessSettings.cs +++ b/src/Cake.Core/IO/ProcessSettings.cs @@ -1,6 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + namespace Cake.Core.IO { /// @@ -38,5 +41,21 @@ public sealed class ProcessSettings /// true if process output will be suppressed; otherwise, false. /// public bool Silent { get; set; } + + /// + /// Gets or sets search paths for files, directories for temporary files, application-specific options, and other similar information. + /// + /// + /// + /// StartProcess("cmd", new ProcessSettings{ + /// Arguments = "/c set", + /// EnvironmentVariables = new Dictionary<string, string>{ + /// { "CI", "True" }, + /// { "TEMP", MakeAbsolute(Directory("./Temp")).FullPath } + /// } + /// }); + /// + /// + public IDictionary EnvironmentVariables { get; set; } } } diff --git a/src/Cake.Core/Tooling/Tool.cs b/src/Cake.Core/Tooling/Tool.cs index c1e84646ab..957fe83e2c 100644 --- a/src/Cake.Core/Tooling/Tool.cs +++ b/src/Cake.Core/Tooling/Tool.cs @@ -205,6 +205,10 @@ protected IProcess RunProcess( { info.WorkingDirectory = workingDirectory.MakeAbsolute(_environment).FullPath; } + if (info.EnvironmentVariables == null) + { + info.EnvironmentVariables = GetEnvironmentVariables(settings); + } // Run the process. var process = _processRunner.Start(toolPath, info); @@ -254,6 +258,16 @@ protected virtual IEnumerable GetAlternativeToolPaths(TSettings settin return Enumerable.Empty(); } + /// + /// Gets the environment variables. + /// + /// The settings. + /// The environment variables for the tool. + protected virtual IDictionary GetEnvironmentVariables(TSettings settings) + { + return settings.EnvironmentVariables; + } + private FilePath GetToolPath(TSettings settings) { if (_tools != null) diff --git a/src/Cake.Core/Tooling/ToolSettings.cs b/src/Cake.Core/Tooling/ToolSettings.cs index a2ee1dc0f2..09d7dbebf6 100644 --- a/src/Cake.Core/Tooling/ToolSettings.cs +++ b/src/Cake.Core/Tooling/ToolSettings.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; using Cake.Core.IO; namespace Cake.Core.Tooling @@ -52,5 +53,18 @@ public class ToolSettings /// /// The delegate used to customize the . public Func ArgumentCustomization { get; set; } + + /// + /// Gets or sets search paths for files, directories for temporary files, application-specific options, and other similar information. + /// + /// + /// + /// MSBuild("./src/Cake.sln", new MSBuildSettings { + /// EnvironmentVariables = new Dictionary<string, string>{ + /// { "TOOLSPATH", MakeAbsolute(Directory("./tools")).FullPath } + /// }}); + /// + /// + public IDictionary EnvironmentVariables { get; set; } } } diff --git a/src/Cake.Testing/FakeRuntime.cs b/src/Cake.Testing/FakeRuntime.cs index 1937338d4a..691859635c 100644 --- a/src/Cake.Testing/FakeRuntime.cs +++ b/src/Cake.Testing/FakeRuntime.cs @@ -1,4 +1,5 @@ -using System.Runtime.Versioning; +using System; +using System.Runtime.Versioning; using Cake.Core; namespace Cake.Testing @@ -13,12 +14,18 @@ public sealed class FakeRuntime : ICakeRuntime /// public FrameworkName TargetFramework { get; set; } + /// + /// Gets the version of Cake executing the script. + /// + public Version CakeVersion { get; private set; } + /// /// Initializes a new instance of the class. /// public FakeRuntime() { TargetFramework = new FrameworkName(".NETFramework,Version=v4.5"); + CakeVersion = typeof(ICakeRuntime).Assembly.GetName().Version; } } } \ No newline at end of file