This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscripts.psm1
246 lines (201 loc) · 14.3 KB
/
scripts.psm1
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
function Exec
{
[CmdletBinding()]
param(
[Parameter(Mandatory=1)]
[scriptblock]$cmd,
[Parameter(Mandatory=0)]
[Switch]
$DirectOutput
)
if($DirectOutput.IsPresent)
{
Invoke-Command $cmd
}
else
{
Invoke-Command $cmd | Out-HostColored
}
if ($lastexitcode -ne 0)
{
Write-HeaderError "ERRORS OCCURED"
throw ("Exec finished with return code: " + $lastexitcode)
}
}
function Out-HostColored
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$True)]
[string]$msg
)
Begin {
$errorRegex = (".*Test Failure.*", ".*(FAIL).*", ".*error MSB.*:.*", ".*error CS.*:.*", ".*error :") -join "|";
$stylecopRegex = (".*error : SA.*") -join "|";
$warningRegex = (".*warning :.*") -join "|";
}
Process {
if($msg -cmatch $stylecopRegex)
{
Write-Host $msg -ForegroundColor Magenta
}
elseif($msg -cmatch $errorRegex)
{
Write-Host $msg -ForegroundColor Red
}
elseif($msg -cmatch $warningRegex)
{
Write-Host $msg -ForegroundColor Yellow
}
else
{
Write-Host $msg
}
}
}
function Write-HeaderSuccess
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$message
)
Write-Host " $message " -BackgroundColor Green -ForegroundColor Blue
}
function Write-HeaderError
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$message
)
Write-Host " $message " -BackgroundColor Red -ForegroundColor White
}
function Write-HeaderMessage
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$message
)
Write-Host " $message " -BackgroundColor Cyan -ForegroundColor Black
}
function Write-Message
{
[CmdletBinding()]
param(
[Parameter(Position=0,Mandatory=1)][string]$message
)
Write-Host $message -ForegroundColor Cyan
}
function Build
{
[CmdletBinding()]
param(
[string]$target="Rebuild"
)
$solutionFilePath = "$PSScriptRoot\source\xbehavemarkdownreport.sln"
Write-HeaderMessage "Restoring packages in solution: $solutionFilePath"
Exec { nuget restore $solutionFilePath }
Write-HeaderMessage "building $solutionFilePath"
Exec { msbuild $solutionFilePath /p:Configuration=Release /target:$target /verbosity:minimal }
Write-HeaderMessage "creating nuget package"
$version = GetVersion $PSScriptRoot $False
Write-Message "version = $version"
&nuget pack "$PSScriptRoot\source\xbehavemarkdownreport\xbehavemarkdownreport.nuspec" -Version $version
Write-HeaderSuccess "done"
}
function GetVersion
{
[CmdletBinding()]
param(
[Parameter(Mandatory=0)]
[string]$path = $PSScriptRoot,
[Parameter(Mandatory=0)]
[bool]$skipAppccelerateVersionInstallation = $False
)
Write-HeaderMessage "Determining version"
if (!$skipAppccelerateVersionInstallation)
{
Write-HeaderMessage "Installing Appccelerate.Version"
Exec { nuget install Appccelerate.Version -OutputDirectory $env:TEMP\Appccelerate.Version }
}
# find path to latest Appccelerate.Version (with natural number sorting, up to 20 digits)
$NaturalSort = { [regex]::Replace($_.FullName, '\d+', { $args[0].Value.PadLeft(20, "0") }) }
$latestVersion = Get-ChildItem -Path $env:TEMP\Appccelerate.Version -Recurse -Filter Appccelerate.Version.exe | Sort-Object $NaturalSort | Select-Object -Last 1
if ($latestVersion -is [io.fileinfo])
{
$versionExe = $latestVersion.FullName
Write-Message "Using $versionExe"
}
else
{
throw "Cannot find path of Appccelerate.Version.exe"
}
# Determine version
$version_output = Exec { & $versionExe $path } -DirectOutput
$version = $version_output[2].ToString().Replace('"', "").Replace(",", "").Replace("NugetVersion: ", "") # output is NugetVersion: <version> e.g. "NugetVersion": "2.5.0",
Write-Message "calculated version: $version"
return $version
}
<#
.SYNOPSIS
Replace the set of valid values on a function parameter that was defined using ValidateSet.
.DESCRIPTION
Replace the set of valid values on a function parameter that was defined using ValidateSet.
.PARAMETER Command
A FunctionInfo object for the command that has the parameter validation to be updated. Get this using:
Get-Command -Name YourCommandName
.PARAMETER ParameterName
The name of the parameter that is using ValidateSet.
.PARAMETER NewSet
The new set of valid values to use for parameter validation.
.EXAMPLE
Define a test function:
PS> Function Test-Function {
param(
[ValidateSet("one")]
$P
)
}
PS> Update-ValidateSet -Command (Get-Command Test-Function) -ParameterName "P" -NewSet @("one","two")
After running Update-ValidateSet, Test-Function will accept the values "one" and "two" as valid input for the -P parameter.
.OUTPUTS
Nothing
.NOTES
This function is updating a private member of ValidateSetAttribute and is thus not following the rules of .Net and could break at any time. Use at your own risk!
Author : Chris Duck
.LINK
http://blog.whatsupduck.net/2013/05/hacking-validateset.html
#>
function Update-ValidateSet {
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[System.Management.Automation.FunctionInfo]$Command,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$ParameterName,
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[String[]]$NewSet
)
#Find the parameter on the command object
$Parameter = $Command.Parameters[$ParameterName]
if($Parameter) {
#Find all of the ValidateSet attributes on the parameter
$ValidateSetAttributes = @($Parameter.Attributes | Where-Object {$_ -is [System.Management.Automation.ValidateSetAttribute]})
if($ValidateSetAttributes) {
$ValidateSetAttributes | ForEach-Object {
#Get the validValues private member of the ValidateSetAttribute class
$ValidValuesField = [System.Management.Automation.ValidateSetAttribute].GetField("validValues", [System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)
if($PsCmdlet.ShouldProcess("$Command -$ParameterName", "Set valid set to: $($NewSet -join ', ')")) {
#Update the validValues array on each instance of ValidateSetAttribute
$ValidValuesField.SetValue($_, $NewSet)
}
}
} else {
Write-Error -Message "Parameter $ParameterName in command $Command doesn't use [ValidateSet()]"
}
} else {
Write-Error -Message "Parameter $ParameterName was not found in command $Command"
}
}