-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-common.ps1
209 lines (172 loc) · 5.23 KB
/
build-common.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
# This is the main function of the script
function Execute {
param (
[string]$target = "package", # Default target is 'package'
[string]$projFile,
[string]$releaseDir
)
if ([string]::IsNullOrEmpty($target)) {
$target = "package"
}
switch ($target) {
"package" {
Package $projFile $releaseDir
}
"build" {
Build $projFile
}
"compile" {
Build $projFile
}
"test" {
Test $projFile
}
"clean" {
Clean $projFile
}
"clear" {
Clean $projFile
}
default {
Write-Host "Unknown target: $target. Valid targets are 'package', 'build', 'test', 'clean', and 'clear'."
exit 1
}
}
}
# Override the Package function to include distribution steps
function Package {
param (
[string]$projFile,
[string]$releaseDir
)
Build $projFile "Release"
# Create the distribution folders
$distPath = Join-Path (Get-ScriptDirectory) "dist"
Create-DistFolders $distPath
# Copy necessary files to the distribution folder
Copy-FilesToDist $distPath $releaseDir
}
# Build target: Build the application (in Debug mode by default)
function Build {
param (
[string]$projFile,
[string]$config = "Debug"
)
if ([string]::IsNullOrEmpty($config)) {
$config = "Debug"
}
# Find the msbuild.exe path
$msbuildPath = Find-MSBuild
if (-not $msbuildPath) {
Write-Host "MSBuild not found. Exiting script."
exit 1
}
# Execute the msbuild command for Build-Debug
Execute-MSBuild $msbuildPath "Build-$config" $projFile
}
# Test target: Run unit tests using vstest.console.exe
function Test {
param (
[string]$projFile
)
# Find the msbuild.exe path
$msbuildPath = Find-MSBuild
if (-not $msbuildPath) {
Write-Host "MSBuild not found. Exiting script."
exit 1
}
# Execute the msbuild command for Run-Tests
Execute-MSBuild $msbuildPath "Run-Tests" $projFile
}
# Clean target: Delete the dist folder and debug/release binaries
function Clean {
param (
[string]$projFile
)
# Find the msbuild.exe path
$msbuildPath = Find-MSBuild
if (-not $msbuildPath) {
Write-Host "MSBuild not found. Exiting script."
exit 1
}
# Execute the msbuild command for Clean
Execute-MSBuild $msbuildPath "Clean" $projFile
$distPath = Join-Path (Get-ScriptDirectory) "dist"
# Remove dist folder
if (Test-Path $distPath) {
Remove-Item -Recurse -Force $distPath
Write-Host "Deleted dist folder."
}
}
# Find the location of msbuild.exe using vswhere
function Find-MSBuild {
$vswherePath = "C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
if (-not (Test-Path $vswherePath)) {
Write-Host "vswhere.exe not found."
return $null
}
$msbuildPath = & "$vswherePath" -latest -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe
if (-not $msbuildPath) {
Write-Host "MSBuild not found."
return $null
}
Write-Host "MSBuild found at: $msbuildPath"
return $msbuildPath
}
# Execute msbuild with the provided target
function Execute-MSBuild {
param (
[string]$msbuildPath,
[string]$target,
[string]$projFile
)
if (-not (Test-Path $projFile)) {
Write-Host "$projFile not found."
exit 1
}
& "$msbuildPath" $projFile /t:$target
if ($LASTEXITCODE -ne 0) {
Write-Host "MSBuild execution failed."
exit $LASTEXITCODE
}
}
# Create the distribution folders
function Create-DistFolders {
param (
[string]$distPath
)
$folders = @("bin", "conf", "lib", "logs")
foreach ($folder in $folders) {
$folderPath = Join-Path $distPath $folder
if (-not (Test-Path $folderPath)) {
New-Item -Path $folderPath -ItemType Directory | Out-Null
}
}
Write-Host "Distribution folders created at: $distPath"
}
# Copy necessary files to the dist folder
function Copy-FilesToDist {
param (
[string]$distPath,
[string]$releaseDir
)
$releasePath = Join-Path (Get-ScriptDirectory) $releaseDir
$solutionPath = Get-ScriptDirectory
# Copy .dll files to dist\lib
Copy-Item -Path "$releasePath\*.dll" -Destination (Join-Path $distPath "lib") -Force
# Copy .exe files to dist\bin
Copy-Item -Path "$releasePath\*.exe" -Destination (Join-Path $distPath "bin") -Force
# Copy .bat files to dist\bin
Copy-Item -Path "$releasePath\*.bat" -Destination (Join-Path $distPath "bin") -Force
# Copy *.config to dist\conf
Copy-Item -Path "$releasePath\*.config" -Destination (Join-Path $distPath "conf") -Force
# Copy Config\*.* to dist\conf
Copy-Item -Path (Join-Path $releasePath "Config\*.*") -Destination (Join-Path $distPath "conf") -Force
# Copy LICENSE.txt to dist
Copy-Item -Path (Join-Path $solutionPath "LICENSE.txt") -Destination $distPath -Force
Write-Host "Files copied to distribution folder."
}
# Get the directory where the script is located
function Get-ScriptDirectory {
return $PSScriptRoot
}