installer-preserve-files.nsi 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_ID "dev.zcode.preserve-files-smoke"
  13. !define isUpdated `1 == 1`
  14. ; This fixture has no electron-builder uninstaller.nsh section; use a normal function for log smoke coverage.
  15. !ifdef BUILD_UNINSTALLER
  16. !define ZCODE_UNINSTALLER_FUNCTION_PREFIX ""
  17. !endif
  18. Var appExe
  19. ; 仅恢复场景使用旧安装目录;普通清理夹具在 /WX 下不能留下未引用变量。
  20. !ifdef RECOVERY_TEST
  21. Var installationDir
  22. !endif
  23. !include "${ZCODE_INSTALLER_NSH}"
  24. ; 正式构建先加载自定义 include,再由 common.nsh 声明卸载器名称。
  25. !define UNINSTALL_FILENAME "uninstaller.exe"
  26. !ifndef BUILD_UNINSTALLER
  27. !insertmacro ZCodeDefineCleanupSnapshot
  28. !endif
  29. Section
  30. SetOutPath "$INSTDIR"
  31. File /oname=ZCode.exe "${FIXTURE_EXE}"
  32. StrCpy $appExe "$INSTDIR\ZCode.exe"
  33. !ifndef OMIT_MANIFEST
  34. FileOpen $0 "$INSTDIR\.zcode-install-manifest" w
  35. FileWrite $0 "ZCode.exe$\r$\n"
  36. FileWrite $0 "owned-by-zcode.txt$\r$\n"
  37. !ifdef SELF_MANIFEST
  38. FileWrite $0 ".zcode-install-manifest$\r$\n"
  39. !endif
  40. FileWrite $0 "..\user-data.txt$\r$\n"
  41. FileClose $0
  42. !endif
  43. !ifdef FAIL_DELETE
  44. ; Keep the file handle open so Windows denies the manifest cleanup delete.
  45. FileOpen $0 "$INSTDIR\owned-by-zcode.txt" w
  46. FileWrite $0 "old app payload"
  47. !else
  48. FileOpen $0 "$INSTDIR\owned-by-zcode.txt" w
  49. FileWrite $0 "old app payload"
  50. FileClose $0
  51. !endif
  52. FileOpen $0 "$INSTDIR\user-data.txt" w
  53. FileWrite $0 "keep me"
  54. FileClose $0
  55. !ifdef RECOVERY_TEST
  56. InitPluginsDir
  57. File /oname=$PLUGINSDIR\old-cleanup.exe "${OLD_CLEANUP_EXE}"
  58. StrCpy $installationDir "$INSTDIR"
  59. !ifdef BACKUP_FAILURE
  60. ; 独占源文件,让 CopyFileW 确定失败;原安装文件不能被旧卸载器删除。
  61. System::Call 'kernel32::CreateFileW(w "$INSTDIR\ZCode.exe", i 0x80000000, i 0, p 0, i 3, i 0, p 0) p.R8'
  62. !endif
  63. !insertmacro customOldUninstallPrepare
  64. FileOpen $8 "$INSTDIR\backup-dir.txt" w
  65. FileWrite $8 "$ZCodeCleanupBackup"
  66. FileClose $8
  67. OneMoreAttempt:
  68. ExecWait '"$PLUGINSDIR\old-cleanup.exe" /S /D=$INSTDIR' $R0
  69. !ifdef RESTORE_FAILURE
  70. ; 同名目录阻止恢复主程序,必须保留备份,不能误报恢复成功。
  71. CreateDirectory "$INSTDIR\ZCode.exe"
  72. !endif
  73. ${If} $R0 != 0
  74. !insertmacro customOldUninstallRetryFailed
  75. ${EndIf}
  76. !insertmacro customUnInstallCheck
  77. !else
  78. !insertmacro customRemoveFiles
  79. !endif
  80. ; 成功清理后模拟新版本文件落盘;失败路径必须在此前返回。
  81. SetOutPath "$INSTDIR"
  82. File /oname=ZCode.exe "${FIXTURE_EXE}"
  83. IfFileExists "$INSTDIR\owned-by-zcode.txt" 0 zcodeOwnedRemoved
  84. StrCpy $1 "present"
  85. Goto zcodeWriteMarker
  86. zcodeOwnedRemoved:
  87. StrCpy $1 "removed"
  88. zcodeWriteMarker:
  89. FileOpen $0 "$INSTDIR\remove-result.txt" w
  90. FileWrite $0 "$1"
  91. FileClose $0
  92. SectionEnd