Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-wolfenden committed Jan 30, 2018
1 parent 6b113f7 commit 91491b3
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 72 deletions.
131 changes: 59 additions & 72 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var projectName = "StarWarsNames";
var artifactsDir = Directory("./artifacts");

var isContinuousIntegrationBuild = !BuildSystem.IsLocalBuild;
var isLocalBuild = BuildSystem.IsLocalBuild;
var semanticVersionNumber = "0.0.0";

//////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -38,24 +38,24 @@ Teardown(context =>
// PRIVATE TASKS
//////////////////////////////////////////////////////////////////////

Task("__Build")
.IsDependentOn("__DumpDotnetInfo")
.IsDependentOn("__Clean")
.IsDependentOn("__GetNextSemanticVersionNumber")
.IsDependentOn("__BuildSolution")
.IsDependentOn("__RunTests")
.IsDependentOn("__Package")
.IsDependentOn("__RunSemanticRelease")
Task("Build")
.IsDependentOn("DumpDotnetInfo")
.IsDependentOn("Clean")
.IsDependentOn("GetNextSemanticVersionNumber")
.IsDependentOn("BuildSolution")
.IsDependentOn("RunTests")
.IsDependentOn("Package")
.IsDependentOn("RunSemanticRelease")
;

Task("__DumpDotnetInfo")
Task("DumpDotnetInfo")
.Does(() =>
{
Information("dotnet --info");
StartProcess("dotnet", new ProcessSettings { Arguments = "--info" });
});

Task("__Clean")
Task("Clean")
.Does(() =>
{
Information("Cleaning {0}, bin and obj folders", artifactsDir);
Expand All @@ -65,38 +65,19 @@ Task("__Clean")
CleanDirectories("./src/**/obj");
});

Task("__GetNextSemanticVersionNumber")
// .WithCriteria(isContinuousIntegrationBuild)
Task("GetNextSemanticVersionNumber")
// .WithCriteria(!isLocalBuild)
.Does(() =>
{
var npxPath = Context.Tools.Resolve("npx.cmd");

IEnumerable<string> semanticReleaseOutputLines;
Information("Running semantic-release in dry run mode to extract next semantic version number");

var exitCode = StartProcess(
npxPath,
new ProcessSettings()
.SetRedirectStandardOutput(true)
.WithArguments(args => args
.AppendSwitch("-p", "semantic-release@next")
.AppendSwitch("-p", "@semantic-release/changelog")
.Append("semantic-release")
.Append("--dry-run")
),
out semanticReleaseOutputLines
);

Information("{0}", string.Join(Environment.NewLine, semanticReleaseOutputLines));

if (exitCode != 0) {
throw new Exception($"semantic-release exited with exit code {exitCode}");
}
var semanticReleaseOutput = ExecuteSemanticRelease(Context, dryRun: false);
var nextSemanticVersionNumber = ExtractNextSemanticVersionNumber(semanticReleaseOutput);

semanticVersionNumber = ExtractNextSemanticVersionNumber(semanticReleaseOutputLines);
Information("Next semantic version number is {0}", semanticVersionNumber);
Information("Next semantic version number is {0}", nextSemanticVersionNumber);
});

Task("__BuildSolution")
Task("BuildSolution")
.Does(() =>
{
var solutions = GetFiles("./src/*.sln");
Expand All @@ -119,7 +100,7 @@ Task("__BuildSolution")
}
});

Task("__RunTests")
Task("RunTests")
.Does(() =>
{
var xunitArgs = "-nobuild -configuration " + configuration;
Expand All @@ -133,7 +114,7 @@ Task("__RunTests")
}
});

Task("__Package")
Task("Package")
.Does(() =>
{
var projects = GetFiles("./src/**/*.csproj");
Expand All @@ -156,58 +137,64 @@ Task("__Package")
}
});

Task("__RunSemanticRelease")
Task("RunSemanticRelease")
// .WithCriteria(isContinuousIntegrationBuild)
.Does(() =>
{
var npxPath = Context.Tools.Resolve("npx.cmd");

var exitCode = StartProcess(
npxPath,
new ProcessSettings()
.WithArguments(args => args
.AppendSwitch("-p", "semantic-release@next")
.AppendSwitch("-p", "@semantic-release/changelog")
.Append("semantic-release")
.Append("--no-ci")
)
);

if (exitCode != 0) {
throw new Exception($"semantic-release exited with exit code {exitCode}");
}
});

///////////////////////////////////////////////////////////////////////////////
// PRIMARY TARGETS
///////////////////////////////////////////////////////////////////////////////

Task("Default")
.IsDependentOn("__Build");
.IsDependentOn("Build");

///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////

RunTarget(target);

///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
string ExtractNextSemanticVersionNumber(IEnumerable<string> semanticReleaseOutputLines)

string[] ExecuteSemanticRelease(ICakeContext context, bool dryRun)
{
var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");
var npxPath = context.Tools.Resolve("npx.cmd");
if (npxPath == null) throw new Exception("Could not locate executable 'npm'.");

var nextSemanticVersionNumber = semanticReleaseOutputLines
.Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
.Where(line => !string.IsNullOrWhiteSpace(line))
.SingleOrDefault();
IEnumerable<string> redirectedStandardOutput;

if (nextSemanticVersionNumber == null)
{
throw new Exception("Could not extract next semantic version number from semantic-release output");
}
var exitCode = StartProcess(
npxPath,
new ProcessSettings()
.SetRedirectStandardOutput(true)
.WithArguments(args => args
.AppendSwitch("-p", "semantic-release@next")
.AppendSwitch("-p", "@semantic-release/changelog")
.Append("semantic-release")
.Append(dryRun ? "--dry-run" : "")
),
out redirectedStandardOutput
);

return nextSemanticVersionNumber;
}
var semanticReleaseOutput = redirectedStandardOutput.ToArray();
Information(string.Join(Environment.NewLine, semanticReleaseOutput));

if (exitCode != 0) throw new Exception($"Process returned an error (exit code {exitCode}).");

///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
return semanticReleaseOutput;
}

RunTarget(target);
string ExtractNextSemanticVersionNumber(string[] semanticReleaseOutput)
{
var extractRegEx = new System.Text.RegularExpressions.Regex("^.+next release version is (?<SemanticVersionNumber>.*)$");

return semanticReleaseOutput
.Select(line => extractRegEx.Match(line).Groups["SemanticVersionNumber"].Value)
.Where(line => !string.IsNullOrWhiteSpace(line))
.SingleOrDefault();
}
Loading

0 comments on commit 91491b3

Please sign in to comment.