-
Notifications
You must be signed in to change notification settings - Fork 320
/
VerifyCopyrightHeaders.ps1
59 lines (51 loc) · 2.15 KB
/
VerifyCopyrightHeaders.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<#
.SYNOPSIS
Verify copyright headers exist for Windows App SDK source files
.PARAMETER Fix
Add copyright headers to files that are missing them
.EXAMPLE
VerifyCopyrightHeaders -Fix
#>
Param(
[switch]$Fix = $false
)
Set-StrictMode -Version 3.0
$ErrorActionPreference = 'Stop'
$copyrightHeaderText = (
"Copyright (c) Microsoft Corporation and Contributors.",
"Licensed under the MIT License."
)
$include = ('*.cs', '*.cpp', '*.h', '*.idl', '*.xaml')
$exclude = [RegEx]'\\dev\\Detours\\|\\BuildOutput\\|\\obj\\|\\localpackages\\|\\packages\\|\\specs\\|\\temp\\|\\Debug\\|\\Release\\|\\ItemTemplates\\|\\ProjectTemplates\\|\\WindowsAppSDKAggregator\\'
$files = dir $PSScriptRoot\..\* -recurse -include $include | Where FullName -notmatch $exclude
$errorCount = 0
foreach ($file in $files) {
$found = Select-String -Path $file.FullName -Pattern 'Copyright (c) Microsoft Corporation' -CaseSensitive -SimpleMatch
if ([string]::IsNullOrEmpty($found)) {
$errorCount++
if ($Fix) {
$copyrightText = "";
if ($file.Extension -eq ".xaml") {
$copyrightText = "<!-- " + $copyrightHeaderText[0] + " -->" + [Environment]::NewLine `
+ "<!-- " + $copyrightHeaderText[1] + " -->" + [Environment]::NewLine + [Environment]::NewLine
} else {
$copyrightText = "// " + $copyrightHeaderText[0] + [Environment]::NewLine `
+ "// " + $copyrightHeaderText[1] + [Environment]::NewLine + [Environment]::NewLine
}
$copyrightText + ((Get-Content $file.FullName) -join [Environment]::NewLine) | Set-Content $file.FullName
Write-Host $file.FullName -ForegroundColor green
} else {
Write-Host $file.FullName -ForegroundColor red
}
}
}
if ($errorCount -gt 0) {
if ($Fix) {
Write-Host "Added copyright text to $errorCount files." -ForegroundColor green
Exit 0
} else {
Write-Host "Copyright missing from $errorCount files." -ForegroundColor red
Write-Host "Run 'tools\\VerifyCopyrightHeaders.cmd -Fix' locally to update files."
Exit 1
}
}