mirror of
https://github.com/zoriya/octokit.net.git
synced 2025-12-06 07:16:09 +00:00
Add a code formatting task to CAKE (#1550)
* add FormatCode build task * add log message to indicate we generate temp .csproj files to run the code formatter
This commit is contained in:
committed by
Ryan Gribble
parent
3369a4aea1
commit
938149f78f
74
build/Tasks/FormatCode.cs
Normal file
74
build/Tasks/FormatCode.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
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 class FormatCode : FrostingTask<BuildContext>
|
||||
{
|
||||
public override void Run(BuildContext 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(BuildContext context)
|
||||
{
|
||||
return context.IsRunningOnWindows();
|
||||
}
|
||||
|
||||
private static string CreateTempCsproj(BuildContext 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;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"GitVersion.CommandLine": "3.6.2",
|
||||
"PdbGit": "3.0.38"
|
||||
"PdbGit": "3.0.38",
|
||||
"Octokit.CodeFormatter": "1.0.0-preview"
|
||||
},
|
||||
"frameworks": {
|
||||
"net45": {
|
||||
|
||||
Reference in New Issue
Block a user