Update cake scripts to support checking file headers

Make file headers consistent for now, TODO: find out how/where we call out .NET foundation now
This commit is contained in:
michael-hawker 2020-05-18 16:30:38 -07:00
Родитель 8d2b183ccf
Коммит e1bf67032f
19 изменённых файлов: 300 добавлений и 21 удалений

Просмотреть файл

@ -1,4 +1,6 @@
using ColorCode.Compilation;
// Copyright (c) Microsoft Corporation. All rights reserved.
using ColorCode.Compilation;
using ColorCode.Parsing;
using ColorCode.Styling;
using System.Collections.Generic;

Просмотреть файл

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Collections.Generic;
using ColorCode.Common;

Просмотреть файл

@ -1,7 +1,7 @@
// Copyright (c) 2015 Christopher Pardi.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) 2015 Christopher Pardi.
using System.Collections.Generic;
using ColorCode.Common;

Просмотреть файл

@ -1,4 +1,6 @@
using ColorCode.Common;
// Copyright (c) Microsoft Corporation. All rights reserved.
using ColorCode.Common;
namespace ColorCode.Styling
{

Просмотреть файл

@ -1,4 +1,6 @@
using ColorCode.Common;
// Copyright (c) Microsoft Corporation. All rights reserved.
using ColorCode.Common;
namespace ColorCode.Styling
{

Просмотреть файл

@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using ColorCode.Common;

Просмотреть файл

@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using Windows.UI.Xaml.Media;
namespace ColorCode.UWP.Common

Просмотреть файл

@ -1,4 +1,6 @@
using System.Collections.Generic;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Collections.Generic;
using ColorCode.Parsing;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Documents;

Просмотреть файл

@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Linq;
using System.Threading.Tasks;

Просмотреть файл

@ -1,4 +1,6 @@
using System;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;

Просмотреть файл

@ -1,4 +1,6 @@
using ColorCode.Common;
// Copyright (c) Microsoft Corporation. All rights reserved.
using ColorCode.Common;
using ColorCode.Styling;
using ColorCode.UWP.Common;
using System;

Просмотреть файл

@ -1,4 +1,6 @@
using System.Reflection;
// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following

Просмотреть файл

@ -0,0 +1,165 @@
$script:ns = 'http://schemas.microsoft.com/developer/msbuild/2003'
$ErrorActionPreference = 'Stop'
# Unique set of Windows SDK versions referenced in files
$versions = New-Object System.Collections.Generic.HashSet[System.String]
function Get-Nodes
{
param(
[parameter(ValueFromPipeline=$true)]
[xml] $xml,
[parameter(Mandatory=$true)]
[string] $nodeName)
# Try the old style csproj. Also format required for .targets and .props files
$n = Select-Xml -Xml $xml.Project -Namespace @{d = $ns } -XPath "//d:$nodeName"
# Try the SDK-style files
if (!$n) {
$r = Select-Xml -Xml $xml.Project -XPath "//$nodeName"
}
return $r
}
function Get-NodeValue
{
param(
[parameter(ValueFromPipeline=$true)]
[xml] $xml,
[string] $nodeName)
$node = get-nodes $xml $nodeName
if ($node) {
if ($node.Node) {
return [string]$node.Node.'#text'
}
}
return [string]""
}
function Get-SdkVersion
{
param(
[Parameter(ValueFromPipeline=$true)] $file)
[xml] $xml = Get-Content $file
# If you want a complete set of SDKs that are required, uncomment the following
# $version = Get-NodeValue $xml 'PropertyGroup/TargetPlatformMinVersion'
# $versions.Add($version) | Out-Null
$version = Get-NodeValue $xml 'PropertyGroup/TargetPlatformVersion'
$versions.Add($version) | Out-Null
# Versions may also be specified without the 10.0.xxxxx.0 format in the
# PropertyGroup/DefaultTargetPlatformVersion and PropertyGroup/DefaultTargetPlatformMinVersion
# If you want a complete set of SDKs that are required, uncomment the following
# $version = Get-NodeValue $xml 'PropertyGroup/DefaultTargetPlatformMinVersion'
# $versions.Add("10.0." + $version + ".0") | Out-Null
$version = Get-NodeValue $xml 'PropertyGroup/DefaultTargetPlatformVersion'
$versions.Add("10.0." + $version + ".0") | Out-Null
}
function Test-RegistryPathAndValue
{
param (
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $path,
[parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string] $value)
try
{
if (Test-Path $path)
{
Get-ItemProperty -Path $path | Select-Object -ExpandProperty $value -ErrorAction Stop | Out-Null
return $true
}
}
catch
{
}
return $false
}
function Test-InstallWindowsSdk([string] $WindowsSDKVersion) {
$retval = $true
$WindowsSDKRegPath = "HKLM:\Software\Microsoft\Windows Kits\Installed Roots"
$WindowsSDKRegRootKey = "KitsRoot10"
$WindowsSDKOptions = @("OptionId.UWPCpp")
$WindowsSDKInstalledRegPath = "$WindowsSDKRegPath\$WindowsSDKVersion\Installed Options"
if (Test-RegistryPathAndValue -Path $WindowsSDKRegPath -Value $WindowsSDKRegRootKey)
{
# A Windows SDK is installed
# Is an SDK of our version installed with the options we need?
if (Test-RegistryPathAndValue -Path $WindowsSDKInstalledRegPath -Value "$WindowsSDKOptions")
{
# It appears we have what we need. Double check the disk
$sdkRoot = Get-ItemProperty -Path $WindowsSDKRegPath | Select-Object -ExpandProperty $WindowsSDKRegRootKey
if ($sdkRoot)
{
if (Test-Path $sdkRoot)
{
$refPath = Join-Path $sdkRoot "References\$WindowsSDKVersion"
if (Test-Path $refPath)
{
$umdPath = Join-Path $sdkRoot "UnionMetadata\$WindowsSDKVersion"
if (Test-Path $umdPath)
{
# Pretty sure we have what we need
$retval = $false
}
}
}
}
}
}
return $retval
}
if(!$PSScriptRoot){
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
Write-Host -NoNewline "Locating referenced Windows SDK versions..."
Get-ChildItem *.csproj -Recurse | ForEach-Object { get-sdkversion $_}
Get-ChildItem *.targets -Recurse | ForEach-Object { get-sdkversion $_ }
Get-ChildItem *.props -Recurse | ForEach-Object { get-sdkversion $_ }
Write-Host "Done"
Write-Host
$anyInstallRequired = $false;
foreach($version in $versions) {
if ($version -match "10\.0\.\d{5}\.0") {
$installRequired = Test-InstallWindowsSDK $version
Write-Host "Windows SDK '$version' install required: $installRequired"
if ($installRequired) {
# Automatically invoke Install-WindowsSDKIso.ps1 ?
$anyInstallRequired = $true
}
}
}
Write-Host
if ($anyInstallRequired) {
throw "At least one Windows SDK is missing from this machine"
} else {
Write-Host "All referenced Windows SDKs are installed!"
}

3
build/UpdateHeaders.bat Normal file
Просмотреть файл

@ -0,0 +1,3 @@
@ECHO OFF
PowerShell.exe -file "%~dp0build.ps1" -target=UpdateHeaders
PAUSE

Просмотреть файл

@ -1,6 +1,12 @@
#addin "Cake.Powershell"
#module nuget:?package=Cake.LongPath.Module&version=0.7.0
#addin nuget:?package=Cake.FileHelpers&version=3.2.1
#addin nuget:?package=Cake.Powershell&version=0.4.8
using System;
using System.Linq;
using System.Text.RegularExpressions;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
@ -18,10 +24,68 @@ var toolsDir = buildDir + "/tools";
var Solution = baseDir + "/ColorCode.sln";
var nupkgDir = buildDir + "/nupkg";
var gitVersioningVersion = "2.1.23";
var gitVersioningVersion = "2.1.65";
var versionClient = toolsDir + "/nerdbank.gitversioning/tools/Get-Version.ps1";
string Version = null;
//////////////////////////////////////////////////////////////////////
// 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>"))
{
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.");
}
}
void RetrieveVersion()
{
Information("\nRetrieving version...");
var results = StartPowershellFile(versionClient);
Version = results[1].Properties["NuGetPackageVersion"].Value.ToString();
Information("\nBuild Version: " + Version);
}
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
@ -40,8 +104,19 @@ Task("Restore-NuGet-Packages")
NuGetRestore(Solution);
});
Task("Verify")
.Description("Run pre-build verifications")
.IsDependentOn("Clean")
.Does(() =>
{
VerifyHeaders(false);
StartPowershellFile("./Find-WindowsSDKVersions.ps1");
});
Task("Version")
.Description("Updates the version information in all Projects")
.IsDependentOn("Verify")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
@ -54,10 +129,7 @@ Task("Version")
NuGetInstall(new []{"nerdbank.gitversioning"}, installSettings);
Information("\nRetrieving version...");
var results = StartPowershellFile(versionClient);
Version = results[1].Properties["NuGetPackageVersion"].Value.ToString();
Information("\nBuild Version: " + Version);
RetrieveVersion();
});
Task("Build")
@ -87,6 +159,13 @@ Task("Build")
Task("Default")
.IsDependentOn("Build");
Task("UpdateHeaders")
.Description("Updates the headers in *.cs files")
.Does(() =>
{
VerifyHeaders(true);
});
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -127,6 +127,17 @@ if (!(Test-Path $PACKAGES_CONFIG)) {
}
}
# Try find NuGet.exe in path if not exists
if (!(Test-Path $NUGET_EXE)) {
Write-Verbose -Message "Trying to find nuget.exe in PATH..."
$existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) }
$NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select -First 1
if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName)) {
Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)."
$NUGET_EXE = $NUGET_EXE_IN_PATH.FullName
}
}
# Try download NuGet.exe if not exists
if (!(Test-Path $NUGET_EXE)) {
Write-Verbose -Message "Downloading NuGet.exe..."

1
build/header.txt Normal file
Просмотреть файл

@ -0,0 +1 @@
// Copyright (c) Microsoft Corporation. All rights reserved.

Просмотреть файл

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Cake" version="0.22.2" />
<package id="Cake" version="0.37.0" />
</packages>

Просмотреть файл

@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"MSBuild.Sdk.Extras": "1.6.55"
"MSBuild.Sdk.Extras": "2.0.54"
}
}