From 7f1878c5ce91f9861809c2e0c10118983ace4cc8 Mon Sep 17 00:00:00 2001 From: Jonson Petard <41122242+greenhat616@users.noreply.github.com> Date: Fri, 20 Sep 2024 14:55:08 +0800 Subject: [PATCH] feat: support fixed webview variants feat: support fixed webview variants --- .../workflows/deps-build-windows-nsis.yaml | 108 +++++++++++++----- .../tauri/overrides/fixed-webview2.conf.json | 20 ++++ scripts/portable.ts | 3 +- scripts/prepare-nightly.ts | 25 +++- scripts/prepare-release.ts | 27 ++++- scripts/utils/env.ts | 4 + 6 files changed, 154 insertions(+), 33 deletions(-) create mode 100644 backend/tauri/overrides/fixed-webview2.conf.json diff --git a/.github/workflows/deps-build-windows-nsis.yaml b/.github/workflows/deps-build-windows-nsis.yaml index 5bf5760cf3..0e82ba54d2 100644 --- a/.github/workflows/deps-build-windows-nsis.yaml +++ b/.github/workflows/deps-build-windows-nsis.yaml @@ -9,6 +9,12 @@ on: type: boolean default: false + fixed-webview: + description: "Fixed WebView" + required: true + type: boolean + default: false + nightly: description: "Nightly prepare" required: true @@ -38,6 +44,12 @@ on: type: boolean default: false + fixed-webview: + description: "Fixed WebView" + required: true + type: boolean + default: false + nightly: description: "Nightly prepare" required: true @@ -108,67 +120,107 @@ jobs: } } + - name: Download fixed WebView + if: ${{ inputs.fixed-webview == true }} + run: | + $condition = '${{ inputs.arch }}' + switch ($condition) { + 'x86_64' { + $arch= 'x64' + } + 'i686' { + $arch = 'x86' + } + 'aarch64' { + $arch = 'arm64' + } + } + + $version = '127.0.2651.105' + $uri = "https://github.com/westinyang/WebView2RuntimeArchive/releases/download/$version/Microsoft.WebView2.FixedVersionRuntime.$version.$arch.cab" + $outfile = "Microsoft.WebView2.FixedVersionRuntime.$version.$arch.cab" + echo "Downloading $uri to $outfile" + invoke-webrequest -uri $uri -outfile $outfile + echo "Download finished, attempting to extract" + Expand-Archive -Path $outfile -DestinationPath './backend/tauri' + echo "Extraction finished" + - name: Prepare (Windows NSIS and Portable) + if: ${{ inputs.fixed-webview == false }} run: ${{ inputs.nightly == true && 'pnpm prepare:nightly --nsis' || 'pnpm prepare:release --nsis' }} + - name: Prepare (Windows NSIS and Portable) with fixed WebView + if: ${{ inputs.fixed-webview == true }} + run: ${{ inputs.nightly == true && 'pnpm prepare:nightly --nsis --fixed-webview' || 'pnpm prepare:release --nsis --fixed-webview' }} + - name: Build UI run: | pnpm -F ui build # TODO: optimize strategy - name: Tauri build x86_64 - uses: tauri-apps/tauri-action@v0 if: ${{ inputs.arch == 'x86_64' }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }} TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }} NIGHTLY: ${{ inputs.nightly == true && 'true' || 'false' }} - with: - tagName: ${{ inputs.tag }} - releaseName: "Clash Nyanpasu Dev" - releaseBody: "More new features are now supported." - releaseDraft: false - prerelease: true - tauriScript: pnpm tauri - args: ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json' || '-f default-meta' }} + run: | + pnpm tauri build ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json' || '-f default-meta' }} - name: Tauri build i686 - uses: tauri-apps/tauri-action@v0 if: ${{ inputs.arch == 'i686' }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }} TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }} NIGHTLY: ${{ inputs.nightly == true && 'true' || 'false' }} - with: - tagName: ${{ inputs.tag }} - releaseName: "Clash Nyanpasu Dev" - releaseBody: "More new features are now supported." - releaseDraft: false - prerelease: true - tauriScript: pnpm tauri - args: ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json --target i686-pc-windows-msvc' || '-f default-meta --target i686-pc-windows-msvc' }} + run: | + pnpm tauri build ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json --target i686-pc-windows-msvc' || '-f default-meta --target i686-pc-windows-msvc' }} - name: Tauri build arm64 - uses: tauri-apps/tauri-action@v0 if: ${{ inputs.arch == 'aarch64' }} env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }} TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }} NIGHTLY: ${{ inputs.nightly == true && 'true' || 'false' }} - with: - tagName: ${{ inputs.tag }} - releaseName: "Clash Nyanpasu Dev" - releaseBody: "More new features are now supported." - releaseDraft: false - prerelease: true - tauriScript: pnpm tauri - args: ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json --target aarch64-pc-windows-msvc' || '-f default-meta --target aarch64-pc-windows-msvc' }} + run: | + pnpm tauri build ${{ inputs.nightly == true && '-f nightly -c ./backend/tauri/tauri.nightly.conf.json --target aarch64-pc-windows-msvc' || '-f default-meta --target aarch64-pc-windows-msvc' }} + + - name: Rename fixed webview bundle name + if: ${{ inputs.fixed-webview == true }} + run: | + $files = Get-ChildItem -Path "./backend/target" -Recurse -Include "*.exe", "*.zip", "*.zip.sig" | Where-Object { $_.FullName -like "*\bundle\*" } + $condition = '${{ inputs.arch }}' + switch ($condition) { + 'x86_64' { + $arch= 'x64' + } + 'i686' { + $arch = 'x86' + } + 'aarch64' { + $arch = 'arm64' + } + } + + foreach ($file in $files) { + echo "Renaming $file.FullName" + $newname = $file.FullName -replace $arch, "fixed-webview-$arch" + Rename-Item -Path $file.FullName -NewName $newname + } + + - name: Upload to release + run: | + $files = Get-ChildItem -Path "./backend/target" -Recurse -Include "*.exe", "*.zip", "*.zip.sig" | Where-Object { $_.FullName -like "*\bundle\*" } + foreach ($file in $files) { + echo "Uploading $file.FullName" + gh release upload ${{ inputs.tag }} "$file.FullName" --clobber + } - name: Portable Bundle if: ${{ inputs.portable == true }} run: | - pnpm portable + pnpm portable ${{ inputs.fixed-webview == true && '--fixed-webview' || '' }} env: RUST_ARCH: ${{ inputs.arch }} TAG_NAME: ${{ inputs.tag }} diff --git a/backend/tauri/overrides/fixed-webview2.conf.json b/backend/tauri/overrides/fixed-webview2.conf.json new file mode 100644 index 0000000000..f7925d3bd9 --- /dev/null +++ b/backend/tauri/overrides/fixed-webview2.conf.json @@ -0,0 +1,20 @@ +{ + "$schema": "../../../node_modules/@tauri-apps/cli/config.schema.json", + "bundle": { + "windows": { + "webviewInstallMode": { + "type": "fixedRuntime", + "path": "SHOULD_BE_REPLACED_WITH_THE_PATH_TO_THE_FIXED_WEBVIEW" + } + } + }, + "plugins": { + "updater": { + "endpoints": [ + "https://mirror.ghproxy.com/https://github.com/LibNyanpasu/clash-nyanpasu/releases/download/updater/update-fixedwebview-proxy.json", + "https://gh-proxy.com/https://github.com/LibNyanpasu/clash-nyanpasu/releases/download/updater/update-fixedwebview-proxy.json", + "https://github.com/LibNyanpasu/clash-nyanpasu/releases/download/updater/update-fixedwebview.json" + ] + } + } +} diff --git a/scripts/portable.ts b/scripts/portable.ts index d16959e811..f1cc7d75b0 100644 --- a/scripts/portable.ts +++ b/scripts/portable.ts @@ -6,6 +6,7 @@ import packageJson from "../package.json"; import { colorize, consola } from "./utils/logger"; const RUST_ARCH = process.env.RUST_ARCH || "x86_64"; +const fixedWebview = process.argv.includes("--fixed-webview"); /// Script for ci /// 打包绿色版/便携版 (only Windows) @@ -43,7 +44,7 @@ async function resolvePortable() { const { version } = packageJson; - const zipFile = `Clash.Nyanpasu_${version}_${RUST_ARCH}_portable.zip`; + const zipFile = `Clash.Nyanpasu_${version}_${RUST_ARCH}${fixedWebview ? "_fixedWebview2" : ""}_portable.zip`; zip.writeZip(zipFile); consola.success("create portable zip successfully"); diff --git a/scripts/prepare-nightly.ts b/scripts/prepare-nightly.ts index 4499783504..70e5d30141 100644 --- a/scripts/prepare-nightly.ts +++ b/scripts/prepare-nightly.ts @@ -2,7 +2,11 @@ import { execSync } from "child_process"; import path from "node:path"; import fs from "fs-extra"; import { merge } from "lodash-es"; -import { cwd, TAURI_APP_DIR } from "./utils/env"; +import { + cwd, + TAURI_APP_DIR, + TAURI_FIXED_WEBVIEW2_CONFIG_OVERRIDE_PATH, +} from "./utils/env"; import { consola } from "./utils/logger"; const TAURI_DEV_APP_CONF_PATH = path.join( @@ -24,15 +28,32 @@ const NYANPASU_PACKAGE_JSON_PATH = path.join( const isNSIS = process.argv.includes("--nsis"); // only build nsis const isMSI = process.argv.includes("--msi"); // only build msi +const fixedWebview = process.argv.includes("--fixed-webview"); async function main() { consola.debug("Read config..."); const tauriAppConf = await fs.readJSON(TAURI_APP_CONF); const tauriAppOverrides = await fs.readJSON(TAURI_DEV_APP_OVERRIDES_PATH); - const tauriConf = merge(tauriAppConf, tauriAppOverrides); + let tauriConf = merge(tauriAppConf, tauriAppOverrides); const packageJson = await fs.readJSON(NYANPASU_PACKAGE_JSON_PATH); const rootPackageJson = await fs.readJSON(ROOT_PACKAGE_JSON_PATH); // const wxsFile = await fs.readFile(WXS_PATH, "utf-8"); + if (fixedWebview) { + const fixedWebview2Config = await fs.readJSON( + TAURI_FIXED_WEBVIEW2_CONFIG_OVERRIDE_PATH, + ); + const webviewPath = (await fs.readdir(TAURI_APP_DIR)).find((file) => + file.includes("WebView2"), + ); + if (!webviewPath) { + throw new Error("WebView2 runtime not found"); + } + tauriConf = merge(tauriConf, fixedWebview2Config); + tauriConf.bundle.windows.webviewInstallMode.path = path.join( + "./", + path.basename(webviewPath), + ); + } if (isNSIS) { tauriConf.bundle.targets = ["nsis"]; diff --git a/scripts/prepare-release.ts b/scripts/prepare-release.ts index b888135354..dda129d16e 100644 --- a/scripts/prepare-release.ts +++ b/scripts/prepare-release.ts @@ -1,6 +1,11 @@ import path from "node:path"; import fs from "fs-extra"; -import { cwd, TAURI_APP_DIR } from "./utils/env"; +import { merge } from "lodash-es"; +import { + cwd, + TAURI_APP_DIR, + TAURI_FIXED_WEBVIEW2_CONFIG_OVERRIDE_PATH, +} from "./utils/env"; import { consola } from "./utils/logger"; const TAURI_APP_CONF = path.join(TAURI_APP_DIR, "tauri.conf.json"); @@ -14,19 +19,37 @@ const PACKAGE_JSON_PATH = path.join(cwd, "package.json"); // const WXS_PATH = path.join(TAURI_APP_DIR, "templates", "nightly.wxs"); const isNSIS = process.argv.includes("--nsis"); // only build nsis +const fixedWebview = process.argv.includes("--fixed-webview"); async function main() { consola.debug("Read config..."); const tauriAppConf = await fs.readJSON(TAURI_APP_CONF); // const tauriAppOverrides = await fs.readJSON(TAURI_DEV_APP_OVERRIDES_PATH); // const tauriConf = merge(tauriAppConf, tauriAppOverrides); - const tauriConf = tauriAppConf; + let tauriConf = tauriAppConf; // const wxsFile = await fs.readFile(WXS_PATH, "utf-8"); // if (isNSIS) { // tauriConf.tauri.bundle.targets = ["nsis", "updater"]; // } + if (fixedWebview) { + const fixedWebview2Config = await fs.readJSON( + TAURI_FIXED_WEBVIEW2_CONFIG_OVERRIDE_PATH, + ); + const webviewPath = (await fs.readdir(TAURI_APP_DIR)).find((file) => + file.includes("WebView2"), + ); + if (!webviewPath) { + throw new Error("WebView2 runtime not found"); + } + tauriConf = merge(tauriConf, fixedWebview2Config); + tauriConf.bundle.windows.webviewInstallMode.path = path.join( + "./", + path.basename(webviewPath), + ); + } + consola.debug("Write tauri version to tauri.conf.json"); await fs.writeJSON(TAURI_APP_CONF, tauriConf, { spaces: 2 }); consola.debug("tauri.conf.json updated"); diff --git a/scripts/utils/env.ts b/scripts/utils/env.ts index 25d779149b..a16733252a 100644 --- a/scripts/utils/env.ts +++ b/scripts/utils/env.ts @@ -2,6 +2,10 @@ import path from "path"; export const cwd = process.cwd(); export const TAURI_APP_DIR = path.join(cwd, "backend/tauri"); +export const TAURI_FIXED_WEBVIEW2_CONFIG_OVERRIDE_PATH = path.join( + TAURI_APP_DIR, + "overrides/fixed-webview2.conf.json", +); export const MANIFEST_DIR = path.join(cwd, "manifest"); export const GITHUB_PROXY = "https://mirror.ghproxy.com/"; export const GITHUB_TOKEN = process.env.GITHUB_TOKEN;