mirror of
https://github.com/CommunityToolkit/dotnet.git
synced 2026-10-20 22:26:51 +08:00
121 lines
3.4 KiB
C#
121 lines
3.4 KiB
C#
#module nuget:?package=Cake.LongPath.Module&version=1.0.1
|
|
|
|
#addin nuget:?package=Cake.FileHelpers&version=4.0.1
|
|
#addin nuget:?package=Cake.Powershell&version=1.0.1
|
|
#addin nuget:?package=Cake.GitVersioning&version=3.4.220
|
|
|
|
#tool nuget:?package=MSTest.TestAdapter&version=2.2.5
|
|
#tool nuget:?package=vswhere&version=2.8.4
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Text.RegularExpressions;
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// ARGUMENTS
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
var target = Argument("target", "Default");
|
|
var configuration = Argument("configuration", "Release");
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// VARIABLES
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
var baseDir = MakeAbsolute(Directory("../")).ToString();
|
|
var binDir = baseDir + "/bin";
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// METHODS
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
void VerifyHeaders(bool Replace)
|
|
{
|
|
var header = FileReadText("header.txt") + "\r\n";
|
|
bool hasMissing = false;
|
|
|
|
Func<IFileSystemInfo, bool> exclude_objDir = fileSystemInfo => !fileSystemInfo.Path.Segments.Contains("obj");
|
|
|
|
var files = GetFiles(baseDir + "/**/*.cs", new GlobberSettings { Predicate = exclude_objDir }).Where(file =>
|
|
{
|
|
var path = file.ToString();
|
|
return !(path.EndsWith(".g.cs") || path.EndsWith(".i.cs") || System.IO.Path.GetFileName(path).Contains("TemporaryGeneratedFile"));
|
|
});
|
|
|
|
Information("\nChecking " + files.Count() + " file header(s)");
|
|
|
|
foreach(var file in files)
|
|
{
|
|
var oldContent = FileReadText(file);
|
|
|
|
if(oldContent.Contains("// <auto-generated>") ||
|
|
oldContent.Contains("// <auto-generated/>"))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var rgx = new Regex("^(//.*\r?\n)*\r?\n");
|
|
var newContent = header + rgx.Replace(oldContent, "");
|
|
|
|
if(!newContent.Equals(oldContent, StringComparison.Ordinal))
|
|
{
|
|
if(Replace)
|
|
{
|
|
Information("\nUpdating " + file + " header...");
|
|
FileWriteText(file, newContent);
|
|
}
|
|
else
|
|
{
|
|
Error("\nWrong/missing header on " + file);
|
|
|
|
hasMissing = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!Replace && hasMissing)
|
|
{
|
|
throw new Exception("Please run UpdateHeaders.bat or '.\\build.ps1 -Target UpdateHeaders' and commit the changes.");
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// TASK TARGETS
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
Task("Clean")
|
|
.Description("Cleans the output folder")
|
|
.Does(() =>
|
|
{
|
|
if(DirectoryExists(binDir))
|
|
{
|
|
Information("\nCleaning working directory");
|
|
CleanDirectory(binDir);
|
|
}
|
|
else
|
|
{
|
|
CreateDirectory(binDir);
|
|
}
|
|
});
|
|
|
|
Task("Verify")
|
|
.Description("Verifies the headers in *.cs files")
|
|
.IsDependentOn("Clean")
|
|
.Does(() =>
|
|
{
|
|
VerifyHeaders(false);
|
|
});
|
|
|
|
Task("UpdateHeaders")
|
|
.Description("Updates the headers in *.cs files")
|
|
.Does(() =>
|
|
{
|
|
VerifyHeaders(true);
|
|
});
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
// EXECUTION
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
RunTarget(target);
|