summaryrefslogtreecommitdiff
path: root/build/windows/prepare-install-package.ps1
blob: 71de1154710c87d5b6afe375319e03462aaf6008 (plain)
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
#
#
# This is the PowerShell equivalent of ../unix/prepare-install-package.sh, *except* that it doesn't
# set Linux permissions, create the install.dat files, or create the final zip (unless you specify
# --windows-only). Due to limitations in PowerShell, the final changes are handled by the
# windows/finalize-install-package.sh file in WSL.
#
# When making changes, make sure to update ../unix/prepare-install-package.ps1 too.
#
#

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


##########
## Fetch values
##########
# paths
$gamePath = "C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley"
$bundleModNames = "ConsoleCommands", "ErrorHandler", "SaveBackup"

# build configuration
$buildConfig = "Release"
$folders = "linux", "macOS", "windows"
$runtimes = @{ linux = "linux-x64"; macOS = "osx-x64"; windows = "win-x64" }
$msBuildPlatformNames = @{ linux = "Unix"; macOS = "OSX"; windows = "Windows_NT" }

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

# Windows-only build
$windowsOnly = $false
foreach ($arg in $args) {
    if ($arg -eq "--windows-only") {
        $windowsOnly = $true
        $folders = "windows"
        $runtimes = @{ windows = "win-x64" }
        $msBuildPlatformNames = @{ windows = "Windows_NT" }
    }
}


##########
## Move to SMAPI root
##########
cd "$PSScriptRoot/../.."


##########
## Clear old build files
##########
echo "Clearing old builds..."
echo "-------------------------------------------------"

foreach ($path in (dir -Recurse -Include ('bin', 'obj'))) {
    echo "$path"
    rm -Recurse -Force "$path"
}
echo ""


##########
## Compile files
##########
. "$PSScriptRoot/set-smapi-version.ps1" "$version"
foreach ($folder in $folders) {
    $runtime = $runtimes[$folder]
    $msbuildPlatformName = $msBuildPlatformNames[$folder]

    echo "Compiling SMAPI for $folder..."
    echo "-------------------------------------------------"
    dotnet publish src/SMAPI --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" --self-contained true
    echo ""
    echo ""

    echo "Compiling installer for $folder..."
    echo "-------------------------------------------------"
    dotnet publish src/SMAPI.Installer --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false" -p:PublishTrimmed=True -p:TrimMode=Link --self-contained true
    echo ""
    echo ""

    foreach ($modName in $bundleModNames) {
        echo "Compiling $modName for $folder..."
        echo "-------------------------------------------------"
        dotnet publish src/SMAPI.Mods.$modName --configuration $buildConfig -v minimal --runtime "$runtime" -p:OS="$msbuildPlatformName" -p:GamePath="$gamePath" -p:CopyToGameFolder="false"
        echo ""
        echo ""
    }
}


##########
## Prepare install package
##########
echo "Preparing install package..."
echo "----------------------------"

# init paths
$installAssets = "src/SMAPI.Installer/assets"
$packagePath = "bin/SMAPI installer"
$packageDevPath = "bin/SMAPI installer for developers"

# init structure
foreach ($folder in $folders) {
    mkdir "$packagePath/internal/$folder/bundle/smapi-internal" > $null
}

# copy base installer files
foreach ($name in @("install on Linux.sh", "install on macOS.command", "install on Windows.bat", "README.txt")) {
    if ($windowsOnly -and ($name -eq "install on Linux.sh" -or $name -eq "install on macOS.command")) {
        continue;
    }

    cp "$installAssets/$name" "$packagePath"
}

# copy per-platform files
foreach ($folder in $folders) {
    $runtime = $runtimes[$folder]

    # get paths
    $smapiBin = "src/SMAPI/bin/$buildConfig/$runtime/publish"
    $internalPath = "$packagePath/internal/$folder"
    $bundlePath = "$internalPath/bundle"

    # installer files
    cp "src/SMAPI.Installer/bin/$buildConfig/$runtime/publish/*" "$internalPath" -Recurse
    rm -Recurse -Force "$internalPath/assets"

    # runtime config for SMAPI
    # This is identical to the one generated by the build, except that the min runtime version is
    # set to 5.0.0 (instead of whatever version it was built with) and rollForward is set to latestMinor instead of
    # minor.
    cp "$installAssets/runtimeconfig.json" "$bundlePath/StardewModdingAPI.runtimeconfig.json"

    # installer DLL config
    if ($folder -eq "windows") {
        cp "$installAssets/windows-exe-config.xml" "$packagePath/internal/windows/install.exe.config"
    }

    # bundle root files
    foreach ($name in @("StardewModdingAPI", "StardewModdingAPI.dll", "StardewModdingAPI.pdb", "StardewModdingAPI.xml", "steam_appid.txt")) {
        if ($name -eq "StardewModdingAPI" -and $folder -eq "windows") {
            $name = "$name.exe"
        }

        cp "$smapiBin/$name" "$bundlePath"
    }

    # bundle i18n
    cp -Recurse "$smapiBin/i18n" "$bundlePath/smapi-internal"

    # bundle smapi-internal
    foreach ($name in @("0Harmony.dll", "0Harmony.xml", "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.pdb", "SMAPI.Toolkit.xml", "SMAPI.Toolkit.CoreInterfaces.dll", "SMAPI.Toolkit.CoreInterfaces.pdb", "SMAPI.Toolkit.CoreInterfaces.xml", "System.Net.Http.Formatting.dll")) {
        cp "$smapiBin/$name" "$bundlePath/smapi-internal"
    }

    if ($folder -eq "windows") {
        cp "$smapiBin/VdfConverter.dll" "$bundlePath/smapi-internal"
    }

    cp "$smapiBin/SMAPI.config.json" "$bundlePath/smapi-internal/config.json"
    cp "$smapiBin/SMAPI.metadata.json" "$bundlePath/smapi-internal/metadata.json"
    if ($folder -eq "linux" -or $folder -eq "macOS") {
        cp "$installAssets/unix-launcher.sh" "$bundlePath"
    }
    else {
        cp "$installAssets/windows-exe-config.xml" "$bundlePath/StardewModdingAPI.exe.config"
    }

    # copy .NET dependencies
    if ($folder -eq "windows") {
        cp "$smapiBin/System.Management.dll" "$bundlePath/smapi-internal"
    }

    # copy legacy .NET dependencies (remove in SMAPI 4.0.0)
    cp "$smapiBin/System.Configuration.ConfigurationManager.dll" "$bundlePath/smapi-internal"
    cp "$smapiBin/System.Runtime.Caching.dll" "$bundlePath/smapi-internal"
    cp "$smapiBin/System.Security.Permissions.dll" "$bundlePath/smapi-internal"

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

        mkdir "$targetPath" > $null

        cp "$fromPath/$modName.dll" "$targetPath"
        cp "$fromPath/$modName.pdb" "$targetPath"
        cp "$fromPath/manifest.json" "$targetPath"
        if (Test-Path "$fromPath/i18n" -PathType Container) {
            cp -Recurse "$fromPath/i18n" "$targetPath"
        }
    }
}

# DISABLED: will be handled by Linux script
# mark scripts executable
#ForEach ($path in @("install on Linux.sh", "install on macOS.command", "bundle/unix-launcher.sh")) {
#    if (Test-Path "$packagePath/$path" -PathType Leaf) {
#        chmod 755 "$packagePath/$path"
#    }
#}

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

    # convert bundle folder into final 'install.dat' files
    if ($windowsOnly)
    {
        foreach ($path in @("$packagePath/internal/$folder", "$packageDevPath/internal/$folder"))
        {
            Compress-Archive -Path "$path/bundle/*" -CompressionLevel Optimal -DestinationPath "$path/install.zip"
            mv "$path/install.zip" "$path/install.dat"
            rm -Recurse -Force "$path/bundle"
        }
    }
}


###########
### Create release zips
###########
# rename folders
mv "$packagePath" "bin/SMAPI $version installer"
mv "$packageDevPath" "bin/SMAPI $version installer for developers"

# package files
if ($windowsOnly)
{
    Compress-Archive -Path "bin/SMAPI $version installer" -DestinationPath "bin/SMAPI $version installer.zip" -CompressionLevel Optimal
    Compress-Archive -Path "bin/SMAPI $version installer for developers" -DestinationPath "bin/SMAPI $version installer for developers.zip" -CompressionLevel Optimal
}

echo ""
echo "Done! See docs/technical/smapi.md to create the release zips."