Skip to content

Instantly share code, notes, and snippets.

@litichevskiydv
Last active October 7, 2017 13:00
Show Gist options
  • Save litichevskiydv/f516faf672d7224153a3ec24a11e7d0e to your computer and use it in GitHub Desktop.
Save litichevskiydv/f516faf672d7224153a3ec24a11e7d0e to your computer and use it in GitHub Desktop.
Cake script for building solution and NuGet packages creation
// Target - The task you want to start. Runs the Default task if not specified.
var target = Argument("Target", "Default");
// Configuration - The build configuration (Debug/Release) to use.
var configuration =
HasArgument("Configuration")
? Argument<string>("Configuration")
: EnvironmentVariable("Configuration") ?? "Release";
// The build number to use in the version number of the built NuGet packages.
var buildNumber =
HasArgument("BuildNumber") ? Argument<int>("BuildNumber") :
AppVeyor.IsRunningOnAppVeyor ? AppVeyor.Environment.Build.Number :
TravisCI.IsRunningOnTravisCI ? TravisCI.Environment.Build.BuildNumber :
EnvironmentVariable("BuildNumber") != null ? int.Parse(EnvironmentVariable("BuildNumber")) : 0;
// Packages version in format major.minor.patch
var version = HasArgument("ShortVersion") ? Argument<string>("ShortVersion") : EnvironmentVariable("ShortVersion");
version = !string.IsNullOrWhiteSpace(version) ? version : "1.0.0";
var assemblyVersion = $"{version}.{buildNumber}";
// Text suffix of the package version
var versionSuffix = HasArgument("VersionSuffix") ? Argument<string>("VersionSuffix") : EnvironmentVariable("VersionSuffix");
var packageVersion = version + (!string.IsNullOrWhiteSpace(versionSuffix) ? $"-{versionSuffix}-build{buildNumber}" : "");
// A directory path to an Artifacts directory.
var artifactsDirectory = MakeAbsolute(Directory("./artifacts"));
// Deletes the contents of the Artifacts folder if it should contain anything from a previous build.
Task("Clean").Does(() => CleanDirectory(artifactsDirectory));
// Find all csproj projects and build them using the build configuration specified as an argument.
Task("Build")
.IsDependentOn("Clean")
.Does(() =>
{
var projects = GetFiles("../src/**/*.csproj").Concat(GetFiles("../test/**/*.csproj"));
foreach(var project in projects)
{
DotNetCoreBuild(
project.GetDirectory().FullPath,
new DotNetCoreBuildSettings()
{
Configuration = configuration,
ArgumentCustomization = args => args
.Append($"/p:Version={version}")
.Append($"/p:AssemblyVersion={assemblyVersion}")
});
}
});
// Run dotnet pack to produce NuGet packages from our projects.
Task("Pack")
.IsDependentOn("Build")
.Does(() =>
{
var projects = GetFiles("../src/Infrastructure/**/*.csproj");
foreach (var project in projects)
{
DotNetCorePack(
project.GetDirectory().FullPath,
new DotNetCorePackSettings()
{
Configuration = configuration,
NoBuild = true,
OutputDirectory = artifactsDirectory,
IncludeSymbols = true,
ArgumentCustomization = args => args
.Append($"/p:PackageVersion={packageVersion}")
});
}
});
// The default task to run if none is explicitly specified.
Task("Default")
.IsDependentOn("Pack");
// Executes the task specified in the target argument.
RunTarget(target);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment