From a687ded843d07119c0279d3806f8125ce78ff481 Mon Sep 17 00:00:00 2001 From: Brendan Forster Date: Tue, 24 Sep 2019 08:58:57 -0300 Subject: [PATCH] migrate to new dotnet-format tool in build script (run on local build) --- build/Context.cs | 1 + build/Lifetime.cs | 13 +------- build/Tasks/Build.cs | 1 + build/Tasks/FormatCode.cs | 67 ++------------------------------------- 4 files changed, 6 insertions(+), 76 deletions(-) diff --git a/build/Context.cs b/build/Context.cs index f0d93e199a..520d46aab2 100644 --- a/build/Context.cs +++ b/build/Context.cs @@ -29,6 +29,7 @@ public class Context : FrostingContext public Project[] Projects { get; set; } + public FilePath DotNetFormatToolPath { get; set; } public FilePath GitVersionToolPath { get; set; } public DotNetCoreTestSettings GetTestSettings() diff --git a/build/Lifetime.cs b/build/Lifetime.cs index 47029d71ac..ff1f19f88f 100644 --- a/build/Lifetime.cs +++ b/build/Lifetime.cs @@ -56,18 +56,7 @@ public override void Setup(Context context) new Project { Name = "Octokit.Tests.Integration", Path = "./Octokit.Tests.Integration/Octokit.Tests.Integration.csproj", IntegrationTests = true } }; - // Install tools - if (context.CoreOnly) - { - context.Information("Skipping tool installation for core-only build"); - } - else - { - context.Information("Installing tools..."); - ToolInstaller.Install(context, "Octokit.CodeFormatter", "1.0.0-preview"); - } - - + context.DotNetFormatToolPath = ToolInstaller.DotNetCoreToolInstall(context, "dotnet-format", "3.1.37601", "dotnet-format"); context.GitVersionToolPath = ToolInstaller.DotNetCoreToolInstall(context, "GitVersion.Tool", "5.0.0", "dotnet-gitversion"); // Calculate semantic version. diff --git a/build/Tasks/Build.cs b/build/Tasks/Build.cs index 99f4d8b750..af1df81e34 100644 --- a/build/Tasks/Build.cs +++ b/build/Tasks/Build.cs @@ -4,6 +4,7 @@ using Cake.Frosting; [Dependency(typeof(Restore))] +[Dependency(typeof(FormatCode))] public class Build : FrostingTask { public override void Run(Context context) diff --git a/build/Tasks/FormatCode.cs b/build/Tasks/FormatCode.cs index 84f7528e9f..2057b34369 100644 --- a/build/Tasks/FormatCode.cs +++ b/build/Tasks/FormatCode.cs @@ -1,76 +1,15 @@ -using System; -using System.IO; -using System.Linq; -using Cake.Common; -using Cake.Common.Diagnostics; -using Cake.Core; -using Cake.Core.IO; +using Cake.Common.Tools.DotNetCore; using Cake.Frosting; public sealed class FormatCode : FrostingTask { public override void Run(Context context) { - var codeFormatterExe = context.FileSystem - .GetDirectory("tools") - .GetFiles("CodeFormatter.exe", SearchScope.Recursive) - .First() - .Path - .MakeAbsolute(context.Environment); - - foreach (var project in context.Projects) - { - context.Information("Formatting code of {0}", project.Name); - - var tempCsprojFile = CreateTempCsproj(context, project.Name); - context.Information("Generated temporary {0} file to run the formatter", new FilePath(tempCsprojFile).GetFilename()); - - var exitCode = context.StartProcess( - codeFormatterExe, - $"{tempCsprojFile} /nocopyright /nounicode"); - - if (exitCode != 0) - { - throw new CakeException($"An error occured while formatting code of {project.Name}"); - } - } - - context.Information("Successfully formatted code of all the projects"); + context.DotNetCoreTool("format"); } public override bool ShouldRun(Context context) { - // Core only builds do not download the formatter exe - // Only windows is guaranteed to be able to run exe files in the first place - return context.IsRunningOnWindows() && !context.CoreOnly; - } - - private static string CreateTempCsproj(Context context, string projectName) - { - DirectoryPath tempFolder = System.IO.Path.GetTempPath(); - var projectCsproj = tempFolder.CombineWithFilePath($"{projectName}.csproj").FullPath; - - var files = context.FileSystem - .GetDirectory(projectName) - .GetFiles("*.cs", SearchScope.Recursive) - .Select(x => x.Path.MakeAbsolute(context.Environment)) - .ToArray(); - - var compileElements = files - .Select(x => $"") - .ToArray(); - - var csprojContent = -$@" - - - {string.Join(Environment.NewLine, compileElements)} - - -"; - - File.WriteAllText(projectCsproj, csprojContent); - - return projectCsproj; + return context.IsLocalBuild; } }