installer-preserve-files.nsi 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Unicode true
  2. Name "ZCode preserve-files smoke"
  3. OutFile "${OUTPUT_FILE}"
  4. InstallDir "$TEMP\zcode-preserve-files-smoke"
  5. RequestExecutionLevel user
  6. SilentInstall silent
  7. !addincludedir "${NSIS_INCLUDE_DIR}"
  8. !addincludedir "${ELECTRON_BUILDER_NSIS_INCLUDE_DIR}"
  9. !addplugindir /x86-unicode "${NSIS_PLUGIN_DIR}"
  10. !include "LogicLib.nsh"
  11. !define APP_FILENAME "ZCode"
  12. !define APP_EXECUTABLE_FILENAME "ZCode.exe"
  13. !define APP_ID "dev.zcode.preserve-files-smoke"
  14. !define UNINSTALL_REGISTRY_KEY "Software\\ZCode\\InstallerPreserveFilesSmoke"
  15. !define isUpdated `1 == 1`
  16. ; This fixture has no electron-builder uninstaller.nsh section; use a normal function for log smoke coverage.
  17. !ifdef BUILD_UNINSTALLER
  18. !define ZCODE_UNINSTALLER_FUNCTION_PREFIX ""
  19. !endif
  20. Var appExe
  21. ; 仅恢复场景使用旧安装目录;普通清理夹具在 /WX 下不能留下未引用变量。
  22. !ifndef BUILD_UNINSTALLER
  23. Var installationDir
  24. !endif
  25. !include "${ZCODE_INSTALLER_NSH}"
  26. ; 正式构建先加载自定义 include,再由 common.nsh 声明卸载器名称。
  27. !define UNINSTALL_FILENAME "uninstaller.exe"
  28. !ifndef BUILD_UNINSTALLER
  29. ; customHeader 已包含 ZCodeDefineCleanupSnapshot 与日志初始化函数;
  30. ; 单独再展开会导致宏重复定义。
  31. !insertmacro customHeader
  32. Function .onInit
  33. !insertmacro preInit
  34. FunctionEnd
  35. !endif
  36. Section
  37. SetOutPath "$INSTDIR"
  38. File /oname=ZCode.exe "${FIXTURE_EXE}"
  39. StrCpy $appExe "$INSTDIR\ZCode.exe"
  40. !ifndef OMIT_MANIFEST
  41. FileOpen $0 "$INSTDIR\.zcode-install-manifest" w
  42. FileWrite $0 "ZCode.exe$\r$\n"
  43. FileWrite $0 "owned-by-zcode.txt$\r$\n"
  44. !ifdef SELF_MANIFEST
  45. FileWrite $0 ".zcode-install-manifest$\r$\n"
  46. !endif
  47. FileWrite $0 "..\user-data.txt$\r$\n"
  48. FileClose $0
  49. !endif
  50. !ifdef FAIL_DELETE
  51. ; Keep the file handle open so Windows denies the manifest cleanup delete.
  52. FileOpen $0 "$INSTDIR\owned-by-zcode.txt" w
  53. FileWrite $0 "old app payload"
  54. !else
  55. FileOpen $0 "$INSTDIR\owned-by-zcode.txt" w
  56. FileWrite $0 "old app payload"
  57. FileClose $0
  58. !endif
  59. FileOpen $0 "$INSTDIR\user-data.txt" w
  60. FileWrite $0 "keep me"
  61. FileClose $0
  62. !ifdef RECOVERY_TEST
  63. FileOpen $0 "$INSTDIR\ZCode.exe" w
  64. FileWrite $0 "old application payload"
  65. FileClose $0
  66. InitPluginsDir
  67. File /oname=$PLUGINSDIR\old-cleanup.exe "${OLD_CLEANUP_EXE}"
  68. StrCpy $installationDir "$INSTDIR"
  69. !ifdef BACKUP_FAILURE
  70. ; 先备份主程序,再用独占句柄阻止下一项复制,验证半份备份不能用于恢复。
  71. System::Call 'kernel32::CreateFileW(w "$INSTDIR\owned-by-zcode.txt", i 0x80000000, i 0, p 0, i 3, i 0, p 0) p.R8'
  72. !endif
  73. !insertmacro customOldUninstallPrepare
  74. !ifdef BACKUP_FAILURE
  75. ; 仅让备份失败;旧卸载和新文件写入不再受此故障句柄影响。
  76. System::Call 'kernel32::CloseHandle(p R8)'
  77. !endif
  78. FileOpen $8 "$INSTDIR\backup-dir.txt" w
  79. FileWrite $8 "$ZCodeCleanupBackup"
  80. FileClose $8
  81. OneMoreAttempt:
  82. ExecWait '"$PLUGINSDIR\old-cleanup.exe" /S /D=$INSTDIR' $R0
  83. !ifdef RESTORE_FAILURE
  84. ; 同名目录阻止恢复主程序,必须保留备份,不能误报恢复成功。
  85. CreateDirectory "$INSTDIR\ZCode.exe"
  86. !endif
  87. !ifdef BACKUP_DELETE_FAILURE
  88. ; 句柄保持打开,阻止安装器删除备份目录;关闭会让 RMDir 成功,场景失效。
  89. CreateDirectory "$ZCodeCleanupBackup\hold"
  90. FileOpen $7 "$ZCodeCleanupBackup\hold\locked.txt" w
  91. FileWrite $7 "keep backup"
  92. !endif
  93. ${If} $R0 != 0
  94. !insertmacro customOldUninstallRetryFailed
  95. ${EndIf}
  96. !insertmacro customUnInstallCheck
  97. !else
  98. !insertmacro customRemoveFiles
  99. !endif
  100. ; 成功清理后模拟新版本文件落盘;失败路径必须在此前返回。
  101. SetOutPath "$INSTDIR"
  102. File /oname=ZCode.exe "${FIXTURE_EXE}"
  103. IfFileExists "$INSTDIR\owned-by-zcode.txt" 0 zcodeOwnedRemoved
  104. StrCpy $1 "present"
  105. Goto zcodeWriteMarker
  106. zcodeOwnedRemoved:
  107. StrCpy $1 "removed"
  108. zcodeWriteMarker:
  109. FileOpen $0 "$INSTDIR\remove-result.txt" w
  110. FileWrite $0 "$1"
  111. FileClose $0
  112. SectionEnd