Archive: Shared functions?


Shared functions?
  What's the easiest way to write a function which is called in both the installer and uninstaller sections without just duplicating the code?


This is not possible, because when writing the uninstaller, the installer must exactly know what to include in the uninstaller. That's why every function in uninstaller should start with 'un.'

So just duplicate code.

Greetz, Hendri.


If you really are desperate, you could probaly use includes by wrapping them around Function...FunctionEnd's. But then you would have to have an include for each function.


Or you could use macro instead of includes.


I never thought about macros (I guess cause I never use them). But I guess this would be the perfect case for when you need to use them.

Rob


Using macros
  Hi!

I tried to use macros. But afterwards makensis (1.95) crashed.
Maybe the script was too long ?

So I did use an external filter program to duplicte on file. Both files are included afterwards.
I used inst.function for the template and changed "inst." to "un." by my filter program.

e.g Master script
script1.nsi
!include inc1.nsi
!include inc1u.nsi
.
.
e.g. include script "inc1.nsi"
Function Inst.function1
.
.
.
FunctionEnd

filtering:
ffilt32 inc1.nsi inc1u.nsi -se "inst." -ch "un."

Please use the attached filter program.

Greetings,
Andreas


Thanks.
  Yeah, I know how each section needs to be different, I've just been cleaning up my scripts and trying to make some things "nicer." Figured since I'm doing the exact same thing in some places I'd try to extract it and was wondering if there was a simple way to do it. Didn't notice there was the !macro section. I suppose it doesn't help in the sense of having a shared function, it'll just make it easier to duplicate the code in one place.

Thanks for all the suggestions, I'll use the macro for now because not only is it something that I'll be sharing between the installer and uninstaller, I'll be sharing it across 9 scripts, so I'll put it in my common script I'm including in them all.

Thanks for the suggestions.


Macros do help. See code snippet. Is there somewhere a place where we can collect includes? NSIS Projecthouse doesn't seem to be ready now.

-Fritz

...


!
macro _util.both
>;
;Usage:
;
; call getNumColors
>; pop $0
>; MessageBox MB_OK "Number of Bits per Pixel: $0"
>;
Function ${_UTIL_UNDOT}getNumColors
push "getScreenColors"
call ${_UTIL_UNDOT}_runDLL
FunctionEnd

>;
;Usage:
;
; call getUserName
>; pop $0
>; MessageBox MB_OK "Current user: $0"
>;
Function ${_UTIL_UNDOT}getUserName
push "getUserName"
call ${_UTIL_UNDOT}_runDLL
FunctionEnd

>;
;Usage:
;
; call getPID
>; pop $0
>; MessageBox MB_OK "Current process ID: $0"
>;
Function ${_UTIL_UNDOT}getPID
push "getPID"
call ${_UTIL_UNDOT}_runDLL
FunctionEnd

>Function ${_UTIL_UNDOT}_removeDLL
Delete "$TEMP\NSISutil.dll"
>FunctionEnd

>Function ${_UTIL_UNDOT}_runDLL
IfFileExists"$TEMP\NSISutil.dll" ret
File "/oname=$TEMP\NSISutil.dll" "${INCLUDES}\NSISutil.dll"
!define _INIT_FUNC "${_UTIL_UNDOT}_removeDLL"
push "${_UTIL_UNDOT}onUserAbort"
!insertmacro ${_UTIL_UNDOT}multicallback.register_callback
!define _INIT_FUNC "${_UTIL_UNDOT}_removeDLL"
push "${_UTIL_UNDOT}on${_UTIL_INST}Failed"
!insertmacro ${_UTIL_UNDOT}multicallback.register_callback
!define _INIT_FUNC "${_UTIL_UNDOT}_removeDLL"
push "${_UTIL_UNDOT}on${_UTIL_INST}Success"
!insertmacro ${_UTIL_UNDOT}multicallback.register_callback
ret:
exch $R0
CallInstDLL"$TEMP\NSISutil.dll" $R0
exch
pop $R0
FunctionEnd

>!undef _UTIL_UNDOT
>!undef _UTIL_INST
>!macroend ; _util.both

>;
;Duplicate the whole stuff with and without "un." prefix
>;
!define _UTIL_UNDOT ""
>!define _UTIL_INST "Inst"
>!insertmacro _util.both

>!ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
>!define _UTIL_UNDOT "un."
>!define _UTIL_INST "Uninst"
>!insertmacro _util.both
>!endif
...