Archive: Code Architecture


Code Architecture
Hello everyone,

I am starting to reorganize some code, and I want to structure it in a clean way. I am trying to get rid of repeated code, as well as modularize my code. So, I would like to see if you could guide me in two things I am trying to structure better.

1) There are two functions that do exactly the same, but one is for the installer, and the other is for the uninstaller. I have tried to create a subfunction for both, but the compiler yells at me. How do you usually avoid duplicating code that is in the installer and uninstaller?

2) My main script mixes the GUI and installation code (driver installation) into a single script. In order to organize it, I would like to pass the driver installation functions into its own file. I am looking something like compiling and linking, or something similar. Does that concept exist in NSIS? what people usually do to organize their scripts?

Thanks,


1) Use macros or CallArtificialFunction (util.nsh)


!macro and !include. To include the same function in the installer and uninstaller, put it in a macro that takes one argument like so:

!macro MyFunction Un
Function ${Un}MyFunction
...
FunctionEnd
!macroend

!insertmacro MyFunction `` # for installer
!insertmacro MyFunction `un.` # for uninstaller

Edit: Or what Anders said :)

Stu


Great Anders and Afrow UK. I will see which approach to take.

About question 2), does people usually link compiled code; or they just drop functions into headers files. What usually people do to break apart (architect) their main script?

Thanks,


You can't link compiled code. You can only !include .nsh and .nsi files. If you must, you could however do File YourFolder\YourCompiledExe.exe and then ExecWait that exe. But... It kind of makes zero sense to do that with an NSIS executable.


MSG, thank you very much for the clarification.

So I am just wondering what is the whole purpose of including nsi files. I could dump all my implementation details into an nsh file and include it. Or, do people separate implementation from interface using nsh and nsi files and then include both into their main script? how do you usually organize your installer architecture?

Thanks


Afrow UK, I followed your example. For some reason when compiling the macro, the compiler yells at me. I have attached the code and error below.

When I get rid of the Function... and FunctionEnd lines. It just compiles fine. Am I missing something?

Thanks,

Code:
!macro cleanFileSystem Un
Function ${Un}cleanFiles
....
....
FunctionEnd
!macroend

Error:
Error in macro cleanFileSystem on macroline 1