Skip to content

Instantly share code, notes, and snippets.

@AdmiringWorm
Last active May 16, 2019 00:47
Show Gist options
  • Save AdmiringWorm/b18e664761f02e49abc6c3100973c94b to your computer and use it in GitHub Desktop.
Save AdmiringWorm/b18e664761f02e49abc6c3100973c94b to your computer and use it in GitHub Desktop.
Collection of cake scripts used to override Cake.Recipe tasks
#load "./modules.cake"
#if !CUSTOM_CODECOV
Setup<CodecovSettings>((context) => {
var settings = new CodecovSettings {
Required = true
};
if (BuildParameters.Version != null && !string.IsNullOrEmpty(BuildParameters.Version.FullSemVersion) && BuildParameters.IsRunningOnAppVeyor) {
// Required to work correctly with appveyor because environment changes isn't detected until cake is done running.
var buildVersion = string.Format("{0}.build.{1}",
BuildParameters.Version.FullSemVersion,
BuildSystem.AppVeyor.Environment.Build.Number);
settings.EnvironmentVariables = new Dictionary<string, string> {{"APPVEYOR_BUILD_VERSION", buildVersion }};
}
return settings;
});
#endif
public static void SetToolPath(ICakeContext context, CodecovSettings settings)
{
if (context.IsRunningOnUnix())
{
// Special case, as the addin version used by Cake.Recipe do not support
// the Correct unix paths.
settings.ToolPath = context.Tools.Resolve("codecov");
}
}
((CakeTask)BuildParameters.Tasks.UploadCodecovReportTask.Task).Actions.Clear();
((CakeTask)BuildParameters.Tasks.UploadCodecovReportTask.Task).Criterias.Clear();
BuildParameters.Tasks.UploadCodecovReportTask
.WithCriteria(() => BuildParameters.IsMainRepository, "This is not the main repository")
.WithCriteria(() => BuildParameters.CanPublishToCodecov)
.Does<CodecovSettings>((settings) => {
var script = "#tool dotnet:?package=Codecov.Tool&version=1.5.0";
RequireTool(script, () => {
SetToolPath(Context, settings);
var files = GetFiles(BuildParameters.Paths.Directories.TestCoverage + "/coverlet/*");
if (FileExists(BuildParameters.Paths.Files.TestCoverageOutputFilePath)) {
files += BuildParameters.Paths.Files.TestCoverageOutputFilePath;
}
if (files.Any()) {
settings.Files = files.Select(f => f.FullPath);
Codecov(settings);
}
});
});
// Do note that this script is dependent on
// Cake.Recipe, make sure that this package
// have already been loaded before loading this
// script.
// If you also wishes to upload the coverage reports
// Then see the associated codecov.cake.
#load "./modules.cake"
#addin nuget:?package=Cake.Coverlet&version=2.2.1
public static CoverletOutputFormat CoverageOutputFormat { get; set; } = CoverletOutputFormat.opencover;
#if !CUSTOM_COVERLET
Setup<CoverletSettings>((context) => {
var coverletSettings = new CoverletSettings {
CollectCoverage = true,
CoverletOutputFormat = CoverageOutputFormat,
ExcludeByFile = ToolSettings.TestCoverageExcludeByFile.Split(';').ToList(),
ExcludeByAttribute = ToolSettings.TestCoverageExcludeByAttribute.Split(';').ToList()
};
foreach (var filter in ToolSettings.TestCoverageFilter.Split(' '))
{
if (filter[0] == '+') {
coverletSettings.WithInclusion(filter.TrimStart('+'));
}
else if (filter[0] == '-') {
coverletSettings.WithFilter(filter.TrimStart('-'));
}
}
return coverletSettings;
});
#endif
public static OpenCoverSettings CreateOpenCoverSettings()
{
return new OpenCoverSettings
{
ReturnTargetCodeOffset = 0,
OldStyle = true,
Register = "user"
}.WithFilter(ToolSettings.TestCoverageFilter)
.ExcludeByAttribute(ToolSettings.TestCoverageExcludeByAttribute)
.ExcludeByFile(ToolSettings.TestCoverageExcludeByFile);
}
((CakeTask)BuildParameters.Tasks.DotNetCoreTestTask.Task).Actions.Clear();
BuildParameters.Tasks.DotNetCoreTestTask
.Does((context) => {
var projects = GetFiles(BuildParameters.TestDirectoryPath + (BuildParameters.TestFilePattern ?? "/**/*Tests.csproj"));
var coverletSettings = context.Data.Get<CoverletSettings>();
// We need to ensure the correct coverage directory is used, if we wish to later upload coverage reports
coverletSettings.CoverletOutputDirectory = BuildParameters.Paths.Directories.TestCoverage.Combine("coverlet");
var openCoverSettings = CreateOpenCoverSettings();
EnsureDirectoryExists(coverletSettings.CoverletOutputDirectory);
Information("Path to coverlet coverage report: {0}", coverletSettings.CoverletOutputDirectory);
foreach (var project in projects)
{
// Because Cake.Coverlet adds parameters to the existing
// Test settings class, we need to re-create it every iteration.
var testSettings = new DotNetCoreTestSettings {
Configuration = BuildParameters.Configuration,
NoBuild = true
};
var parsedProject = ParseProject(project, BuildParameters.Configuration);
if (parsedProject.IsNetCore && parsedProject.HasPackage("coverlet.msbuild"))
{
// A . in the file name causes codecov to not output the correct format
coverletSettings.CoverletOutputName = parsedProject.RootNameSpace.Replace('.', '-');
DotNetCoreTest(project.FullPath, testSettings, coverletSettings);
}
else if (BuildParameters.IsRunningOnWindows)
{
openCoverSettings.MergeOutput = FileExists(BuildParameters.Paths.Files.TestCoverageOutputFilePath);
OpenCover((tool) => tool.DotNetCoreTest(project.FullPath, testSettings),
BuildParameters.Paths.Files.TestCoverageOutputFilePath,
openCoverSettings);
}
else
{
DotNetCoreTest(project.FullPath, testSettings);
}
}
}).Does(() => {
var files = GetFiles(BuildParameters.Paths.Directories.TestCoverage + "/coverlet/*");
if (FileExists(BuildParameters.Paths.Files.TestCoverageOutputFilePath)) {
files += BuildParameters.Paths.Files.TestCoverageOutputFilePath;
}
if (files.Any()) {
ReportGenerator(files, BuildParameters.Paths.Directories.TestCoverage);
}
});
public static List<string> Modules = new List<string>{
"#module nuget:?package=Cake.DotNetTool.Module&version=0.1.0"
};
public static void InstallModules(ICakeContext context)
{
var script = context.MakeAbsolute(context.File($"./{Guid.NewGuid()}.cake"));
try
{
var arguments = new Dictionary<string, string>();
arguments.Add("bootstrap", "");
if(BuildParameters.CakeConfiguration.GetValue("NuGet_UseInProcessClient") != null) {
arguments.Add("nuget_useinprocessclient", BuildParameters.CakeConfiguration.GetValue("NuGet_UseInProcessClient"));
}
if(BuildParameters.CakeConfiguration.GetValue("Settings_SkipVerification") != null) {
arguments.Add("settings_skipverification", BuildParameters.CakeConfiguration.GetValue("Settings_SkipVerification"));
}
System.IO.File.WriteAllText(script.FullPath, string.Join("\n", Modules));
context.CakeExecuteScript(script,
new CakeSettings
{
Arguments = arguments
});
}
finally
{
if (context.FileExists(script))
{
context.DeleteFile(script);
}
}
}
Setup((context) => InstallModules(context));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment