Skip to content

Commit

Permalink
Add environment variable support for StartProcess & Tools
Browse files Browse the repository at this point in the history
* Add EnvironmentVariables to ProcessSettings
* Add EnvironmentVariables to ToolSettings
* Fixes cake-build#1092
  • Loading branch information
devlead committed Jul 21, 2016
1 parent 8b44082 commit 1acc02f
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Cake.Core/CakeRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public sealed class CakeRuntime : ICakeRuntime
/// </summary>
public FrameworkName TargetFramework { get; private set; }

/// <summary>
/// Gets the version of Cake executing the script.
/// </summary>
public Version CakeVersion { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="CakeRuntime"/> class.
/// </summary>
Expand All @@ -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;
}
}
}
8 changes: 8 additions & 0 deletions src/Cake.Core/ICakeRuntime.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,5 +17,11 @@ public interface ICakeRuntime
/// </summary>
/// <returns>The target framework.</returns>
FrameworkName TargetFramework { get; }

/// <summary>
/// Gets the version of Cake executing the script.
/// </summary>
/// <returns>The Cake.exe version.</returns>
Version CakeVersion { get; }
}
}
14 changes: 14 additions & 0 deletions src/Cake.Core/IO/ProcessRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions src/Cake.Core/IO/ProcessSettings.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
Expand Down Expand Up @@ -38,5 +41,21 @@ public sealed class ProcessSettings
/// <c>true</c> if process output will be suppressed; otherwise, <c>false</c>.
/// </value>
public bool Silent { get; set; }

/// <summary>
/// Gets or sets search paths for files, directories for temporary files, application-specific options, and other similar information.
/// </summary>
/// <example>
/// <code>
/// StartProcess("cmd", new ProcessSettings{
/// Arguments = "/c set",
/// EnvironmentVariables = new Dictionary&lt;string, string&gt;{
/// { "CI", "True" },
/// { "TEMP", MakeAbsolute(Directory("./Temp")).FullPath }
/// }
/// });
/// </code>
/// </example>
public IDictionary<string, string> EnvironmentVariables { get; set; }
}
}
14 changes: 14 additions & 0 deletions src/Cake.Core/Tooling/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -254,6 +258,16 @@ protected virtual IEnumerable<FilePath> GetAlternativeToolPaths(TSettings settin
return Enumerable.Empty<FilePath>();
}

/// <summary>
/// Gets the environment variables.
/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>The environment variables for the tool.</returns>
protected virtual IDictionary<string, string> GetEnvironmentVariables(TSettings settings)
{
return settings.EnvironmentVariables;
}

private FilePath GetToolPath(TSettings settings)
{
if (_tools != null)
Expand Down
14 changes: 14 additions & 0 deletions src/Cake.Core/Tooling/ToolSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -52,5 +53,18 @@ public class ToolSettings
/// </example>
/// <value>The delegate used to customize the <see cref="Cake.Core.IO.ProcessArgumentBuilder" />.</value>
public Func<ProcessArgumentBuilder, ProcessArgumentBuilder> ArgumentCustomization { get; set; }

/// <summary>
/// Gets or sets search paths for files, directories for temporary files, application-specific options, and other similar information.
/// </summary>
/// <example>
/// <code>
/// MSBuild("./src/Cake.sln", new MSBuildSettings {
/// EnvironmentVariables = new Dictionary&lt;string, string&gt;{
/// { "TOOLSPATH", MakeAbsolute(Directory("./tools")).FullPath }
/// }});
/// </code>
/// </example>
public IDictionary<string, string> EnvironmentVariables { get; set; }
}
}
9 changes: 8 additions & 1 deletion src/Cake.Testing/FakeRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Runtime.Versioning;
using System;
using System.Runtime.Versioning;
using Cake.Core;

namespace Cake.Testing
Expand All @@ -13,12 +14,18 @@ public sealed class FakeRuntime : ICakeRuntime
/// </summary>
public FrameworkName TargetFramework { get; set; }

/// <summary>
/// Gets the version of Cake executing the script.
/// </summary>
public Version CakeVersion { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="FakeRuntime"/> class.
/// </summary>
public FakeRuntime()
{
TargetFramework = new FrameworkName(".NETFramework,Version=v4.5");
CakeVersion = typeof(ICakeRuntime).Assembly.GetName().Version;
}
}
}

0 comments on commit 1acc02f

Please sign in to comment.