-
Notifications
You must be signed in to change notification settings - Fork 3
/
xunit-build-module.psm1
145 lines (122 loc) · 4.29 KB
/
xunit-build-module.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
<# .SYNOPSIS
Powershell build module for xUnit.net. Defines useful helper functions for
creating Powershell-based build scripts. Copyright (C) .NET Foundation.
License: Apache 2.0 (https://github.com/xunit/xunit/blob/master/license.txt)
#>
Set-StrictMode -Version 2
if ($args.Count -eq 0) {
$nugetVersion = "3.5.0"
} else {
$nugetVersion = $args[0]
}
$nugetExe = [System.IO.Path]::Combine($home, ".nuget", "cli", $nugetVersion, "nuget.exe")
function _build_step([string] $message) {
Write-Host -ForegroundColor White $("==> " + $message + " <==")
Write-Host ""
}
function _dotnet([string] $command, [string] $message = "") {
_exec ("dotnet " + $command) $message
}
function _download_nuget() {
$cliVersionPath = split-path $nugetExe -Parent
if ((test-path $cliVersionPath) -eq $false) {
New-Item -Type Directory -Path $cliVersionPath | out-null
}
if ((test-path $nugetExe) -eq $false) {
_build_step ("Downloading NuGet version " + $nugetVersion)
Invoke-WebRequest ("https://dist.nuget.org/win-x86-commandline/v" + $nugetVersion + "/nuget.exe") -OutFile $nugetExe
}
}
function _exec([string] $command, [string] $message = "") {
if ($message -eq "") {
$message = $command
}
Write-Host -ForegroundColor DarkGray ("EXEC: " + $message)
Write-Host ""
Invoke-Expression $command
Write-Host ""
if ($LASTEXITCODE -ne 0) {
exit 1
}
}
function _fatal([string] $message) {
Write-Host -ForegroundColor Red ("Error: " + $message)
exit -1
}
function _mkdir([string] $path) {
if ((test-path $path) -eq $false) {
New-Item -Type directory -Path $path | out-null
}
}
function _msbuild([string] $project, [string] $configuration, [string] $target = "build", [string] $verbosity = "minimal", [string] $message = "", [string] $binlogFile = "") {
$cmd = "msbuild " + $project + " /t:" + $target + " /p:Configuration=" + $configuration + " /v:" + $verbosity + " /m"
if ($binlogFile -ne "") {
$cmd = $cmd + " /bl:" + $binlogFile
}
_exec $cmd $message
}
function _nuget_pack {
[cmdletbinding()]
Param(
[System.IO.FileInfo[]][Parameter(ValueFromPipeLine=$True)] $nuspecFiles,
[string] $outputFolder,
[string] $configuration
)
Process {
_download_nuget
$nuspecFiles | ForEach-Object {
_exec ('& "' + $nugetExe + '" pack ' + $_.FullName + ' -NonInteractive -NoPackageAnalysis -OutputDirectory "' + $outputFolder + '" -Properties Configuration=' + $configuration)
}
}
}
function _nuget_push {
Param(
[System.IO.FileInfo[]][Parameter(ValueFromPipeLine=$True)] $nupkgFiles,
[string] $source,
[string] $apiKey
)
Process {
_download_nuget
$nupkgFiles | ForEach-Object {
$cmd = '& "' + $nugetExe + '" push "' + $_.FullName + '" -Source ' + $source + ' -NonInteractive -ApiKey ' + $apiKey
$message = $cmd.Replace($env:MyGetApiKey, "[redacted]")
_exec $cmd $message
}
}
}
function _replace {
[cmdletbinding()]
Param(
[System.IO.FileInfo[]][Parameter(ValueFromPipeLine=$True)] $files,
[regex] $match,
[string] $replacement
)
Process {
$files | ForEach-Object {
$content = Get-Content -raw $_.FullName
$content = $match.Replace($content, $replacement)
Set-Content $_.FullName $content -Encoding UTF8 -NoNewline
}
}
}
function _require([string] $command, [string] $message) {
if ((get-command $command -ErrorAction SilentlyContinue) -eq $null) {
_fatal $message
}
}
function _verify_version([string]$version, [string]$minVersion, [string]$appName) {
$dashIndex = $version.IndexOf('-')
if ($dashIndex -gt -1) {
$version = $version.Substring(0, $dashIndex)
}
if ([version]$version -lt [version]$minVersion) {
_fatal ("Unsupported " + $appName + " version '$version' (must be '$minVersion' or later).")
}
}
function _verify_dotnetsdk_version([string]$minVersion) {
_verify_version (& dotnet --version) $minVersion ".NET SDK"
}
function _verify_msbuild_version([string]$minVersion) {
_verify_version (& msbuild /nologo /ver) $minVersion "MSBuild"
}
Export-ModuleMember -Function * -Variable nugetExe