Skip to content

Commit

Permalink
Refactor MSBuild setup and reduce amount of logging
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinCampbell committed Jul 13, 2017
1 parent 2caf248 commit 7d2a745
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 147 deletions.
108 changes: 21 additions & 87 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#addin "Newtonsoft.Json"

#load "scripts/common.cake"
#load "scripts/runhelpers.cake"
#load "scripts/archiving.cake"
Expand All @@ -8,8 +6,6 @@

using System.ComponentModel;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

// Arguments
var target = Argument("target", "Default");
Expand All @@ -20,62 +16,9 @@ var installFolder = Argument("install-path",
var requireArchive = HasArgument("archive");
var useGlobalDotNetSdk = HasArgument("use-global-dotnet-sdk");

var env = new BuildEnvironment(IsRunningOnWindows(), useGlobalDotNetSdk);

/// <summary>
/// Class representing build.json
/// </summary>
public class BuildPlan
{
public string DotNetInstallScriptURL { get; set; }
public string DotNetChannel { get; set; }
public string DotNetVersion { get; set; }
public string LegacyDotNetVersion { get; set; }
public string DownloadURL { get; set; }
public string MSBuildRuntimeForMono { get; set; }
public string MSBuildLibForMono { get; set; }
public string[] Frameworks { get; set; }
public string MainProject { get; set; }
public string[] TestProjects { get; set; }
public string[] TestAssets { get; set; }
public string[] LegacyTestAssets { get; set; }

private string currentRid;
private string[] targetRids;

public void SetCurrentRid(string currentRid)
{
this.currentRid = currentRid;
}

public string CurrentRid => currentRid;
public string[] TargetRids => targetRids;

public void SetTargetRids(params string[] targetRids)
{
this.targetRids = targetRids;
}

public string GetDefaultRid()
{
if (currentRid.StartsWith("win"))
{
return currentRid.EndsWith("-x86")
? "win7-x86"
: "win7-x64";
}

return currentRid;
}

public static BuildPlan Load(BuildEnvironment env)
{
var buildJsonPath = PathHelper.Combine(env.WorkingDirectory, "build.json");
return JsonConvert.DeserializeObject<BuildPlan>(
System.IO.File.ReadAllText(buildJsonPath));
}
}
Log.Context = Context;

var env = new BuildEnvironment(IsRunningOnWindows(), useGlobalDotNetSdk);
var buildPlan = BuildPlan.Load(env);

/// <summary>
Expand All @@ -84,15 +27,15 @@ var buildPlan = BuildPlan.Load(env);
Task("Cleanup")
.Does(() =>
{
if (DirectoryExists(env.Folders.Artifacts))
if (DirectoryHelper.Exists(env.Folders.Artifacts))
{
DeleteDirectory(env.Folders.Artifacts, recursive: true);
DirectoryHelper.Delete(env.Folders.Artifacts, recursive: true);
}
CreateDirectory(env.Folders.Artifacts);
CreateDirectory(env.Folders.ArtifactsLogs);
CreateDirectory(env.Folders.ArtifactsPackage);
CreateDirectory(env.Folders.ArtifactsScripts);
DirectoryHelper.Create(env.Folders.Artifacts);
DirectoryHelper.Create(env.Folders.ArtifactsLogs);
DirectoryHelper.Create(env.Folders.ArtifactsPackage);
DirectoryHelper.Create(env.Folders.ArtifactsScripts);
});

/// <summary>
Expand All @@ -110,7 +53,7 @@ Task("SetupMSBuild")
.IsDependentOn("BuildEnvironment")
.Does(() =>
{
SetupMSBuild(env);
SetupMSBuild(env, buildPlan);
});

/// <summary>
Expand Down Expand Up @@ -295,31 +238,27 @@ Task("PrepareTestAssets")
// Restore and build test assets
foreach (var project in buildPlan.TestAssets)
{
var folder = CombinePaths(env.Folders.TestAssets, "test-projects", project);
Information("Restoring and building: {0}...", project);
Information($"Restoring packages in {folder}...");
var folder = CombinePaths(env.Folders.TestAssets, "test-projects", project);
RunTool(env.DotNetCommand, "restore", folder)
.ExceptionOnError($"Failed to restore '{folder}'.");
Information($"Building {folder}...");
RunTool(env.DotNetCommand, "build", folder)
.ExceptionOnError($"Failed to restore '{folder}'.");
}
// Restore and build legacy test assets with legacy .NET Core SDK
foreach (var project in buildPlan.LegacyTestAssets)
{
var folder = CombinePaths(env.Folders.TestAssets, "test-projects", project);
Information("Restoring and building project.json: {0}...", project);
Information($"Restoring project.json packages in {folder}...");
var folder = CombinePaths(env.Folders.TestAssets, "test-projects", project);
RunTool(env.LegacyDotNetCommand, "restore", folder)
.ExceptionOnError($"Failed to restore '{folder}'.");
Information($"Building {folder}...");
RunTool(env.LegacyDotNetCommand, $"build", folder)
.ExceptionOnError($"Failed to restore '{folder}'.");
}
Expand All @@ -337,7 +276,7 @@ void BuildProject(BuildEnvironment env, string projectName, string projectFilePa

var logFileName = CombinePaths(env.Folders.ArtifactsLogs, $"{projectName}-build.log");

Information($"Building {projectName}...");
Information("Building {0}...", projectName);

RunTool(command, arguments, env.WorkingDirectory, logFileName)
.ExceptionOnError($"Building {projectName} failed.");
Expand Down Expand Up @@ -425,8 +364,8 @@ Task("Test")
// Copy xunit executable to test folder to solve path errors
var xunitToolsFolder = CombinePaths(env.Folders.Tools, "xunit.runner.console", "tools");
var xunitInstancePath = CombinePaths(instanceFolder, "xunit.console.exe");
System.IO.File.Copy(CombinePaths(xunitToolsFolder, "xunit.console.exe"), xunitInstancePath, true);
System.IO.File.Copy(CombinePaths(xunitToolsFolder, "xunit.runner.utility.net452.dll"), CombinePaths(instanceFolder, "xunit.runner.utility.net452.dll"), true);
FileHelper.Copy(CombinePaths(xunitToolsFolder, "xunit.console.exe"), xunitInstancePath, overwrite: true);
FileHelper.Copy(CombinePaths(xunitToolsFolder, "xunit.runner.utility.net452.dll"), CombinePaths(instanceFolder, "xunit.runner.utility.net452.dll"), overwrite: true);
var targetPath = CombinePaths(instanceFolder, $"{testProject}.dll");
var logFile = CombinePaths(env.Folders.ArtifactsLogs, $"{testProject}-desktop-result.xml");
var arguments = $"\"{targetPath}\" -parallel none -xml \"{logFile}\" -notrait category=failing";
Expand All @@ -439,7 +378,7 @@ Task("Test")
else
{
// Copy the Mono-built Microsoft.Build.* binaries to the test folder.
CopyDirectory($"{env.Folders.MonoMSBuildLib}", instanceFolder);
DirectoryHelper.Copy($"{env.Folders.MonoMSBuildLib}", instanceFolder);
Run("mono", $"\"{xunitInstancePath}\" {arguments}", instanceFolder)
.ExceptionOnError($"Test {testProject} failed for net46");
Expand Down Expand Up @@ -533,18 +472,18 @@ Task("OnlyPublish")
? $"{env.ShellArgument} msbuild.{env.ShellScriptFileExtension} {args}"
: args;
Information($"Publishing {projectName} for {framework}/{rid}...");
Information("Publishing {0} for {1}/{2}...", projectName, framework, rid);
RunTool(command, args, env.WorkingDirectory)
.ExceptionOnError($"Failed to publish {project} for {framework}/{rid}");
// Copy MSBuild and SDKs to output
CopyDirectory($"{env.Folders.MSBuildBase}-{framework}", CombinePaths(outputFolder, "msbuild"));
DirectoryHelper.Copy($"{env.Folders.MSBuildBase}-{framework}", CombinePaths(outputFolder, "msbuild"));
// For OSX/Linux net46 builds, copy the MSBuild libraries built for Mono.
if (!IsRunningOnWindows() && framework == "net46")
{
CopyDirectory($"{env.Folders.MonoMSBuildLib}", outputFolder);
DirectoryHelper.Copy($"{env.Folders.MonoMSBuildLib}", outputFolder);
}
if (requireArchive)
Expand Down Expand Up @@ -619,12 +558,7 @@ Task("TestPublished")
Task("CleanupInstall")
.Does(() =>
{
if (System.IO.Directory.Exists(installFolder))
{
System.IO.Directory.Delete(installFolder, true);
}
System.IO.Directory.CreateDirectory(installFolder);
DirectoryHelper.ForceCreate(installFolder);
});

/// <summary>
Expand Down
142 changes: 142 additions & 0 deletions scripts/common.cake
Original file line number Diff line number Diff line change
@@ -1,8 +1,96 @@
#addin "Newtonsoft.Json"

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static class Log
{
public static ICakeContext Context { get; set; }

public static void Write(Verbosity verbosity, LogLevel logLevel, string message, params object[] args) =>
Context.Log.Write(verbosity, logLevel, message, args);

public static void Debug(Verbosity verbosity, string message, params object[] args) =>
Write(verbosity, LogLevel.Debug, message, args);
}

public static class FileHelper
{
public static void Copy(string source, string destination, bool overwrite = false)
{
Log.Debug(Verbosity.Diagnostic, "Copy file: {0} to {1}.", source, destination);
System.IO.File.Copy(source, destination, overwrite);
}

public static void Delete(string path)
{
Log.Debug(Verbosity.Diagnostic, "Delete file: {0}.", path);
System.IO.File.Delete(path);
}
}

public static class DirectoryHelper
{
public static void Copy(string source, string destination)
{
var files = System.IO.Directory.GetFiles(source);
var subDirectories = System.IO.Directory.GetDirectories(source);

if (!Exists(destination))
{
Create(destination);
}

foreach (var file in files)
{
var newFile = PathHelper.Combine(destination, PathHelper.GetFileName(file));
FileHelper.Copy(file, newFile);
}

foreach (var subDirectory in subDirectories)
{
var newSubDirectory = PathHelper.Combine(destination, PathHelper.GetFileName(subDirectory));
Copy(subDirectory, newSubDirectory);
}
}

public static void Create(string path)
{
Log.Debug(Verbosity.Diagnostic, "Create directory: {0}.", path);
System.IO.Directory.CreateDirectory(path);
}

public static void Delete(string path, bool recursive)
{
Log.Debug(Verbosity.Diagnostic, "Delete directory: {0}.", path);
System.IO.Directory.Delete(path, recursive);
}

public static bool Exists(string path) =>
System.IO.Directory.Exists(path);

public static void ForceCreate(string path)
{
if (Exists(path))
{
Delete(path, recursive: true);
}

Create(path);
}
}

public static class PathHelper
{
public static string Combine(params string[] paths) =>
System.IO.Path.Combine(paths);

public static string GetDirectoryName(string path) =>
System.IO.Path.GetDirectoryName(path);

public static string GetFileName(string path) =>
System.IO.Path.GetFileName(path);

public static string GetFullPath(string path) =>
System.IO.Path.Combine(path);
}
Expand Down Expand Up @@ -86,6 +174,60 @@ public class BuildEnvironment
}
}

/// <summary>
/// Class representing build.json
/// </summary>
public class BuildPlan
{
public string DotNetInstallScriptURL { get; set; }
public string DotNetChannel { get; set; }
public string DotNetVersion { get; set; }
public string LegacyDotNetVersion { get; set; }
public string DownloadURL { get; set; }
public string MSBuildRuntimeForMono { get; set; }
public string MSBuildLibForMono { get; set; }
public string[] Frameworks { get; set; }
public string MainProject { get; set; }
public string[] TestProjects { get; set; }
public string[] TestAssets { get; set; }
public string[] LegacyTestAssets { get; set; }

private string currentRid;
private string[] targetRids;

public void SetCurrentRid(string currentRid)
{
this.currentRid = currentRid;
}

public string CurrentRid => currentRid;
public string[] TargetRids => targetRids;

public void SetTargetRids(params string[] targetRids)
{
this.targetRids = targetRids;
}

public string GetDefaultRid()
{
if (currentRid.StartsWith("win"))
{
return currentRid.EndsWith("-x86")
? "win7-x86"
: "win7-x64";
}

return currentRid;
}

public static BuildPlan Load(BuildEnvironment env)
{
var buildJsonPath = PathHelper.Combine(env.WorkingDirectory, "build.json");
return JsonConvert.DeserializeObject<BuildPlan>(
System.IO.File.ReadAllText(buildJsonPath));
}
}

void PrintBlankLine()
{
Information("");
Expand Down
Loading

0 comments on commit 7d2a745

Please sign in to comment.