Skip to content

Commit

Permalink
Add tool delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
arodus authored and matkoch committed Oct 30, 2018
1 parent 3255748 commit 49976a1
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 3 deletions.
2 changes: 1 addition & 1 deletion source/Nuke.Common/Git/GitRepositoryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Nuke.Common.Git
{
/// <inheritdoc/>
/// <summary>
/// Implements auto-injection for <see cref="GitRepository"/>.
/// Injects an instance of <see cref="GitRepository"/> based on the local repository.
/// <para/>
/// <inheritdoc/>
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions source/Nuke.Common/IconClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@
[assembly: IconClass(typeof(NunitTasks), "bug2")]
[assembly: IconClass(typeof(OctopusTasks), "cloud-upload")]
[assembly: IconClass(typeof(OpenCoverTasks), "shield2")]
[assembly: IconClass(typeof(PackageExecutableAttribute), "terminal")]
[assembly: IconClass(typeof(PaketTasks), "box")]
[assembly: IconClass(typeof(ParameterAttribute), "terminal")]
[assembly: IconClass(typeof(PathConstruction), "price-tag2")]
[assembly: IconClass(typeof(PathExecutableAttribute), "terminal")]
[assembly: IconClass(typeof(ProcessTasks), "terminal")]
[assembly: IconClass(typeof(ProjectModelTasks), "tree7")]
[assembly: IconClass(typeof(ReportGeneratorTasks), "pie-chart4")]
Expand Down
24 changes: 23 additions & 1 deletion source/Nuke.Common/ProjectModel/SolutionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@

namespace Nuke.Common.ProjectModel
{
/// <inheritdoc />
/// <inheritdoc cref="InjectionAttributeBase"/>
/// <summary>
/// Injects an instance of <see cref="Solution"/>. The solution path is resolved in the following order:
/// <ul>
/// <li>From command-line arguments (e.g., <c>-solution path/to/solution.sln</c>)</li>
/// <li>From environment variables (e.g., <c>SOLUTION=path/to/solution.sln</c>)</li>
/// <li>From the constructor argument</li>
/// <li>From the <c>.nuke</c> configuration file</li>
/// </ul>
/// <inheritdoc cref="InjectionAttributeBase"/>
/// </summary>
/// <example>
/// <code>
/// [Solution("common.sln")] readonly Solution Solution;
/// Target FooBar => _ => _
/// .Executes(() =>
/// {
/// Logger.Log($"File: {Solution}");
/// Logger.Log($"Directory: {Solution.Directory}");
/// Logger.Log($"Projects: {Solution.AllProjects.Select(x => x.Name).JoinComma()}");
/// });
/// </code>
/// </example>
[PublicAPI]
[UsedImplicitly(ImplicitUseKindFlags.Assign)]
public class SolutionAttribute : ParameterAttribute
Expand Down
55 changes: 55 additions & 0 deletions source/Nuke.Common/Tooling/PackageExecutableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Linq;
using Nuke.Common.Execution;

namespace Nuke.Common.Tooling
{
/// <inheritdoc/>
/// <summary>
/// Injects a delegate for process execution. The path to the executable is resolved in the following order:
/// <ul>
/// <li>From environment variables (e.g., <c>[MEMBER_NAME]_EXE=path</c>)</li>
/// <li>
/// From the <c>.csproj</c> or <c>packages.config</c> file using <c>packageId</c> and
/// <c>packageExecutable</c> to look up the NuGet package cache
/// </li>
/// </ul>
/// <inheritdoc/>
/// </summary>
/// <example>
/// <code>
/// [PackageExecutable("NuGet.CommandLine", "nuget.exe")] readonly Tool NuGet;
/// Target FooBar => _ => _
/// .Executes(() =>
/// {
/// var process = NuGet($"pack {ProjectFile}");
/// process.AssertZeroExitCode();
/// });
/// </code>
/// </example>
public class PackageExecutableAttribute : InjectionAttributeBase
{
private readonly string _packageId;
private readonly string _packageExecutable;
private readonly string _framework;

public PackageExecutableAttribute(string packageId, string packageExecutable, string framework = null)
{
_packageId = packageId;
_packageExecutable = packageExecutable;
_framework = framework;
}

public override object GetValue(string memberName, Type memberType)
{
var toolPath =
ToolPathResolver.TryGetEnvironmentExecutable($"{memberName.ToUpperInvariant()}_EXE") ??
ToolPathResolver.GetPackageExecutable(_packageId, _packageExecutable, _framework);
return new Tool(new ToolExecutor(toolPath).Execute);
}
}
}
50 changes: 50 additions & 0 deletions source/Nuke.Common/Tooling/PathExecutableAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Linq;
using Nuke.Common.Execution;

namespace Nuke.Common.Tooling
{
/// <inheritdoc/>
/// <summary>
/// Injects a delegate for process execution. The executable name is derived from the member name or can be
/// passed as constructor argument. The path to the executable is resolved in the following order:
/// <ul>
/// <li>From environment variables (e.g., <c>[NAME]_EXE=path</c>)</li>
/// <li>From the PATH variable using <c>which</c> or <c>where</c></li>
/// </ul>
/// <inheritdoc/>
/// </summary>
/// <example>
/// <code>
/// [PathExecutable] readonly Tool Echo;
/// Target FooBar => _ => _
/// .Executes(() =>
/// {
/// var process = Echo("test");
/// process.AssertZeroExitCode();
/// });
/// </code>
/// </example>
public class PathExecutableAttribute : InjectionAttributeBase
{
private readonly string _name;

public PathExecutableAttribute(string name = null)
{
_name = name;
}

public override object GetValue(string memberName, Type memberType)
{
var name = _name ?? memberName;
var toolPath =
ToolPathResolver.TryGetEnvironmentExecutable($"{name.ToUpperInvariant()}_EXE") ??
ToolPathResolver.GetPathExecutable(name);
return new Tool(new ToolExecutor(toolPath).Execute);
}
}
}
19 changes: 19 additions & 0 deletions source/Nuke.Common/Tooling/Tool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Collections.Generic;
using System.Linq;

namespace Nuke.Common.Tooling
{
public delegate IProcess Tool(
string arguments = null,
string workingDirectory = null,
IReadOnlyDictionary<string, string> environmentVariables = null,
int? timeout = null,
bool logOutput = true,
Func<string, LogLevel> logLevelParser = null,
Func<string, string> outputFilter = null);
}
40 changes: 40 additions & 0 deletions source/Nuke.Common/Tooling/ToolExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Collections.Generic;
using System.Linq;

namespace Nuke.Common.Tooling
{
internal class ToolExecutor
{
private readonly string _toolPath;

public ToolExecutor(string toolPath)
{
_toolPath = toolPath;
}

public IProcess Execute(
string arguments = null,
string workingDirectory = null,
IReadOnlyDictionary<string, string> environmentVariables = null,
int? timeout = null,
bool logOutput = true,
Func<string, LogLevel> logLevelParser = null,
Func<string, string> outputFilter = null)
{
return ProcessTasks.StartProcess(
_toolPath,
arguments,
workingDirectory,
environmentVariables,
timeout,
logOutput,
logLevelParser,
outputFilter);
}
}
}
2 changes: 1 addition & 1 deletion source/Nuke.Common/Tools/GitVersion/GitVersionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Nuke.Common.Tools.GitVersion
{
/// <inheritdoc/>
/// <summary>
/// Implements auto-injection for <see cref="GitVersionTasks"/>.
/// Injects an instance of <see cref="GitVersion"/> based on the local repository.
/// <para/>
/// <inheritdoc/>
/// </summary>
Expand Down

0 comments on commit 49976a1

Please sign in to comment.