Files
octokit.net/build/Tasks/FormatCode.cs
Clemens Lieb 5e751a6c38 Enable building on linux without project file changes (#1930)
* Introduce 'NoFramework' switch

This enables building via cake on linux without requiring adjustments on
other platforms, fixes #1745

* Revert hardcoding NoFramework in build.sh

* Skip GitVersion query for NoFramework build

This implies that NoFramework is intended for local use only
Accordingly a warning message has been set up in the Lifetime of the Build

* Rename NoFramework switch to CoreOnly

* Skip tool installation and formatting step for CoreOnly builds

* Default CoreOnly to false on windows and true otherwise

* Add CoreOnly switch to build variable output

* Fix 'native' msbuild on windows for both platforms

'native' msbuild invocations do not automatically set CoreOnly according to the platform
they are running on. As such we set CoreOnly to False on Windows, unless CoreOnly has been
specified already.
2019-01-31 15:36:30 +10:00

77 lines
2.5 KiB
C#

using System;
using System.IO;
using System.Linq;
using Cake.Common;
using Cake.Common.Diagnostics;
using Cake.Core;
using Cake.Core.IO;
using Cake.Frosting;
public sealed class FormatCode : FrostingTask<Context>
{
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");
}
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 => $"<Compile Include=\"{x}\" />")
.ToArray();
var csprojContent =
$@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""4.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<ItemGroup>
{string.Join(Environment.NewLine, compileElements)}
</ItemGroup>
<Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
</Project>";
File.WriteAllText(projectCsproj, csprojContent);
return projectCsproj;
}
}