Files
Jidekin 81c90ed060 feat(build): Tauri 交叉编译出 Windows 安装包(1.6MB),装完 .md 默认用它打开
老板要求:用 Tauri 打包成 exe,安装后 .md 默认由小银 Markdown 打开。

一、交叉编译(Linux → Windows,无需 Windows 机器)
本机从零装了 rustc 1.100 / mingw-w64(GCC 14)/ NSIS 3.11,
target `x86_64-pc-windows-gnu`,crates 走清华镜像:
  npx tauri build --target x86_64-pc-windows-gnu --bundles nsis
产物:
  · XiaoyinMarkdown_2.6.1_x64-setup.exe  1.52 MiB(NSIS 安装包)
  · xiaoyin-markdown.exe                  3.5 MiB(免安装版)
electron-packager 那版是 152 MiB —— 因为 Tauri 用系统 WebView2,不捆绑运行时。
已核对导入表:只依赖系统 DLL + 安装包内自带的 WebView2Loader.dll,
没有 libgcc_s_seh-1 / libwinpthread 之类的 mingw 运行时依赖,干净 Windows 可跑。

二、默认打开方式(两步,缺一不可)
1. tauri.conf.json 的 fileAssociations 早就配了 md/markdown/mdown/mkd/mdwn/txt;
   已核对生成的 installer.nsi:6 条 APP_ASSOCIATE,写
   HKCU\Software\Classes\.md = XiaoyinMarkdown.Document(默认 ProgId,不只是「打开方式」列表)
2. 新增 src-tauri/installer-hooks.nsh(NSIS_HOOK_POSTINSTALL):
   删掉 HKCU\...\Explorer\FileExts\.md\UserChoice —— 这个键带哈希保护、只能删不能写,
   不删的话用户原来的默认程序永远赢;删完再 SHChangeNotify 刷新图标缓存。
   已用「故意 !error 探针」证明该钩子确实被 makensis 编译进去(installer.nsi:758),
   不是写了没生效。

三、首次双击 .md 能打开文件
原来只有 single_instance 回调处理 argv(= 程序已在运行时才生效)。
新增 on_page_load(Finished) + std::sync::Once:冷启动时把 argv 里的文件路径
emit 给前端(前端 tauri-bridge 监听 open-external → doOpenPath)。

四、官网
下载区改成三档:安装版(1.6MB,推荐)/ 免安装 exe(3.5MB)/ 源码 + 旧 Electron 版。
两个新文件已部署到 /download/,公网 200。

README 补了「用 Tauri 打包」整节(含 UserChoice 这个坑),site/README 更新发布步骤。
2026-11-19 20:00:55 +08:00

35 lines
2.1 KiB
NSIS
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
; ── 小银 Markdown NSIS 安装钩子 ────────────────────────────────────────────
; 目标:安装完成后,双击 .md 默认就用小银 Markdown 打开。
;
; 背景:Tauri 的 fileAssociations 会写 HKCU\Software\Classes 下的关联,但 Windows 10/11
; 还有一个带哈希保护的「用户选择」键:
; HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.md\UserChoice
; 它优先级**最高**,而且不能直接写(哈希由系统算)。但它可以**删** ——
; 删掉之后系统就回落到我们刚写的 HKCU\Software\Classes 关联,等于把默认打开方式交给本程序。
;
; 只动 HKCU(安装模式是 currentUser),不需要管理员权限。
!macro NSIS_HOOK_POSTINSTALL
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.md\UserChoice"
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.markdown\UserChoice"
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mdown\UserChoice"
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mkd\UserChoice"
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mdwn\UserChoice"
DeleteRegKey HKCU "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice"
; 让资源管理器立刻刷新图标与「打开方式」缓存(SHCNE_ASSOCCHANGED)
System::Call 'shell32::SHChangeNotify(l 0x08000000, l 0, l 0, l 0)'
!macroend
!macro NSIS_HOOK_POSTUNINSTALL
; 卸载时一并清掉可能残留的关联(不怕清不到)
DeleteRegKey HKCU "Software\Classes\.md"
DeleteRegKey HKCU "Software\Classes\.markdown"
DeleteRegKey HKCU "Software\Classes\.mdown"
DeleteRegKey HKCU "Software\Classes\.mkd"
DeleteRegKey HKCU "Software\Classes\.mdwn"
DeleteRegKey HKCU "Software\Classes\XiaoyinMarkdown.Document"
DeleteRegKey HKCU "Software\Classes\XiaoyinMarkdown.Text"
System::Call 'shell32::SHChangeNotify(l 0x08000000, l 0, l 0, l 0)'
!macroend