-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApplyGPLBoilerplate.ps1
158 lines (145 loc) · 3.94 KB
/
ApplyGPLBoilerplate.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
Param
(
[Switch] $verifyOnly, # No changes will be made if set to true
[Switch] $verbose # Additional output provided if set to true
)
function GetFileContent([string] $path)
{
[String]::Join($newLine, (Get-Content -Path $path))
}
function GetUserName()
{
$strComputer = "."
$Var = GWMI -Comp $strComputer -CL Win32_ComputerSystem
return $Var.UserName
}
function Recurse([string] $path)
{
$files = Get-ChildItem -Path $path
if ($files -ne $null) # Did we find anything?
{
foreach ($file in $files)
{
# Is it a folder?
if ($file -isnot [System.IO.DirectoryInfo])
{
$extension = $file.Extension.ToLower()
if ($licencesByExtension.ContainsKey($extension) -and $file.Name -notmatch $excludedFilePattern)
{
$fileContent = GetFileContent $file.FullName
$relativeName = $file.FullName.Replace($startPath, "")
$firstLicenseLineReplace = [string]::Format($firstLicenseLine, $file.Name)
if (-not $fileContent.Contains($firstLicenseLineReplace))
{
$filesToUpdate.Add($relativeName) | Out-Null
if (-not $verifyOnly)
{
echo ("Updating " + $relativeName)
([string]::Format($licencesByExtension[$file.Extension.ToLower()], $file.Name, $copyrightDate, $userName, $date) + $fileContent) | Set-Content $file.FullName
# $file.Name
# $licencesByExtension[$file.Extension.ToLower()]
}
}
else
{
if ($verbose)
{
# Not sure we need this
#echo ("File " + $file.FullName + " is up to date")
}
}
}
else
{
if ($verbose)
{
# Not sure we need this
#echo ("Skipping file " + $file.FullName + " because extension " + $file.Extension + " has not associated license" )
}
}
}
else
{
if ($file.Name -match $excludedFolderPattern)
{
if ($verbose)
{
$skippedFolders.Add($file.FullName.Replace($startPath, "")) | Out-Null
}
}
else
{
Recurse $file.FullName
}
}
}
}
}
$newLine = [Environment]::NewLine
$userName = GetUserName
$copyrightDate = (Get-Date -format yyyy)
$date = (Get-Date -format d)
$licenseFile = "./OxTail/COPYRIGHT.txt"
$licenseContent = GetFileContent $licenseFile
# We need the first line of the license to search for it in each file
$firstLicenseLine = (Get-Content $licenseFile)[0].Replace("// ", "")
# For each extension a license must be specified
$licencesByExtension = @{}
$licencesByExtension[".cs"] = ($licenseContent + $newLine + $newLine)
## We don't really want most, if any, xml files processed.
## Quite a few of them belong to third party libraries or are configuration
## markup. Maybe we could turn it on selectively for a few of them. --Jeff.
## $licencesByExtension[".xml"] = ("<!--" + $newLine + $licenseContent + "-->" + $newLine + $newLine)
# Special folder patterns to exclude
$excludedFolderPattern = "^(bin|obj|Web References|Logs|Images|Config|Resources|SharpSSH)$"
# Special file patterns can also be excluded
$excludedFilePattern = "Designer.cs$"
$filesToUpdate = New-Object System.Collections.ArrayList
$skippedFolders = New-Object System.Collections.ArrayList
cls
if ($verifyOnly) { echo ("** Verify Only **" + $newLine) }
"Processing..."
$startPath = gl # Need to save it for later
$startPath | Recurse
if ($verifyOnly)
{
""
"Username: " + $userName
"Copyright date: " + $copyrightDate
"Date: " + $date
""
if($filesToUpdate.Count -gt 0)
{
"The following files need to be updated:"
""
foreach ($file in $filesToUpdate)
{
echo (" " + $file)
}
}
else
{
echo "** All files are up to date **"
}
}
else
{
if($filesToUpdate.Count -eq 0)
{
echo "** All files are up to date **"
}
}
if ($verbose)
{
if($skippedFolders.Count -gt 0)
{
""
"Skipped folders:"
""
foreach ($folder in $skippedFolders)
{
echo (" " + $folder)
}
}
}
""