简体   繁体   English

Electron-builder (NSIS): 重装时不询问删除数据(无法区分重装和卸载)

[英]Electron-builder (NSIS): do not ask about deleting data on reinstall (cannot distinguish reinstall and uninstall)

I use Electron-builder (of version 22.14.13) to create installer for our app (Windows).我使用 Electron-builder(版本 22.14.13)为我们的应用程序 (Windows) 创建安装程序。

I needed to create custom installer, so when user uninstalls the app the dialog appears if he wants to remove app data either or not.我需要创建自定义安装程序,因此当用户卸载应用程序时,无论他是否想删除应用程序数据,都会出现对话框。

The problem was that in case when user reinstalls app (installing over existing one) he shouldn't be asked about that and app data should be saved.问题在于,万一用户重新安装应用程序(安装现有应用程序)时,不应询问他并且应保存应用程序数据。 Based on answers of people with similar issues ( #5633 , #4141 ) I've come to the next solution.根据有类似问题的人的回答( #5633#4141 ),我找到了下一个解决方案。 I customized macros with following script:我使用以下脚本自定义宏:

!macro customInit
  #If app is installed over previous version we shouldn't ask whether to delete app data or not
  StrCpy $1 "install"
!macroend

!macro customUnInit
  SetSilent normal
!macroend

!macro customUnInstall
  ${if} $1 == "install"
    Goto done
  ${EndIf}

  MessageBox MB_YESNO "Delete application data?" \
    /SD IDNO IDNO Skipped IDYES Accepted

  Accepted:
    !ifdef APP_PRODUCT_FILENAME
      RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
    !endif
    Goto done
  Skipped:
    Goto done
  done:
!macroend

So we use a variable to distinguish when app is reinstalled and when it's uninstalled.所以我们用一个变量来区分app什么时候重装,什么时候卸载。 It was working until we updated electron-builder (previous version 22.11.7) and electron (previous version was 13.1.6).在我们更新 electron-builder(以前的版本 22.11.7)和 electron(以前的版本是 13.1.6)之前,它一直在工作。 The problem now is that variable doesn't store value "install" on reinstall and the dialog always appears, on reinstall too.现在的问题是变量在重新安装时不存储值“install”,并且对话框总是出现,在重新安装时也是如此。 Any help would be great.任何帮助都会很棒。

The problem was solved with a flag isUpdated which is mentioned in documentation.该问题已通过文档中提到的标志isUpdated解决。 ( https://www.electron.build/configuration/nsis.html ) ( https://www.electron.build/configuration/nsis.html )

So updated script looks like this:所以更新后的脚本如下所示:

!macro customUnInstall
  # when App is updated we want to preserve user data by default
  ${if} ${isUpdated}
    Goto done
  ${endIf}


  MessageBox MB_YESNO "Delete application data?" \
    /SD IDNO IDNO Skipped IDYES Accepted

  Accepted:
    !ifdef APP_PRODUCT_FILENAME
      RMDir /r "$APPDATA\${APP_PRODUCT_FILENAME}"
    !endif
    Goto done
  Skipped:
    Goto done
  done:
!macroend

So simple.很简单。 I tried to use it some time ago and by some reason it wasn't working.前段时间我尝试使用它,但由于某种原因它无法正常工作。 Not it works and we don't need to use variable!不行,我们不需要使用变量!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM