Skip to content

Commit a12950b

Browse files
committed
unify Unix/Windows build scripts with crossplatform PowerShell
1 parent b0f1077 commit a12950b

File tree

8 files changed

+152
-376
lines changed

8 files changed

+152
-376
lines changed

.github/workflows/build-smapi.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ jobs:
7474
7575
Write-Host "This is a dev version. Building as version: $updatedVersion"
7676
77-
bash build/unix/set-smapi-version.sh "$updatedVersion"
78-
Write-Host 'Version updated using build/unix/set-smapi-version.sh.'
77+
./build/scripts/set-smapi-version.ps1 "$updatedVersion"
78+
Write-Host 'Version updated.'
7979
8080
- name: Checkout game reference assemblies
8181
run: |
@@ -85,7 +85,7 @@ jobs:
8585
shell: pwsh
8686
run: |
8787
Write-Host "Building SMAPI $env:VERSION."
88-
bash build/unix/prepare-install-package.sh "$env:VERSION"
88+
./build/scripts/prepare-install-package.ps1 "$env:VERSION"
8989
9090
- name: Rename build zips
9191
run: |

build/scripts/lib/in-place-regex.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function In-Place-Regex {
1+
function In-Place-Regex {
22
param (
33
[Parameter(Mandatory)][string]$Path,
44
[Parameter(Mandatory)][string]$Search,
Lines changed: 137 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,63 @@
11
#
22
#
3-
# This is the PowerShell equivalent of ../unix/prepare-install-package.sh, *except* that it doesn't
4-
# set Linux permissions, create the install.dat files, or create the final zip (unless you specify
5-
# --windows-only). Due to limitations in PowerShell, the final changes are handled by the
6-
# windows/finalize-install-package.sh file in WSL.
7-
#
8-
# When making changes, make sure to update ../unix/prepare-install-package.ps1 too.
3+
# Note: On Windows, this script *does not* set Linux permissions. The final changes are handled by the
4+
# finalize-install-package.sh file in WSL.
95
#
106
#
117

128
. "$PSScriptRoot/lib/in-place-regex.ps1"
139

1410

11+
##########
12+
## Read arguments
13+
##########
14+
$windowsOnly = $false # Windows-only build
15+
$skipBundleDeletion = $false # skip bundle deletion (only applies when using WSL to finalize the build on Windows)
16+
foreach ($arg in $args) {
17+
if ($arg -eq "--windows-only" -and $IsWindows) {
18+
$windowsOnly = $true
19+
}
20+
elseif ($arg -eq "--skip-bundle-deletion") {
21+
$skipBundleDeletion = $true
22+
}
23+
}
24+
25+
1526
##########
1627
## Find the game folder
1728
##########
18-
$possibleGamePaths=(
19-
# GOG
20-
"C:\Program Files\GalaxyClient\Games\Stardew Valley",
21-
"C:\Program Files\GOG Galaxy\Games\Stardew Valley",
22-
"C:\Program Files\GOG Games\Stardew Valley",
23-
"C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley",
24-
"C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley",
25-
"C:\Program Files (x86)\GOG Games\Stardew Valley",
26-
27-
# Steam
28-
"C:\Program Files\Steam\steamapps\common\Stardew Valley",
29-
"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley"
30-
)
29+
if ($IsWindows) {
30+
$possibleGamePaths=(
31+
# GOG
32+
"C:\Program Files\GalaxyClient\Games\Stardew Valley",
33+
"C:\Program Files\GOG Galaxy\Games\Stardew Valley",
34+
"C:\Program Files\GOG Games\Stardew Valley",
35+
"C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley",
36+
"C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley",
37+
"C:\Program Files (x86)\GOG Games\Stardew Valley",
38+
39+
# Steam
40+
"C:\Program Files\Steam\steamapps\common\Stardew Valley",
41+
"C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley"
42+
)
43+
}
44+
else {
45+
$possibleGamePaths=(
46+
# override
47+
"$HOME/StardewValley",
48+
49+
# Linux
50+
"$HOME/GOG Games/Stardew Valley/game",
51+
"$HOME/.steam/steam/steamapps/common/Stardew Valley",
52+
"$HOME/.local/share/Steam/steamapps/common/Stardew Valley",
53+
"$HOME/.var/app/com.valvesoftware.Steam/data/Steam/steamapps/common/Stardew Valley",
54+
55+
# macOS
56+
"/Applications/Stardew Valley.app/Contents/MacOS",
57+
"$HOME/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS"
58+
)
59+
}
60+
3161
$gamePath = ""
3262
foreach ($possibleGamePath in $possibleGamePaths) {
3363
if (Test-Path $possibleGamePath -PathType Container) {
@@ -46,45 +76,41 @@ $bundleModNames = "ConsoleCommands", "SaveBackup"
4676
# build configuration
4777
$buildConfig = "Release"
4878
$framework = "net6.0"
49-
$folders = "linux", "macOS", "windows"
50-
$runtimes = @{ linux = "linux-x64"; macOS = "osx-x64"; windows = "win-x64" }
51-
$msBuildPlatformNames = @{ linux = "Unix"; macOS = "OSX"; windows = "Windows_NT" }
79+
if ($windowsOnly) {
80+
$folders = "windows"
81+
$runtimes = @{ windows = "win-x64" }
82+
$msBuildPlatformNames = @{ windows = "Windows_NT" }
83+
}
84+
else {
85+
$folders = "linux", "macOS", "windows"
86+
$runtimes = @{ linux = "linux-x64"; macOS = "osx-x64"; windows = "win-x64" }
87+
$msBuildPlatformNames = @{ linux = "Unix"; macOS = "OSX"; windows = "Windows_NT" }
88+
}
5289

5390
# version number
5491
$version = $args[0]
5592
if (!$version) {
5693
$version = Read-Host "SMAPI release version (like '4.0.0')"
5794
}
5895

59-
# Windows-only build
60-
$windowsOnly = $false
61-
foreach ($arg in $args) {
62-
if ($arg -eq "--windows-only") {
63-
$windowsOnly = $true
64-
$folders = "windows"
65-
$runtimes = @{ windows = "win-x64" }
66-
$msBuildPlatformNames = @{ windows = "Windows_NT" }
67-
}
68-
}
69-
7096

7197
##########
7298
## Move to SMAPI root
7399
##########
74-
cd "$PSScriptRoot/../.."
100+
Set-Location "$PSScriptRoot/../.."
75101

76102

77103
##########
78104
## Clear old build files
79105
##########
80-
echo "Clearing old builds..."
81-
echo "-------------------------------------------------"
106+
Write-Output "Clearing old builds..."
107+
Write-Output "-------------------------------------------------"
82108

83-
foreach ($path in (dir -Recurse -Include ('bin', 'obj'))) {
84-
echo "$path"
85-
rm -Recurse -Force "$path"
109+
foreach ($path in (Get-ChildItem -Recurse -Include ('bin', 'obj'))) {
110+
Write-Output "$path"
111+
Remove-Item -Recurse -Force "$path"
86112
}
87-
echo ""
113+
Write-Output ""
88114

89115

90116
##########
@@ -95,33 +121,33 @@ foreach ($folder in $folders) {
95121
$runtime = $runtimes[$folder]
96122
$msbuildPlatformName = $msBuildPlatformNames[$folder]
97123

98-
echo "Compiling SMAPI for $folder..."
99-
echo "-------------------------------------------------"
124+
Write-Output "Compiling SMAPI for $folder..."
125+
Write-Output "-------------------------------------------------"
100126
dotnet publish src/SMAPI --configuration $buildConfig -v minimal --runtime "$runtime" --framework "$framework" -p:OS="$msbuildPlatformName" -p:TargetFrameworks="$framework" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained true
101-
echo ""
102-
echo ""
127+
Write-Output ""
128+
Write-Output ""
103129

104-
echo "Compiling installer for $folder..."
105-
echo "-------------------------------------------------"
130+
Write-Output "Compiling installer for $folder..."
131+
Write-Output "-------------------------------------------------"
106132
dotnet publish src/SMAPI.Installer --configuration $buildConfig -v minimal --runtime "$runtime" --framework "$framework" -p:OS="$msbuildPlatformName" -p:TargetFrameworks="$framework" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained true
107-
echo ""
108-
echo ""
133+
Write-Output ""
134+
Write-Output ""
109135

110136
foreach ($modName in $bundleModNames) {
111-
echo "Compiling $modName for $folder..."
112-
echo "-------------------------------------------------"
137+
Write-Output "Compiling $modName for $folder..."
138+
Write-Output "-------------------------------------------------"
113139
dotnet publish src/SMAPI.Mods.$modName --configuration $buildConfig -v minimal --runtime "$runtime" --framework "$framework" -p:OS="$msbuildPlatformName" -p:TargetFrameworks="$framework" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained false
114-
echo ""
115-
echo ""
140+
Write-Output ""
141+
Write-Output ""
116142
}
117143
}
118144

119145

120146
##########
121147
## Prepare install package
122148
##########
123-
echo "Preparing install package..."
124-
echo "----------------------------"
149+
Write-Output "Preparing install package..."
150+
Write-Output "----------------------------"
125151

126152
# init paths
127153
$installAssets = "src/SMAPI.Installer/assets"
@@ -130,7 +156,16 @@ $packageDevPath = "bin/SMAPI installer for developers"
130156

131157
# init structure
132158
foreach ($folder in $folders) {
133-
mkdir "$packagePath/internal/$folder/bundle/smapi-internal" > $null
159+
$folderPath = "$packagePath/internal/$folder/bundle/smapi-internal"
160+
161+
if ($IsWindows) {
162+
# On Windows, mkdir creates parent directories automatically and the --parents argument isn't recognized.
163+
mkdir "$folderPath" > $null
164+
}
165+
else
166+
{
167+
mkdir "$folderPath" --parents
168+
}
134169
}
135170

136171
# copy base installer files
@@ -139,7 +174,7 @@ foreach ($name in @("install on Linux.sh", "install on macOS.command", "install
139174
continue;
140175
}
141176

142-
cp "$installAssets/$name" "$packagePath"
177+
Copy-Item "$installAssets/$name" "$packagePath"
143178
}
144179

145180
# copy per-platform files
@@ -152,18 +187,18 @@ foreach ($folder in $folders) {
152187
$bundlePath = "$internalPath/bundle"
153188

154189
# installer files
155-
cp "src/SMAPI.Installer/bin/$buildConfig/$runtime/publish/*" "$internalPath" -Recurse
156-
rm -Recurse -Force "$internalPath/assets"
190+
Copy-Item "src/SMAPI.Installer/bin/$buildConfig/$runtime/publish/*" "$internalPath" -Recurse
191+
Remove-Item -Recurse -Force "$internalPath/assets"
157192

158193
# runtime config for SMAPI
159194
# This is identical to the one generated by the build, except that the min runtime version is
160-
# set to 5.0.0 (instead of whatever version it was built with) and rollForward is set to latestMinor instead of
195+
# set to 6.0.0 (instead of whatever version it was built with) and rollForward is set to latestMinor instead of
161196
# minor.
162-
cp "$installAssets/runtimeconfig.json" "$bundlePath/StardewModdingAPI.runtimeconfig.json"
197+
Copy-Item "$installAssets/runtimeconfig.json" "$bundlePath/StardewModdingAPI.runtimeconfig.json"
163198

164199
# installer DLL config
165200
if ($folder -eq "windows") {
166-
cp "$installAssets/windows-exe-config.xml" "$packagePath/internal/windows/install.exe.config"
201+
Copy-Item "$installAssets/windows-exe-config.xml" "$packagePath/internal/windows/install.exe.config"
167202
}
168203

169204
# bundle root files
@@ -172,68 +207,77 @@ foreach ($folder in $folders) {
172207
$name = "$name.exe"
173208
}
174209

175-
cp "$smapiBin/$name" "$bundlePath"
210+
Copy-Item "$smapiBin/$name" "$bundlePath"
176211
}
177212

178213
# bundle i18n
179-
cp -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal"
214+
Copy-Item -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal"
180215

181216
# bundle smapi-internal
182217
foreach ($name in @("0Harmony.dll", "0Harmony.xml", "Markdig.dll", "Mono.Cecil.dll", "Mono.Cecil.Mdb.dll", "Mono.Cecil.Pdb.dll", "MonoMod.Common.dll", "Newtonsoft.Json.dll", "Pathoschild.Http.Client.dll", "Pintail.dll", "TMXTile.dll", "SMAPI.Toolkit.dll", "SMAPI.Toolkit.xml", "SMAPI.Toolkit.CoreInterfaces.dll", "SMAPI.Toolkit.CoreInterfaces.xml", "System.Net.Http.Formatting.dll")) {
183-
cp "$smapiBin/$name" "$bundlePath/smapi-internal"
218+
Copy-Item "$smapiBin/$name" "$bundlePath/smapi-internal"
184219
}
185220

186221
if ($folder -eq "windows") {
187-
cp "$smapiBin/VdfConverter.dll" "$bundlePath/smapi-internal"
222+
Copy-Item "$smapiBin/VdfConverter.dll" "$bundlePath/smapi-internal"
188223
}
189224

190-
cp "$smapiBin/SMAPI.blacklist.json" "$bundlePath/smapi-internal/blacklist.json"
191-
cp "$smapiBin/SMAPI.config.json" "$bundlePath/smapi-internal/config.json"
192-
cp "$smapiBin/SMAPI.metadata.json" "$bundlePath/smapi-internal/metadata.json"
225+
Copy-Item "$smapiBin/SMAPI.blacklist.json" "$bundlePath/smapi-internal/blacklist.json"
226+
Copy-Item "$smapiBin/SMAPI.config.json" "$bundlePath/smapi-internal/config.json"
227+
Copy-Item "$smapiBin/SMAPI.metadata.json" "$bundlePath/smapi-internal/metadata.json"
193228
if ($folder -eq "linux" -or $folder -eq "macOS") {
194-
cp "$installAssets/unix-launcher.sh" "$bundlePath"
229+
Copy-Item "$installAssets/unix-launcher.sh" "$bundlePath"
195230
}
196231
else {
197-
cp "$installAssets/windows-exe-config.xml" "$bundlePath/StardewModdingAPI.exe.config"
232+
Copy-Item "$installAssets/windows-exe-config.xml" "$bundlePath/StardewModdingAPI.exe.config"
198233
}
199234

200235
# copy bundled mods
201236
foreach ($modName in $bundleModNames) {
202237
$fromPath = "src/SMAPI.Mods.$modName/bin/$buildConfig/$runtime/publish"
203238
$targetPath = "$bundlePath/Mods/$modName"
204239

205-
mkdir "$targetPath" > $null
240+
if ($IsWindows) {
241+
# On Windows, mkdir creates parent directories automatically and the --parents argument isn't recognized.
242+
mkdir "$targetPath" > $null
243+
}
244+
else
245+
{
246+
mkdir "$targetPath" --parents
247+
}
206248

207-
cp "$fromPath/$modName.dll" "$targetPath"
208-
cp "$fromPath/manifest.json" "$targetPath"
249+
Copy-Item "$fromPath/$modName.dll" "$targetPath"
250+
Copy-Item "$fromPath/manifest.json" "$targetPath"
209251
if (Test-Path "$fromPath/i18n" -PathType Container) {
210-
cp -Recurse "$fromPath/i18n" "$targetPath"
252+
Copy-Item -Recurse "$fromPath/i18n" "$targetPath"
211253
}
212254
}
213255
}
214256

215-
# DISABLED: will be handled by Linux script
216257
# mark scripts executable
217-
#ForEach ($path in @("install on Linux.sh", "install on macOS.command", "bundle/unix-launcher.sh")) {
218-
# if (Test-Path "$packagePath/$path" -PathType Leaf) {
219-
# chmod 755 "$packagePath/$path"
220-
# }
221-
#}
258+
if ($IsWindows) {
259+
Write-Warning "Can't set Unix file permissions on Windows. This may cause issues for Linux/macOS players."
260+
}
261+
else {
262+
ForEach ($path in @("install on Linux.sh", "install on macOS.command", "bundle/unix-launcher.sh")) {
263+
if (Test-Path "$packagePath/$path" -PathType Leaf) {
264+
chmod 755 "$packagePath/$path"
265+
}
266+
}
267+
}
222268

223269
# split into main + for-dev folders
224-
cp -Recurse "$packagePath" "$packageDevPath"
270+
Copy-Item -Recurse "$packagePath" "$packageDevPath"
225271
foreach ($folder in $folders) {
226272
# disable developer mode in main package
227273
In-Place-Regex -Path "$packagePath/internal/$folder/bundle/smapi-internal/config.json" -Search "`"DeveloperMode`": true" -Replace "`"DeveloperMode`": false"
228274

229275
# convert bundle folder into final 'install.dat' files
230-
if ($windowsOnly)
276+
foreach ($path in @("$packagePath/internal/$folder", "$packageDevPath/internal/$folder"))
231277
{
232-
foreach ($path in @("$packagePath/internal/$folder", "$packageDevPath/internal/$folder"))
233-
{
234-
Compress-Archive -Path "$path/bundle/*" -CompressionLevel Optimal -DestinationPath "$path/install.zip"
235-
mv "$path/install.zip" "$path/install.dat"
236-
rm -Recurse -Force "$path/bundle"
278+
Compress-Archive -Path "$path/bundle/*" -CompressionLevel Optimal -DestinationPath "$path/install.dat"
279+
if (!$skipBundleDeletion) {
280+
Remove-Item -Recurse -Force "$path/bundle"
237281
}
238282
}
239283
}
@@ -243,15 +287,12 @@ foreach ($folder in $folders) {
243287
### Create release zips
244288
###########
245289
# rename folders
246-
mv "$packagePath" "bin/SMAPI $version installer"
247-
mv "$packageDevPath" "bin/SMAPI $version installer for developers"
290+
Move-Item "$packagePath" "bin/SMAPI $version installer"
291+
Move-Item "$packageDevPath" "bin/SMAPI $version installer for developers"
248292

249293
# package files
250-
if ($windowsOnly)
251-
{
252-
Compress-Archive -Path "bin/SMAPI $version installer" -DestinationPath "bin/SMAPI $version installer.zip" -CompressionLevel Optimal
253-
Compress-Archive -Path "bin/SMAPI $version installer for developers" -DestinationPath "bin/SMAPI $version installer for developers.zip" -CompressionLevel Optimal
254-
}
294+
Compress-Archive -Path "bin/SMAPI $version installer" -DestinationPath "bin/SMAPI $version installer.zip" -CompressionLevel Optimal
295+
Compress-Archive -Path "bin/SMAPI $version installer for developers" -DestinationPath "bin/SMAPI $version installer for developers.zip" -CompressionLevel Optimal
255296

256-
echo ""
257-
echo "Done! See docs/technical/smapi.md to create the release zips."
297+
Write-Output ""
298+
Write-Output "Done! Package created in ${pwd.Path}/bin."

0 commit comments

Comments
 (0)