-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.ps1
More file actions
208 lines (160 loc) · 5.65 KB
/
build.ps1
File metadata and controls
208 lines (160 loc) · 5.65 KB
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
<#
Title: NW_Builder (build.ps1)
Author: numbworks@gmail.com
Last Update: 27.12.2020
Description:
Builds all the projects listed into a solution file and places the artifact(s) in the "artifacts" sub-folder.
The script expects :
- a solution directory containing only one .sln file
- an "artifacts" sub-folder to place artifacts into
- dotnet.exe correctly installed on the machine
Worth to mention:
- the artifacts will be NuGet packages
- functions provide basic logging functionality
- the script can be run both in VSCode and in a shell/Windows Terminal
#>
# Functions
function Assert-ThatDotnetIsInstalled
{
try
{
. dotnet | Out-Null
Write-Host -Object "'dotnet' is installed."
return $true
}
catch {
Write-Host -Object "'dotnet' is not installed."
return $false
}
}
function Get-CurrentDirectory() {
[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-ArtifactsSubfolder
{
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionDir
)
[System.IO.DirectoryInfo]$artifactsDir = [System.IO.Path]::Combine($SolutionDir, "artifacts")
if ($artifactsDir.Exists.Equals($false))
{
Write-Host -Object "The artifacts sub-directory doesn't exist in the provided solution directory."
return $null
}
Write-Host -Object "The artifacts sub-directory does exist in the provided solution directory."
return $artifactsDir
}
function New-Artifact
{
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionDir,
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$ArtifactsDir
)
try {
if ($SolutionDir.Exists.Equals($false))
{
Write-Host -Object "The provided solution directory doesn't exist: '$($SolutionDir.FullName)'."
return $false
}
if ($ArtifactsDir.Exists.Equals($false))
{
Write-Host -Object "The provided artifacts directory doesn't exist: '$($ArtifactsDir.FullName)'."
return $false
}
Write-Host -Object "The following solution directory has been provided: '$($SolutionDir.FullName)'."
Write-Host -Object "The following artifacts directory has been provided: '$($ArtifactsDir.FullName)'."
[System.IO.FileInfo[]]$solutionFiles = (Get-ChildItem -Path "$SolutionDir\*" -Include @("*.sln"))
if ($solutionFiles.Count.Equals(1).Equals($false))
{
Write-Host -Object "An unexpected amount of solution files have been found in the current directory."
return $false
}
[System.IO.FileInfo]$solutionFileName = $solutionFiles[0]
Write-Host -Object "The following solution file has been found: '$($solutionFileName.Name)'."
Write-Host -Object "Creating NuGet package(s) using 'dotnet pack'..."
Write-Host -Object ""
dotnet pack $solutionFileName --output $ArtifactsDir
Write-Host -Object ""
Write-Host -Object "Exit code is: '$($LASTEXITCODE)'."
if ($LASTEXITCODE.Equals(0))
{
return $true
}
else
{
return $false
}
}
catch
{
Write-Host -Object $_.Exception.Message
return $false
}
}
function Get-ArtifactNames
{
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$ArtifactsDir
)
if ($ArtifactsDir.Exists.Equals($false))
{
Write-Host -Object "The provided artifacts directory doesn't exist: '$($ArtifactsDir.FullName)'."
return $null
}
[string[]]$artifactNames = (Get-ChildItem -Path "$ArtifactsDir\*" -Include @("*.nupkg") | ForEach-Object { $_.Name} )
Write-Host -Object "'$($artifactNames.Count)' NuGet packages have been found in the provided artifacts sub-directory."
return $artifactNames
}
function Invoke-BuildProcess
{
param(
[parameter(Mandatory=$true)] [System.IO.DirectoryInfo]$SolutionDir
)
if ($SolutionDir.Exists.Equals($false))
{
Write-Host -Object "The provided solution directory doesn't exist: '$($SolutionDir.FullName)'."
return $false
}
Write-Host -Object "Build process started for '$($SolutionDir.FullName)'."
[System.IO.DirectoryInfo]$artifactsDir = (Get-ArtifactsSubfolder -SolutionDir $solutionDir)
if (-not $artifactsDir)
{
Write-Host -Object "Aborting."
break
}
$buildStatus = (New-Artifact -SolutionDir $solutionDir -ArtifactsDir $artifactsDir)
if ($buildStatus.Equals($false))
{
Write-Host -Object "Aborting."
break
}
[string[]]$artifactNames = (Get-ArtifactNames -ArtifactsDir $artifactsDir)
if ($artifactNames)
{
Write-Host -Object "The files are:"
$artifactNames
}
Write-Host -Object "Build process completed."
}
# Main
Clear-Host
if ($(Assert-ThatDotnetIsInstalled).Equals($false))
{
Write-Host -Object "Aborting."
break
}
[System.IO.DirectoryInfo]$solutionDir = Get-CurrentDirectory
Invoke-BuildProcess -SolutionDir $solutionDir