This repository has been archived by the owner on Sep 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCopyrightCheck.ps1
54 lines (41 loc) · 1.77 KB
/
CopyrightCheck.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
param([string]$SrcRoot,
[string]$TestRoot)
$rootDir = Split-Path $MyInvocation.MyCommand.Path
$rootDir = [IO.Path]::GetFullPath($rootDir)
if(!$SrcRoot) {
$SrcRoot = Join-Path $rootDir "src"
}
if(!$TestRoot) {
$TestRoot = Join-Path $rootDir "tests"
}
$Header = "// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ----------------------------------------------------------------------------"
function NeedsCopyright([string]$FileName) {
# Skip designer files bin and org files
if($FileName.Contains("Designer") -or $FileName.Contains("bin\") -or $FileName.Contains("obj\")) {
return $false;
}
# Check the first line
$line = Get-Content $FileName -totalCount 1;
# Does it have the header?
return !$line -or !$line.StartsWith("// ----------------------------------------------------------------------------")
}
function Get-FilesWithoutCopyright([string]$Directory) {
return Get-ChildItem $Directory -Recurse -Filter *.cs | Where-Object { NeedsCopyright $_.FullName } | Select-Object FullName
}
function WriteWarnings([string]$Directory, $Files) {
Write-Warning "There are $($Files.Length) files in '$Directory' without a copyright header:"
if($Files.Length) {
$Files | ForEach-Object { Write-Warning $_.FullName }
}
}
function FixCopyright([string]$FileName) {
$lines = New-Object System.Collections.ArrayList(,(Get-Content $FileName))
$lines.Insert(0, "")
$lines.Insert(0, $Header)
$lines | Set-Content $FileName -Encoding UTF8
}
$srcFilesWithoutCopyright = Get-FilesWithoutCopyright $SrcRoot
WriteWarnings $SrcRoot $srcFilesWithoutCopyright
$srcFilesWithoutCopyright | ForEach-Object { FixCopyright $_.FullName }