-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
philo
committed
Sep 23, 2020
0 parents
commit 16809e9
Showing
40 changed files
with
2,444 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Task("Artifacts:Copy:ARM") | ||
.WithCriteria<Configuration>((ctx, config) => ctx.DirectoryExists("./src/arm") && ctx.GetSubDirectories("./src/arm").Any()) | ||
.IsDependentOn("Build") | ||
.IsDependeeOf("Publish") | ||
.Does<Configuration>(config => | ||
{ | ||
var artifacts = $"{config.Artifacts.Root}/arm"; | ||
EnsureDirectoryExists(artifacts); | ||
foreach(var directory in GetSubDirectories("./src/arm")) | ||
{ | ||
if(DirectoryExists(directory)) { | ||
var copyFrom = directory; | ||
var copyTo = $"{artifacts}/{directory.GetDirectoryName()}"; | ||
Information("{0} -> {1}", copyFrom, copyTo); | ||
EnsureDirectoryExists(copyTo); | ||
CopyDirectory(directory, copyTo); | ||
config.Artifacts.Add(ArtifactTypeOption.Other, directory.GetDirectoryName(), directory.FullPath); | ||
} | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#load "Configuration.cake" | ||
|
||
public static Configuration IncludeArtifactCopyTarget(this Configuration configuration, DirectoryPath sourceDirectory) | ||
{ | ||
var copyList = new HashSet<DirectoryPath>(); | ||
if(configuration.TaskParameters.TryGetValue("Artifacts:Copy__Items", out object value) && value is HashSet<DirectoryPath>) | ||
{ | ||
copyList = value as HashSet<DirectoryPath>; | ||
} | ||
else | ||
{ | ||
configuration.TaskParameters.Add("Artifacts:Copy__Items", copyList); | ||
} | ||
|
||
copyList.Add(sourceDirectory); | ||
|
||
return configuration; | ||
} | ||
|
||
public static bool HasArtifactCopyTargets(this Configuration configuration) | ||
{ | ||
var copyList = new HashSet<DirectoryPath>(); | ||
if(configuration.TaskParameters.TryGetValue("Artifacts:Copy__Items", out object value) && value is HashSet<DirectoryPath>) | ||
{ | ||
copyList = value as HashSet<DirectoryPath>; | ||
} | ||
|
||
return copyList.Any(); | ||
} | ||
|
||
public static IEnumerable<DirectoryPath> GetArtifactCopyTargets(this Configuration configuration) | ||
{ | ||
var copyList = new HashSet<DirectoryPath>(); | ||
if(configuration.TaskParameters.TryGetValue("Artifacts:Copy__Items", out object value) && value is HashSet<DirectoryPath>) | ||
{ | ||
copyList = value as HashSet<DirectoryPath>; | ||
} | ||
return copyList; | ||
} | ||
|
||
Task("Artifacts:Copy") | ||
.WithCriteria<Configuration>((ctx, config) => config.HasArtifactCopyTargets()) | ||
.IsDependentOn("Build") | ||
.IsDependeeOf("Publish") | ||
.Does<Configuration>(config => | ||
{ | ||
var artifacts = $"{config.Artifacts.Root}"; | ||
EnsureDirectoryExists(artifacts); | ||
foreach(var directory in config.GetArtifactCopyTargets()) | ||
{ | ||
var copyFrom = directory; | ||
var copyTo = $"{artifacts}/{directory.GetDirectoryName()}"; | ||
Information("{0} -> {1}", copyFrom, copyTo); | ||
EnsureDirectoryExists(copyTo); | ||
CopyDirectory(directory, copyTo); | ||
config.Artifacts.Add(ArtifactTypeOption.Other, directory.GetDirectoryName(), directory.FullPath); | ||
} | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#load "Configuration.cake" | ||
#addin nuget:?package=Newtonsoft.Json&version=12.0.1 | ||
using Newtonsoft.Json; | ||
|
||
public class EfMigration | ||
{ | ||
public string Id { get; set; } | ||
public string Name { get; set; } | ||
public string SafeName { get; set; } | ||
} | ||
|
||
public class EfContext | ||
{ | ||
public string FullName { get; set; } | ||
public string SafeName { get; set; } | ||
public string Name { get; set; } | ||
public string AssemblyQualifiedName { get; set; } | ||
} | ||
|
||
IEnumerable<EfContext> GetAllDbContexts(DirectoryPath workingDirectory, string configuration) | ||
{ | ||
var settings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = workingDirectory, | ||
RedirectStandardOutput = true | ||
}; | ||
|
||
settings.Arguments = string.Format("ef dbcontext list --configuration {0} --json --prefix-output", configuration); | ||
var list = Enumerable.Empty<EfContext>(); | ||
|
||
using(var process = StartAndReturnProcess("dotnet", settings)) | ||
{ | ||
process.WaitForExit(); | ||
if(process.GetExitCode() == 0) | ||
{ | ||
try | ||
{ | ||
var outputAsJson = string.Join(Environment.NewLine, process.GetStandardOutput().Where(l => l.StartsWith("data:")).Select(l => l.Replace("data:", ""))); | ||
list = JsonConvert.DeserializeObject<List<EfContext>>(outputAsJson); | ||
Verbose("Found {0} Db contexts", list.Count()); | ||
} | ||
catch(Exception exception) | ||
{ | ||
Error("Unable to determine db context's for {0} : {1}", workingDirectory, exception.Message); | ||
} | ||
} | ||
} | ||
return list.ToList(); | ||
} | ||
|
||
IEnumerable<EfMigration> GetMigrationsForContext(string dbContext, DirectoryPath workingDirectory, string configuration) | ||
{ | ||
var settings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = workingDirectory, | ||
RedirectStandardOutput = true | ||
}; | ||
|
||
settings.Arguments = string.Format("ef migrations list --configuration {0} --context {1} --json --prefix-output", configuration, dbContext); | ||
|
||
var list = Enumerable.Empty<EfMigration>(); | ||
using(var process = StartAndReturnProcess("dotnet", settings)) | ||
{ | ||
process.WaitForExit(); | ||
if(process.GetExitCode() == 0) | ||
{ | ||
try | ||
{ | ||
var outputAsJson = string.Join(Environment.NewLine, process.GetStandardOutput().Where(l => l.StartsWith("data:")).Select(l => l.Replace("data:", ""))); | ||
list = JsonConvert.DeserializeObject<List<EfMigration>>(outputAsJson); | ||
} | ||
catch(Exception exception) | ||
{ | ||
Error("Unable to determine db migration list for {0} : {1}", dbContext, exception.Message); | ||
} | ||
} | ||
} | ||
return list; | ||
} | ||
|
||
public static Configuration IncludeAsEfDbContext(this Configuration configuration, Func<CustomProjectParserResult, bool> includeAsEFDbContext) | ||
{ | ||
var projects = new HashSet<CustomProjectParserResult>(); | ||
if(configuration.TaskParameters.TryGetValue("Artifacts:DotNetCore:Ef:Migration-Script", out object value) && value is HashSet<CustomProjectParserResult>) | ||
{ | ||
projects = value as HashSet<CustomProjectParserResult>; | ||
} | ||
else | ||
{ | ||
configuration.TaskParameters.Add("Artifacts:DotNetCore:Ef:Migration-Script", projects); | ||
} | ||
|
||
var projectsToInclude = configuration.Solution.Projects.Where(includeAsEFDbContext).ToList(); | ||
|
||
if(projectsToInclude != null && projectsToInclude.Any()) | ||
{ | ||
projects.UnionWith(projectsToInclude); | ||
} | ||
|
||
return configuration; | ||
} | ||
|
||
public static bool HasCustomEfDbContextTargets(this Configuration configuration) | ||
{ | ||
var projectList = configuration.GetEfDbContextTargets(); | ||
return projectList?.Any() ?? false; | ||
} | ||
|
||
public static IEnumerable<CustomProjectParserResult> GetEfDbContextTargets(this Configuration configuration) | ||
{ | ||
var projects = new HashSet<CustomProjectParserResult>(); | ||
if(configuration.TaskParameters.TryGetValue("Artifacts:DotNetCore:Ef:Migration-Script", out object value) && value is HashSet<CustomProjectParserResult>) | ||
{ | ||
projects = value as HashSet<CustomProjectParserResult>; | ||
} | ||
return projects; | ||
} | ||
|
||
Task("Artifacts:DotNetCore:Ef:Migration-Script") | ||
.IsDependentOn("Build") | ||
.IsDependeeOf("Publish") | ||
.Does<Configuration>(config => | ||
{ | ||
var efProjects = (config.HasCustomEfDbContextTargets() ? config.GetEfDbContextTargets() : config.Solution.Projects).ToList(); | ||
|
||
Information("Generating scripts for {0} projects", efProjects.Count()); | ||
foreach(var project in efProjects) { | ||
var assemblyName = config.Solution.GetProjectName(project); | ||
var workingDirectory = project.ProjectFilePath.GetDirectory(); | ||
var availableDbContexts = GetAllDbContexts(workingDirectory, config.Solution.BuildConfiguration).ToList(); | ||
|
||
if(availableDbContexts.Any()) | ||
{ | ||
Information("Generating scripts for {0} containing {1} contexts", assemblyName, availableDbContexts.Count); | ||
foreach(var dbContext in availableDbContexts) | ||
{ | ||
Information("Generating Sql Script for {0}", dbContext.SafeName); | ||
var migrations = GetMigrationsForContext(dbContext.SafeName, workingDirectory, config.Solution.BuildConfiguration); | ||
|
||
var sqlScript = MakeAbsolute(File($"{config.Artifacts.Root}/sql/{dbContext.SafeName}.sql")); | ||
if(FileExists(sqlScript)) { | ||
DeleteFile(sqlScript); | ||
} | ||
|
||
var settings = new ProcessSettings() | ||
{ | ||
WorkingDirectory = workingDirectory | ||
}; | ||
|
||
settings.Arguments = string.Format("ef migrations script -i -o {0} --configuration {1} --context {2}", sqlScript, config.Solution.BuildConfiguration, dbContext.SafeName); | ||
|
||
using(var process = StartAndReturnProcess("dotnet", settings)) | ||
{ | ||
process.WaitForExit(); | ||
Verbose("Exit code: {0}", process.GetExitCode()); | ||
} | ||
|
||
config.Artifacts.Add(ArtifactTypeOption.Other, sqlScript.GetFilename().ToString(), sqlScript); | ||
} | ||
} | ||
} | ||
|
||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#load "Configuration.cake" | ||
|
||
Task("Build:DotNetCore") | ||
.IsDependentOn("Restore") | ||
.IsDependeeOf("Build") | ||
.Does<Configuration>(config => | ||
{ | ||
var buildSettings = new DotNetCoreBuildSettings() { | ||
NoRestore = true, | ||
Configuration = config.Solution.BuildConfiguration, | ||
NoIncremental = true, | ||
Verbosity = DotNetCoreVerbosity.Minimal | ||
}; | ||
|
||
DotNetCoreBuild(config.Solution.Path.ToString(), buildSettings); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#load "Configuration.cake" | ||
#load "Configuration-MsBuild.cake" | ||
|
||
Task("Build:MsBuild") | ||
.IsDependentOn("Restore") | ||
.IsDependeeOf("Build") | ||
.Does<Configuration>(config => | ||
{ | ||
var toolVersion = config.GetTaskParameter<MSBuildToolVersion>("MsBuild:Version", MSBuildToolVersion.Default); | ||
|
||
Information("MS Build Tool Version: " + toolVersion.ToString()); | ||
|
||
MSBuild(config.Solution.Path.ToString(), c => c | ||
.SetConfiguration(config.Solution.BuildConfiguration) | ||
.SetVerbosity(Verbosity.Minimal) | ||
.UseToolVersion(toolVersion) | ||
.WithWarningsAsError() | ||
.WithTarget("Build") | ||
); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#load "Configuration.cake" | ||
|
||
Task("CI:AppVeyor:UploadArtifacts") | ||
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnAppVeyor) | ||
.IsDependentOn("Publish") | ||
.IsDependeeOf("CI:UploadArtifacts") | ||
.Does<Configuration>(config => | ||
{ | ||
foreach(var artifact in config.Artifacts) { | ||
if(FileExists(artifact.Path)) { | ||
BuildSystem.AppVeyor.UploadArtifact(artifact.Path, new AppVeyorUploadArtifactsSettings { | ||
ArtifactType = artifact.Type == ArtifactTypeOption.WebDeploy ? AppVeyorUploadArtifactType.WebDeployPackage : AppVeyorUploadArtifactType.Auto | ||
}); | ||
} | ||
} | ||
}); | ||
|
||
Task("CI:AppVeyor:UpdateBuildNumber") | ||
.IsDependeeOf("CI:UpdateBuildNumber") | ||
.WithCriteria<Configuration>((ctx, config) => AppVeyor.IsRunningOnAppVeyor) | ||
.Does<Configuration>(config => | ||
{ | ||
AppVeyor.UpdateBuildVersion(config.Version.FullSemVersion +"." +AppVeyor.Environment.Build.Number); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#load "Configuration.cake" | ||
|
||
Task("CI:VSTS:UploadArtifacts") | ||
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnAzurePipelinesHosted || AzurePipelines.IsRunningOnAzurePipelines) | ||
.IsDependentOn("Publish") | ||
.IsDependeeOf("CI:UploadArtifacts") | ||
.Does<Configuration>(config => | ||
{ | ||
Information("Uploading artifacts from {0}", config.Artifacts.Root); | ||
AzurePipelines.Commands.UploadArtifact("artifacts", config.Artifacts.Root.ToString(), "artifacts"); | ||
}); | ||
|
||
Task("CI:VSTS:UpdateBuildNumber") | ||
.IsDependeeOf("CI:UpdateBuildNumber") | ||
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnAzurePipelinesHosted || AzurePipelines.IsRunningOnAzurePipelines) | ||
.Does<Configuration>(config => | ||
{ | ||
Information( | ||
@"Repository: | ||
Branch: {0} | ||
SourceVersion: {1} | ||
Shelveset: {2}", | ||
BuildSystem.AzurePipelines.Environment.Repository.SourceBranchName, | ||
BuildSystem.AzurePipelines.Environment.Repository.SourceVersion, | ||
BuildSystem.AzurePipelines.Environment.Repository.Shelveset | ||
); | ||
|
||
AzurePipelines.Commands.UpdateBuildNumber(config.Version.FullSemVersion); | ||
AzurePipelines.Commands.SetVariable("GitVersion.Version", config.Version.Version); | ||
AzurePipelines.Commands.SetVariable("GitVersion.SemVer", config.Version.SemVersion); | ||
AzurePipelines.Commands.SetVariable("GitVersion.InformationalVersion", config.Version.InformationalVersion); | ||
AzurePipelines.Commands.SetVariable("GitVersion.FullSemVer", config.Version.FullSemVersion); | ||
AzurePipelines.Commands.SetVariable("Cake.Version", config.Version.CakeVersion); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#load "CI-Azure-DevOps.cake" | ||
#load "CI-AppVeyor.cake" | ||
|
||
Task("CI") | ||
.IsDependentOn("CI:UpdateBuildNumber") | ||
.IsDependeeOf("Default") | ||
.IsDependentOn("CI:UploadArtifacts"); | ||
|
||
Task("CI:UpdateBuildNumber") | ||
.IsDependeeOf("CI").Does<Configuration>(config => Information("Build Number: {0}", config.Version.SemVersion)); | ||
|
||
Task("CI:UploadArtifacts") | ||
.IsDependeeOf("CI") | ||
.IsDependentOn("Publish"); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#load "Configuration.cake" | ||
public partial class Configuration { | ||
|
||
public Configuration SetMSBuildToolVersion(MSBuildToolVersion toolVersion, bool allowArgumentOverride = true) | ||
{ | ||
var argument = ParseEnum<MSBuildToolVersion>(context.Argument("MSBuildToolVersion", "Default")); | ||
|
||
var version = (allowArgumentOverride && argument != MSBuildToolVersion.Default) ? argument : toolVersion; | ||
|
||
this.TaskParameters.Add("MsBuild:Version", version); | ||
|
||
return this; | ||
} | ||
|
||
private static T ParseEnum<T>(string value) | ||
{ | ||
return (T) Enum.Parse(typeof(T), value, true); | ||
} | ||
} |
Oops, something went wrong.