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