aboutsummaryrefslogtreecommitdiff
path: root/.github/workflows/package-win.yml
blob: 3942cb353d0933ab416d44b0ec6931b72a4ab9ae (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
name: Windows Installer Packaging

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      tag_name:
        description: 'Release Tag (e.g., v1.0.0). Leave empty to use "latest".'
        required: false
        default: ''

jobs:
  package-windows:
    runs-on: windows-latest
    permissions:
      contents: write
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install NSIS
        run: choco install nsis

      - name: Get Version from Cargo.toml
        id: get_version
        shell: pwsh
        run: |
          $VersionLine = Get-Content Cargo.toml | Select-String '^version\s*=\s*"(.*)"'
          if ($VersionLine.Matches.Success) {
            $Version = $VersionLine.Matches.Groups[1].Value
            Write-Host "Found version in Cargo.toml: $Version"
            "version=$Version" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
          } else {
            Write-Error "Could not find version in Cargo.toml"
            exit 1
          }

      - name: Determine Release Tag
        id: get_tag
        shell: pwsh
        env:
          GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          $TAG = "${{ inputs.tag_name }}"
          if (-not $TAG) { $TAG = "${{ github.event.release.tag_name }}" }
          
          if (-not $TAG) { 
            Write-Host "No tag provided, fetching latest release..."
            $TAG = $(gh release view --json tagName --jq .tagName) 
          }
          
          if (-not $TAG) {
            Write-Error "Could not determine release tag."
            exit 1
          }
          
          Write-Host "Using Tag: $TAG"
          "tag=$TAG" | Out-File -FilePath $env:GITHUB_OUTPUT -Append

      - name: Download Release Assets
        env:
          GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          $TAG = "${{ steps.get_tag.outputs.tag }}"
          $Version = "${{ steps.get_version.outputs.version }}"
          gh release download $TAG -p "mikusays-windows-x86_64-$Version.exe"
          gh release download $TAG -p "mikusays-windows-aarch64-$Version.exe"

      - name: Generate NSIS Script
        shell: pwsh
        run: |
          $Version = "${{ steps.get_version.outputs.version }}"
          $InstallerName = "mikusays-windows-inst-any-$Version.exe"
          
          # Prepend 0. to create a 4-part version string (0.X.Y.Z)
          $NsisVersion = "0.$Version"
          
          Write-Host "Installer Name: $InstallerName"
          Write-Host "NSIS Internal Version: $NsisVersion"

          $nsisContent = @'
          !include "MUI2.nsh"
          !include "LogicLib.nsh"
          !include "x64.nsh"
          !include "WinVer.nsh"
          ; Removed duplicate LogicLib.nsh inclusion

          ; --- Helper Macros ---
          !macro StrContains ResultVar Needle Haystack
            Push "${Haystack}"
            Push "${Needle}"
            Call StrContains
            Pop "${ResultVar}"
          !macroend

          Function StrContains
            Exch $R1
            Exch
            Exch $R0
            Push $R2
            Push $R3
            Push $R4
            Push $R5
            StrLen $R2 $R0
            StrLen $R3 $R1
            StrCpy $R5 ""
            Loop:
              StrCpy $R4 $R0 $R3
              StrCmp $R4 $R1 Found
              StrCpy $R0 $R0 "" 1
              StrLen $R2 $R0
              IntCmp $R2 $R3 Loop Loop End
            Found:
              StrCpy $R5 "1"
            End:
              Pop $R5
              Pop $R4
              Pop $R3
              Pop $R2
              Pop $R0
              Pop $R1
              Exch $R5
          FunctionEnd

          !macro StrRep ResultVar String SubString RepString
            Push "${String}"
            Push "${SubString}"
            Push "${RepString}"
            Call un.StrRep
            Pop "${ResultVar}"
          !macroend

          !macro un.StrRep ResultVar String SubString RepString
            Push "${String}"
            Push "${SubString}"
            Push "${RepString}"
            Call un.StrRep
            Pop "${ResultVar}"
          !macroend

          Function un.StrRep
            Exch $R2
            Exch
            Exch $R1
            Exch
            Exch $R0
            Push $R3
            Push $R4
            Push $R5
            Push $R6
            Push $R7
            StrCpy $R3 0
            StrLen $R4 $R1
            StrLen $R6 $R0
            Start:
              StrCpy $R5 $R0 $R4 $R3
              StrCmp $R5 $R1 Found
              IntOp $R3 $R3 + 1
              IntCmp $R3 $R6 End End Start
            Found:
              StrCpy $R5 $R0 $R3
              IntOp $R7 $R3 + $R4
              StrCpy $R7 $R0 "" $R7
              StrCpy $R0 "$R5$R2$R7"
              StrLen $R6 $R0
              IntOp $R3 $R3 + $R4
              Goto Start
            End:
              Pop $R7
              Pop $R6
              Pop $R5
              Pop $R4
              Pop $R3
              Pop $R2
              Pop $R1
              Exch $R0
          FunctionEnd

          !define StrContains `!insertmacro StrContains`
          !define StrRep `!insertmacro StrRep`
          !define un.StrRep `!insertmacro un.StrRep`

          ; --- Main Script ---
          Name "Mikusays"
          OutFile "__OUTFILE__"
          Unicode True

          ; --- Metadata & Versioning ---
          VIProductVersion "__NSISVERSION__"
          VIAddVersionKey "ProductName" "Mikusays"
          VIAddVersionKey "CompanyName" "xxanqw"
          VIAddVersionKey "LegalCopyright" "© xxanqw"
          VIAddVersionKey "FileDescription" "Mikusays Installer"
          VIAddVersionKey "FileVersion" "__VERSION__"
          VIAddVersionKey "ProductVersion" "__VERSION__"

          RequestExecutionLevel user
          InstallDir "$LOCALAPPDATA\Programs\mikusays"

          !define MUI_ABORTWARNING
          !insertmacro MUI_PAGE_WELCOME
          !insertmacro MUI_PAGE_INSTFILES
          !insertmacro MUI_PAGE_FINISH
          !insertmacro MUI_UNPAGE_CONFIRM
          !insertmacro MUI_UNPAGE_INSTFILES
          !insertmacro MUI_LANGUAGE "English"

          Section "Install"
              SetOutPath "$INSTDIR"

              ; --- Architecture Detection & Redist Selection ---
              ; We select both the app binary and the correct VC Redist URL here
              ReadEnvStr $0 "PROCESSOR_ARCHITECTURE"
              ${If} $0 == "ARM64"
                  File "/oname=mikusays.exe" "mikusays-windows-aarch64.exe"
                  StrCpy $1 "https://aka.ms/vs/17/release/vc_redist.arm64.exe"
              ${Else}
                  File "/oname=mikusays.exe" "mikusays-windows-x86_64.exe"
                  StrCpy $1 "https://aka.ms/vs/17/release/vc_redist.x64.exe"
              ${EndIf}

              WriteUninstaller "$INSTDIR\uninstall.exe"

              ; --- Download and Install VC++ Redist (Using PowerShell) ---
              DetailPrint "Downloading Visual C++ Redistributable..."
              
              ; Define temp path for the installer
              StrCpy $2 "$TEMP\vc_redist.exe"
              
              ; Use PowerShell to download. Quotes are carefully escaped:
              ; Outer quotes '...' for NSIS ExecWait string
              ; Inner quotes "..." for PowerShell command
              ; Escaped quotes \' for values inside the PowerShell string
              ExecWait 'powershell -NoProfile -NonInteractive -Command "Invoke-WebRequest -Uri \'$1\' -OutFile \'$2\'"' $3
              
              ${If} $3 != 0
                  DetailPrint "Download failed (Error code: $3). Skipping Redistributable."
              ${Else}
                  DetailPrint "Installing Visual C++ Redistributable..."
                  ; /install /quiet /norestart are standard flags for VC Redist
                  ExecWait '"$2" /install /quiet /norestart'
                  Delete "$2" ; Clean up the downloaded file
              ${EndIf}

              ; --- Add to PATH ---
              ReadRegStr $0 HKCU "Environment" "Path"
              ${StrContains} $1 "$INSTDIR" "$0"
              ${If} $1 == ""
                  WriteRegExpandStr HKCU "Environment" "Path" "$0;$INSTDIR"
                  SendMessage ${HWND_BROADCAST} ${WM_SETTINGCHANGE} 0 "STR:Environment" /TIMEOUT=5000
              ${EndIf}
          SectionEnd

          Section "Uninstall"
              Delete "$INSTDIR\mikusays.exe"
              Delete "$INSTDIR\uninstall.exe"
              RMDir "$INSTDIR"

              ; Remove from PATH
              ReadRegStr $0 HKCU "Environment" "Path"
              ${un.StrRep} $0 "$0" ";$INSTDIR" ""
              ${un.StrRep} $0 "$0" "$INSTDIR;" ""
              ${un.StrRep} $0 "$0" "$INSTDIR" ""
              WriteRegExpandStr HKCU "Environment" "Path" "$0"
              SendMessage ${HWND_BROADCAST} ${WM_SETTINGCHANGE} 0 "STR:Environment" /TIMEOUT=5000
          SectionEnd
          '@
          
          $nsisContent = $nsisContent.Replace("__OUTFILE__", $InstallerName)
          $nsisContent = $nsisContent.Replace("__NSISVERSION__", $NsisVersion)
          $nsisContent = $nsisContent.Replace("__VERSION__", $Version)
          
          Set-Content -Path installer.nsi -Value $nsisContent

      - name: Compile Installer
        shell: cmd
        run: '"C:\Program Files (x86)\NSIS\makensis.exe" installer.nsi'

      - name: Upload Installer to Release
        env:
          GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
        run: |
          $TAG = "${{ steps.get_tag.outputs.tag }}"
          $Version = "${{ steps.get_version.outputs.version }}"
          $UploadName = "mikusays-windows-inst-any-$Version.exe"
          gh release upload $TAG $UploadName --clobber