Skip to content

Commit

Permalink
Merge pull request #2 from stormid/feature/fix-build
Browse files Browse the repository at this point in the history
Add updated cake mix files
  • Loading branch information
owen-roberts authored Oct 28, 2019
2 parents 38f65e6 + 778cd2c commit 282b857
Show file tree
Hide file tree
Showing 18 changed files with 299 additions and 167 deletions.
58 changes: 58 additions & 0 deletions .cake/Artifacts-Copy.cake
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);
}
});
95 changes: 68 additions & 27 deletions .cake/Artifacts-DotNetCore-Ef.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#load "Configuration.cake"
#addin nuget:?package=Newtonsoft.Json
#addin nuget:?package=Newtonsoft.Json&version=12.0.1
using Newtonsoft.Json;

public class EfMigration
Expand All @@ -25,7 +25,7 @@ IEnumerable<EfContext> GetAllDbContexts(DirectoryPath workingDirectory, string c
RedirectStandardOutput = true
};

settings.Arguments = string.Format("ef dbcontext list --configuration {0} --json", configuration);
settings.Arguments = string.Format("ef dbcontext list --configuration {0} --json --prefix-output", configuration);
var list = Enumerable.Empty<EfContext>();

using(var process = StartAndReturnProcess("dotnet", settings))
Expand All @@ -35,7 +35,7 @@ IEnumerable<EfContext> GetAllDbContexts(DirectoryPath workingDirectory, string c
{
try
{
var outputAsJson = string.Join(Environment.NewLine, process.GetStandardOutput());
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());
}
Expand All @@ -56,7 +56,7 @@ IEnumerable<EfMigration> GetMigrationsForContext(string dbContext, DirectoryPath
RedirectStandardOutput = true
};

settings.Arguments = string.Format("ef migrations list --configuration {0} --context {1} --json", configuration, dbContext);
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))
Expand All @@ -66,7 +66,7 @@ IEnumerable<EfMigration> GetMigrationsForContext(string dbContext, DirectoryPath
{
try
{
var outputAsJson = string.Join(Environment.NewLine, process.GetStandardOutput());
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)
Expand All @@ -78,45 +78,86 @@ IEnumerable<EfMigration> GetMigrationsForContext(string dbContext, DirectoryPath
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.Solution.Projects.ToList();
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();

Information("Generating scripts for {0} containing {1} contexts", assemblyName, availableDbContexts.Count);
foreach(var dbContext in availableDbContexts)
if(availableDbContexts.Any())
{
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);
}
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
};
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);
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());
}
using(var process = StartAndReturnProcess("dotnet", settings))
{
process.WaitForExit();
Verbose("Exit code: {0}", process.GetExitCode());
}

config.Artifacts.Add(ArtifactTypeOption.Other, sqlScript.GetFilename().ToString(), sqlScript);
config.Artifacts.Add(ArtifactTypeOption.Other, sqlScript.GetFilename().ToString(), sqlScript);
}
}
}

});
});
1 change: 0 additions & 1 deletion .cake/Build-MsBuild.cake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Task("Build:MsBuild")
MSBuild(config.Solution.Path.ToString(), c => c
.SetConfiguration(config.Solution.BuildConfiguration)
.SetVerbosity(Verbosity.Minimal)
.UseToolVersion(MSBuildToolVersion.VS2017)
.WithWarningsAsError()
.WithTarget("Build")
);
Expand Down
6 changes: 3 additions & 3 deletions .cake/CI-VSTS.cake → .cake/CI-Azure-DevOps.cake
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#load "Configuration.cake"

Task("CI:VSTS:UploadArtifacts")
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnVSTS || TFBuild.IsRunningOnTFS)
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnAzurePipelinesHosted || TFBuild.IsRunningOnAzurePipelines)
.IsDependentOn("Publish")
.IsDependeeOf("CI:UploadArtifacts")
.Does<Configuration>(config =>
{
Information("Uploading artifacts from {0}", config.Artifacts.Root);
TFBuild.Commands.UploadArtifact("artifacts", config.Artifacts.Root.ToString(), "artifacts");
TFBuild.Commands.UploadArtifact("artifacts", config.Artifacts.Root.ToString(), "artifacts");
});

Task("CI:VSTS:UpdateBuildNumber")
.IsDependeeOf("CI:UpdateBuildNumber")
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnVSTS || TFBuild.IsRunningOnTFS)
.WithCriteria<Configuration>((ctx, config) => BuildSystem.IsRunningOnAzurePipelinesHosted || TFBuild.IsRunningOnAzurePipelines)
.Does<Configuration>(config =>
{
Information(
Expand Down
2 changes: 1 addition & 1 deletion .cake/CI.cake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#load "CI-VSTS.cake"
#load "CI-Azure-DevOps.cake"
#load "CI-AppVeyor.cake"

Task("CI")
Expand Down
4 changes: 2 additions & 2 deletions .cake/Configuration-Version.cake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#tool "nuget:?package=GitVersion.CommandLine&prerelease"
#tool "nuget:?package=GitVersion.CommandLine&version=4.0.0&prerelease"

public class BuildVersion {
public FilePath VersionAssemblyInfo { get; private set; }
Expand Down Expand Up @@ -97,4 +97,4 @@ public class BuildVersion {
throw new Exception("Version information has not been determined, build failed!");
}
}
}
}
15 changes: 10 additions & 5 deletions .cake/Configuration.cake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#addin "Cake.Incubator&version=3.1.0"
#addin "Cake.Incubator&version=5.0.1"
#load "Configuration-Version.cake"

const string _solutionFilePathPattern = "*.sln";
Expand Down Expand Up @@ -56,6 +56,8 @@ public partial class Configuration {
return config;
}

public Dictionary<string, object> TaskParameters { get; } = new Dictionary<string, object>();

public BuildVersion Version { get; }

public SolutionParameters Solution { get; }
Expand All @@ -81,6 +83,8 @@ public partial class Configuration {
Artifacts = new ArtifactsParameters(context, artifactsRootPath ?? context.Directory("artifacts"));
}

public ICakeLog Logger => context.Log;

public void Log(ICakeLog logger)
{
Solution.Log(logger);
Expand Down Expand Up @@ -110,7 +114,7 @@ public struct Artifact {
}

public override string ToString() {
return $"{Name} ({Type}/{Category}) - {Path}";
return $"{Name} ({Type}/{Category}) - {Path.FullPath}";
}
}

Expand All @@ -133,7 +137,7 @@ public class ArtifactsParameters : List<Artifact> {
}

public void Add(ArtifactTypeOption type, string name, FilePath path) {
Add(new Artifact(type, name, path));
Add(new Artifact(type, name, context.MakeAbsolute(path)));
}
}

Expand All @@ -158,11 +162,12 @@ public class SolutionParameters {
}

private ISet<Func<CustomProjectParserResult, bool>> WebProjectResolvers { get; } = new HashSet<Func<CustomProjectParserResult, bool>>() {
p => p.IsWebApplication()
p => p.IsWebApplication(),
p => p.HasPackage("Microsoft.NET.Sdk.Functions")
};

private ISet<Func<CustomProjectParserResult, bool>> TestProjectResolvers { get; } = new HashSet<Func<CustomProjectParserResult, bool>>() {
p => p.IsDotNetCliTestProject()
p => p.IsTestProject()
};

private ISet<Func<CustomProjectParserResult, bool>> NuGetProjectResolvers { get; } = new HashSet<Func<CustomProjectParserResult, bool>>() {
Expand Down
1 change: 0 additions & 1 deletion .cake/Npm-RunScript.cake
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class NpmConfiguration

public bool CanExecuteNpm {
get {
cakeContext.Information($"{WorkingDirectory.TrimEnd('/')}/package.json");
return cakeContext.FileExists($"{WorkingDirectory.TrimEnd('/')}/package.json");
}
}
Expand Down
3 changes: 1 addition & 2 deletions .cake/Publish-Pack-DotNetCore.cake
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ Task("Publish:Pack:DotNetCore")
NoRestore = true,
IncludeSymbols = true,
Configuration = config.Solution.BuildConfiguration,
OutputDirectory = projectArtifactDirectory,
Verbosity = DotNetCoreVerbosity.Minimal
OutputDirectory = projectArtifactDirectory
};
settings.MSBuildSettings = new DotNetCoreMSBuildSettings();
settings.MSBuildSettings
Expand Down
4 changes: 3 additions & 1 deletion .cake/Publish-Pack-NuGet.cake
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Task("Publish:Pack:NuGet")
var projectArtifactDirectory = config.Artifacts.GetRootFor(ArtifactTypeOption.NuGet);

foreach(var nugetProject in config.Solution.NuGetProjects) {
var properties = new Dictionary<string,string>{ { "Configuration", "Release"} };
var nuGetPackSettings = new NuGetPackSettings {
Version = $"{config.Version.SemVersion}",
OutputDirectory = $"{projectArtifactDirectory}/"
OutputDirectory = $"{projectArtifactDirectory}/",
Properties = properties
};

NuGetPack(nugetProject.ProjectFilePath.ToString(), nuGetPackSettings);
Expand Down
3 changes: 1 addition & 2 deletions .cake/Publish-Zip-DotNetCore.cake
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ Task("Publish:Zip:DotNetCore")
{
NoRestore = true,
Configuration = config.Solution.BuildConfiguration,
OutputDirectory = publishDirectory,
Verbosity = DotNetCoreVerbosity.Minimal,
OutputDirectory = publishDirectory
};

settings.MSBuildSettings = new DotNetCoreMSBuildSettings();
Expand Down
Loading

0 comments on commit 282b857

Please sign in to comment.