Skip to content

Commit

Permalink
Merge pull request #8 from augustoproiete/support-env-variables
Browse files Browse the repository at this point in the history
Add support for environment variables for default values for MinVer settings
  • Loading branch information
augustoproiete authored Nov 27, 2020
2 parents bdbd750 + 8db1af7 commit 4145340
Show file tree
Hide file tree
Showing 28 changed files with 1,273 additions and 507 deletions.
1 change: 1 addition & 0 deletions Cake.MinVer.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CommentTypo/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MarkupAttributeTypo/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MarkupTextTypo/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantAnonymousTypePropertyName/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=StringLiteralTypo/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticReadonly/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String></wpf:ResourceDictionary>
6 changes: 6 additions & 0 deletions src/Cake.MinVer/IMinVerGlobalTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cake.MinVer
{
internal interface IMinVerGlobalTool : IMinVerTool
{
}
}
6 changes: 6 additions & 0 deletions src/Cake.MinVer/IMinVerLocalTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Cake.MinVer
{
internal interface IMinVerLocalTool : IMinVerTool
{
}
}
9 changes: 9 additions & 0 deletions src/Cake.MinVer/IMinVerTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Cake.MinVer
{
internal interface IMinVerTool
{
string ToolName { get; }

int TryRun(MinVerSettings settings, out MinVerVersion result);
}
}
5 changes: 0 additions & 5 deletions src/Cake.MinVer/MinVerAutoIncrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
/// </summary>
public enum MinVerAutoIncrement
{
/// <summary>
/// Omits the --auto-increment argument (and uses MinVer's default)
/// </summary>
Default,

/// <summary>
/// --auto-increment major
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/Cake.MinVer/MinVerEnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Cake.MinVer
{
internal sealed class MinVerEnvironmentVariables
{
// ReSharper disable InconsistentNaming

// Environment variables that translate to MinVer arguments
internal static readonly string MINVERAUTOINCREMENT = $"MINVER{nameof(MinVerSettings.AutoIncrement).ToUpperInvariant()}";
internal static readonly string MINVERBUILDMETADATA = $"MINVER{nameof(MinVerSettings.BuildMetadata).ToUpperInvariant()}";
internal static readonly string MINVERDEFAULTPRERELEASEPHASE = $"MINVER{nameof(MinVerSettings.DefaultPreReleasePhase).ToUpperInvariant()}";
internal static readonly string MINVERMINIMUMMAJORMINOR = $"MINVER{nameof(MinVerSettings.MinimumMajorMinor).ToUpperInvariant()}";
internal static readonly string MINVERTAGPREFIX = $"MINVER{nameof(MinVerSettings.TagPrefix).ToUpperInvariant()}";
internal static readonly string MINVERVERBOSITY = $"MINVER{nameof(MinVerSettings.Verbosity).ToUpperInvariant()}";

// Environment variables that change Cake.MinVer behavior
internal static readonly string MINVERPREFERGLOBALTOOL = $"MINVER{nameof(MinVerSettings.PreferGlobalTool).ToUpperInvariant()}";
internal static readonly string MINVERNOFALLBACK = $"MINVER{nameof(MinVerSettings.NoFallback).ToUpperInvariant()}";
internal static readonly string MINVERTOOLPATH = $"MINVER{nameof(MinVerSettings.ToolPath).ToUpperInvariant()}";

// ReSharper restore InconsistentNaming
}
}
13 changes: 5 additions & 8 deletions src/Cake.MinVer/MinVerGlobalTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

namespace Cake.MinVer
{
internal class MinVerGlobalTool : MinVerToolBase
internal class MinVerGlobalTool : MinVerToolBase, IMinVerGlobalTool
{
public MinVerGlobalTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log) : base(fileSystem, environment, processRunner, tools, log)
public MinVerGlobalTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner,
IToolLocator tools, ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}

Expand All @@ -28,7 +25,7 @@ protected override ProcessArgumentBuilder GetArguments(MinVerSettings settings)
args.CopyTo(command);
}

CakeLog.Verbose("{0} arguments: {1}", GetToolName(), args.RenderSafe());
CakeLog.Verbose("{0} arguments: [{1}]", GetToolName(), args.RenderSafe());

return command;
}
Expand Down
13 changes: 5 additions & 8 deletions src/Cake.MinVer/MinVerLocalTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@

namespace Cake.MinVer
{
internal class MinVerLocalTool : MinVerToolBase
internal class MinVerLocalTool : MinVerToolBase, IMinVerLocalTool
{
public MinVerLocalTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log) : base(fileSystem, environment, processRunner, tools, log)
public MinVerLocalTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner,
IToolLocator tools, ICakeLog log)
: base(fileSystem, environment, processRunner, tools, log)
{
}

Expand All @@ -29,7 +26,7 @@ protected override ProcessArgumentBuilder GetArguments(MinVerSettings settings)
args.CopyTo(command);
}

CakeLog.Verbose("{0} arguments: {1}", GetToolName(), args.RenderSafe());
CakeLog.Verbose("{0} arguments: [{1}]", GetToolName(), args.RenderSafe());

return command;
}
Expand Down
55 changes: 48 additions & 7 deletions src/Cake.MinVer/MinVerSettings.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
using Cake.Common.Tools.DotNetCore;
using System;
using Cake.Common.Tools.DotNetCore;
using Cake.Core.IO;

namespace Cake.MinVer
{
/// <summary>
/// Contains settings used by <see cref="MinVerTool" />.
/// </summary>
public class MinVerSettings : DotNetCoreSettings
public class MinVerSettings : DotNetCoreSettings, ICloneable
{
/// <summary>
/// Set the version part to be automatically incremented.
/// --auto-increment &lt;VERSION_PART&gt;
/// major, minor, or patch (default)
/// </summary>
public MinVerAutoIncrement AutoIncrement { get; set; }
public MinVerAutoIncrement? AutoIncrement { get; set; }

/// <summary>
/// Set the build metadata.
Expand Down Expand Up @@ -57,7 +58,7 @@ public class MinVerSettings : DotNetCoreSettings
/// Local tool = `dotnet minver`
/// Global tool = `minver`
/// </summary>
public bool PreferGlobalTool { get; set; }
public bool? PreferGlobalTool { get; set; }

/// <summary>
/// By default, MinVer is executed as a local tool first(*) and, in case of error, fallback to global tool(*)
Expand All @@ -69,15 +70,55 @@ public class MinVerSettings : DotNetCoreSettings
/// Local tool = `dotnet minver`
/// Global tool = `minver`
/// </summary>
public bool NoFallback { get; set; }
public bool? NoFallback { get; set; }

/// <summary>
/// Set the verbosity.
/// --verbosity &lt;VERBOSITY&gt;
/// error, warn, info (default), debug, or trace
/// </summary>
public new MinVerVerbosity Verbosity { get; set; }
public new MinVerVerbosity? Verbosity { get; set; }

internal DotNetCoreVerbosity? ToolVerbosity => base.Verbosity;
internal DotNetCoreVerbosity? ToolVerbosity
{
get => base.Verbosity;
set => base.Verbosity = value;
}

/// <summary>
/// Creates a shallow clone of this <see cref="MinVerSettings" /> instance
/// </summary>
/// <returns></returns>
public MinVerSettings Clone()
{
var clone = new MinVerSettings
{
AutoIncrement = AutoIncrement,
BuildMetadata = BuildMetadata,
DefaultPreReleasePhase = DefaultPreReleasePhase,
MinimumMajorMinor = MinimumMajorMinor,
Repo = Repo,
TagPrefix = TagPrefix,
PreferGlobalTool = PreferGlobalTool,
NoFallback = NoFallback,
Verbosity = Verbosity,
ToolVerbosity = ToolVerbosity,

DiagnosticOutput = DiagnosticOutput,
ToolPath = ToolPath,
ToolTimeout = ToolTimeout,
WorkingDirectory = WorkingDirectory,
NoWorkingDirectory = NoWorkingDirectory,
ArgumentCustomization = ArgumentCustomization,
EnvironmentVariables = EnvironmentVariables,
};

return clone;
}

object ICloneable.Clone()
{
return Clone();
}
}
}
108 changes: 78 additions & 30 deletions src/Cake.MinVer/MinVerTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;
using Cake.MinVer.Utils;

namespace Cake.MinVer
{
Expand All @@ -13,8 +14,9 @@ namespace Cake.MinVer
/// </summary>
public class MinVerTool : DotNetCoreTool<MinVerSettings>
{
private readonly MinVerLocalTool _localTool;
private readonly MinVerGlobalTool _globalTool;
private readonly IMinVerLocalTool _localTool;
private readonly IMinVerGlobalTool _globalTool;
private readonly IEnvironmentProvider _environmentProvider;

/// <summary>
/// Initializes a new instance of the <see cref="MinVerTool" /> class.
Expand All @@ -24,24 +26,18 @@ public class MinVerTool : DotNetCoreTool<MinVerSettings>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
/// <param name="log">Cake log instance.</param>
public MinVerTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log) : this(fileSystem, environment, processRunner, tools, log, localTool: null, globalTool: null)
public MinVerTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner,
IToolLocator tools, ICakeLog log)
: this(fileSystem, environment, processRunner, tools, log, localTool: null, globalTool: null)
{
}

internal MinVerTool(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log,
MinVerLocalTool localTool,
MinVerGlobalTool globalTool) : base(fileSystem, environment, processRunner, tools)
internal MinVerTool(IFileSystem fileSystem, ICakeEnvironment environment, IProcessRunner processRunner,
IToolLocator tools, ICakeLog log, IMinVerLocalTool localTool, IMinVerGlobalTool globalTool)
: base(fileSystem, environment, processRunner, tools)
{
_environmentProvider = new EnvironmentProvider(environment);

CakeLog = log ?? throw new ArgumentNullException(nameof(log));

_localTool = localTool ?? new MinVerLocalTool(fileSystem, environment, processRunner, tools, log);
Expand All @@ -60,25 +56,21 @@ internal MinVerTool(
/// <returns>The MinVer calculated version information</returns>
public MinVerVersion Run(MinVerSettings settings)
{
CakeLog.Verbose("Executing {0} tool", GetToolName());

if (settings is null)
{
throw new ArgumentNullException(nameof(settings));
}

if (!(settings.ToolPath is null))
{
// If ToolPath is specified, it means it's a global tool in a custom location (also known as a tool-path tool)
// https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools
settings.PreferGlobalTool = true;
_environmentProvider.SetOverrides(settings.EnvironmentVariables);

// If ToolPath is specified, we try to run that specific tool only... No fallback
settings.NoFallback = true;
}
var finalSettings = CloneSettingsAndApplyEnvVariables(settings);

MinVerToolBase preferredTool;
MinVerToolBase fallbackTool;
IMinVerTool preferredTool;
IMinVerTool fallbackTool;

if (settings.PreferGlobalTool)
if (finalSettings.PreferGlobalTool.GetValueOrDefault())
{
preferredTool = _globalTool;
fallbackTool = _localTool;
Expand All @@ -89,19 +81,19 @@ public MinVerVersion Run(MinVerSettings settings)
fallbackTool = _globalTool;
}

var preferredToolExitCode = preferredTool.TryRun(settings, out var minVerVersion);
var preferredToolExitCode = preferredTool.TryRun(finalSettings, out var minVerVersion);
if (preferredToolExitCode == 0)
{
return minVerVersion;
}

if (settings.NoFallback)
if (finalSettings.NoFallback.GetValueOrDefault())
{
ProcessExitCode(preferredToolExitCode);
return null;
}

var fallbackToolExitCode = fallbackTool.TryRun(settings, out minVerVersion);
var fallbackToolExitCode = fallbackTool.TryRun(finalSettings, out minVerVersion);
if (fallbackToolExitCode == 0)
{
CakeLog.Verbose(string.Format(CultureInfo.InvariantCulture,
Expand All @@ -127,5 +119,61 @@ protected override string GetToolName()
{
return "MinVer";
}

private MinVerSettings CloneSettingsAndApplyEnvVariables(MinVerSettings settings)
{
var finalSettings = settings.Clone();

finalSettings.AutoIncrement = settings.AutoIncrement ?? _environmentProvider
.GetEnvironmentVariableAsEnum<MinVerAutoIncrement>(MinVerEnvironmentVariables.MINVERAUTOINCREMENT);

if (string.IsNullOrWhiteSpace(finalSettings.BuildMetadata))
{
finalSettings.BuildMetadata = _environmentProvider
.GetEnvironmentVariable(MinVerEnvironmentVariables.MINVERBUILDMETADATA);
}

if (string.IsNullOrWhiteSpace(finalSettings.DefaultPreReleasePhase))
{
finalSettings.DefaultPreReleasePhase = _environmentProvider
.GetEnvironmentVariable(MinVerEnvironmentVariables.MINVERDEFAULTPRERELEASEPHASE);
}

if (string.IsNullOrWhiteSpace(finalSettings.MinimumMajorMinor))
{
finalSettings.MinimumMajorMinor = _environmentProvider
.GetEnvironmentVariable(MinVerEnvironmentVariables.MINVERMINIMUMMAJORMINOR);
}

if (string.IsNullOrWhiteSpace(finalSettings.TagPrefix))
{
finalSettings.TagPrefix = _environmentProvider
.GetEnvironmentVariable(MinVerEnvironmentVariables.MINVERTAGPREFIX);
}

finalSettings.PreferGlobalTool ??= _environmentProvider
.GetEnvironmentVariableAsBool(MinVerEnvironmentVariables.MINVERPREFERGLOBALTOOL);

finalSettings.NoFallback ??= _environmentProvider
.GetEnvironmentVariableAsBool(MinVerEnvironmentVariables.MINVERNOFALLBACK);

finalSettings.Verbosity ??= _environmentProvider
.GetEnvironmentVariableAsEnum<MinVerVerbosity>(MinVerEnvironmentVariables.MINVERVERBOSITY);

finalSettings.ToolPath ??= _environmentProvider
.GetEnvironmentVariableAsFilePath(MinVerEnvironmentVariables.MINVERTOOLPATH);

if (!(finalSettings.ToolPath is null))
{
// If ToolPath is specified, it means it's a global tool in a custom location (also known as a tool-path tool)
// https://docs.microsoft.com/en-us/dotnet/core/tools/global-tools
finalSettings.PreferGlobalTool = true;

// If ToolPath is specified, we try to run that specific tool only... No fallback
finalSettings.NoFallback = true;
}

return finalSettings;
}
}
}
Loading

0 comments on commit 4145340

Please sign in to comment.