避免在NSIS中重复的功能代码

时间:2011-11-28 06:02:56

标签: nsis

目前,我有以下脚本代码。

Section "Uninstall"
...
...
Call un.DeleteDirIfEmpty 
SectionEnd


Function GetJRE
    ; Call must not be used with functions starting with "un." in the non-uninstall sections.
    Call
FunctionEnd


Function un.DeleteDirIfEmpty
...
...
FunctionEnd

Function DeleteDirIfEmpty
...
...
FunctionEnd

请注意,我需要提供2个版本的DeleteDirIfEmpty,以便在非卸载部分和卸载部分中执行相同的操作。

他们的代码是一样的,只是命名不同。 un.DeleteDirIfEmptyDeleteDirIfEmpty

如何只有1个函数,但是可以被任何部分调用吗?

2 个答案:

答案 0 :(得分:5)

看看\ Include \ Util.nsh,它用于将宏转换为函数:

!include Util.nsh

!macro MyFunction
MessageBox mb_ok "Hello World"
!macroend
!define MyFunction "${CallArtificialFunction} MyFunction"

Section
${MyFunction}
SectionEnd

注意:要删除空目录,只需使用RMDir(无/ r开关)

答案 1 :(得分:0)

link也让我明白了。它提供了一个示例,说明如何在安装程序和卸载程序之间共享功能。比如,你有一个应该被共享的函数myfunc,然后你创建一个宏来从卸载程序中调用它。引用链接:

; Name of our installer.
Name "Function Sharing Example"
OutFile "FunctionShareExample.exe"
InstallDir "$PROGRAMFILES\Function Sharing Example\"

; We need some pages.
Page directory
Page instfiles
; And uninstaller pages.
UninstPage uninstconfirm
UninstPage instfiles

; Show the details.
ShowInstDetails show
ShowUninstDetails show

; ******************* The shared function. *******************
!macro MYMACRO un
  Function ${un}myfunc
    MessageBox MB_OK "This is the function ${un}myfunc."
    DetailPrint "Very ${un}funny text."
    DetailPrint "More ${un}funny text."
  FunctionEnd
!macroend

; Insert function as an installer and uninstaller function.
!insertmacro MYMACRO ""
!insertmacro MYMACRO "un."

Section "Install"
  ; ******************* Call the installer function. *******************
  Call myfunc

  SetOutPath "$INSTDIR"
  ; Write an uninstaller.
  WriteUninstaller "$INSTDIR\uninstall.exe"
  ShowWindow $HWNDPARENT 6
  ; Show the install directory, so you can run the uninstaller straight away.
  ExecShell open "$INSTDIR"
  Sleep 1000
  ShowWindow $HWNDPARENT 9
SectionEnd

Section "Uninstall"
  ; ******************* Call the un.installer function. *******************
  Call un.myfunc

  ; Clean up install directory (delete it).
  Delete "$INSTDIR\uninstall.exe"
  RMDir "$INSTDIR"
SectionEnd