From d7bba4cb4b8c6c338a3a8a6144ad00d2456478e6 Mon Sep 17 00:00:00 2001 From: Juwen Lie <73946469+juw12@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:43:34 +0700 Subject: [PATCH 1/3] Fade for Mac: the web app in its own window, built and opened on each system before it is published --- .github/workflows/fade-mac.yml | 172 ++++++++++ apps/fade/README.md | 6 + apps/fade/shell/desktop/.gitignore | 3 + apps/fade/shell/desktop/README.md | 25 ++ apps/fade/shell/desktop/build.py | 174 ++++++++++ apps/fade/shell/desktop/fade.iss | 73 ++++ apps/fade/shell/desktop/fade.spec | 64 ++++ apps/fade/shell/desktop/fade_app.py | 467 ++++++++++++++++++++++++++ apps/fade/shell/desktop/icon-512.png | Bin 0 -> 4247 bytes apps/fade/shell/desktop/offline.html | 25 ++ apps/fade/shell/desktop/shell.json | 3 + apps/fade/shell/desktop/smoke_test.py | 92 +++++ 12 files changed, 1104 insertions(+) create mode 100644 .github/workflows/fade-mac.yml create mode 100644 apps/fade/shell/desktop/.gitignore create mode 100644 apps/fade/shell/desktop/README.md create mode 100644 apps/fade/shell/desktop/build.py create mode 100644 apps/fade/shell/desktop/fade.iss create mode 100644 apps/fade/shell/desktop/fade.spec create mode 100644 apps/fade/shell/desktop/fade_app.py create mode 100644 apps/fade/shell/desktop/icon-512.png create mode 100644 apps/fade/shell/desktop/offline.html create mode 100644 apps/fade/shell/desktop/shell.json create mode 100644 apps/fade/shell/desktop/smoke_test.py diff --git a/.github/workflows/fade-mac.yml b/.github/workflows/fade-mac.yml new file mode 100644 index 00000000..c3a37eb7 --- /dev/null +++ b/.github/workflows/fade-mac.yml @@ -0,0 +1,172 @@ +name: Fade apps + +# Builds the downloadable Fade apps and publishes them to the "fade-mac" release from main: +# macOS Fade-macOS-AppleSilicon.dmg + Fade-macOS-Intel.dmg +# The apps are thin native windows around the live site, so every push to the site reaches them through GitHub +# Pages without a rebuild. This workflow only runs when the shell in desktop/ changes. Every build is opened and +# checked on its own system first; only builds that passed are published, and the download links never change. +on: + push: + branches: [main] + paths: + - 'apps/fade/shell/desktop/**' + - '.github/workflows/fade-mac.yml' + pull_request: + paths: + - 'apps/fade/shell/desktop/**' + - '.github/workflows/fade-mac.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: fade-mac-${{ github.ref }} + cancel-in-progress: true + +jobs: + desktop: + name: Desktop (${{ matrix.name }}) + strategy: + fail-fast: false + matrix: + include: + - name: macOS Apple silicon + os: macos-15 + - name: macOS Intel + os: macos-15-intel + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install PyInstaller, Pillow and pywebview + run: python -m pip install --disable-pip-version-check pyinstaller pillow pywebview + - name: Install Inno Setup + if: runner.os == 'Windows' + shell: pwsh + run: | + $iscc = Join-Path ${env:ProgramFiles(x86)} 'Inno Setup 6\ISCC.exe' + if (-not (Test-Path $iscc) -and -not (Get-Command iscc -ErrorAction SilentlyContinue)) { + choco install innosetup --no-progress -y + } + - name: Make sure the WebView2 runtime is there (the Windows window uses it) + if: runner.os == 'Windows' + shell: pwsh + run: | + $client = '{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' + $keys = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$client", + "HKCU:\Software\Microsoft\EdgeUpdate\Clients\$client" + $version = $keys | Where-Object { Test-Path $_ } | ForEach-Object { (Get-ItemProperty $_).pv } | + Where-Object { $_ -and $_ -ne '0.0.0.0' } | Select-Object -First 1 + if ($version) { Write-Host "WebView2 runtime $version"; exit 0 } + $setup = Join-Path $env:RUNNER_TEMP 'MicrosoftEdgeWebview2Setup.exe' + Invoke-WebRequest 'https://go.microsoft.com/fwlink/p/?LinkId=2124703' -OutFile $setup + Start-Process -FilePath $setup -ArgumentList '/silent', '/install' -Wait + - name: Stop XProtect scans (they make hdiutil fail with "Resource busy") + if: runner.os == 'macOS' + run: | + sudo pkill -9 XProtect > /dev/null 2>&1 || true + for attempt in $(seq 1 20); do pgrep -x XProtect > /dev/null || break; sleep 3; done + - name: Build and package + run: python apps/fade/shell/desktop/build.py --require-installer + - name: Open the built app and check its window shows this commit's Fade + run: python apps/fade/shell/desktop/smoke_test.py --built --local-site + - name: Install with Fade-Windows-Setup.exe, run it, uninstall it + if: runner.os == 'Windows' + shell: pwsh + run: | + $setup = Resolve-Path 'apps/fade/shell/desktop/dist/Fade-Windows-Setup.exe' + $log = Join-Path $env:RUNNER_TEMP 'fade-setup.log' + $process = Start-Process -FilePath $setup -Wait -PassThru -ArgumentList @( + '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/TASKS=desktopicon', "/LOG=$log") + if ($process.ExitCode -ne 0) { Get-Content $log -Tail 40; throw "Setup failed with exit code $($process.ExitCode)" } + $app = Join-Path $env:LOCALAPPDATA 'Programs\Fade\Fade.exe' + if (-not (Test-Path $app)) { throw "Fade.exe was not installed to $app" } + $shortcuts = @( + (Join-Path ([Environment]::GetFolderPath('Programs')) 'Fade.lnk'), + (Join-Path ([Environment]::GetFolderPath('Desktop')) 'Fade.lnk')) + foreach ($link in $shortcuts) { if (-not (Test-Path $link)) { throw "Missing shortcut: $link" } } + $entry = Get-ChildItem 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall' | + Get-ItemProperty | Where-Object DisplayName -eq 'Fade' + if (-not $entry) { throw 'Fade is missing from Settings > Apps' } + python apps/fade/shell/desktop/smoke_test.py --local-site $app + if ($LASTEXITCODE -ne 0) { throw 'The installed app failed its checks' } + Start-Process -FilePath $entry.UninstallString.Trim('"') -Wait -ArgumentList '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART' + for ($i = 0; $i -lt 60 -and (Test-Path $app); $i++) { Start-Sleep -Seconds 1 } + if (Test-Path $app) { throw 'Uninstalling left Fade.exe behind' } + foreach ($link in $shortcuts) { if (Test-Path $link) { throw "Uninstalling left a shortcut: $link" } } + Write-Host 'Installed, ran and uninstalled cleanly' + - name: Check the Mac app and disk image + if: runner.os == 'macOS' + run: | + APP=apps/fade/shell/desktop/dist/Fade.app + test "$(plutil -extract CFBundleIdentifier raw "$APP/Contents/Info.plist")" = "io.github.juw12.fade.desktop" + test "$(plutil -extract CFBundleShortVersionString raw "$APP/Contents/Info.plist")" = "$(python3 -c 'import json; print(json.load(open("apps/fade/shell/desktop/shell.json"))["version"])')" + echo "Architecture: $(lipo -archs "$APP/Contents/MacOS/Fade")" + codesign --verify --deep --verbose=2 "$APP" + for image in apps/fade/shell/desktop/dist/*.dmg; do hdiutil verify "$image"; done + - uses: actions/upload-artifact@v4 + with: + name: Fade-${{ runner.os }}-${{ runner.arch }} + path: apps/fade/shell/desktop/dist/Fade-* + if-no-files-found: error + retention-days: 14 + + publish: + needs: [desktop] + if: ${{ !cancelled() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/download-artifact@v4 + with: + pattern: Fade-* + path: dist + merge-multiple: true + - name: Publish the apps to the apps release + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + run: | + cd dist + FILES="" + for name in Fade-macOS-AppleSilicon.dmg Fade-macOS-Intel.dmg; do + if [ -f "$name" ]; then + FILES="$FILES $name" + else + echo "::warning::$name was not built in this run; the release keeps its previous copy (if any)." + fi + done + if [ -z "$FILES" ]; then + echo "::error::No app passed its checks." + exit 1 + fi + sha256sum $FILES > SHA256SUMS.txt + cat SHA256SUMS.txt + cat > notes.md <<'EOF' + **Fade**: book barbers anywhere, watch haircut shorts and earn creator. The apps open the same Fade as https://juw12.github.io/projects/fade/ in their own window. + + | Your device | Download | How to install | + | --- | --- | --- | + | **Mac with Apple chip (M1 and newer)** | [Fade-macOS-AppleSilicon.dmg](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-AppleSilicon.dmg) | Open it and drag Fade to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**. | + | **Mac with Intel chip** | [Fade-macOS-Intel.dmg](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-Intel.dmg) | Same as above. Not sure which Mac? Apple menu → About This Mac shows **Chip** (Apple) or **Processor** (Intel). | + | **iPhone / iPad** | [Open in Safari](https://juw12.github.io/projects/fade/) | Share → **Add to Home Screen** (free, full screen, updates itself). | + | **Android** | [Fade-android.apk](https://github.com/juw12/projects/releases/download/fade-android/Fade-android.apk) | Open the file on the phone and allow your browser to install apps once. | + + The apps load Fade from the web, so new versions arrive the moment they are published, with no reinstall. Checksums for this build: `SHA256SUMS.txt`. + EOF + { + echo + echo "Built from ${GITHUB_SHA::7}." + } >> notes.md + if gh release view fade-mac --repo "$REPO" > /dev/null 2>&1; then + gh release upload fade-mac $FILES SHA256SUMS.txt --clobber --repo "$REPO" + gh release edit fade-mac --notes-file notes.md --repo "$REPO" + else + gh release create fade-mac $FILES SHA256SUMS.txt --title "Fade for Mac" \ + --notes-file notes.md --latest=false --repo "$REPO" + fi diff --git a/apps/fade/README.md b/apps/fade/README.md index c2998cde..780737f2 100644 --- a/apps/fade/README.md +++ b/apps/fade/README.md @@ -17,6 +17,12 @@ shorts, DMs and payment records live on public decentralised infrastructure (Nostr relays, Blossom media servers, ntfy push). Money moves directly between people with the payment apps they already use. Fade takes 0% and never holds funds. +## On a Mac + +- **Mac:** [`Fade-macOS-AppleSilicon.dmg`](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-AppleSilicon.dmg) (M1 and newer) or [`Fade-macOS-Intel.dmg`](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-Intel.dmg). Open it and drag Fade to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**. + +It is the web edition (https://juw12.github.io/projects/fade/) in its own window ([`desktop/`](shell/desktop/README.md)) and changes with it. Each build is opened on an Apple-silicon Mac and an Intel Mac on GitHub before it is published: the page loads, the app starts and can store data. No Mac of ours has run it yet. + ## On an Android phone - **Android:** [`Fade-android.apk`](https://github.com/juw12/projects/releases/download/fade-android/Fade-android.apk): open the file on the phone and allow your browser to install apps once. diff --git a/apps/fade/shell/desktop/.gitignore b/apps/fade/shell/desktop/.gitignore new file mode 100644 index 00000000..398ca165 --- /dev/null +++ b/apps/fade/shell/desktop/.gitignore @@ -0,0 +1,3 @@ +build/ +dist/ +__pycache__/ diff --git a/apps/fade/shell/desktop/README.md b/apps/fade/shell/desktop/README.md new file mode 100644 index 00000000..fef9e524 --- /dev/null +++ b/apps/fade/shell/desktop/README.md @@ -0,0 +1,25 @@ +# Fade for Windows and Mac + +A small native window around the live site (https://juw12.github.io/projects/fade/): Microsoft Edge WebView2 on Windows, the system +WebKit on macOS, both through [pywebview](https://pywebview.flowrl.com/). The window keeps its own browser data +(what you saved in it), so it is all still there the next time Fade opens. Because it loads the live +site, every push to `main` reaches the installed app on its next start; only a change to this folder needs a new build. + +| File | What it is | +| --- | --- | +| `fade_app.py` | the program: one window, remembers its size, a second start brings the open window forward | +| `shell.json` | the shell's version (bump it when this folder changes) | +| `build.py` | builds the app for the system it runs on | +| `fade.spec`, `fade.iss` | PyInstaller recipe, Windows installer (Inno Setup 6, per user, no admin rights) | +| `smoke_test.py` | opens the built app and checks its window shows Fade | +| `offline.html` | shown when the site cannot be reached on the very first start | + +``` +python -m pip install pyinstaller pillow pywebview +python desktop/build.py +python desktop/smoke_test.py --built --local-site +``` + +`.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls +the Windows setup, and publishes the files that passed to the +[apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. diff --git a/apps/fade/shell/desktop/build.py b/apps/fade/shell/desktop/build.py new file mode 100644 index 00000000..0ae1e653 --- /dev/null +++ b/apps/fade/shell/desktop/build.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Build the Fade desktop app for the system this runs on (Windows or macOS). + + python -m pip install pyinstaller pillow pywebview + python desktop/build.py [--require-installer] + +Output in desktop/dist/ (the names the "apps" GitHub release uses): + Windows Fade-Windows-Setup.exe per-user installer, no admin rights (needs Inno Setup 6) + Fade-Windows-Portable.zip unzip and run Fade.exe + macOS Fade-macOS-AppleSilicon.dmg or Fade-macOS-Intel.dmg (the chip of this Mac) +""" + +import argparse +import json +import os +import platform +import shutil +import subprocess +import sys +import time +import zipfile + +DESKTOP = os.path.dirname(os.path.abspath(__file__)) +APP = os.path.dirname(DESKTOP) +BUILD = os.path.join(DESKTOP, "build") +DIST = os.path.join(DESKTOP, "dist") +ICON_SOURCE = os.path.join(APP, *"desktop/icon-512.png".split("/")) + +WINDOWS_VERSION_INFO = """VSVersionInfo( + ffi=FixedFileInfo(filevers={numbers}, prodvers={numbers}, mask=0x3f, flags=0x0, OS=0x40004, + fileType=0x1, subtype=0x0, date=(0, 0)), + kids=[ + StringFileInfo([StringTable('040904B0', [ + StringStruct('CompanyName', 'Juwen Lie'), + StringStruct('FileDescription', 'Fade - Book barbers anywhere, watch haircut shorts and earn creator'), + StringStruct('FileVersion', '{version}'), + StringStruct('InternalName', 'Fade'), + StringStruct('LegalCopyright', 'Open source - github.com/juw12/projects'), + StringStruct('OriginalFilename', 'Fade.exe'), + StringStruct('ProductName', 'Fade'), + StringStruct('ProductVersion', '{version}')])]), + VarFileInfo([VarStruct('Translation', [1033, 1200])]) + ] +) +""" + + +def read_version(): + """The desktop shell's version: desktop/shell.json (the built app carries the same file).""" + with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle: + return json.load(handle)["version"] + + +def make_icon(): + """The app icon from the app's 512 px PNG: .ico on Windows, the PNG itself on macOS.""" + if sys.platform != "win32": + return ICON_SOURCE + from PIL import Image + + target = os.path.join(BUILD, "fade.ico") + with Image.open(ICON_SOURCE) as image: + image.convert("RGBA").save(target, sizes=[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]) + return target + + +def run_pyinstaller(version, icon): + env = dict(os.environ, FADE_VERSION=version, FADE_ICON=icon) + if sys.platform == "win32": + numbers = tuple(int(part) for part in (version.split(".") + ["0", "0", "0"])[:4]) + version_file = os.path.join(BUILD, "version_info.txt") + with open(version_file, "w", encoding="utf-8") as handle: + handle.write(WINDOWS_VERSION_INFO.format(numbers=numbers, version=version)) + env["FADE_VERSION_FILE"] = version_file + subprocess.run( + [ + sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", + "--distpath", DIST, + "--workpath", os.path.join(BUILD, "pyinstaller"), + os.path.join(DESKTOP, "fade.spec"), + ], + check=True, + env=env, + ) + + +def zip_folder(folder, archive, root_name): + with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as bundle: + for current, _dirs, files in os.walk(folder): + for name in files: + full = os.path.join(current, name) + bundle.write(full, os.path.join(root_name, os.path.relpath(full, folder))) + + +def find_iscc(): + candidates = [ + shutil.which("iscc"), + os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"), "Inno Setup 6", "ISCC.exe"), + os.path.join(os.environ.get("ProgramFiles", r"C:\Program Files"), "Inno Setup 6", "ISCC.exe"), + os.path.join(os.environ.get("LOCALAPPDATA", ""), "Programs", "Inno Setup 6", "ISCC.exe"), + ] + return next((path for path in candidates if path and os.path.isfile(path)), None) + + +def package_windows(version, icon, require_installer): + folder = os.path.join(DIST, "Fade") + portable = os.path.join(DIST, "Fade-Windows-Portable.zip") + zip_folder(folder, portable, "Fade") + made = [portable] + iscc = find_iscc() + if not iscc: + if require_installer: + raise SystemExit("Inno Setup 6 (ISCC.exe) was not found - install it to build Fade-Windows-Setup.exe") + print("Inno Setup 6 not found: skipped Fade-Windows-Setup.exe (the portable zip was built)") + return made + subprocess.run( + [iscc, "/Qp", "/DAppVersion=" + version, "/DSourceDir=" + folder, "/DOutputDir=" + DIST, + "/DIconFile=" + icon, os.path.join(DESKTOP, "fade.iss")], + check=True, + ) + return [os.path.join(DIST, "Fade-Windows-Setup.exe")] + made + + +def package_macos(): + app = os.path.join(DIST, "Fade.app") + chip = "AppleSilicon" if platform.machine() == "arm64" else "Intel" + image = os.path.join(DIST, "Fade-macOS-%s.dmg" % chip) + stage = os.path.join(BUILD, "dmg") + shutil.rmtree(stage, ignore_errors=True) + os.makedirs(stage) + subprocess.run(["ditto", app, os.path.join(stage, "Fade.app")], check=True) # keeps links and signature + os.symlink("/Applications", os.path.join(stage, "Applications")) + command = ["hdiutil", "create", "-volname", "Fade", "-srcfolder", stage, "-ov", "-format", "UDZO", image] + # hdiutil sometimes fails with "Resource busy" while macOS scans the new volume (common on CI runners). + for attempt in range(1, 6): + if os.path.exists(image): + os.remove(image) + result = subprocess.run(command, capture_output=True, text=True) + if result.returncode == 0: + return [image] + message = (result.stderr or result.stdout).strip() or "exit code %d" % result.returncode + if attempt == 5: + raise SystemExit("hdiutil create failed: %s" % message) + print("hdiutil create failed (%s); trying again in %d s" % (message, 10 * attempt)) + time.sleep(10 * attempt) + return [image] + + +def main(): + parser = argparse.ArgumentParser(description="Build the Fade desktop app for this system") + parser.add_argument("--require-installer", action="store_true", + help="Windows: fail if Inno Setup is missing instead of building only the portable zip") + args = parser.parse_args() + if sys.platform not in ("win32", "darwin"): + raise SystemExit("Fade desktop builds are made on Windows and macOS") + try: + import webview # noqa: F401 (bundled by fade.spec) + except ImportError: + raise SystemExit("The Fade window needs pywebview: python -m pip install pywebview") + + version = read_version() + os.makedirs(BUILD, exist_ok=True) + for leftover in ("Fade", "Fade.app"): + shutil.rmtree(os.path.join(DIST, leftover), ignore_errors=True) + print("Building Fade %s for %s %s" % (version, platform.system(), platform.machine())) + icon = make_icon() + run_pyinstaller(version, icon) + made = package_windows(version, icon, args.require_installer) if sys.platform == "win32" else package_macos() + print("\nBuilt:") + for path in made: + print(" %s (%.1f MB)" % (path, os.path.getsize(path) / 1e6)) + + +if __name__ == "__main__": + main() diff --git a/apps/fade/shell/desktop/fade.iss b/apps/fade/shell/desktop/fade.iss new file mode 100644 index 00000000..38a26078 --- /dev/null +++ b/apps/fade/shell/desktop/fade.iss @@ -0,0 +1,73 @@ +; Fade for Windows - per-user installer (no admin rights). build.py compiles it with: +; iscc /DAppVersion=1.0.0 /DSourceDir= /DOutputDir= /DIconFile= fade.iss +; Installs to %LOCALAPPDATA%\Programs\Fade, adds Fade to the Start menu (and optionally the desktop) and lists it +; in Settings > Apps. The window keeps its data (what you saved in it) in %LOCALAPPDATA%\Fade, which +; uninstalling leaves in place so a reinstall finds it again. + +#ifndef AppVersion + #define AppVersion "1.0.0" +#endif +#ifndef SourceDir + #define SourceDir "dist\Fade" +#endif +#ifndef OutputDir + #define OutputDir "dist" +#endif +#ifndef IconFile + #define IconFile "build\fade.ico" +#endif + +[Setup] +AppId={{E81247BA-698B-569E-A6DE-E1E1C366EEFB} +AppName=Fade +AppVersion={#AppVersion} +AppVerName=Fade {#AppVersion} +AppPublisher=Juwen Lie +AppPublisherURL=https://juw12.github.io/projects/fade/ +AppSupportURL=https://github.com/juw12/projects +DefaultDirName={autopf}\Fade +DisableDirPage=yes +DisableProgramGroupPage=yes +PrivilegesRequired=lowest +OutputDir={#OutputDir} +OutputBaseFilename=Fade-Windows-Setup +SetupIconFile={#IconFile} +UninstallDisplayIcon={app}\Fade.exe +UninstallDisplayName=Fade +Compression=lzma2 +SolidCompression=yes +WizardStyle=modern +ArchitecturesAllowed=x64compatible +ArchitecturesInstallIn64BitMode=x64compatible +CloseApplications=no +SetupLogging=yes + +[Languages] +Name: "english"; MessagesFile: "compiler:Default.isl" + +[Tasks] +Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}" + +[Files] +Source: "{#SourceDir}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs + +[Icons] +Name: "{userprograms}\Fade"; Filename: "{app}\Fade.exe"; Comment: "Book barbers anywhere, watch haircut shorts and earn creator" +Name: "{userdesktop}\Fade"; Filename: "{app}\Fade.exe"; Comment: "Book barbers anywhere, watch haircut shorts and earn creator"; Tasks: desktopicon + +[Run] +Filename: "{app}\Fade.exe"; Description: "{cm:LaunchProgram,Fade}"; Flags: nowait postinstall skipifsilent + +[UninstallRun] +Filename: "{app}\Fade.exe"; Parameters: "--stop"; Flags: runhidden waituntilterminated; RunOnceId: "StopFade" + +[Code] +function PrepareToInstall(var NeedsRestart: Boolean): String; +var + ResultCode: Integer; +begin + Result := ''; + { Updating: close an open Fade window first so its files can be replaced. } + if FileExists(ExpandConstant('{app}\Fade.exe')) then + Exec(ExpandConstant('{app}\Fade.exe'), '--stop', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); +end; diff --git a/apps/fade/shell/desktop/fade.spec b/apps/fade/shell/desktop/fade.spec new file mode 100644 index 00000000..4adde0a7 --- /dev/null +++ b/apps/fade/shell/desktop/fade.spec @@ -0,0 +1,64 @@ +# -*- mode: python ; coding: utf-8 -*- +# PyInstaller recipe for Fade on Windows and macOS. Build with python desktop/build.py +# Windows dist/Fade/Fade.exe (+ _internal/) - no console; its window uses Edge WebView2 (pywebview) +# macOS dist/Fade.app - a normal Dock app; its window uses WKWebView (pywebview) +import os +import sys + +DESKTOP = os.path.dirname(os.path.abspath(SPEC)) # noqa: F821 (provided by PyInstaller) +VERSION = os.environ.get("FADE_VERSION", "1.0.0") +VERSION_FILE = os.environ.get("FADE_VERSION_FILE") or None +ICON = os.environ.get("FADE_ICON") or None +IS_WIN = sys.platform == "win32" + +EXCLUDES = ["tkinter", "unittest", "test", "idlelib", "lib2to3"] +# pywebview: bundle only the backend this system uses (the others would drag in Qt, GTK or CEF). +OTHER_BACKENDS = ["webview.platforms.qt", "webview.platforms.gtk", "webview.platforms.cef", + "webview.platforms.android", "PyQt5", "PyQt6", "PySide2", "PySide6", "qtpy", "gi"] +if IS_WIN: + HIDDEN = ["webview", "webview.platforms.winforms", "webview.platforms.edgechromium", "clr"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.cocoa"] +else: + HIDDEN = ["webview", "webview.platforms.cocoa"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.winforms", "webview.platforms.edgechromium", + "webview.platforms.mshtml", "webview.platforms.win32"] + +a = Analysis( # noqa: F821 + [os.path.join(DESKTOP, "fade_app.py")], + pathex=[DESKTOP], + datas=[(os.path.join(DESKTOP, "offline.html"), "."), (os.path.join(DESKTOP, "shell.json"), ".")], + hiddenimports=HIDDEN, + excludes=EXCLUDES, +) +pyz = PYZ(a.pure) # noqa: F821 +exe = EXE( # noqa: F821 + pyz, + a.scripts, + [], + exclude_binaries=True, + name="Fade", + console=False, + icon=ICON, + version=VERSION_FILE if IS_WIN else None, + upx=False, +) +coll = COLLECT(exe, a.binaries, a.datas, name="Fade", upx=False) # noqa: F821 + +if sys.platform == "darwin": + app = BUNDLE( # noqa: F821 + coll, + name="Fade.app", + icon=ICON, + bundle_identifier="io.github.juw12.fade.desktop", + version=VERSION, + info_plist={ + "CFBundleName": "Fade", + "CFBundleDisplayName": "Fade", + "CFBundleShortVersionString": VERSION, + "CFBundleVersion": VERSION, + "NSHighResolutionCapable": True, + "NSRequiresAquaSystemAppearance": False, + "LSApplicationCategoryType": "public.app-category.lifestyle", + "NSHumanReadableCopyright": "Open source - github.com/juw12/projects", + }, + ) diff --git a/apps/fade/shell/desktop/fade_app.py b/apps/fade/shell/desktop/fade_app.py new file mode 100644 index 00000000..4268ec79 --- /dev/null +++ b/apps/fade/shell/desktop/fade_app.py @@ -0,0 +1,467 @@ +"""Fade for Windows and macOS: the Fade web app in its own window. + + Windows Microsoft Edge WebView2 (part of Windows 10 and 11), through pywebview + macOS the system WebKit (WKWebView), through pywebview + +The window opens https://juw12.github.io/projects/fade/ and keeps its browser data (what you saved in it) in a per-user +folder, so everything is still there the next time Fade opens. Because the window loads the live site, every +push to main reaches the desktop app on its next launch; the app shell itself only changes when +desktop/shell.json gets a new version. Links to other sites open in the default browser. Without WebView2 +Fade opens in the default browser instead. + +Opening Fade while its window is open brings that window to the front; `Fade --stop` closes it. + +Automated checks: FADE_WINDOW_TEST= makes the window write a report once the page has +loaded and then close itself; FADE_DESKTOP_URL overrides the address. +""" + +import hmac +import json +import os +import secrets +import socket +import subprocess +import sys +import threading +import time +import urllib.request +import webbrowser + +TITLE = "Fade" +APP_URL = os.environ.get("FADE_DESKTOP_URL") or "https://juw12.github.io/projects/fade/" +DEFAULT_SIZE = (1280, 860) +MIN_SIZE = (420, 560) +LIGHT_BACKGROUND = "#0b0b0f" +DARK_BACKGROUND = "#0b0b0f" +CONTROL_FILE = "window.json" +SIZE_FILE = "window-size.json" +TEST_TIMEOUT = 60 +PAGE_SCRIPT = ( + "JSON.stringify({title: document.title, url: location.href, userAgent: navigator.userAgent," + " app: !!(document.getElementById('view') && document.getElementById('view').children.length)," + " storage: (function () { try { localStorage.setItem('fade.desktop.check', '1');" + " return localStorage.getItem('fade.desktop.check') === '1'; } catch (e) { return false; } })()," + " ready: document.readyState, width: window.innerWidth, height: window.innerHeight})" +) + + +def log(message): + if sys.stdout is not None: + print("[fade] " + message, flush=True) + + +def here(): + """Folder with this program's own files (the PyInstaller bundle, or this checkout).""" + return getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) + + +def read_version(): + """The shell's version: desktop/shell.json, bundled next to the program.""" + try: + with open(os.path.join(here(), "shell.json"), encoding="utf-8") as handle: + return str(json.load(handle)["version"]) + except (OSError, ValueError, KeyError): + return "0.0.0" + + +VERSION = read_version() + + +def data_dir(): + """Per-user folder for the window's browser data and its own small settings.""" + home = os.path.expanduser("~") + if sys.platform == "win32": + return os.path.join(os.environ.get("LOCALAPPDATA") or os.path.join(home, "AppData", "Local"), "Fade") + if sys.platform == "darwin": + return os.path.join(home, "Library", "Application Support", "Fade") + return os.path.join(os.environ.get("XDG_DATA_HOME") or os.path.join(home, ".local", "share"), "fade") + + +def system_prefers_dark(): + try: + if sys.platform == "win32": + import winreg + + with winreg.OpenKey(winreg.HKEY_CURRENT_USER, + r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize") as key: + return winreg.QueryValueEx(key, "AppsUseLightTheme")[0] == 0 + if sys.platform == "darwin": + result = subprocess.run(["defaults", "read", "-g", "AppleInterfaceStyle"], + capture_output=True, text=True, timeout=5) + return "dark" in result.stdout.lower() + except (OSError, ValueError, subprocess.SubprocessError): + pass + return False + + +def saved_size(folder): + try: + with open(os.path.join(folder, SIZE_FILE), encoding="utf-8") as handle: + data = json.load(handle) + width, height = int(data["width"]), int(data["height"]) + except (OSError, ValueError, KeyError, TypeError): + return DEFAULT_SIZE + if MIN_SIZE[0] <= width <= 8000 and MIN_SIZE[1] <= height <= 8000: + return width, height + return DEFAULT_SIZE + + +def save_size(folder, width, height): + try: + width, height = int(width), int(height) + if width < MIN_SIZE[0] or height < MIN_SIZE[1]: + return + os.makedirs(folder, exist_ok=True) + with open(os.path.join(folder, SIZE_FILE), "w", encoding="utf-8") as handle: + json.dump({"width": width, "height": height}, handle) + except (OSError, ValueError, TypeError): + pass + + +def screen_work_area(): + """(width, height) of the primary screen's work area in window units, or None.""" + try: + if sys.platform == "win32": + import ctypes + from ctypes import wintypes + + rect = wintypes.RECT() + if ctypes.windll.user32.SystemParametersInfoW(0x0030, 0, ctypes.byref(rect), 0): # SPI_GETWORKAREA + return rect.right - rect.left, rect.bottom - rect.top + if sys.platform == "darwin": + from AppKit import NSScreen # comes with pywebview + + frame = NSScreen.mainScreen().visibleFrame() + return int(frame.size.width), int(frame.size.height) + except Exception: # sizing is cosmetic + return None + return None + + +def fit_size(size, work_area): + width, height = size + if work_area: + width = min(width, work_area[0] - 40) + height = min(height, work_area[1] - 40) + return max(width, MIN_SIZE[0]), max(height, MIN_SIZE[1]) + + +def online(url, timeout=6): + """True when the Fade site answers (the window then loads it; otherwise the offline page).""" + try: + request = urllib.request.Request(url, method="HEAD", headers={"User-Agent": "FadeDesktop/" + VERSION}) + with urllib.request.urlopen(request, timeout=timeout) as response: + return response.status < 500 + except Exception: + return False + + +def offline_page(): + path = os.path.join(here(), "offline.html") + return "file:///" + path.replace("\\", "/").lstrip("/") if os.path.isfile(path) else None + + +# ------------------------------------------------------- one window at a time +class WindowControl: + """Loopback endpoint of the open window: 'show' brings it to the front, 'quit' closes it.""" + + def __init__(self, folder, on_show, on_quit): + self.path = os.path.join(folder, CONTROL_FILE) + self.token = secrets.token_hex(16) + self.on_show = on_show + self.on_quit = on_quit + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.server.bind(("127.0.0.1", 0)) + self.server.listen(4) + os.makedirs(folder, exist_ok=True) + with open(self.path, "w", encoding="utf-8") as handle: + json.dump({"pid": os.getpid(), "port": self.server.getsockname()[1], "token": self.token}, handle) + threading.Thread(target=self._serve, name="fade-window-control", daemon=True).start() + + def _serve(self): + while True: + try: + connection, _address = self.server.accept() + except OSError: + return + with connection: + try: + connection.settimeout(2) + line = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + continue + token, _space, command = line.partition(" ") + action = {"show": self.on_show, "quit": self.on_quit}.get(command) + if not action or not hmac.compare_digest(token, self.token): + continue + try: + connection.sendall(b"ok\n") + except OSError: + pass + try: + action() + except Exception as error: + log("control %s failed: %s" % (command, error)) + + def close(self): + try: + self.server.close() + except OSError: + pass + try: + with open(self.path, encoding="utf-8") as handle: + mine = json.load(handle).get("token") == self.token + if mine: + os.remove(self.path) + except (OSError, ValueError): + pass + + +def send_to_window(folder, command, timeout=2.0): + """Ask an open Fade window to 'show' itself or 'quit'. True if one answered.""" + try: + with open(os.path.join(folder, CONTROL_FILE), encoding="utf-8") as handle: + info = json.load(handle) + port, token, pid = int(info["port"]), str(info["token"]), int(info.get("pid") or 0) + except (OSError, ValueError, KeyError, TypeError): + return False + if command == "show" and sys.platform == "win32" and pid: + try: + import ctypes + + ctypes.windll.user32.AllowSetForegroundWindow(pid) + except (OSError, AttributeError): + pass + try: + with socket.create_connection(("127.0.0.1", port), timeout=timeout) as connection: + connection.settimeout(timeout) + connection.sendall(("%s %s\n" % (token, command)).encode("utf-8")) + reply = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + return False + if reply == "ok" and command == "quit" and pid: + deadline = time.time() + 15 + while time.time() < deadline and process_alive(pid): + time.sleep(0.2) + return reply == "ok" + + +def process_alive(pid): + if sys.platform == "win32": + import ctypes + + kernel32 = ctypes.windll.kernel32 + handle = kernel32.OpenProcess(0x00100000, False, pid) # SYNCHRONIZE + if not handle: + return False + try: + return kernel32.WaitForSingleObject(handle, 0) != 0 + finally: + kernel32.CloseHandle(handle) + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True + + +# ------------------------------------------------------------ the window +def webview2_runtime(): + """Version of the installed Microsoft Edge WebView2 runtime, or None (Windows only).""" + if sys.platform != "win32": + return None + import platform + import winreg + + clients = [ + "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}", # WebView2 Runtime + "{2CD8A007-E189-409D-A2C8-9AF4EF3C72AA}", # Beta + "{0D50BFEC-CD6A-4F9A-964C-C7416E3ACB10}", # Dev + "{65C35B14-6C1D-4122-AC46-7148CC9D6497}", # Canary + ] + for client in clients: + for hive in (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE): + native = hive == winreg.HKEY_CURRENT_USER or platform.machine() == "x86" + path = r"SOFTWARE\%sMicrosoft\EdgeUpdate\Clients\%s" % ("" if native else "WOW6432Node\\", client) + try: + with winreg.OpenKey(hive, path) as key: + version = str(winreg.QueryValueEx(key, "pv")[0]) + except OSError: + continue + if version and version != "0.0.0.0": + return version + return None + + +def bring_to_front(window): + if sys.platform == "win32": + try: + import ctypes + + user32 = ctypes.windll.user32 + hwnd = int(window.native.Handle.ToInt64()) + if user32.IsIconic(hwnd): + user32.ShowWindow(hwnd, 9) # SW_RESTORE + user32.SetForegroundWindow(hwnd) + return + except Exception: + pass + else: + window.restore() + window.show() + + +def write_report(path, **fields): + fields.setdefault("pid", os.getpid()) + fields.setdefault("version", VERSION) + temporary = path + ".tmp" + with open(temporary, "w", encoding="utf-8") as handle: + json.dump(fields, handle, indent=2) + os.replace(temporary, path) + + +def run_window(url, folder, report_path): + """Show Fade in a native window until it is closed. Returns False when this system can't.""" + if sys.platform == "win32": + runtime = webview2_runtime() + if not runtime: + log("Microsoft Edge WebView2 runtime not found") + return False + gui = "edgechromium" + elif sys.platform == "darwin": + runtime = None + gui = "cocoa" + else: + return False + try: + import webview + except ImportError as error: + log("pywebview is not available: %s" % error) + return False + + # Let the page save files and send other sites to the default browser. + webview.settings["ALLOW_DOWNLOADS"] = True + webview.settings["OPEN_EXTERNAL_LINKS_IN_BROWSER"] = True + + start_url = url if (report_path or online(url)) else (offline_page() or url) + width, height = fit_size(saved_size(folder), screen_work_area()) + window = webview.create_window( + TITLE, + start_url, + width=width, + height=height, + min_size=MIN_SIZE, + background_color=DARK_BACKGROUND if system_prefers_dark() else LIGHT_BACKGROUND, + text_select=True, + zoomable=True, + focus=not report_path, + ) + control = WindowControl(folder, on_show=lambda: bring_to_front(window), on_quit=window.destroy) + state = {"tested": False} + + def remember_size(): + try: + save_size(folder, window.width, window.height) + except Exception: + pass + + def self_test(): + deadline = time.time() + TEST_TIMEOUT + page = {} + settled = 0 + while time.time() < deadline: + try: + page = json.loads(read_page(window, PAGE_SCRIPT) or "{}") + except Exception as error: + page = {"error": str(error)} + # Wait until a fully loaded Fade page stays put for a moment. + settled = settled + 1 if page.get("app") and page.get("ready") == "complete" else 0 + if settled >= 3: + break + time.sleep(0.5) + renderer = gui + if sys.platform == "win32": + renderer = getattr(sys.modules.get("webview.platforms.winforms"), "renderer", gui) + write_report(report_path, method="window", toolkit=renderer, runtime=runtime, **page) + window.destroy() + + def on_loaded(): + if report_path and not state["tested"]: + state["tested"] = True + threading.Thread(target=self_test, name="fade-window-test", daemon=True).start() + + window.events.closing += remember_size + window.events.loaded += on_loaded + storage = None + if sys.platform == "win32": + # Automated checks use a throwaway profile so they never touch a person's own data. + base = os.path.dirname(os.path.abspath(report_path)) if report_path else folder + storage = os.path.join(base, "WebView2") + try: + webview.start(gui=gui, private_mode=False, storage_path=storage) + except Exception as error: + log("the window failed: %s" % error) + return False + finally: + control.close() + return True + + +def read_page(window, script): + """The page's answer to `script`. + + A page that forbids eval (Content-Security-Policy: script-src 'self') refuses pywebview's own + evaluate_js on a Mac, which wraps the script in eval. There the question is put to WebKit itself, + which the page's policy does not bind. + """ + try: + return window.evaluate_js(script) + except Exception: + if sys.platform != "darwin": + raise + from PyObjCTools import AppHelper + from webview.platforms.cocoa import BrowserView + + answer, done = {}, threading.Event() + + def ask(): + def finished(result, error): + answer["value"] = result + answer["error"] = None if error is None else str(error) + done.set() + + BrowserView.instances[window.uid].webview.evaluateJavaScript_completionHandler_(script, finished) + + AppHelper.callAfter(ask) + if not done.wait(20): + raise RuntimeError("the page did not answer") + if answer["error"]: + raise RuntimeError(answer["error"]) + return answer["value"] + + +def main(argv=None): + args = list(sys.argv[1:] if argv is None else argv) + folder = data_dir() + if "--version" in args: + print("Fade %s" % VERSION) + return 0 + if "--stop" in args: + send_to_window(folder, "quit") + return 0 + report_path = os.environ.get("FADE_WINDOW_TEST") or None + if not report_path and send_to_window(folder, "show"): + return 0 # already open: brought to the front + if run_window(APP_URL, folder, report_path): + return 0 + if report_path: + write_report(report_path, method="none", error="no native window on this system") + return 1 + webbrowser.open(APP_URL) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/apps/fade/shell/desktop/icon-512.png b/apps/fade/shell/desktop/icon-512.png new file mode 100644 index 0000000000000000000000000000000000000000..d79b04769dae678381ff06e6d980b09272425037 GIT binary patch literal 4247 zcmcIod0bQ1wqEBXfe@xBL7)YbmWn6{2+nxJajGb2nF>K~OH7_5krz+P zH07_FFB?{x@#waAAtiEUb%?>0=|pLG#y~>QfODoUPyhpT2t2 zFgBSKD2aKW*#1_|f4nAVSW5&!%cV?8!LVDNm>a#YdNQ^RJh;}kFCw=*58|D`-eR-b zCir>o8mErQ7I1?P^#(TercLva;Ple3MsV?@0G{-<#zujAR%SM*; z;Te*9tp<_JM+33K;P7{_QjxD z=4@e6N(NQwYVD4{^X^xN8zE?581(ymZG?bcS@`E#^^s+%zWpIIP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fade/shell/desktop/offline.html b/apps/fade/shell/desktop/offline.html new file mode 100644 index 00000000..3f13d40a --- /dev/null +++ b/apps/fade/shell/desktop/offline.html @@ -0,0 +1,25 @@ + + + + + + Fade + + + +
+ +

Fade can't reach the internet

+

Fade needs a connection the first time it opens. After that it also opens offline. Check your connection and try again.

+ Try again +
+ + diff --git a/apps/fade/shell/desktop/shell.json b/apps/fade/shell/desktop/shell.json new file mode 100644 index 00000000..1587a669 --- /dev/null +++ b/apps/fade/shell/desktop/shell.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/apps/fade/shell/desktop/smoke_test.py b/apps/fade/shell/desktop/smoke_test.py new file mode 100644 index 00000000..39001c60 --- /dev/null +++ b/apps/fade/shell/desktop/smoke_test.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Open the built Fade desktop app and check its window really shows Fade. + + python desktop/smoke_test.py --built the app in desktop/dist/ (after build.py) + python desktop/smoke_test.py PATH/TO/Fade.exe an installed copy + add --local-site to serve this checkout instead of the live site + +The app runs with FADE_WINDOW_TEST set, so it writes a report once the page has loaded and closes +itself. Passes when the page is Fade, the app started (what the app draws into #view when it starts) and the page can store data. +""" + +import argparse +import functools +import http.server +import json +import os +import subprocess +import sys +import tempfile +import threading +import time + +DESKTOP = os.path.dirname(os.path.abspath(__file__)) +REPO = os.path.normpath(os.path.join(DESKTOP, os.pardir, os.pardir, os.pardir, os.pardir, "apps", "fade", "web")) # the folder GitHub Pages serves +LIVE_URL = "https://juw12.github.io/projects/fade/" + + +def built_app(): + if sys.platform == "win32": + return os.path.join(DESKTOP, "dist", "Fade", "Fade.exe") + return os.path.join(DESKTOP, "dist", "Fade.app", "Contents", "MacOS", "Fade") + + +def serve_site(): + """Serve this checkout (the site is the repository itself) on a free loopback port.""" + handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=REPO) + handler.log_message = lambda *args: None + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + return "http://127.0.0.1:%d/" % server.server_address[1] + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("app", nargs="?", help="the Fade program to open") + parser.add_argument("--built", action="store_true", help="open the app in desktop/dist/") + parser.add_argument("--local-site", action="store_true", help="serve this checkout instead of the live site") + args = parser.parse_args() + app = built_app() if args.built or not args.app else args.app + if not os.path.isfile(app): + raise SystemExit("Not found: %s" % app) + + url = serve_site() if args.local_site else LIVE_URL + folder = tempfile.mkdtemp(prefix="fade-smoke-") + report = os.path.join(folder, "report.json") + env = dict(os.environ, FADE_WINDOW_TEST=report, FADE_DESKTOP_URL=url) + print("Opening %s on %s" % (app, url), flush=True) + started = time.time() + process = subprocess.Popen([app], env=env) + try: + process.wait(timeout=180) + except subprocess.TimeoutExpired: + process.kill() + raise SystemExit("The app did not close itself within 180 s") + if not os.path.isfile(report): + raise SystemExit("The app exited (code %s) without a report" % process.returncode) + with open(report, encoding="utf-8") as handle: + result = json.load(handle) + print(json.dumps(result, indent=2)) + + with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle: + version = json.load(handle)["version"] + problems = [] + if result.get("method") != "window": + problems.append("no native window (%s)" % (result.get("error") or result.get("method"))) + if "Fade" not in str(result.get("title") or ""): + problems.append("page title is %r" % result.get("title")) + if not str(result.get("url") or "").startswith(url): + problems.append("the window is on %r" % result.get("url")) + if not result.get("app"): + problems.append("Fade did not start in the page (missing: what the app draws into #view when it starts)") + if not result.get("storage"): + problems.append("the page cannot store data") + if result.get("version") != version: + problems.append("the app says version %r, desktop/shell.json says %r" % (result.get("version"), version)) + if problems: + raise SystemExit("FAILED: " + "; ".join(problems)) + print("OK: Fade %s opened in its own window (%s) in %.0f s" % (version, result.get("toolkit"), time.time() - started)) + + +if __name__ == "__main__": + main() From 7e428031d84e4d50d16ab16c36a24b3bb4175843 Mon Sep 17 00:00:00 2001 From: Juwen Lie <73946469+juw12@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:43:46 +0700 Subject: [PATCH 2/3] Datum Bench for Mac: the web app in its own window, built and opened on each system before it is published --- .github/workflows/me-rd-toolkit-mac.yml | 172 +++++++ apps/me-rd-toolkit/README.md | 6 + apps/me-rd-toolkit/shell/desktop/.gitignore | 3 + apps/me-rd-toolkit/shell/desktop/README.md | 25 + apps/me-rd-toolkit/shell/desktop/build.py | 174 +++++++ apps/me-rd-toolkit/shell/desktop/icon-512.png | Bin 0 -> 8056 bytes .../shell/desktop/merdtoolkit.iss | 73 +++ .../shell/desktop/merdtoolkit.spec | 64 +++ .../shell/desktop/merdtoolkit_app.py | 467 ++++++++++++++++++ apps/me-rd-toolkit/shell/desktop/offline.html | 25 + apps/me-rd-toolkit/shell/desktop/shell.json | 3 + .../me-rd-toolkit/shell/desktop/smoke_test.py | 92 ++++ 12 files changed, 1104 insertions(+) create mode 100644 .github/workflows/me-rd-toolkit-mac.yml create mode 100644 apps/me-rd-toolkit/shell/desktop/.gitignore create mode 100644 apps/me-rd-toolkit/shell/desktop/README.md create mode 100644 apps/me-rd-toolkit/shell/desktop/build.py create mode 100644 apps/me-rd-toolkit/shell/desktop/icon-512.png create mode 100644 apps/me-rd-toolkit/shell/desktop/merdtoolkit.iss create mode 100644 apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec create mode 100644 apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py create mode 100644 apps/me-rd-toolkit/shell/desktop/offline.html create mode 100644 apps/me-rd-toolkit/shell/desktop/shell.json create mode 100644 apps/me-rd-toolkit/shell/desktop/smoke_test.py diff --git a/.github/workflows/me-rd-toolkit-mac.yml b/.github/workflows/me-rd-toolkit-mac.yml new file mode 100644 index 00000000..a91d2c7c --- /dev/null +++ b/.github/workflows/me-rd-toolkit-mac.yml @@ -0,0 +1,172 @@ +name: DatumBench apps + +# Builds the downloadable DatumBench apps and publishes them to the "me-rd-toolkit-mac" release from main: +# macOS DatumBench-macOS-AppleSilicon.dmg + DatumBench-macOS-Intel.dmg +# The apps are thin native windows around the live site, so every push to the site reaches them through GitHub +# Pages without a rebuild. This workflow only runs when the shell in desktop/ changes. Every build is opened and +# checked on its own system first; only builds that passed are published, and the download links never change. +on: + push: + branches: [main] + paths: + - 'apps/me-rd-toolkit/shell/desktop/**' + - '.github/workflows/me-rd-toolkit-mac.yml' + pull_request: + paths: + - 'apps/me-rd-toolkit/shell/desktop/**' + - '.github/workflows/me-rd-toolkit-mac.yml' + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: me-rd-toolkit-mac-${{ github.ref }} + cancel-in-progress: true + +jobs: + desktop: + name: Desktop (${{ matrix.name }}) + strategy: + fail-fast: false + matrix: + include: + - name: macOS Apple silicon + os: macos-15 + - name: macOS Intel + os: macos-15-intel + runs-on: ${{ matrix.os }} + timeout-minutes: 30 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install PyInstaller, Pillow and pywebview + run: python -m pip install --disable-pip-version-check pyinstaller pillow pywebview + - name: Install Inno Setup + if: runner.os == 'Windows' + shell: pwsh + run: | + $iscc = Join-Path ${env:ProgramFiles(x86)} 'Inno Setup 6\ISCC.exe' + if (-not (Test-Path $iscc) -and -not (Get-Command iscc -ErrorAction SilentlyContinue)) { + choco install innosetup --no-progress -y + } + - name: Make sure the WebView2 runtime is there (the Windows window uses it) + if: runner.os == 'Windows' + shell: pwsh + run: | + $client = '{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}' + $keys = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$client", + "HKCU:\Software\Microsoft\EdgeUpdate\Clients\$client" + $version = $keys | Where-Object { Test-Path $_ } | ForEach-Object { (Get-ItemProperty $_).pv } | + Where-Object { $_ -and $_ -ne '0.0.0.0' } | Select-Object -First 1 + if ($version) { Write-Host "WebView2 runtime $version"; exit 0 } + $setup = Join-Path $env:RUNNER_TEMP 'MicrosoftEdgeWebview2Setup.exe' + Invoke-WebRequest 'https://go.microsoft.com/fwlink/p/?LinkId=2124703' -OutFile $setup + Start-Process -FilePath $setup -ArgumentList '/silent', '/install' -Wait + - name: Stop XProtect scans (they make hdiutil fail with "Resource busy") + if: runner.os == 'macOS' + run: | + sudo pkill -9 XProtect > /dev/null 2>&1 || true + for attempt in $(seq 1 20); do pgrep -x XProtect > /dev/null || break; sleep 3; done + - name: Build and package + run: python apps/me-rd-toolkit/shell/desktop/build.py --require-installer + - name: Open the built app and check its window shows this commit's DatumBench + run: python apps/me-rd-toolkit/shell/desktop/smoke_test.py --built --local-site + - name: Install with DatumBench-Windows-Setup.exe, run it, uninstall it + if: runner.os == 'Windows' + shell: pwsh + run: | + $setup = Resolve-Path 'apps/me-rd-toolkit/shell/desktop/dist/DatumBench-Windows-Setup.exe' + $log = Join-Path $env:RUNNER_TEMP 'merdtoolkit-setup.log' + $process = Start-Process -FilePath $setup -Wait -PassThru -ArgumentList @( + '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/TASKS=desktopicon', "/LOG=$log") + if ($process.ExitCode -ne 0) { Get-Content $log -Tail 40; throw "Setup failed with exit code $($process.ExitCode)" } + $app = Join-Path $env:LOCALAPPDATA 'Programs\DatumBench\DatumBench.exe' + if (-not (Test-Path $app)) { throw "DatumBench.exe was not installed to $app" } + $shortcuts = @( + (Join-Path ([Environment]::GetFolderPath('Programs')) 'DatumBench.lnk'), + (Join-Path ([Environment]::GetFolderPath('Desktop')) 'DatumBench.lnk')) + foreach ($link in $shortcuts) { if (-not (Test-Path $link)) { throw "Missing shortcut: $link" } } + $entry = Get-ChildItem 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall' | + Get-ItemProperty | Where-Object DisplayName -eq 'DatumBench' + if (-not $entry) { throw 'DatumBench is missing from Settings > Apps' } + python apps/me-rd-toolkit/shell/desktop/smoke_test.py --local-site $app + if ($LASTEXITCODE -ne 0) { throw 'The installed app failed its checks' } + Start-Process -FilePath $entry.UninstallString.Trim('"') -Wait -ArgumentList '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART' + for ($i = 0; $i -lt 60 -and (Test-Path $app); $i++) { Start-Sleep -Seconds 1 } + if (Test-Path $app) { throw 'Uninstalling left DatumBench.exe behind' } + foreach ($link in $shortcuts) { if (Test-Path $link) { throw "Uninstalling left a shortcut: $link" } } + Write-Host 'Installed, ran and uninstalled cleanly' + - name: Check the Mac app and disk image + if: runner.os == 'macOS' + run: | + APP=apps/me-rd-toolkit/shell/desktop/dist/DatumBench.app + test "$(plutil -extract CFBundleIdentifier raw "$APP/Contents/Info.plist")" = "io.github.juw12.merdtoolkit.desktop" + test "$(plutil -extract CFBundleShortVersionString raw "$APP/Contents/Info.plist")" = "$(python3 -c 'import json; print(json.load(open("apps/me-rd-toolkit/shell/desktop/shell.json"))["version"])')" + echo "Architecture: $(lipo -archs "$APP/Contents/MacOS/DatumBench")" + codesign --verify --deep --verbose=2 "$APP" + for image in apps/me-rd-toolkit/shell/desktop/dist/*.dmg; do hdiutil verify "$image"; done + - uses: actions/upload-artifact@v4 + with: + name: DatumBench-${{ runner.os }}-${{ runner.arch }} + path: apps/me-rd-toolkit/shell/desktop/dist/DatumBench-* + if-no-files-found: error + retention-days: 14 + + publish: + needs: [desktop] + if: ${{ !cancelled() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }} + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - uses: actions/download-artifact@v4 + with: + pattern: DatumBench-* + path: dist + merge-multiple: true + - name: Publish the apps to the apps release + env: + GH_TOKEN: ${{ github.token }} + REPO: ${{ github.repository }} + run: | + cd dist + FILES="" + for name in DatumBench-macOS-AppleSilicon.dmg DatumBench-macOS-Intel.dmg; do + if [ -f "$name" ]; then + FILES="$FILES $name" + else + echo "::warning::$name was not built in this run; the release keeps its previous copy (if any)." + fi + done + if [ -z "$FILES" ]; then + echo "::error::No app passed its checks." + exit 1 + fi + sha256sum $FILES > SHA256SUMS.txt + cat SHA256SUMS.txt + cat > notes.md <<'EOF' + **Datum Bench**: mechanical RandD checklists. The apps open the same Datum Bench as https://juw12.github.io/projects/me-rd-toolkit/ in their own window. + + | Your device | Download | How to install | + | --- | --- | --- | + | **Mac with Apple chip (M1 and newer)** | [DatumBench-macOS-AppleSilicon.dmg](https://github.com/juw12/projects/releases/download/me-rd-toolkit-mac/DatumBench-macOS-AppleSilicon.dmg) | Open it and drag DatumBench to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**. | + | **Mac with Intel chip** | [DatumBench-macOS-Intel.dmg](https://github.com/juw12/projects/releases/download/me-rd-toolkit-mac/DatumBench-macOS-Intel.dmg) | Same as above. Not sure which Mac? Apple menu → About This Mac shows **Chip** (Apple) or **Processor** (Intel). | + | **iPhone / iPad** | [Open in Safari](https://juw12.github.io/projects/me-rd-toolkit/) | Share → **Add to Home Screen** (free, full screen, updates itself). | + | **Android** | [DatumBench-android.apk](https://github.com/juw12/projects/releases/download/me-rd-toolkit-android/DatumBench-android.apk) | Open the file on the phone and allow your browser to install apps once. | + + The apps load DatumBench from the web, so new versions arrive the moment they are published, with no reinstall. Checksums for this build: `SHA256SUMS.txt`. + EOF + { + echo + echo "Built from ${GITHUB_SHA::7}." + } >> notes.md + if gh release view me-rd-toolkit-mac --repo "$REPO" > /dev/null 2>&1; then + gh release upload me-rd-toolkit-mac $FILES SHA256SUMS.txt --clobber --repo "$REPO" + gh release edit me-rd-toolkit-mac --notes-file notes.md --repo "$REPO" + else + gh release create me-rd-toolkit-mac $FILES SHA256SUMS.txt --title "Datum Bench for Mac" \ + --notes-file notes.md --latest=false --repo "$REPO" + fi diff --git a/apps/me-rd-toolkit/README.md b/apps/me-rd-toolkit/README.md index 751876ba..a237e9f6 100644 --- a/apps/me-rd-toolkit/README.md +++ b/apps/me-rd-toolkit/README.md @@ -51,6 +51,12 @@

+## On a Mac + +- **Mac:** [`DatumBench-macOS-AppleSilicon.dmg`](https://github.com/juw12/projects/releases/download/me-rd-toolkit-mac/DatumBench-macOS-AppleSilicon.dmg) (M1 and newer) or [`DatumBench-macOS-Intel.dmg`](https://github.com/juw12/projects/releases/download/me-rd-toolkit-mac/DatumBench-macOS-Intel.dmg). Open it and drag DatumBench to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**. + +It is the web edition (https://juw12.github.io/projects/me-rd-toolkit/) in its own window ([`desktop/`](shell/desktop/README.md)) and changes with it. The three engineering sub-apps under R&D Apps run from local servers that only the Windows app starts, so they are not in it. Each build is opened on an Apple-silicon Mac and an Intel Mac on GitHub before it is published: the page loads, the app starts and can store data. No Mac of ours has run it yet. + ## On an Android phone - **Android:** [`DatumBench-android.apk`](https://github.com/juw12/projects/releases/download/me-rd-toolkit-android/DatumBench-android.apk): open the file on the phone and allow your browser to install apps once. diff --git a/apps/me-rd-toolkit/shell/desktop/.gitignore b/apps/me-rd-toolkit/shell/desktop/.gitignore new file mode 100644 index 00000000..398ca165 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/.gitignore @@ -0,0 +1,3 @@ +build/ +dist/ +__pycache__/ diff --git a/apps/me-rd-toolkit/shell/desktop/README.md b/apps/me-rd-toolkit/shell/desktop/README.md new file mode 100644 index 00000000..175a2f3d --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/README.md @@ -0,0 +1,25 @@ +# DatumBench for Windows and Mac + +A small native window around the live site (https://juw12.github.io/projects/me-rd-toolkit/): Microsoft Edge WebView2 on Windows, the system +WebKit on macOS, both through [pywebview](https://pywebview.flowrl.com/). The window keeps its own browser data +(what you saved in it), so it is all still there the next time DatumBench opens. Because it loads the live +site, every push to `main` reaches the installed app on its next start; only a change to this folder needs a new build. + +| File | What it is | +| --- | --- | +| `merdtoolkit_app.py` | the program: one window, remembers its size, a second start brings the open window forward | +| `shell.json` | the shell's version (bump it when this folder changes) | +| `build.py` | builds the app for the system it runs on | +| `merdtoolkit.spec`, `merdtoolkit.iss` | PyInstaller recipe, Windows installer (Inno Setup 6, per user, no admin rights) | +| `smoke_test.py` | opens the built app and checks its window shows DatumBench | +| `offline.html` | shown when the site cannot be reached on the very first start | + +``` +python -m pip install pyinstaller pillow pywebview +python desktop/build.py +python desktop/smoke_test.py --built --local-site +``` + +`.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls +the Windows setup, and publishes the files that passed to the +[apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. diff --git a/apps/me-rd-toolkit/shell/desktop/build.py b/apps/me-rd-toolkit/shell/desktop/build.py new file mode 100644 index 00000000..6221cde7 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/build.py @@ -0,0 +1,174 @@ +#!/usr/bin/env python3 +"""Build the DatumBench desktop app for the system this runs on (Windows or macOS). + + python -m pip install pyinstaller pillow pywebview + python desktop/build.py [--require-installer] + +Output in desktop/dist/ (the names the "apps" GitHub release uses): + Windows DatumBench-Windows-Setup.exe per-user installer, no admin rights (needs Inno Setup 6) + DatumBench-Windows-Portable.zip unzip and run DatumBench.exe + macOS DatumBench-macOS-AppleSilicon.dmg or DatumBench-macOS-Intel.dmg (the chip of this Mac) +""" + +import argparse +import json +import os +import platform +import shutil +import subprocess +import sys +import time +import zipfile + +DESKTOP = os.path.dirname(os.path.abspath(__file__)) +APP = os.path.dirname(DESKTOP) +BUILD = os.path.join(DESKTOP, "build") +DIST = os.path.join(DESKTOP, "dist") +ICON_SOURCE = os.path.join(APP, *"desktop/icon-512.png".split("/")) + +WINDOWS_VERSION_INFO = """VSVersionInfo( + ffi=FixedFileInfo(filevers={numbers}, prodvers={numbers}, mask=0x3f, flags=0x0, OS=0x40004, + fileType=0x1, subtype=0x0, date=(0, 0)), + kids=[ + StringFileInfo([StringTable('040904B0', [ + StringStruct('CompanyName', 'Juwen Lie'), + StringStruct('FileDescription', 'DatumBench - Mechanical RandD checklists'), + StringStruct('FileVersion', '{version}'), + StringStruct('InternalName', 'DatumBench'), + StringStruct('LegalCopyright', 'Open source - github.com/juw12/projects'), + StringStruct('OriginalFilename', 'DatumBench.exe'), + StringStruct('ProductName', 'DatumBench'), + StringStruct('ProductVersion', '{version}')])]), + VarFileInfo([VarStruct('Translation', [1033, 1200])]) + ] +) +""" + + +def read_version(): + """The desktop shell's version: desktop/shell.json (the built app carries the same file).""" + with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle: + return json.load(handle)["version"] + + +def make_icon(): + """The app icon from the app's 512 px PNG: .ico on Windows, the PNG itself on macOS.""" + if sys.platform != "win32": + return ICON_SOURCE + from PIL import Image + + target = os.path.join(BUILD, "merdtoolkit.ico") + with Image.open(ICON_SOURCE) as image: + image.convert("RGBA").save(target, sizes=[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)]) + return target + + +def run_pyinstaller(version, icon): + env = dict(os.environ, MERDTOOLKIT_VERSION=version, MERDTOOLKIT_ICON=icon) + if sys.platform == "win32": + numbers = tuple(int(part) for part in (version.split(".") + ["0", "0", "0"])[:4]) + version_file = os.path.join(BUILD, "version_info.txt") + with open(version_file, "w", encoding="utf-8") as handle: + handle.write(WINDOWS_VERSION_INFO.format(numbers=numbers, version=version)) + env["MERDTOOLKIT_VERSION_FILE"] = version_file + subprocess.run( + [ + sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean", + "--distpath", DIST, + "--workpath", os.path.join(BUILD, "pyinstaller"), + os.path.join(DESKTOP, "merdtoolkit.spec"), + ], + check=True, + env=env, + ) + + +def zip_folder(folder, archive, root_name): + with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as bundle: + for current, _dirs, files in os.walk(folder): + for name in files: + full = os.path.join(current, name) + bundle.write(full, os.path.join(root_name, os.path.relpath(full, folder))) + + +def find_iscc(): + candidates = [ + shutil.which("iscc"), + os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"), "Inno Setup 6", "ISCC.exe"), + os.path.join(os.environ.get("ProgramFiles", r"C:\Program Files"), "Inno Setup 6", "ISCC.exe"), + os.path.join(os.environ.get("LOCALAPPDATA", ""), "Programs", "Inno Setup 6", "ISCC.exe"), + ] + return next((path for path in candidates if path and os.path.isfile(path)), None) + + +def package_windows(version, icon, require_installer): + folder = os.path.join(DIST, "DatumBench") + portable = os.path.join(DIST, "DatumBench-Windows-Portable.zip") + zip_folder(folder, portable, "DatumBench") + made = [portable] + iscc = find_iscc() + if not iscc: + if require_installer: + raise SystemExit("Inno Setup 6 (ISCC.exe) was not found - install it to build DatumBench-Windows-Setup.exe") + print("Inno Setup 6 not found: skipped DatumBench-Windows-Setup.exe (the portable zip was built)") + return made + subprocess.run( + [iscc, "/Qp", "/DAppVersion=" + version, "/DSourceDir=" + folder, "/DOutputDir=" + DIST, + "/DIconFile=" + icon, os.path.join(DESKTOP, "merdtoolkit.iss")], + check=True, + ) + return [os.path.join(DIST, "DatumBench-Windows-Setup.exe")] + made + + +def package_macos(): + app = os.path.join(DIST, "DatumBench.app") + chip = "AppleSilicon" if platform.machine() == "arm64" else "Intel" + image = os.path.join(DIST, "DatumBench-macOS-%s.dmg" % chip) + stage = os.path.join(BUILD, "dmg") + shutil.rmtree(stage, ignore_errors=True) + os.makedirs(stage) + subprocess.run(["ditto", app, os.path.join(stage, "DatumBench.app")], check=True) # keeps links and signature + os.symlink("/Applications", os.path.join(stage, "Applications")) + command = ["hdiutil", "create", "-volname", "DatumBench", "-srcfolder", stage, "-ov", "-format", "UDZO", image] + # hdiutil sometimes fails with "Resource busy" while macOS scans the new volume (common on CI runners). + for attempt in range(1, 6): + if os.path.exists(image): + os.remove(image) + result = subprocess.run(command, capture_output=True, text=True) + if result.returncode == 0: + return [image] + message = (result.stderr or result.stdout).strip() or "exit code %d" % result.returncode + if attempt == 5: + raise SystemExit("hdiutil create failed: %s" % message) + print("hdiutil create failed (%s); trying again in %d s" % (message, 10 * attempt)) + time.sleep(10 * attempt) + return [image] + + +def main(): + parser = argparse.ArgumentParser(description="Build the DatumBench desktop app for this system") + parser.add_argument("--require-installer", action="store_true", + help="Windows: fail if Inno Setup is missing instead of building only the portable zip") + args = parser.parse_args() + if sys.platform not in ("win32", "darwin"): + raise SystemExit("DatumBench desktop builds are made on Windows and macOS") + try: + import webview # noqa: F401 (bundled by merdtoolkit.spec) + except ImportError: + raise SystemExit("The DatumBench window needs pywebview: python -m pip install pywebview") + + version = read_version() + os.makedirs(BUILD, exist_ok=True) + for leftover in ("DatumBench", "DatumBench.app"): + shutil.rmtree(os.path.join(DIST, leftover), ignore_errors=True) + print("Building DatumBench %s for %s %s" % (version, platform.system(), platform.machine())) + icon = make_icon() + run_pyinstaller(version, icon) + made = package_windows(version, icon, args.require_installer) if sys.platform == "win32" else package_macos() + print("\nBuilt:") + for path in made: + print(" %s (%.1f MB)" % (path, os.path.getsize(path) / 1e6)) + + +if __name__ == "__main__": + main() diff --git a/apps/me-rd-toolkit/shell/desktop/icon-512.png b/apps/me-rd-toolkit/shell/desktop/icon-512.png new file mode 100644 index 0000000000000000000000000000000000000000..330d65b7a42da1c8940460ef7fe439473dcf0fca GIT binary patch literal 8056 zcmZ{Jc_39?+x|L(F%l8VScc3QqJ(oQ88T)nWGM42LuThxB4nNt8KN9Xl6fvtnH7a( zCdoYH7>={QeLU~`Jn#GczTf#{pFQ07wXgeHYwf+)wIcO()fwrJ(gOe(H8oTX06@aK zNI**s|84m9?*PDy(NsNeJpogukmO7OYE%S zPuGqL20Z95PL&Zc$Peu@Oioj6H_tceXSyP0kQf@7tWnsE&;4~*$cX*4^u2eRt=c>s z3_MFpnT}S&o^E6FLCc(*W1i_j4ua@m4tY}hC6E9@W(hCc?dozH2ykl^;HnxKI#da z3w$>*V#bVwNmeYC6epgc{6#X_$c+xWLc?cQtj}z0iX?P0Qt<^9m-+F{Ib+$L12hi2 zAf$M)#P6`+$C0JHV|LLaFqjnG&ULSRQG&7JA61`0Ib z_t9B{$4HHz&jEHM2x(9VXvttYpKTXu@Cd0f&I0&KE@KzyDWKhEr7G2&t7TL;KzmCR zQ7TFSRecyO%>v%>g5w`Q+86zJdT*Qw(23I|nvYQ-y;Lv$a zI(!ulXo!byD(*+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_+)azNVQ!D zFIFl5uu(^IFT8kz_T<+>g8ZkN?BF#IEef62=Ol)n&x_loj}SNBx`UL1vU*Mc9Fv|zf3Std0I=^3jE_;_Om z+2fIPZ`-Y+w6CfIXU*OwJlfjs1rmG}UMb /DOutputDir= /DIconFile= merdtoolkit.iss +; Installs to %LOCALAPPDATA%\Programs\DatumBench, adds DatumBench to the Start menu (and optionally the desktop) and lists it +; in Settings > Apps. The window keeps its data (what you saved in it) in %LOCALAPPDATA%\DatumBench, which +; uninstalling leaves in place so a reinstall finds it again. + +#ifndef AppVersion + #define AppVersion "1.0.0" +#endif +#ifndef SourceDir + #define SourceDir "dist\DatumBench" +#endif +#ifndef OutputDir + #define OutputDir "dist" +#endif +#ifndef IconFile + #define IconFile "build\merdtoolkit.ico" +#endif + +[Setup] +AppId={{E81247BA-698B-569E-A6DE-E1E1C366EEFB} +AppName=DatumBench +AppVersion={#AppVersion} +AppVerName=DatumBench {#AppVersion} +AppPublisher=Juwen Lie +AppPublisherURL=https://juw12.github.io/projects/me-rd-toolkit/ +AppSupportURL=https://github.com/juw12/projects +DefaultDirName={autopf}\DatumBench +DisableDirPage=yes +DisableProgramGroupPage=yes +PrivilegesRequired=lowest +OutputDir={#OutputDir} +OutputBaseFilename=DatumBench-Windows-Setup +SetupIconFile={#IconFile} +UninstallDisplayIcon={app}\DatumBench.exe +UninstallDisplayName=DatumBench +Compression=lzma2 +SolidCompression=yes +WizardStyle=modern +ArchitecturesAllowed=x64compatible +ArchitecturesInstallIn64BitMode=x64compatible +CloseApplications=no +SetupLogging=yes + +[Languages] +Name: "english"; MessagesFile: "compiler:Default.isl" + +[Tasks] +Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}" + +[Files] +Source: "{#SourceDir}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs + +[Icons] +Name: "{userprograms}\DatumBench"; Filename: "{app}\DatumBench.exe"; Comment: "Mechanical RandD checklists" +Name: "{userdesktop}\DatumBench"; Filename: "{app}\DatumBench.exe"; Comment: "Mechanical RandD checklists"; Tasks: desktopicon + +[Run] +Filename: "{app}\DatumBench.exe"; Description: "{cm:LaunchProgram,DatumBench}"; Flags: nowait postinstall skipifsilent + +[UninstallRun] +Filename: "{app}\DatumBench.exe"; Parameters: "--stop"; Flags: runhidden waituntilterminated; RunOnceId: "StopDatumBench" + +[Code] +function PrepareToInstall(var NeedsRestart: Boolean): String; +var + ResultCode: Integer; +begin + Result := ''; + { Updating: close an open DatumBench window first so its files can be replaced. } + if FileExists(ExpandConstant('{app}\DatumBench.exe')) then + Exec(ExpandConstant('{app}\DatumBench.exe'), '--stop', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); +end; diff --git a/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec b/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec new file mode 100644 index 00000000..848e3871 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec @@ -0,0 +1,64 @@ +# -*- mode: python ; coding: utf-8 -*- +# PyInstaller recipe for DatumBench on Windows and macOS. Build with python desktop/build.py +# Windows dist/DatumBench/DatumBench.exe (+ _internal/) - no console; its window uses Edge WebView2 (pywebview) +# macOS dist/DatumBench.app - a normal Dock app; its window uses WKWebView (pywebview) +import os +import sys + +DESKTOP = os.path.dirname(os.path.abspath(SPEC)) # noqa: F821 (provided by PyInstaller) +VERSION = os.environ.get("MERDTOOLKIT_VERSION", "1.0.0") +VERSION_FILE = os.environ.get("MERDTOOLKIT_VERSION_FILE") or None +ICON = os.environ.get("MERDTOOLKIT_ICON") or None +IS_WIN = sys.platform == "win32" + +EXCLUDES = ["tkinter", "unittest", "test", "idlelib", "lib2to3"] +# pywebview: bundle only the backend this system uses (the others would drag in Qt, GTK or CEF). +OTHER_BACKENDS = ["webview.platforms.qt", "webview.platforms.gtk", "webview.platforms.cef", + "webview.platforms.android", "PyQt5", "PyQt6", "PySide2", "PySide6", "qtpy", "gi"] +if IS_WIN: + HIDDEN = ["webview", "webview.platforms.winforms", "webview.platforms.edgechromium", "clr"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.cocoa"] +else: + HIDDEN = ["webview", "webview.platforms.cocoa"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.winforms", "webview.platforms.edgechromium", + "webview.platforms.mshtml", "webview.platforms.win32"] + +a = Analysis( # noqa: F821 + [os.path.join(DESKTOP, "merdtoolkit_app.py")], + pathex=[DESKTOP], + datas=[(os.path.join(DESKTOP, "offline.html"), "."), (os.path.join(DESKTOP, "shell.json"), ".")], + hiddenimports=HIDDEN, + excludes=EXCLUDES, +) +pyz = PYZ(a.pure) # noqa: F821 +exe = EXE( # noqa: F821 + pyz, + a.scripts, + [], + exclude_binaries=True, + name="DatumBench", + console=False, + icon=ICON, + version=VERSION_FILE if IS_WIN else None, + upx=False, +) +coll = COLLECT(exe, a.binaries, a.datas, name="DatumBench", upx=False) # noqa: F821 + +if sys.platform == "darwin": + app = BUNDLE( # noqa: F821 + coll, + name="DatumBench.app", + icon=ICON, + bundle_identifier="io.github.juw12.merdtoolkit.desktop", + version=VERSION, + info_plist={ + "CFBundleName": "DatumBench", + "CFBundleDisplayName": "DatumBench", + "CFBundleShortVersionString": VERSION, + "CFBundleVersion": VERSION, + "NSHighResolutionCapable": True, + "NSRequiresAquaSystemAppearance": False, + "LSApplicationCategoryType": "public.app-category.lifestyle", + "NSHumanReadableCopyright": "Open source - github.com/juw12/projects", + }, + ) diff --git a/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py b/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py new file mode 100644 index 00000000..cd417d5d --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py @@ -0,0 +1,467 @@ +"""DatumBench for Windows and macOS: the DatumBench web app in its own window. + + Windows Microsoft Edge WebView2 (part of Windows 10 and 11), through pywebview + macOS the system WebKit (WKWebView), through pywebview + +The window opens https://juw12.github.io/projects/me-rd-toolkit/ and keeps its browser data (what you saved in it) in a per-user +folder, so everything is still there the next time DatumBench opens. Because the window loads the live site, every +push to main reaches the desktop app on its next launch; the app shell itself only changes when +desktop/shell.json gets a new version. Links to other sites open in the default browser. Without WebView2 +DatumBench opens in the default browser instead. + +Opening DatumBench while its window is open brings that window to the front; `DatumBench --stop` closes it. + +Automated checks: MERDTOOLKIT_WINDOW_TEST= makes the window write a report once the page has +loaded and then close itself; MERDTOOLKIT_DESKTOP_URL overrides the address. +""" + +import hmac +import json +import os +import secrets +import socket +import subprocess +import sys +import threading +import time +import urllib.request +import webbrowser + +TITLE = "Datum Bench" +APP_URL = os.environ.get("MERDTOOLKIT_DESKTOP_URL") or "https://juw12.github.io/projects/me-rd-toolkit/" +DEFAULT_SIZE = (1280, 860) +MIN_SIZE = (420, 560) +LIGHT_BACKGROUND = "#0f172a" +DARK_BACKGROUND = "#0f172a" +CONTROL_FILE = "window.json" +SIZE_FILE = "window-size.json" +TEST_TIMEOUT = 60 +PAGE_SCRIPT = ( + "JSON.stringify({title: document.title, url: location.href, userAgent: navigator.userAgent," + " app: !!(document.getElementById('view') && document.getElementById('view').children.length)," + " storage: (function () { try { localStorage.setItem('merdtoolkit.desktop.check', '1');" + " return localStorage.getItem('merdtoolkit.desktop.check') === '1'; } catch (e) { return false; } })()," + " ready: document.readyState, width: window.innerWidth, height: window.innerHeight})" +) + + +def log(message): + if sys.stdout is not None: + print("[merdtoolkit] " + message, flush=True) + + +def here(): + """Folder with this program's own files (the PyInstaller bundle, or this checkout).""" + return getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) + + +def read_version(): + """The shell's version: desktop/shell.json, bundled next to the program.""" + try: + with open(os.path.join(here(), "shell.json"), encoding="utf-8") as handle: + return str(json.load(handle)["version"]) + except (OSError, ValueError, KeyError): + return "0.0.0" + + +VERSION = read_version() + + +def data_dir(): + """Per-user folder for the window's browser data and its own small settings.""" + home = os.path.expanduser("~") + if sys.platform == "win32": + return os.path.join(os.environ.get("LOCALAPPDATA") or os.path.join(home, "AppData", "Local"), "DatumBench") + if sys.platform == "darwin": + return os.path.join(home, "Library", "Application Support", "DatumBench") + return os.path.join(os.environ.get("XDG_DATA_HOME") or os.path.join(home, ".local", "share"), "merdtoolkit") + + +def system_prefers_dark(): + try: + if sys.platform == "win32": + import winreg + + with winreg.OpenKey(winreg.HKEY_CURRENT_USER, + r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize") as key: + return winreg.QueryValueEx(key, "AppsUseLightTheme")[0] == 0 + if sys.platform == "darwin": + result = subprocess.run(["defaults", "read", "-g", "AppleInterfaceStyle"], + capture_output=True, text=True, timeout=5) + return "dark" in result.stdout.lower() + except (OSError, ValueError, subprocess.SubprocessError): + pass + return False + + +def saved_size(folder): + try: + with open(os.path.join(folder, SIZE_FILE), encoding="utf-8") as handle: + data = json.load(handle) + width, height = int(data["width"]), int(data["height"]) + except (OSError, ValueError, KeyError, TypeError): + return DEFAULT_SIZE + if MIN_SIZE[0] <= width <= 8000 and MIN_SIZE[1] <= height <= 8000: + return width, height + return DEFAULT_SIZE + + +def save_size(folder, width, height): + try: + width, height = int(width), int(height) + if width < MIN_SIZE[0] or height < MIN_SIZE[1]: + return + os.makedirs(folder, exist_ok=True) + with open(os.path.join(folder, SIZE_FILE), "w", encoding="utf-8") as handle: + json.dump({"width": width, "height": height}, handle) + except (OSError, ValueError, TypeError): + pass + + +def screen_work_area(): + """(width, height) of the primary screen's work area in window units, or None.""" + try: + if sys.platform == "win32": + import ctypes + from ctypes import wintypes + + rect = wintypes.RECT() + if ctypes.windll.user32.SystemParametersInfoW(0x0030, 0, ctypes.byref(rect), 0): # SPI_GETWORKAREA + return rect.right - rect.left, rect.bottom - rect.top + if sys.platform == "darwin": + from AppKit import NSScreen # comes with pywebview + + frame = NSScreen.mainScreen().visibleFrame() + return int(frame.size.width), int(frame.size.height) + except Exception: # sizing is cosmetic + return None + return None + + +def fit_size(size, work_area): + width, height = size + if work_area: + width = min(width, work_area[0] - 40) + height = min(height, work_area[1] - 40) + return max(width, MIN_SIZE[0]), max(height, MIN_SIZE[1]) + + +def online(url, timeout=6): + """True when the DatumBench site answers (the window then loads it; otherwise the offline page).""" + try: + request = urllib.request.Request(url, method="HEAD", headers={"User-Agent": "DatumBenchDesktop/" + VERSION}) + with urllib.request.urlopen(request, timeout=timeout) as response: + return response.status < 500 + except Exception: + return False + + +def offline_page(): + path = os.path.join(here(), "offline.html") + return "file:///" + path.replace("\\", "/").lstrip("/") if os.path.isfile(path) else None + + +# ------------------------------------------------------- one window at a time +class WindowControl: + """Loopback endpoint of the open window: 'show' brings it to the front, 'quit' closes it.""" + + def __init__(self, folder, on_show, on_quit): + self.path = os.path.join(folder, CONTROL_FILE) + self.token = secrets.token_hex(16) + self.on_show = on_show + self.on_quit = on_quit + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.server.bind(("127.0.0.1", 0)) + self.server.listen(4) + os.makedirs(folder, exist_ok=True) + with open(self.path, "w", encoding="utf-8") as handle: + json.dump({"pid": os.getpid(), "port": self.server.getsockname()[1], "token": self.token}, handle) + threading.Thread(target=self._serve, name="merdtoolkit-window-control", daemon=True).start() + + def _serve(self): + while True: + try: + connection, _address = self.server.accept() + except OSError: + return + with connection: + try: + connection.settimeout(2) + line = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + continue + token, _space, command = line.partition(" ") + action = {"show": self.on_show, "quit": self.on_quit}.get(command) + if not action or not hmac.compare_digest(token, self.token): + continue + try: + connection.sendall(b"ok\n") + except OSError: + pass + try: + action() + except Exception as error: + log("control %s failed: %s" % (command, error)) + + def close(self): + try: + self.server.close() + except OSError: + pass + try: + with open(self.path, encoding="utf-8") as handle: + mine = json.load(handle).get("token") == self.token + if mine: + os.remove(self.path) + except (OSError, ValueError): + pass + + +def send_to_window(folder, command, timeout=2.0): + """Ask an open DatumBench window to 'show' itself or 'quit'. True if one answered.""" + try: + with open(os.path.join(folder, CONTROL_FILE), encoding="utf-8") as handle: + info = json.load(handle) + port, token, pid = int(info["port"]), str(info["token"]), int(info.get("pid") or 0) + except (OSError, ValueError, KeyError, TypeError): + return False + if command == "show" and sys.platform == "win32" and pid: + try: + import ctypes + + ctypes.windll.user32.AllowSetForegroundWindow(pid) + except (OSError, AttributeError): + pass + try: + with socket.create_connection(("127.0.0.1", port), timeout=timeout) as connection: + connection.settimeout(timeout) + connection.sendall(("%s %s\n" % (token, command)).encode("utf-8")) + reply = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + return False + if reply == "ok" and command == "quit" and pid: + deadline = time.time() + 15 + while time.time() < deadline and process_alive(pid): + time.sleep(0.2) + return reply == "ok" + + +def process_alive(pid): + if sys.platform == "win32": + import ctypes + + kernel32 = ctypes.windll.kernel32 + handle = kernel32.OpenProcess(0x00100000, False, pid) # SYNCHRONIZE + if not handle: + return False + try: + return kernel32.WaitForSingleObject(handle, 0) != 0 + finally: + kernel32.CloseHandle(handle) + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True + + +# ------------------------------------------------------------ the window +def webview2_runtime(): + """Version of the installed Microsoft Edge WebView2 runtime, or None (Windows only).""" + if sys.platform != "win32": + return None + import platform + import winreg + + clients = [ + "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}", # WebView2 Runtime + "{2CD8A007-E189-409D-A2C8-9AF4EF3C72AA}", # Beta + "{0D50BFEC-CD6A-4F9A-964C-C7416E3ACB10}", # Dev + "{65C35B14-6C1D-4122-AC46-7148CC9D6497}", # Canary + ] + for client in clients: + for hive in (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE): + native = hive == winreg.HKEY_CURRENT_USER or platform.machine() == "x86" + path = r"SOFTWARE\%sMicrosoft\EdgeUpdate\Clients\%s" % ("" if native else "WOW6432Node\\", client) + try: + with winreg.OpenKey(hive, path) as key: + version = str(winreg.QueryValueEx(key, "pv")[0]) + except OSError: + continue + if version and version != "0.0.0.0": + return version + return None + + +def bring_to_front(window): + if sys.platform == "win32": + try: + import ctypes + + user32 = ctypes.windll.user32 + hwnd = int(window.native.Handle.ToInt64()) + if user32.IsIconic(hwnd): + user32.ShowWindow(hwnd, 9) # SW_RESTORE + user32.SetForegroundWindow(hwnd) + return + except Exception: + pass + else: + window.restore() + window.show() + + +def write_report(path, **fields): + fields.setdefault("pid", os.getpid()) + fields.setdefault("version", VERSION) + temporary = path + ".tmp" + with open(temporary, "w", encoding="utf-8") as handle: + json.dump(fields, handle, indent=2) + os.replace(temporary, path) + + +def run_window(url, folder, report_path): + """Show DatumBench in a native window until it is closed. Returns False when this system can't.""" + if sys.platform == "win32": + runtime = webview2_runtime() + if not runtime: + log("Microsoft Edge WebView2 runtime not found") + return False + gui = "edgechromium" + elif sys.platform == "darwin": + runtime = None + gui = "cocoa" + else: + return False + try: + import webview + except ImportError as error: + log("pywebview is not available: %s" % error) + return False + + # Let the page save files and send other sites to the default browser. + webview.settings["ALLOW_DOWNLOADS"] = True + webview.settings["OPEN_EXTERNAL_LINKS_IN_BROWSER"] = True + + start_url = url if (report_path or online(url)) else (offline_page() or url) + width, height = fit_size(saved_size(folder), screen_work_area()) + window = webview.create_window( + TITLE, + start_url, + width=width, + height=height, + min_size=MIN_SIZE, + background_color=DARK_BACKGROUND if system_prefers_dark() else LIGHT_BACKGROUND, + text_select=True, + zoomable=True, + focus=not report_path, + ) + control = WindowControl(folder, on_show=lambda: bring_to_front(window), on_quit=window.destroy) + state = {"tested": False} + + def remember_size(): + try: + save_size(folder, window.width, window.height) + except Exception: + pass + + def self_test(): + deadline = time.time() + TEST_TIMEOUT + page = {} + settled = 0 + while time.time() < deadline: + try: + page = json.loads(read_page(window, PAGE_SCRIPT) or "{}") + except Exception as error: + page = {"error": str(error)} + # Wait until a fully loaded DatumBench page stays put for a moment. + settled = settled + 1 if page.get("app") and page.get("ready") == "complete" else 0 + if settled >= 3: + break + time.sleep(0.5) + renderer = gui + if sys.platform == "win32": + renderer = getattr(sys.modules.get("webview.platforms.winforms"), "renderer", gui) + write_report(report_path, method="window", toolkit=renderer, runtime=runtime, **page) + window.destroy() + + def on_loaded(): + if report_path and not state["tested"]: + state["tested"] = True + threading.Thread(target=self_test, name="merdtoolkit-window-test", daemon=True).start() + + window.events.closing += remember_size + window.events.loaded += on_loaded + storage = None + if sys.platform == "win32": + # Automated checks use a throwaway profile so they never touch a person's own data. + base = os.path.dirname(os.path.abspath(report_path)) if report_path else folder + storage = os.path.join(base, "WebView2") + try: + webview.start(gui=gui, private_mode=False, storage_path=storage) + except Exception as error: + log("the window failed: %s" % error) + return False + finally: + control.close() + return True + + +def read_page(window, script): + """The page's answer to `script`. + + A page that forbids eval (Content-Security-Policy: script-src 'self') refuses pywebview's own + evaluate_js on a Mac, which wraps the script in eval. There the question is put to WebKit itself, + which the page's policy does not bind. + """ + try: + return window.evaluate_js(script) + except Exception: + if sys.platform != "darwin": + raise + from PyObjCTools import AppHelper + from webview.platforms.cocoa import BrowserView + + answer, done = {}, threading.Event() + + def ask(): + def finished(result, error): + answer["value"] = result + answer["error"] = None if error is None else str(error) + done.set() + + BrowserView.instances[window.uid].webview.evaluateJavaScript_completionHandler_(script, finished) + + AppHelper.callAfter(ask) + if not done.wait(20): + raise RuntimeError("the page did not answer") + if answer["error"]: + raise RuntimeError(answer["error"]) + return answer["value"] + + +def main(argv=None): + args = list(sys.argv[1:] if argv is None else argv) + folder = data_dir() + if "--version" in args: + print("DatumBench %s" % VERSION) + return 0 + if "--stop" in args: + send_to_window(folder, "quit") + return 0 + report_path = os.environ.get("MERDTOOLKIT_WINDOW_TEST") or None + if not report_path and send_to_window(folder, "show"): + return 0 # already open: brought to the front + if run_window(APP_URL, folder, report_path): + return 0 + if report_path: + write_report(report_path, method="none", error="no native window on this system") + return 1 + webbrowser.open(APP_URL) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/apps/me-rd-toolkit/shell/desktop/offline.html b/apps/me-rd-toolkit/shell/desktop/offline.html new file mode 100644 index 00000000..56a6093e --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/offline.html @@ -0,0 +1,25 @@ + + + + + + DatumBench + + + +
+ +

DatumBench can't reach the internet

+

DatumBench needs a connection the first time it opens. After that it also opens offline. Check your connection and try again.

+ Try again +
+ + diff --git a/apps/me-rd-toolkit/shell/desktop/shell.json b/apps/me-rd-toolkit/shell/desktop/shell.json new file mode 100644 index 00000000..1587a669 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/shell.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/apps/me-rd-toolkit/shell/desktop/smoke_test.py b/apps/me-rd-toolkit/shell/desktop/smoke_test.py new file mode 100644 index 00000000..9d055fe8 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/smoke_test.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Open the built DatumBench desktop app and check its window really shows DatumBench. + + python desktop/smoke_test.py --built the app in desktop/dist/ (after build.py) + python desktop/smoke_test.py PATH/TO/DatumBench.exe an installed copy + add --local-site to serve this checkout instead of the live site + +The app runs with MERDTOOLKIT_WINDOW_TEST set, so it writes a report once the page has loaded and closes +itself. Passes when the page is DatumBench, the app started (what the app draws into #view when it starts) and the page can store data. +""" + +import argparse +import functools +import http.server +import json +import os +import subprocess +import sys +import tempfile +import threading +import time + +DESKTOP = os.path.dirname(os.path.abspath(__file__)) +REPO = os.path.normpath(os.path.join(DESKTOP, os.pardir, os.pardir, os.pardir, os.pardir, "apps", "me-rd-toolkit", "web")) # the folder GitHub Pages serves +LIVE_URL = "https://juw12.github.io/projects/me-rd-toolkit/" + + +def built_app(): + if sys.platform == "win32": + return os.path.join(DESKTOP, "dist", "DatumBench", "DatumBench.exe") + return os.path.join(DESKTOP, "dist", "DatumBench.app", "Contents", "MacOS", "DatumBench") + + +def serve_site(): + """Serve this checkout (the site is the repository itself) on a free loopback port.""" + handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=REPO) + handler.log_message = lambda *args: None + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + return "http://127.0.0.1:%d/" % server.server_address[1] + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("app", nargs="?", help="the DatumBench program to open") + parser.add_argument("--built", action="store_true", help="open the app in desktop/dist/") + parser.add_argument("--local-site", action="store_true", help="serve this checkout instead of the live site") + args = parser.parse_args() + app = built_app() if args.built or not args.app else args.app + if not os.path.isfile(app): + raise SystemExit("Not found: %s" % app) + + url = serve_site() if args.local_site else LIVE_URL + folder = tempfile.mkdtemp(prefix="merdtoolkit-smoke-") + report = os.path.join(folder, "report.json") + env = dict(os.environ, MERDTOOLKIT_WINDOW_TEST=report, MERDTOOLKIT_DESKTOP_URL=url) + print("Opening %s on %s" % (app, url), flush=True) + started = time.time() + process = subprocess.Popen([app], env=env) + try: + process.wait(timeout=180) + except subprocess.TimeoutExpired: + process.kill() + raise SystemExit("The app did not close itself within 180 s") + if not os.path.isfile(report): + raise SystemExit("The app exited (code %s) without a report" % process.returncode) + with open(report, encoding="utf-8") as handle: + result = json.load(handle) + print(json.dumps(result, indent=2)) + + with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle: + version = json.load(handle)["version"] + problems = [] + if result.get("method") != "window": + problems.append("no native window (%s)" % (result.get("error") or result.get("method"))) + if "ME R&D Toolkit" not in str(result.get("title") or ""): + problems.append("page title is %r" % result.get("title")) + if not str(result.get("url") or "").startswith(url): + problems.append("the window is on %r" % result.get("url")) + if not result.get("app"): + problems.append("DatumBench did not start in the page (missing: what the app draws into #view when it starts)") + if not result.get("storage"): + problems.append("the page cannot store data") + if result.get("version") != version: + problems.append("the app says version %r, desktop/shell.json says %r" % (result.get("version"), version)) + if problems: + raise SystemExit("FAILED: " + "; ".join(problems)) + print("OK: DatumBench %s opened in its own window (%s) in %.0f s" % (version, result.get("toolkit"), time.time() - started)) + + +if __name__ == "__main__": + main() From ee25c92bbabba66179159e352f973cab68d600d6 Mon Sep 17 00:00:00 2001 From: Juwen Lie <73946469+juw12@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:44:48 +0700 Subject: [PATCH 3/3] Fade and Datum Bench: the shell's README says only its Mac builds are made here --- apps/fade/shell/desktop/README.md | 2 ++ apps/me-rd-toolkit/shell/desktop/README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/apps/fade/shell/desktop/README.md b/apps/fade/shell/desktop/README.md index fef9e524..d10bbcdb 100644 --- a/apps/fade/shell/desktop/README.md +++ b/apps/fade/shell/desktop/README.md @@ -23,3 +23,5 @@ python desktop/smoke_test.py --built --local-site `.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls the Windows setup, and publishes the files that passed to the [apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. + +In this project only the Mac builds are made and published (`.github/workflows/fade-mac.yml`): Windows has its own app, so the Windows half of this shell is not built here. diff --git a/apps/me-rd-toolkit/shell/desktop/README.md b/apps/me-rd-toolkit/shell/desktop/README.md index 175a2f3d..839abdbb 100644 --- a/apps/me-rd-toolkit/shell/desktop/README.md +++ b/apps/me-rd-toolkit/shell/desktop/README.md @@ -23,3 +23,5 @@ python desktop/smoke_test.py --built --local-site `.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls the Windows setup, and publishes the files that passed to the [apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. + +In this project only the Mac builds are made and published (`.github/workflows/me-rd-toolkit-mac.yml`): Windows has its own app, so the Windows half of this shell is not built here.