Created
January 28, 2019 13:25
-
-
Save luis-fss/67c5a5cfdba4da2bff20584b8c0e7e6b to your computer and use it in GitHub Desktop.
Aquivo build.cake (ainda em desenvolvimento) preparado para gerar pacotes ClickOnce e controle de versão
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#tool nuget:?package=NUnit.ConsoleRunner&version=3.4.0 | |
//#addin nuget:?package=Cake.ClickTwice | |
#addin nuget:?package=Cake.Powershell&version=0.4.7 | |
#r "C:\Users\lukia\source\repos\Cake\ClickTwice\src\Cake.ClickTwice\bin\Debug\Cake.ClickTwice.dll" | |
#r "C:\Users\lukia\source\repos\Cake\ClickTwice\src\Cake.ClickTwice\bin\Debug\ClickTwice.Handlers.AppDetailsPage.dll" | |
////////////////////////////////////////////////////////////////////// | |
// ARGUMENTS | |
////////////////////////////////////////////////////////////////////// | |
var version = Argument("version", "1.8.8.20"); | |
var solution = "Example"; | |
var mainproject = "WindowsFormsApp1"; | |
var testtarget = "*.Tests"; | |
var artifacts = "./artifacts/"; | |
var configuration = Argument("configuration", "Release"); | |
var mainprojectpath = Argument("mainprojectpath", $"./src/{mainproject}/{mainproject}.csproj"); | |
var solutionpath = Argument("solutionpath", $"./src/{solution}.sln"); | |
var publishpath = Argument("publishpath", $"./{artifacts}/publish"); | |
var target = Argument("target", "Default"); | |
var solutionObj = ParseSolution(solutionpath); | |
var projects = solutionObj.Projects; | |
var projectPaths = projects.Select(p => p.Path.GetDirectory()); | |
//var testAssemblies = projects.Where(p => p.Name.Contains("Tests")).Select(p => p.Path.GetDirectory() + "/bin/" + configuration + "/" + p.Name + ".dll"); | |
//var testResultsPath = MakeAbsolute(Directory(artifacts + "./test-results")); | |
////////////////////////////////////////////////////////////////////// | |
// PREPARATION | |
////////////////////////////////////////////////////////////////////// | |
// Define directories. | |
var buildDir = Directory($"./src/{mainproject}/bin") + Directory(configuration); | |
var publishDir = Directory(publishpath); | |
/////////////////////////////////////////////////////////////////////////////// | |
// SETUP / TEARDOWN | |
/////////////////////////////////////////////////////////////////////////////// | |
Setup(ctx => | |
{ | |
// Executed BEFORE the first task. | |
Information("Running tasks..."); | |
Information($"Building for soulution {solutionpath}"); | |
Information($"Main project {mainprojectpath}"); | |
Information($"Building for version {version}"); | |
}); | |
Teardown(ctx => | |
{ | |
// Executed AFTER the last task. | |
Information("Finished running tasks."); | |
}); | |
////////////////////////////////////////////////////////////////////// | |
// TASKS | |
////////////////////////////////////////////////////////////////////// | |
Task("Clean") | |
.Does(() => | |
{ | |
// Clean solution directories. | |
foreach(var path in projectPaths) | |
{ | |
Information($"Cleaning {path}"); | |
CleanDirectories($"{path}/**/bin/"); | |
CleanDirectories($"{path}/**/obj/"); | |
} | |
Information($"Cleaning: {publishDir}"); | |
CleanDirectory(publishDir); | |
}); | |
Task("Restore-NuGet-Packages") | |
.IsDependentOn("Clean") | |
.Does(() => | |
{ | |
// Restore all NuGet packages. | |
Information("Restore all NuGet packages..."); | |
NuGetRestore(solutionpath); | |
}); | |
Task("Transform") | |
.Does(() => | |
{ | |
Information("Muda a versão do ClickOnce no arquivo de projeto, sem essa alteração o pacote vai gerar a versão errada."); | |
//TODO calcular a versão atual e nova a partir de um valor armazenado em um arquivo | |
//TODO https://gsferreira.com/archive/2018/06/versioning-net-core-applications-using-cake/ | |
XmlPoke(mainprojectpath, | |
"/ns:Project/ns:PropertyGroup/ns:ApplicationVersion", | |
version, | |
new XmlPokeSettings { | |
Namespaces = new Dictionary<string, string> { | |
{ "ns", "http://schemas.microsoft.com/developer/msbuild/2003" } | |
} | |
}); | |
}); | |
Task("Set-AssemblyInfo-Version") | |
.Description("Set the version in all the AssemblyInfo.cs or AssemblyInfo.vb files in any subdirectory.") | |
.Does(() => | |
{ | |
StartPowershellFile("./SetAssemblyInfoVersion.ps1", args => | |
{ | |
args.Append("Version", version); | |
}); | |
}); | |
Task("Build") | |
.IsDependentOn("Restore-NuGet-Packages") | |
.IsDependentOn("Set-AssemblyInfo-Version") | |
.Does(() => | |
{ | |
Information("Building solution..."); | |
if(IsRunningOnWindows()) | |
{ | |
// Use MSBuild | |
MSBuild(solutionpath, settings => | |
settings.SetConfiguration(configuration) | |
//.SetVerbosity(Verbosity.Diagnostic) | |
.SetVerbosity(Verbosity.Quiet)); | |
} | |
else | |
{ | |
// Use XBuild | |
XBuild(solutionpath, settings => | |
settings.SetConfiguration(configuration) | |
//.SetVerbosity(Verbosity.Diagnostic) | |
.SetVerbosity(Verbosity.Quiet)); | |
} | |
}); | |
Task("Run-Unit-Tests") | |
.IsDependentOn("Build") | |
.Does(() => | |
{ | |
Information($"Running test for {testtarget}.dll"); | |
Information($"Mode: {configuration}"); | |
NUnit3($"./src/**/bin/{configuration}/{testtarget}.dll", new NUnit3Settings { NoResults = true }); | |
}); | |
Task("Publish-ClickOnce") | |
.IsDependentOn("Run-Unit-Tests") | |
.Does(() => | |
{ | |
Information("Publish ClickOnce..."); | |
Information($"Mode: {configuration}"); | |
PublishApp(mainprojectpath) | |
.SetConfiguration(configuration) | |
//.ForceRebuild() | |
//.SetBuildPlatform(MSBuildPlatform.x86) | |
//.SetBuildPlatform(MSBuildPlatform.x64) | |
//.SetBuildPlatform(MSBuildPlatform.AnyCPU) | |
//.DoNotBuild() | |
.ThrowOnHandlerFailure() | |
//.WithVersion(version) | |
.To(publishpath); | |
}) | |
.OnError(ex => | |
{ | |
// Handle the error here. | |
Information("Message: " + ex.Message); | |
Information("StackTrace: " + ex.StackTrace); | |
}); | |
Task("Restore-AssemblyInfo-Version") | |
.IsDependentOn("Publish-ClickOnce") | |
.Description("Restore the default version in all the AssemblyInfo.cs or AssemblyInfo.vb files in any subdirectory.") | |
.Does(() => | |
{ | |
StartPowershellFile("./SetAssemblyInfoVersion.ps1", args => | |
{ | |
args.Append("Version", "1.0.0.0"); | |
}); | |
}); | |
////////////////////////////////////////////////////////////////////// | |
// TASK TARGETS | |
////////////////////////////////////////////////////////////////////// | |
Task("Default") | |
.IsDependentOn("Restore-AssemblyInfo-Version"); | |
////////////////////////////////////////////////////////////////////// | |
// EXECUTION | |
////////////////////////////////////////////////////////////////////// | |
RunTarget(target); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment