-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ps1
350 lines (230 loc) · 9.17 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<#
Title: build.ps1
Author: [email protected]
Last Update: 10.02.2024
Description:
This script automates the process of building:
- this library as NuGet package
Pre-requisites:
- The script expects the project source to be stored into the '{solution_folder}\src\{project_name}' folder
- The script requires 'dotnet.exe' to be installed on the system.
Features
- The script can be run both in VSCode and in a shell/Windows Terminal
- If the '{solution_folder}\artifacts' folder is not found, '{desktop}\artifacts' will be used.
- The user can even force the second option by enabling the $forcePublishToDesktop flag.
- The script supports timestamped logging
- The library artifact will be something like:
- "NW.UnivariateForecasting.2.5.0.nupkg"
- The script is a wrapper around the following 'dotnet' commands:
- dotnet pack
#>
# Variables
[bool]$forcePublishToDesktop = $true
# Classes
class Logger {
# Fields
# Properties
# Constructors()
hidden Logger() { }
# Methods
static [void] Log([string]$message) {
Write-Host $("{0} {1}" -f $([Logger]::GetTimeStamp()), $message)
}
static hidden [string] GetTimeStamp() {
return $(Get-Date -Format "[yyyy-MM-dd HH:mm]")
}
}
# Functions
function Assert-ThatDotnetIsInstalled {
[OutputType([bool])]
param()
try
{
. dotnet | Out-Null
[Logger]::Log("'dotnet.exe' is installed.")
return $true
}
catch {
[Logger]::Log("'dotnet.exe' is not installed.")
return $false
}
}
function Get-CurrentDirectory() {
[OutputType([System.IO.DirectoryInfo])]
param()
[string]$currentDir = $null
if ($PSISE) {
$currentDir = (Split-Path -Path $psISE.CurrentFile.FullPath)
}
elseif ($profile.Contains("VSCode")) {
$currentDir = (Split-Path $PSEditor.GetEditorContext().CurrentFile.Path)
}
elseif (-not $PSScriptRoot) {
$currentDir = (Get-ChildItem | ForEach-Object { $_.DirectoryName } | Select-Object -Unique)
}
else {
$currentDir = $PSScriptRoot
}
return [System.IO.DirectoryInfo]::new($currentDir)
}
function Get-ArtifactsFolder {
[OutputType([System.IO.DirectoryInfo])]
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionFolder,
[parameter(Mandatory=$true)] [bool]$ForcePublishToDesktop
)
# TO-DO: validation
[System.IO.DirectoryInfo]$desktopartifactFolder = [System.IO.Path]::Combine([Environment]::GetFolderPath('Desktop'), "artifacts")
if($ForcePublishToDesktop.Equals($true)) {
return $desktopartifactFolder
}
[System.IO.DirectoryInfo]$artifactsFolder = [System.IO.Path]::Combine($SolutionFolder, "artifacts")
if ($artifactsFolder.Exists.Equals($false)) {
[Logger]::Log("'artifacts' folder doesn't exist: '$artifactsFolder'.")
$artifactsFolder = $desktopartifactFolder
[Logger]::Log("'desktop\artifacts' folder will be used instead: '$artifactsFolder'.")
}
else {
[Logger]::Log("'artifacts' folder: '$artifactsFolder'.")
}
return $artifactsFolder
}
function Get-ProjectFile {
[OutputType([System.IO.FileInfo])]
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionFolder,
[parameter(Mandatory=$true)] [string]$ProjectName
)
# TO-DO: validation
[System.IO.DirectoryInfo]$projectFolder = [System.IO.Path]::Combine($SolutionFolder, "src")
if ($projectFolder.Exists.Equals($false)) {
[Logger]::Log("'src' folder doesn't exist: '$projectFolder'.")
return $null
}
$projectFolder = [System.IO.Path]::Combine($projectFolder, $ProjectName)
if ($projectFolder.Exists.Equals($false)) {
[Logger]::Log("'$ProjectName' folder doesn't exist: '$projectFolder'.")
return $null
}
[string]$projectFileName = "$($projectFolder)\$($ProjectName).csproj"
[System.IO.FileInfo]$projectFile = [System.IO.FileInfo]::new($projectFileName)
[Logger]::Log("Project's file: '$projectFile'.")
return $projectFile
}
function Publish-Project {
[OutputType([System.IO.FileInfo])]
param(
[parameter(Mandatory=$true)] [System.IO.FileInfo]$ProjectFile,
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$ArtifactsFolder,
[parameter(Mandatory=$true)] [string]$Runtime
)
# TO-DO: validation
[Logger]::Log("Publishing project for runtime '$Runtime'...")
[Logger]::Log("")
dotnet publish $ProjectFile -r $Runtime -c Release /p:PublishSingleFile=true /p:PublishReadyToRun=false /p:DebugSymbols=false /p:DebugType=None -o $ArtifactsFolder | Out-Host
[Logger]::Log("")
if ($LASTEXITCODE.Equals(1)) {
[Logger]::Log("The project has failed to publish.")
return $null
}
[System.IO.FileInfo]$publishedFile = $(Get-ChildItem $ArtifactsFolder | Sort-Object -Descending -Property LastWriteTime -Top 1)
[Logger]::Log("The project has been successfully published: '$publishedFile'.")
return $publishedFile
}
function Get-ProjectFileAssemblyName {
[OutputType([string])]
param(
[parameter(Mandatory=$true)] [System.IO.FileInfo]$ProjectFile
)
# TO-DO: validation
[xml]$projectFileContent = $(Get-Content -Path $ProjectFile)
return $projectFileContent.Project.PropertyGroup.AssemblyName
}
function Get-ProjectFileVersion {
[OutputType([string])]
param(
[parameter(Mandatory=$true)] [System.IO.FileInfo]$ProjectFile
)
# TO-DO: validation
[xml]$projectFileContent = $(Get-Content -Path $ProjectFile)
return $projectFileContent.Project.PropertyGroup.Version
}
function Get-NuGetPackages {
[OutputType([string[]])]
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$ArtifactsFolder
)
# TO-DO: validation
[string[]]$packageNames = (Get-ChildItem -Path "$ArtifactsFolder\*" -Include @("*.nupkg") | ForEach-Object { $_.Name} )
[Logger]::Log("'$($packageNames.Count)' NuGet packages have been found in the provided artifacts folder.")
return $packageNames
}
function Build-Library {
<#
By default, this function will create a .nupkg package out of every project found in the .sln file.
If you want to exclude one or more projects, please add the <IsPackable>false</IsPackable> field to the .csproj as shown below:
<Project>
<PropertyGroup>
<IsPackable>false</IsPackable>
</PropertyGroup>
</Project>
#>
[OutputType([string[]])]
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionFolder,
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$ArtifactsFolder
)
# TO-DO: validation
try {
[Logger]::Log("The following solution directory has been provided: '$($SolutionFolder.FullName)'.")
[Logger]::Log("The following artifacts directory has been provided: '$($ArtifactsFolder.FullName)'.")
[System.IO.FileInfo[]]$solutionFiles = (Get-ChildItem -Path "$SolutionFolder\*" -Include @("*.sln"))
if ($solutionFiles.Count.Equals(1).Equals($false))
{
[Logger]::Log("An unexpected amount of solution files have been found in the current directory.")
return $null
}
[System.IO.FileInfo]$solutionFileName = $solutionFiles[0]
[Logger]::Log("The following solution file has been found: '$($solutionFileName.Name)'.")
[Logger]::Log("Creating NuGet package(s) using 'dotnet pack'...")
dotnet pack $solutionFileName --output $ArtifactsFolder
[Logger]::Log("Exit code is: '$($LASTEXITCODE)'.")
if ($LASTEXITCODE.Equals(0)) {
[string[]]$packageNames = (Get-NuGetPackages -ArtifactsFolder $ArtifactsFolder)
if ($packageNames) {
[Logger]::Log("The NuGet packages are: '$packageNames'")
}
else {
[Logger]::Log("No NuGet packages found in the artifacts folder (?).")
}
return $packageNames
}
else {
return $null
}
}
catch {
[Logger]::Log($_.Exception.Message)
return $null
}
}
function Invoke-BuildScript {
[OutputType([System.Void])]
param()
$ErrorActionPreference = "Stop"
try {
if ($(Assert-ThatDotnetIsInstalled).Equals($false)) {
[Logger]::Log("Aborting.")
break
}
[System.IO.DirectoryInfo]$solutionFolder = Get-CurrentDirectory
[System.IO.DirectoryInfo]$artifactsFolder = $(Get-ArtifactsFolder -SolutionFolder $solutionFolder -ForcePublishToDesktop $forcePublishToDesktop)
[string[]]$packageNames = (Build-Library -SolutionFolder $solutionFolder -ArtifactsFolder $artifactsFolder)
}
catch {
[Logger]::Log($_.Exception.Message)
}
}
# Main
Clear-Host
Invoke-BuildScript