Monday, March 11, 2019

Simple Windows Installer

For windows system, is there any possibilities of creating a packaged installer like DEB in Ubuntu system? I have carried out some research and studies to find out the solution for this. Although there are possibilities to create a packages exetutable installers using different tools. After thorough comparison and reviews I got to conclusion that NSIS (Nullsoft Scriptable Install System) provides the best solution for that. It is really simple, everything is scripts, also very scalabale and powerful.


So, lets get started!

I create a basic installer using some files and scripts as resources. Please note that there are many possibilities we could add more functionalities. This article describes a very basic example on how we can create a simple installable packaged executable which can be deployed for installation.

We just write scripts into a file with extension *.nsi and installer is created from the scripts defined in the file. There are possibilities to create shortcuts, copy resources, define UI and many more. 

The first step to begin with is to install NSIS software which can be found at

https://sourceforge.net/projects/nsis/

After installation, we have to install NSIS plugin for visual studio code. Then, we have advantage of autocompletion and easy compilation. 

Writing a NSI file is divied into different blocks with their specific tasks and all blocks are run sequentially(there are some exceptions like Uninstall block runs at the time of uninstalling the application.) 

1) Definition Block

This block defines different variables which we wanted to define here. We can define a variable using

!define MUI_PRODUCT "Frietec Google Service"
!define AUTHOR "paudekri@frietec.com"

2) Basic Block

The basic block is very important because we define the Name, output file and install directory where the application is supposed to be installed. For example

Name "${MUI_PRODUCT}"
OutFile "FE-Google.exe"
InstallDir "C:\Frietec\${MUI_PRODUCT}"

3) Macro Block

We can create installed without macro block, but using macro block makes further improvements such as greater looking GUI and so on. A sample macro block is 

!include MUI2.nsh
!define MUI_ICON "Resources\installer.ico"
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "German"

4) Section Block

The section block backbone where we define the processese of installation in different sections. We define scripts to copy resources or create uninstaller and define scripts and so on. 

For example:

Section "install" Installation
  # Copy scripts 
  SetOutPath $INSTDIR
  #File Scripts\Calendar.bat
  #File Scripts\Drive.bat
  #File Scripts\Email.bat
  File /r Scripts scripts
  SetOutPath $INSTDIR\resources
  File Resources\email.ico
  File Resources\drive.ico
  File Resources\calendar.ico
  File Resources\installer.ico  
  WriteUninstaller $INSTDIR\uninstall.exe
SectionEnd

Section "Shortcuts"
  CreateDirectory "$Desktop\${MUI_PRODUCT}"
  CreateShortCut "$Desktop\${MUI_PRODUCT}\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
  CreateShortCut "$Desktop\${MUI_PRODUCT}\Shortcut to Email.lnk" $INSTDIR\scripts\"Email.bat" Icon $INSTDIR\resources\"email.ico"
  CreateShortCut "$Desktop\${MUI_PRODUCT}\Shortcut to Drive.lnk" $INSTDIR\scripts\"Drive.bat" Icon $INSTDIR\resources\"drive.ico"
  CreateShortCut "$Desktop\${MUI_PRODUCT}\Shortcut to Kalendar.lnk" $INSTDIR\scripts\"Calendar.bat" Icon $INSTDIR\resources\"calendar.ico"  

  CreateDirectory "$SMPROGRAMS\${MUI_PRODUCT}"
  CreateShortCut "$SMPROGRAMS\${MUI_PRODUCT}\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
  CreateShortCut "$SMPROGRAMS\${MUI_PRODUCT}\Email.lnk" "$INSTDIR\scripts\Email.bat" Icon $INSTDIR\resources\"email.ico"
  CreateShortCut "$SMPROGRAMS\${MUI_PRODUCT}\Drive.lnk" "$INSTDIR\scripts\Drive.bat" Icon $INSTDIR\resources\"drive.ico"
  CreateShortCut "$SMPROGRAMS\${MUI_PRODUCT}\Kalendar.lnk" "$INSTDIR\scripts\Calendar.bat" Icon $INSTDIR\resources\"calendar.ico"
SectionEnd

Section "Uninstall"
  Delete $INSTDIR\uninstall.exe
  RMDir /r "$INSTDIR\*.*"
  #Delete $INSTDIR\*.*
  RMDir $INSTDIR
  
  Delete "$Desktop\${MUI_PRODUCT}\*.*"
  RMDir "$Desktop\${MUI_PRODUCT}"
  Delete "$SMPROGRAMS\${MUI_PRODUCT}\*.*"
  RMDir "$SMPROGRAMS\${MUI_PRODUCT}"
  #Delete "$Desktop\FE-Google\Shortcut to Kalendar.lnk"
SectionEnd


5) Function Block

The block block is where we define functions. A typical usage is to display messagebox when installation success or uninstallation is done.

For example:

#Function that calls a messagebox when installation finished correctly
Function .onInstSuccess
  MessageBox MB_OK "You have successfully installed ${MUI_PRODUCT}. Use the desktop icon to start the program."
FunctionEnd
  
Function un.onUninstSuccess 
  MessageBox MB_OK "Sie haben erfolgereich deinstalliert=> ${MUI_PRODUCT}."
FunctionEnd


The compilation creates an executable installer file in the same directory. We can execute this file to get installed. Similarly we can install it by clicking the unistall link in the installation directory or can be defined as a short for simplicity.

The full project is available at the following github location:

https://github.com/krishna444/MyWindowsInstallaer.git




No comments:

Post a Comment