Debian Codegraph @

script/windows-installer/inno-setup-git-lfs-installer.iss

raw
1 #define MyAppName "Git LFS"
2
3 #define PathToX86Binary "..\..\git-lfs-x86.exe"
4 #ifnexist PathToX86Binary
5 #pragma error PathToX86Binary + " does not exist, please build it first."
6 #endif
7
8 #define PathToX64Binary "..\..\git-lfs-x64.exe"
9 #ifnexist PathToX64Binary
10 #pragma error PathToX64Binary + " does not exist, please build it first."
11 #endif
12
13 #define PathToARM64Binary "..\..\git-lfs-arm64.exe"
14 #ifnexist PathToARM64Binary
15 #pragma error PathToARM64Binary + " does not exist, please build it first."
16 #endif
17
18 ; Arbitrarily choose the x86 executable here as both have the version embedded.
19 #define MyVersionInfoVersion GetFileVersion(PathToX86Binary)
20
21 ; Misuse RemoveFileExt to strip the 4th patch-level version number.
22 #define MyAppVersion RemoveFileExt(MyVersionInfoVersion)
23
24 #define MyAppPublisher "GitHub, Inc."
25 #define MyAppURL "https://git-lfs.github.com/"
26 #define MyAppFilePrefix "git-lfs-windows"
27
28 [Setup]
29 ; NOTE: The value of AppId uniquely identifies this application.
30 ; Do not use the same AppId value in installers for other applications.
31 ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
32 AppCopyright=GitHub, Inc. and Git LFS contributors
33 AppId={{286391DE-F778-44EA-9375-1B21AAA04FF0}
34 AppName={#MyAppName}
35 AppPublisher={#MyAppPublisher}
36 AppPublisherURL={#MyAppURL}
37 AppSupportURL={#MyAppURL}
38 AppUpdatesURL={#MyAppURL}
39 AppVersion={#MyAppVersion}
40 ArchitecturesInstallIn64BitMode=x64 arm64
41 ChangesEnvironment=yes
42 Compression=lzma
43 DefaultDirName={code:GetDefaultDirName}
44 DirExistsWarning=no
45 DisableReadyPage=True
46 LicenseFile=..\..\LICENSE.md
47 OutputBaseFilename={#MyAppFilePrefix}-{#MyAppVersion}
48 OutputDir=..\..\
49 PrivilegesRequired=none
50 SetupIconFile=git-lfs-logo.ico
51 SolidCompression=yes
52 UninstallDisplayIcon={app}\git-lfs.exe
53 UsePreviousAppDir=no
54 VersionInfoVersion={#MyVersionInfoVersion}
55 WizardImageFile=git-lfs-wizard-image.bmp
56 WizardSmallImageFile=git-lfs-logo.bmp
57
58 [Languages]
59 Name: "english"; MessagesFile: "compiler:Default.isl"
60
61 [Files]
62 Source: {#PathToX86Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX86
63 Source: {#PathToX64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsX64
64 Source: {#PathToARM64Binary}; DestDir: "{app}"; Flags: ignoreversion; DestName: "git-lfs.exe"; AfterInstall: InstallGitLFS; Check: IsARM64
65
66 [Registry]
67 Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{app}"; Check: IsAdminLoggedOn and NeedsAddPath('{app}')
68 Root: HKLM; Subkey: "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"; ValueType: string; ValueName: "GIT_LFS_PATH"; ValueData: "{app}"; Check: IsAdminLoggedOn
69 Root: HKCU; Subkey: "Environment"; ValueType: expandsz; ValueName: "Path"; ValueData: "{olddata};{app}"; Check: (not IsAdminLoggedOn) and NeedsAddPath('{app}')
70 Root: HKCU; Subkey: "Environment"; ValueType: string; ValueName: "GIT_LFS_PATH"; ValueData: "{app}"; Check: not IsAdminLoggedOn
71
72 [Code]
73 function GetDefaultDirName(Dummy: string): string;
74 begin
75 if IsAdminLoggedOn then begin
76 Result:=ExpandConstant('{pf}\{#MyAppName}');
77 end else begin
78 Result:=ExpandConstant('{userpf}\{#MyAppName}');
79 end;
80 end;
81
82 // Checks to see if we need to add the dir to the env PATH variable.
83 function NeedsAddPath(Param: string): boolean;
84 var
85 OrigPath: string;
86 ParamExpanded: string;
87 begin
88 //expand the setup constants like {app} from Param
89 ParamExpanded := ExpandConstant(Param);
90 if not RegQueryStringValue(HKEY_LOCAL_MACHINE,
91 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',
92 'Path', OrigPath)
93 then begin
94 Result := True;
95 exit;
96 end;
97 // look for the path with leading and trailing semicolon and with or without \ ending
98 // Pos() returns 0 if not found
99 Result := Pos(';' + UpperCase(ParamExpanded) + ';', ';' + UpperCase(OrigPath) + ';') = 0;
100 if Result = True then
101 Result := Pos(';' + UpperCase(ParamExpanded) + '\;', ';' + UpperCase(OrigPath) + ';') = 0;
102 end;
103
104 function SetEnvironmentVariable(lpName,lpValue:String):Boolean;
105 external '[email protected] stdcall delayload';
106
107 // When Git for Windows is installed with the PATH option "Bash only", i.e.
108 // _without_ adding anything to the global `PATH`, we will not find `git.exe`
109 // there.
110 //
111 // Detect that situation and add `<Git>\cmd` to the `PATH` so that we find it
112 // when registering Git LFS later.
113 function AddGitForWindowsCMDToPATHIfNeeded: boolean;
114 var
115 Domain: Integer;
116 Key, PathOption, AppPath, Path: string;
117 begin
118 Result := False;
119
120 Key := 'Microsoft\Windows\CurrentVersion\Uninstall\Git_is1';
121 if RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\Wow6432Node\' + Key) then begin
122 Domain := HKEY_LOCAL_MACHINE;
123 Key := 'Software\Wow6432Node\' + Key;
124 end else if RegKeyExists(HKEY_CURRENT_USER, 'Software\Wow6432Node\' + Key) then begin
125 Domain := HKEY_CURRENT_USER;
126 Key := 'Software\Wow6432Node\' + Key;
127 end else if RegKeyExists(HKEY_LOCAL_MACHINE, 'Software\' + Key) then begin
128 Domain := HKEY_LOCAL_MACHINE;
129 Key := 'Software\' + Key;
130 end else if RegKeyExists(HKEY_CURRENT_USER, 'Software\' + Key) then begin
131 Domain := HKEY_CURRENT_USER;
132 Key := 'Software\' + Key;
133 end else
134 Exit;
135
136 if (not RegQueryStringValue(Domain, Key, 'Inno Setup CodeFile: Path Option', PathOption)) or
137 (PathOption <> 'BashOnly') or
138 (not RegQueryStringValue(Domain, Key, 'Inno Setup: App Path', AppPath)) or
139 (not FileExists(AppPath + '\cmd\git.exe'))
140 then
141 Exit;
142
143 // Extend PATH so that it finds `git.exe`
144 Path := GetEnv('PATH');
145 if Path = '' then
146 Path := AppPath + '\cmd'
147 else
148 Path := AppPath + '\cmd;' + Path;
149
150 SetEnvironmentVariable('PATH', Path);
151 Result := True;
152 end;
153
154 // Verify that a Git executable is found in the PATH, and if it does not
155 // reside in either 'C:\Program Files' or 'C:\Program Files (x86)', warn
156 // the user in case it is not the Git installation they expected.
157 function GitFoundInPath(): boolean;
158 var
159 PFiles32,PFiles64: string;
160 PathEnv,Path: string;
161 PathExt,Ext: string;
162 i,j: integer;
163 RegisterOrDeregister: string;
164 begin
165 if IsUninstaller then
166 RegisterOrDeregister := 'deregister'
167 else
168 RegisterOrDeregister := 'register';
169
170 PFiles32 := AnsiLowercase(ExpandConstant('{commonpf32}\'))
171 if IsWin64 then
172 PFiles64 := AnsiLowercase(ExpandConstant('{commonpf64}\'))
173 else
174 PFiles64 := PFiles32; // `commonpf64` is not available on 32-bit Windows
175
176 PathEnv := GetEnv('PATH') + ';';
177 repeat
178 i := Pos(';', PathEnv);
179 Path := Copy(PathEnv, 1, i-1) + '\git';
180 PathEnv := Copy(PathEnv, i+1, Length(PathEnv)-i);
181
182 PathExt := AnsiLowercase(GetEnv('PATHEXT')) + ';';
183 repeat
184 j := Pos(';', PathExt);
185 Ext := Copy(PathExt, 1, j-1);
186 PathExt := Copy(PathExt, j+1, Length(PathExt)-j);
187
188 if FileExists(Path + Ext) then begin
189 if (Pos(PFiles32, AnsiLowercase(Path)) = 1) or (Pos(PFiles64, AnsiLowercase(Path)) = 1) then begin
190 Result := True;
191 Exit;
192 end;
193 Log('Warning: Found Git in unexpected location: "' + Path + Ext + '"');
194 Result := (SuppressibleMsgBox(
195 'An executable Git program was found in an unexpected location outside of Program Files:' + #13+#10 +
196 ' "' + Path + Ext + '"' + #13+#10 +
197 'If this looks dubious, Git LFS should not be ' + RegisterOrDeregister + 'ed using it.' + #13+#10 + #13+#10 +
198 'Do you want to ' + RegisterOrDeregister + ' Git LFS using this Git program?',
199 mbConfirmation, MB_YESNO, IDNO) = IDYES);
200 if Result then
201 Log('Using Git found at: "' + Path + Ext + '"')
202 else
203 Log('Refusing to use Git found at: "' + Path + Ext + '"');
204 Exit;
205 end;
206 until Result or (PathExt = '');
207 until Result or (PathEnv = '');
208
209 if AddGitForWindowsCMDToPATHIfNeeded
210 then
211 Result := True
212 else
213 SuppressibleMsgBox(
214 'Could not find Git; can not ' + RegisterOrDeregister + ' Git LFS.', mbError, MB_OK, IDOK);
215 end;
216
217 // Runs the lfs initialization.
218 procedure InstallGitLFS();
219 var
220 ResultCode: integer;
221 begin
222 Exec(
223 ExpandConstant('{cmd}'),
224 ExpandConstant('/C ""{app}\git-lfs.exe" install"'),
225 '', SW_HIDE, ewWaitUntilTerminated, ResultCode
226 );
227 if not ResultCode = 1 then
228 MsgBox(
229 'Git LFS was not able to automatically initialize itself. ' +
230 'Please run "git lfs install" from the commandline.', mbInformation, MB_OK);
231 end;
232
233 // Event function automatically called when installing:
234 function InitializeSetup(): Boolean;
235 begin
236 Result := GitFoundInPath();
237 end;
238
239 // Event function automatically called when uninstalling:
240 function InitializeUninstall(): Boolean;
241 var
242 ResultCode: integer;
243 begin
244 Result := False;
245
246 if GitFoundInPath() then begin
247 Exec(
248 ExpandConstant('{cmd}'),
249 ExpandConstant('/C ""{app}\git-lfs.exe" uninstall"'),
250 '', SW_HIDE, ewWaitUntilTerminated, ResultCode
251 );
252 Result := True;
253 end;
254 end;