-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
240 lines (197 loc) · 7.48 KB
/
build.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
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
param (
[string]
$ProjectOrSolutionPath = "Oetools.Sakoe/Oetools.Sakoe.csproj",
[bool]
$TestOnly = $False,
[bool]
$UpdateDocumentationOnly = $False,
[bool]
$ShouldExit = $False
)
function Main {
# inspired by ci script from https://github.com/datalust/piggy.
# inspired by ci script from https://github.com/Azure/azure-functions-core-tools.
$path = $ProjectOrSolutionPath
[string] $ciTag = If ([string]::IsNullOrEmpty($env:APPVEYOR_REPO_TAG_NAME)) {$env:CI_COMMIT_TAG} Else {$env:APPVEYOR_REPO_TAG_NAME}
$isReleaseBuild = ![string]::IsNullOrEmpty($ciTag)
[string] $versionToBuild = $NULL
if ($isReleaseBuild) {
$versionToBuild = If ($ciTag.StartsWith('v')) {$ciTag.SubString(1)} Else {$ciTag}
}
Write-Host "Building $(If ([string]::IsNullOrEmpty($versionToBuild)) { "default version" } Else { "version $versionToBuild" } )"
Push-Location $PSScriptRoot
if ($UpdateDocumentationOnly) {
Update-Documentation
} elseif ($TestOnly) {
Start-Tests -AllTests $True
} else {
Invoke-Script -ScriptWithParameters ".\Oetools.Builder\build.ps1 -UpdateXsdOnly 1"
Invoke-Script -ScriptWithParameters ".\SoftwareUpdater\build.ps1 -BuildSimpleUpdaterOnly 1"
Start-Tests
New-ArtifactDir
Publish-DotNetCore -Path "$path" -Version "$versionToBuild"
Publish-DotNetFramework -Path "$path" -Version "$versionToBuild"
Update-Documentation
}
Pop-Location
}
function New-ArtifactDir {
if (Test-Path "./artifacts") {
Remove-Item -Path "./artifacts" -Force -Recurse
}
New-Item -Path "." -Name "artifacts" -ItemType "directory" | Out-Null
}
function Update-Documentation {
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
Write-Host "Updating markdown documentation"
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
if (-Not (Test-Exe("msbuild"))) {
Throw "The executable msbuild was not found in your path."
}
$publishDir = $(Join-Path -Path $(Resolve-Path -Path "./artifacts") -ChildPath "sakoe")
iu msbuild "$path" "/verbosity:minimal" "/t:Restore,Publish" "/p:Configuration=Release" "/p:TargetFramework=net461" "/bl:net461.binlog" "/p:PublishDir=$publishDir" $(If ([string]::IsNullOrEmpty($Version)) { "" } Else { "/p:Version=$Version" })
iu "$(Join-Path -Path "$publishDir" -ChildPath "sakoe.exe")" manual export-md docs\index.md
Remove-Item -Path "$publishDir" -Force -Recurse
}
function Start-Tests {
param (
[bool]
$AllTests = $False
)
if (-Not (Test-Exe("dotnet"))) {
Throw "The executable dotnet was not found in your path."
}
if ($AllTests) {
foreach ($file in Get-ChildItem -Path . -Recurse | Where-Object {$_.Name -like "*.Test.csproj"}) {
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
Write-Host "Testing assembly $($file.Name)"
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
iu dotnet test "$($file.FullName)" --verbosity "minimal"
}
} else {
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
Write-Host "Testing assembly"
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
iu dotnet test "Oetools.Sakoe.Test/Oetools.Sakoe.Test.csproj" --verbosity "minimal"
}
}
function Publish-DotNetFramework {
param (
$Path,
$Version
)
if (-Not (Test-Exe("msbuild"))) {
Throw "The executable msbuild was not found in your path."
}
# msbuild binlog viewer: http://msbuildlog.com/
$artifactDir = Resolve-Path -Path "./artifacts"
$publishDir = $(Join-Path -Path "$artifactDir" -ChildPath "sakoe")
$platforms = @("x64", "x86") # "Any CPU", "x86", "x64"
foreach ($platform in $platforms) {
$frameworks = @("net461")
foreach ($framework in $frameworks) {
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
Write-Host "Building $framework $platform"
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
iu msbuild "$path" "/verbosity:minimal" "/t:Restore,Publish" "/p:Configuration=Release" "/p:TargetFramework=$framework" "/bl:$framework.binlog" "/p:PublishDir=$publishDir" "/p:Platform=$platform" $(If ([string]::IsNullOrEmpty($Version)) { "" } Else { "/p:Version=$Version" })
$archiveName = "sakoe.win-$platform"
Push-Location "$artifactDir"
iu 7z a "$archiveName.zip" "sakoe\sakoe.exe"
iu 7z a "$archiveName.zip" "sakoe\sakoe.pdb"
iu 7z a "$archiveName.zip" "sakoe\sakoe.exe.config"
Move-Item -Path "sakoe" -Destination "$archiveName"
Pop-Location
}
}
}
function Publish-DotNetCore {
param (
$Path,
$Version
)
if (-Not (Test-Exe("dotnet"))) {
Throw "The executable dotnet was not found in your path."
}
if (Test-Exe("nuget")) {
iu nuget restore $path -Recursive -NonInteractive
} else {
iu dotnet restore $path
}
$artifactDir = Resolve-Path -Path "./artifacts"
$publishDir = $(Join-Path -Path "$artifactDir" -ChildPath "sakoe")
iu dotnet restore $path
$rids = @("linux-x64", "")
foreach ($rid in $rids) {
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
Write-Host "Building $rid $platform"
Write-Host "@@@@@@@@@@@@@@@@@@@@@@@"
$selfContainedSuffix = If ([string]::IsNullOrEmpty($rid)) { ".no-runtime" } Else { ".$rid" }
if ([string]::IsNullOrEmpty($rid)) {
# /p:PublishTrimmed=true (from dotnet 3.0)
iu dotnet publish "$path" --framework "netcoreapp2.2" --configuration "Release" --verbosity "minimal" "/p:TrimUnusedDependencies=true" "/p:ShowLinkerSizeComparison=true" "/p:PublishDir=$publishDir" $(If ([string]::IsNullOrEmpty($Version)) { "" } Else { "/p:Version=$Version" })
} else {
iu dotnet publish "$path" --framework "netcoreapp2.2" --configuration "Release" --verbosity "minimal" --runtime "$rid" --self-contained "true" "/p:UseAppHost=true" "/p:TrimUnusedDependencies=true" "/p:ShowLinkerSizeComparison=true" "/p:PublishDir=$publishDir" $(If ([string]::IsNullOrEmpty($Version)) { "" } Else { "/p:Version=$Version" })
}
$archiveName = "sakoe.core$selfContainedSuffix"
Push-Location "$artifactDir"
iu tar -zcvf "$archiveName.tar.gz" "sakoe/*"
Move-Item -Path "sakoe" -Destination "$archiveName"
Pop-Location
}
}
function Test-Exe($exeName) {
return [bool](Get-Command $exeName -ErrorAction SilentlyContinue);
}
function Invoke-Script {
param (
[Parameter(Mandatory = $true)]
[string]
$ScriptWithParameters
)
$ErrorActionPreference = 'Stop' # in case script isn't found
$ScriptBlock = [ScriptBlock]::Create("$ScriptWithParameters")
try {
Invoke-Command -ScriptBlock $ScriptBlock
} catch {
$exceptionCatched = $_.Exception.ToString()
Throw "sub script failure (full command: $ScriptWithParameters): $exceptionCatched"
}
}
function Invoke-Utility {
<#
.SYNOPSIS
Invokes an external utility, ensuring successful execution.
.DESCRIPTION
Invokes an external utility (program) and, if the utility indicates failure by
way of a nonzero exit code, throws a script-terminating error.
* Pass the command the way you would execute the command directly.
* Do NOT use & as the first argument if the executable name is not a literal.
.EXAMPLE
Invoke-Utility git push
Executes `git push` and throws a script-terminating error if the exit code
is nonzero.
#>
$exe, $argsForExe = $Args
$ErrorActionPreference = 'Stop' # in case $exe isn't found
& $exe $argsForExe
if ($LASTEXITCODE) {
Throw "$exe indicated failure (exit code $LASTEXITCODE; full command: $Args)."
}
}
[int] $exitcode = 0
try {
Set-Alias iu Invoke-Utility
Main
} catch {
if (-Not $ShouldExit) {
throw $_.Exception
}
$exceptionCatched = $_.Exception.ToString()
Write-Host "Exception : $exceptionCatched"
$exitcode = 1
}
if ($ShouldExit) {
Write-Host "Exit code $exitcode"
$host.SetShouldExit($exitcode)
exit
}