From 1f741dec0a8086af5dbab8ec45e3919d23e14a00 Mon Sep 17 00:00:00 2001 From: Tlntin Date: Wed, 8 Mar 2023 11:40:10 +0800 Subject: [PATCH] replace cmd .bat with powershell .ps1 --- package.json | 2 +- script/build.bat | 112 ------------------------------------------- script/build.ps1 | 120 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 113 deletions(-) delete mode 100644 script/build.bat create mode 100644 script/build.ps1 diff --git a/package.json b/package.json index beb7886..7f0097e 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "build": "npm run tauri build --release", "build:mac": "npm run tauri build -- --target universal-apple-darwin", "build:all-unix": "chmod +x ./script/build.sh && ./script/build.sh", - "build:all-windows": ".\\script\\build.bat", + "build:all-windows": ".\\script\\build.ps1", "tauri": "tauri", "cli": "rollup -c rollup.config.js --watch", "cli:build": "cross-env NODE_ENV=production rollup -c rollup.config.js", diff --git a/script/build.bat b/script/build.bat deleted file mode 100644 index 3c6974a..0000000 --- a/script/build.bat +++ /dev/null @@ -1,112 +0,0 @@ -@echo off -chcp 65001 - -if not exist node_modules ( - call npm i -) - -if not exist output ( - mkdir output -) - - -if not exist output\windows ( - mkdir output\windows -) - -echo. -echo ======================= -echo "build for windows" -echo ======================= -echo. - -set identifier_prefix="com.tw93" - - - -:: total package number -set /A index=1 -for /f %%a in (' find /c /v "" ^<"app.csv" ') do set /A total=%%a -:: ignore first header line -set /A total=total-1 - - -:: for windows, we need replace package name to title -:: .\script\sd.exe "\"productName\": \"weread\"" "\"productName\": \"WeRead\"" src-tauri\tauri.conf.json - -for /f "skip=1 tokens=1-4 delims=," %%i in (app.csv) do ( - setlocal enabledelayedexpansion - set name=%%i - set title=%%j - set name_zh=%%k - set url=%%l - @echo on - - :: replace url - :: clear url with regex - .\script\sd.exe "\"url\": \"(.*?)\"," "\"url\": \"\"," src-tauri/tauri.conf.json - :: replace url with no regex - .\script\sd.exe -s "\"url\": \"\"," "\"url\": \"!url!\"," src-tauri/tauri.conf.json - - ::replace pacakge name - :: clear package_name with regex - .\script\sd.exe "\"productName\": \"(.*?)\"," "\"productName\": \"\"," src-tauri/tauri.conf.json - :: replace package_name with no regex - .\script\sd.exe -s "\"productName\": \"\"," "\"productName\": \"!title!\"," src-tauri/tauri.conf.json - - :: replace icon - ::clear icon path with regex - .\script\sd.exe "\"icon\": \[\"(.*?)\"\]," "\"icon\": [\"\"]," src-tauri/tauri.windows.conf.json - :: replace icon path with no regex - .\script\sd.exe -s "\"icon\": [\"\"]," "\"icon\": [\"icons/!name!_256.ico\", \"!name!_32.ico\"]," src-tauri/tauri.windows.conf.json - - :: replace identifier - :: clear identifier with regex - .\script\sd.exe "\"identifier\": \"(.*?)\"," "\"identifier\": \"\"," src-tauri/tauri.windows.conf.json - :: replace identifier with not regex - .\script\sd.exe -s "\"identifier\": \"\"," "\"identifier\": \"!identifier_prefix!.!name!\"," src-tauri/tauri.windows.conf.json - - :: replace icon resources - :: clear resources with regex - .\script\sd.exe "\"resources\": \[\"(.*?)\"\]" "\"resources\": \[\"\"\]" src-tauri/tauri.windows.conf.json - :: replace resources with no regex - .\script\sd.exe -s "\"resources\": [\"\"]" "\"resources\": [\"!name!_32.ico\"]" src-tauri/tauri.windows.conf.json - - if not exist src-tauri\png\!name!_32.ico ( - copy src-tauri\png\icon_32.ico src-tauri\png\!name!_32.ico - ) - - if not exist src-tauri\png\!name!_256.ico ( - copy src-tauri\png\icon_256.ico src-tauri\png\!name!_256.ico - ) - echo. - - ::build package - echo building package !index!/!total! - echo package name is !name! !name_zh! - echo npm run build:windows - @echo off - call npm run tauri build -- --target x86_64-pc-windows-msvc - move src-tauri\target\x86_64-pc-windows-msvc\release\bundle\msi\*.msi output\windows\!title!_x64.msi - ::rm cache - del /q /f /s src-tauri\target\x86_64-pc-windows-msvc\release\*.exe - del /q /f /s src-tauri\target\x86_64-pc-windows-msvc\release\resources\*.ico - del /q /f /s src-tauri\target\x86_64-pc-windows-msvc\release\png\*.ico - del /q /f /s src-tauri\target\x86_64-pc-windows-msvc\release\wix\*.* - del /q /f /s src-tauri\target\x86_64-pc-windows-msvc\release\app.* - rd /s /q src-tauri\target\x86_64-pc-windows-msvc\release\resources - rd /s /q src-tauri\target\x86_64-pc-windows-msvc\release\png - rd /s /q src-tauri\target\x86_64-pc-windows-msvc\release\wix - @echo on - echo package build success! - echo. - echo. - - set /A index=index+1 - @echo off - -) - -:: for windows, we need replace package name to lower again -:: .\script\sd.exe "\"productName\": \"WeRead\"" "\"productName\": \"weread\"" src-tauri\tauri.conf.json -echo "output dir is output\windows" diff --git a/script/build.ps1 b/script/build.ps1 new file mode 100644 index 0000000..886cde2 --- /dev/null +++ b/script/build.ps1 @@ -0,0 +1,120 @@ +chcp 65001 | Out-Null + +if (-not (Test-Path node_modules)) { + npm i +} + +if (-not (Test-Path output)) { + New-Item -ItemType Directory -Path output +} + +if (-not (Test-Path output\windows)) { + New-Item -ItemType Directory -Path output\windows +} + +Write-Host "`n=======================" +Write-Host "build for windows" +Write-Host "=======================`n" + +$identifier_prefix = "com.tw93" + +# total package number +$index = 1 +$total = (Get-Content ./app.csv | Measure-Object -Line).Lines +$common_conf_path = "src-tauri/tauri.conf.json" +$windows_conf_path = "src-tauri/tauri.windows.conf.json" + +# ignore first header line +$total = $total - 1 + +# for windows, we need replace package name to title +# .\script\sd.exe "\"productName\": \"weread\"" "\"productName\": \"WeRead\"" src-tauri\tauri.conf.json +ForEach ($line in (Get-Content -Path .\app.csv | Select-Object -Skip 1)) { + $name, $title, $name_zh, $url = $line.Split(",") + Write-Host "building package ${index}/${total}" + Write-Host "package name is ${name} ${name_zh}" + Write-Host "==========================" + Write-Host "building Args is:" + Write-Host "name = ${name}" + Write-Host "title = ${title}" + Write-Host "name_zh = ${name_zh}" + Write-Host "url = ${url}" + Write-Host "==========================" + # -- replace url -- + # clear url with regex + (Get-Content -Path $common_conf_path -Raw) -replace '"url":\s*"[^"]*"', '"url": ""' | Set-Content -Path $common_conf_path + # replace url with no regex + .\script\sd.exe -s '"url": ""' "`"url`": `"${url}`"" $common_conf_path + + # replace package name + # clear package_name with regex + (Get-Content -Path $common_conf_path -Raw) -replace '"productName":\s*"[^"]*"', '"productName": ""' | Set-Content -Path $common_conf_path + # replace package_name with no regex + .\script\sd.exe -s '"productName": ""' "`"productName`": `"$title`"" $common_conf_path + + # -- replace icon -- + # clear icon path with regex + (Get-Content -Path $windows_conf_path -Raw) -replace '(?s)"icon":\s*\[[^\]]*\]', '"icon": []' | Set-Content -Path $windows_conf_path + # replace icon path with no regex + .\script\sd.exe -s '"icon": []' "`"icon`": [`"png/${name}_256.ico`", `"png/${name}_32.ico`"]" $windows_conf_path + + # -- replace identifier -- + # clear identifier with regex + (Get-Content -Path $windows_conf_path -Raw) -replace '"identifier":\s*"[^"]*"', '"identifier": ""' | Set-Content -Path $windows_conf_path + # -- replace identifier with no regex -- + .\script\sd.exe -s '"identifier": ""' "`"identifier`": `"${identifier_prefix}.${name}`"" $windows_conf_path + + # -- replace icon resources -- + # clear resources with regex + (Get-Content -Path $windows_conf_path -Raw) -replace '(?s)"resources":\s*\[[^\]]*\]', '"resources": []' | Set-Content -Path $windows_conf_path + # replace resources with no regex + .\script\sd.exe -s '"resources": []' "`"resources`": [`"png/${name}_32.ico`"]" $windows_conf_path + + if (-not (Test-Path "src-tauri\png\${name}_32.ico")) { + Copy-Item "src-tauri\png\icon_32.ico" "src-tauri\png\${name}_32.ico" + } + + if (-not (Test-Path "src-tauri\png\${name}_256.ico")) { + Copy-Item "src-tauri\png\icon_256.ico" "src-tauri\png\${name}_256.ico" + } + + # build package + Write-Host "npm run build:windows" + npm run tauri build -- --target x86_64-pc-windows-msvc + Move-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\bundle\msi\*.msi" -Destination "output\windows\${title}_x64.msi" + #rm cache + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\*.exe" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\resources\*.ico" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\png\*.ico" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\wix\*.*" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\app.*" -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\resources" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\png" -Recurse -Force + Remove-Item -Path "src-tauri\target\x86_64-pc-windows-msvc\release\wix" -Recurse -Force + Write-Host "package build success!" + Write-Host "" + $index++ + # strip blank line for common_conf_path + $lines = Get-Content ${common_conf_path} + $lastNonEmptyLineIndex = ($lines.Count - 1) + while ($lastNonEmptyLineIndex -ge 0 -and -not $lines[$lastNonEmptyLineIndex].Trim()) { + $lastNonEmptyLineIndex-- + } + if ($lastNonEmptyLineIndex -lt ($lines.Count - 1)) { + $lines = $lines[0..$lastNonEmptyLineIndex] + } + Set-Content -Path ${common_conf_path} -Value $lines + + # strip blank line for windows conf_path + $lines = Get-Content ${windows_conf_path} + $lastNonEmptyLineIndex = ($lines.Count - 1) + while ($lastNonEmptyLineIndex -ge 0 -and -not $lines[$lastNonEmptyLineIndex].Trim()) { + $lastNonEmptyLineIndex-- + } + if ($lastNonEmptyLineIndex -lt ($lines.Count - 1)) { + $lines = $lines[0..$lastNonEmptyLineIndex] + } + Set-Content -Path ${windows_conf_path} -Value $lines +} + +Write-Host "output dir is output\windows"