]> git.evergreen-ils.org Git - working/Hatch.git/blob - installer/windows/hatch.nsi
LP#1733692 Bumping Hatch versions to 0.1.3
[working/Hatch.git] / installer / windows / hatch.nsi
1 ;-----------------------------------------------------------------------
2 ; NSIS Installation Script                                              ;
3 ; Kyle Huckins                                                          ;
4 ; khuckins@catalystdevworks.com                                         ;
5 ; This will be a guide to working with NSIS to create an installer.     ;
6 ; It is a heavily commented practice installer, basically.              ;
7 ;                                                                       ;
8 ; Comments are designated by # and ;                                    ;
9 ; # tends to be at the beginning of a line, while ; is                  ;
10 ; primarilly used as end-of-line comments.  However it's                ;
11 ; common to use just ; for comments as well.                            ;
12 ;                                                                       ;
13 ;------------------------------------------------------------------------
14
15 ;==================
16 ; Basic Information
17 ;--------
18 ; Includes
19 !include "MUI2.nsh" ;This enables the use of ModernUI, and must go at the top
20 !include "x64.nsh"
21 !include "defines.nsh" ;Definitions for our variables
22 !include "LogicLib.nsh" ;Sparsly documented library, but necessary for any real logic
23 !include "nsDialogs.nsh" ;Need this to create custom pages
24 !include StrRep.nsh
25 !include ReplaceInFile.nsh
26 ;---------------------------------------------------------------
27
28 ; Installer's filename
29 Outfile "${APPNAME} Installer.exe"
30 RequestExecutionLevel admin
31
32
33 ;-----------------
34 ; Titlebar Content
35 Name "${APPNAME}"
36
37 ;==================================
38 ; Page system
39 !insertmacro MUI_PAGE_WELCOME
40 !define MUI_PAGE_CUSTOMFUNCTION_PRE ValidateInstall
41 !insertmacro MUI_PAGE_LICENSE "license.rtf" ;Loads licence.rtf to show license content
42 !insertmacro MUI_PAGE_DIRECTORY
43 !insertmacro MUI_PAGE_INSTFILES
44 !insertmacro MUI_PAGE_FINISH
45
46 !insertmacro MUI_LANGUAGE "English"
47
48 ;-------------------------------------
49 ; Code to verify if a user is an admin
50 !macro VerifyUserIsAdmin
51 UserInfo::GetAccountType
52 pop $0
53 ${If} $0 != "admin" ;Require admin rights
54     messageBox MB_OK|MB_ICONSTOP "Administrator rights required!"
55     setErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
56     quit
57 ${EndIf}
58 !macroend
59
60 ; Find any installed JRE/JDK and return the version or -1
61 Function DetectJava
62     ; 32 bit JRE >= 9
63     ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\JRE" "CurrentVersion"
64     StrCmp $0 "" +1 Found
65
66     ; 32 bit JDK >= 9
67     ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\JDK" "CurrentVersion"
68     StrCmp $0 "" +1 Found
69
70     ; 64 bit JRE >= 9
71     ${If} ${RunningX64}
72         SetRegView 64
73         ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\JRE" "CurrentVersion"
74         SetRegView 32 ; basically SetRegView Default since NSIS only creates 32 bit installers
75         StrCmp $0 "" +1 Found
76     ${EndIf}
77
78     ; 64 bit JDK >= 9
79     ${If} ${RunningX64}
80         SetRegView 64
81         ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\JDK" "CurrentVersion"
82         SetRegView 32
83         StrCmp $0 "" +1 Found
84     ${EndIf}
85
86     ; 32 bit JRE < 9
87     ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
88     StrCmp $0 "" +1 Found
89
90     ; 32 bit JDK < 9
91     ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"
92     StrCmp $0 "" +1 Found
93
94     ; 64 bit JRE < 9
95     ${If} ${RunningX64}
96         SetRegView 64
97         ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
98         SetRegView 32
99         StrCmp $0 "" +1 Found
100     ${EndIf}
101
102     ; 64 bit JDK < 9
103     ${If} ${RunningX64}
104         SetRegView 64
105         ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"
106         SetRegView 32
107         StrCmp $0 "" +1 Found
108     ${EndIf}
109
110     ; Nuthin.
111     Push "-1"
112     Return
113
114     Found:
115     Push $0
116     Return
117 FunctionEnd
118
119 function .onInit
120     setShellVarContext all
121     !insertmacro VerifyUserIsAdmin
122     Return
123 functionEnd
124
125 ;-----------------
126 ;Check our version
127 function ValidateInstall
128     Call DetectJava
129     Pop $0
130     StrCmp $0 "-1" NoJava
131     ${If} $0 S< ${JRE_MIN_VERSION}
132         MessageBox MB_OK "Please update Java to version ${JRE_MIN_VERSION} or higher. Setup will now exit." /SD IDOK IDOK QUIT
133     ${EndIf}
134     ; Hatch Version Check
135     ReadRegStr $1 HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Version"
136     StrCmp $1 "" INSTALL
137     ${If} $1 S== ${FULLVERSION} ;Same version is installed
138         MessageBox MB_OKCANCEL|MB_ICONINFORMATION "You already have this version of ${APPNAME} installed.  You must uninstall the currently installed version to continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
139     ${ElseIf} $1 S> ${FULLVERSION} ;Older version is installed
140         MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "You are tring to install an older version of ${APPNAME} than the one you currently have installed. You must uninstall the currently installed verion to continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
141     ${ElseIf} $1 S< ${FULLVERSION} ;Newer version is installed
142         MessageBox MB_OKCANCEL|MB_ICONINFORMATION "You have a previous version of ${APPNAME} installed. The previous version will be uninrtalled so installation can continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
143     ${EndIf}
144     UNINSTALL:
145         ReadRegStr $1 HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Install Path"
146         ; Manually copying the uninstaller like this allows the use of the _? param which is required for ExecWait while also allowing all files to be deleted.
147         CopyFiles /SILENT "$1\Uninstall ${APPNAME}.exe" "$TEMP\Uninstall ${APPNAME}.exe"
148         ExecWait '"$TEMP\Uninstall ${APPNAME}.exe" /S _?=$1'
149         Delete /REBOOTOK "$TEMP\Uninstall ${APPNAME}.exe"
150         Goto INSTALL
151     NoJava:
152         MessageBox MB_OK|MB_ICONSTOP "Java Not Detected. Please install a JRE of version ${JRE_MIN_VERSION} or greater." /SD IDOK
153     QUIT:
154         Quit
155     INSTALL:
156         Return
157 functionEnd
158
159 ;--------------------------
160 ; Where our install files go
161 InstallDir "$PROGRAMFILES\${APPNAME}" ;This is where our variables come in.
162 ;----------------------------------------------
163
164 section "install"
165     ; Install directory files - keep these in the same directory
166     ; as the script before compiling.
167     SetOutPath $INSTDIR\ ;Sets output path to our InstallDir
168     File /r ..\..\lib
169     File /r ..\..\extension
170     File ..\..\hatch.bat
171     File ..\..\hatch.properties
172     File ..\..\logging.properties
173     
174     ; Set path variable in org.ils_evergreen.hatch.json to $INSTDIR\hatch.bat
175     ${StrRep} '$0' '$INSTDIR' '\' '\\'
176     !insertmacro _ReplaceInFile  "$INSTDIR\extension\host\org.evergreen_ils.hatch.json" "/path/to/hatch.sh" "$0\\hatch.bat"
177
178     ; Uninstaller
179     writeUninstaller "$INSTDIR\Uninstall ${APPNAME}.exe"
180
181     ; Registry info for Add/Remove Programs
182     WriteRegStr HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Install Path" $INSTDIR
183     WriteRegStr HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Version" "${FULLVERSION}"
184     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${COMPANYNAME} - ${APPNAME} - ${DESCRIPTION}"
185     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$INSTDIR\Uninstall ${APPNAME}.exe"
186     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "QuietUninstallString" "$INSTDIR\Uninstall ${APPNAME}.exe /S"
187     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "InstallLocation" "$INSTDIR"
188     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayIcon" "$INSTDIR\logo.ico"
189     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "Publisher" "${COMPANYNAME}"
190     StrCmp "${HELPURL}" "" +2
191     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "HelpLink" "${HELPURL}"
192     StrCmp "${UPDATEURL}" "" +2
193     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "URLUpdateInfo" "${UPDATEURL}"
194     StrCmp "${ABOUTURL}" "" +2
195     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "URLInfoAbout" "${ABOUTURL}"
196     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayVersion" "${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}"
197     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "VersionMajor" ${VERSIONMAJOR}
198     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "VersionMinor" ${VERSIONMINOR}
199     # There is no option for modifying or repairing the install
200     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoModify" 1
201     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoRepair" 1
202     # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
203     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "EstimatedSize" ${INSTALLSIZE}
204
205     ; Connect Hatch to Chrome and install the Hatch extension from the Chrome Web Store
206     WriteRegStr HKLM "SOFTWARE\Google\Chrome\NativeMessagingHosts\org.evergreen_ils.hatch" "" "$INSTDIR\extension\host\org.evergreen_ils.hatch.json"
207     WriteRegStr HKLM "Software\Google\Chrome\Extensions\${EXTENSIONID}" "update_url" "${EXTENSION_UPDATEURL}"
208 SectionEnd
209
210
211 #############################
212 # Uninstaller code
213
214 function un.onInit
215     SetShellVarContext all
216     
217     # Verify uninstaller
218     MessageBox MB_OKCANCEL "Permanently remove ${APPNAME}?" /SD IDOK IDOK next
219         Abort
220     next:
221     !insertmacro VerifyUserIsAdmin
222 functionEnd
223
224 section "uninstall"    
225     # Remove the actual files
226     Delete /REBOOTOK "$INSTDIR\hatch.bat"
227     Delete /REBOOTOK "$INSTDIR\hatch.properties"
228     Delete /REBOOTOK "$INSTDIR\logging.properties"
229     ; blindly using /r isn't ideal but the extreme unlikelyhood of there being \lib or \extension folders under $PROGRAMFILES makes it low risk.
230     RmDir /r /REBOOTOK "$INSTDIR\extension"
231     RmDir /r /REBOOTOK "$INSTDIR\lib"
232     # Delete uninstaller last
233     Delete /REBOOTOK "$INSTDIR\Uninstall ${APPNAME}.exe"
234     
235     # Remove installation directory
236     RmDir /REBOOTOK "$INSTDIR"
237     
238     # Remove uninstaller info from registry
239     DeleteRegKey HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}"
240     DeleteRegKey HKLM "SOFTWARE\${COMPANYNAME}"
241     DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
242     DeleteRegKey HKLM "SOFTWARE\Google\Chrome\NativeMessagingHosts\org.evergreen_ils.hatch"
243     DeleteRegKey HKLM "SOFTWARE\Google\Chrome\Extensions\${EXTENSIONID}"
244     ${If} ${RunningX64}
245         DeleteRegKey HKLM "SOFTWARE\Wow6432Node\Google\Chrome\Extensions\${EXTENSIONID}"
246     ${EndIf}
247
248     IfRebootFlag 0 Done
249     MessageBox MB_YESNO "A reboot is required to finish the installation. Do you wish to reboot now?" /SD IDNO IDNO Done
250     Reboot
251
252     Done:
253     
254 sectionEnd