forked from DSharpPlus/DSharpPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild-lib.ps1
executable file
·175 lines (154 loc) · 4.52 KB
/
rebuild-lib.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
#!/usr/bin/env pwsh
# Rebuild-lib
#
# Rebuilds the entire DSharpPlus project, and places artifacts in specified directory.
#
# Author: Emzi0767
# Version: 2018-08-30 14:41
#
# Arguments:
# .\rebuild-lib.ps1 <output path> <configuration> [version suffix] [build number]
#
# Run as:
# .\rebuild-lib.ps1 .\path\to\artifact\location Debug/Release version-suffix build-number
#
# or
# .\rebuild-lib.ps1 .\path\to\artifact\location Debug/Release
param
(
[parameter(Mandatory = $true)]
[string] $ArtifactLocation,
[parameter(Mandatory = $true)]
[string] $Configuration,
[parameter(Mandatory = $false)]
[string] $VersionSuffix,
[parameter(Mandatory = $false)]
[int] $BuildNumber = -1
)
# Check if configuration is valid
if ($Configuration -ne "Debug" -and $Configuration -ne "Release")
{
Write-Host "Invalid configuration specified. Must be Release or Debug."
Exit 1
}
# Restores the environment
function Restore-Environment()
{
Write-Host "Restoring environment"
Remove-Item ./NuGet.config
}
# Prepares the environment
function Prepare-Environment([string] $target_dir_path)
{
# Prepare the environment
Copy-Item ./.nuget/NuGet.config ./
# Check if the target directory exists
# If it does, remove it
if (Test-Path "$target_dir_path")
{
Write-Host "Target directory exists, deleting"
Remove-Item -recurse -force "$target_dir_path"
}
# Create target directory
$dir = New-Item -type directory "$target_dir_path"
}
# Builds everything
function Build-All([string] $target_dir_path, [string] $version_suffix, [string] $build_number, [string] $bcfg)
{
# Form target path
$dir = Get-Item "$target_dir_path"
$target_dir = $dir.FullName
Write-Host "Will place packages in $target_dir"
# Clean previous build results
Write-Host "Cleaning previous build"
& dotnet clean -v minimal -c "$bcfg" | Out-Host
if ($LastExitCode -ne 0)
{
Write-Host "Cleanup failed"
Return $LastExitCode
}
# Restore nuget packages
Write-Host "Restoring NuGet packages"
& dotnet restore -v minimal | Out-Host
if ($LastExitCode -ne 0)
{
Write-Host "Restoring packages failed"
Return $LastExitCode
}
# Create build number string
if (-not $build_number)
{
$build_number_string = ""
}
else
{
$build_number_string = [int]::Parse($build_number).ToString("00000")
}
# Build in specified configuration
Write-Host "Building everything"
if (-not $version_suffix)
{
& dotnet build -v minimal -c "$bcfg" | Out-Host
}
elseif (-not $build_number_string)
{
& dotnet build -v minimal -c "$bcfg" --version-suffix "$version_suffix" | Out-Host
}
else
{
& dotnet build -v minimal -c "$bcfg" --version-suffix "$version_suffix" -p:BuildNumber="$build_number_string" | Out-Host
}
if ($LastExitCode -ne 0)
{
Write-Host "Build failed"
Return $LastExitCode
}
# Package for NuGet
Write-Host "Creating NuGet packages"
if (-not $version_suffix)
{
& dotnet pack -v minimal -c "$bcfg" --no-build -o "$target_dir" --include-symbols | Out-Host
}
elseif (-not $build_number_string)
{
& dotnet pack -v minimal -c "$bcfg" --version-suffix "$version_suffix" --no-build -o "$target_dir" --include-symbols | Out-Host
}
else
{
& dotnet pack -v minimal -c "$bcfg" --version-suffix "$version_suffix" -p:BuildNumber="$build_number_string" --no-build -o "$target_dir" --include-symbols | Out-Host
}
if ($LastExitCode -ne 0)
{
Write-Host "Packaging failed"
Return $LastExitCode
}
Return 0
}
# Check if building a pre-production package
if ($VersionSuffix -and $BuildNumber -ge 0)
{
Write-Host "Building pre-production package with version suffix of `"$VersionSuffix-$($BuildNumber.ToString("00000"))`""
}
elseif ($VersionSuffix -and (-not $BuildNumber -or $BuildNumber -lt 0))
{
Write-Host "Building pre-production package with version suffix of `"$VersionSuffix`""
Remove-Variable BuildNumber
$BuildNumber = $null
}
# Prepare environment
Prepare-Environment "$ArtifactLocation"
# Build everything
$BuildResult = Build-All "$ArtifactLocation" "$VersionSuffix" "$BuildNumber" "$Configuration"
# Restore environment
Restore-Environment
# Check if there were any errors
if ($BuildResult -ne 0)
{
Write-Host "Build failed with code $BuildResult"
$host.SetShouldExit($BuildResult)
Exit $BuildResult
}
else
{
Exit 0
}