WinGit(Inno): Make installer optionally modify PATH
[msysgit/mtrensch.git] / share / WinGit-InnoSetup / modpath.iss
blob4c1725035e7620657c98cbe68c0c1a1ee9a07966
1 // ----------------------------------------------------------------------------
2 //
3 // Inno Setup Ver: 5.2.1
4 // Script Version: 1.3.1
5 // Author: Jared Breland <jbreland@legroom.net>
6 // Homepage: http://www.legroom.net/software
7 // Modified by: Sebastian Schuberth <sschuberth@gmail.com>
8 //
9 // Script Function:
10 // Enable modification of system path directly from Inno Setup installers
12 // Instructions:
13 // Copy modpath.iss to the same directory as your setup script
15 // Add this statement to your [Setup] section
16 // ChangesEnvironment=yes
18 // Add this statement to your [Tasks] section
19 // You can change the Description or Flags, but the Name must be modifypath
20 // Name: modifypath; Description: &Add application directory to your system path; Flags: unchecked
22 // Add the following to the end of your [Code] section
23 // setArrayLength must specify the total number of dirs to be added
24 // Dir[0] contains first directory, Dir[1] contains second, etc.
25 // function ModPathDir(): TArrayOfString;
26 // var
27 // Dir: TArrayOfString;
28 // begin
29 // setArrayLength(Dir, 1)
30 // Dir[0] := ExpandConstant('{app}');
31 // Result := Dir;
32 // end;
33 // #include "modpath.iss"
34 // ----------------------------------------------------------------------------
36 procedure ModPath();
37 var
38 oldpath: String;
39 newpath: String;
40 pathArr: TArrayOfString;
41 aExecFile: String;
42 aExecArr: TArrayOfString;
43 i, d: Integer;
44 pathdir: TArrayOfString;
45 begin
46 // Get array of new directories and act on each individually
47 pathdir := ModPathDir();
48 for d := 0 to GetArrayLength(pathdir)-1 do begin
50 // Modify WinNT path
51 if UsingWinNT() = true then begin
53 // Get current path, split into an array
54 RegQueryStringValue(HKEY_CURRENT_USER, 'Environment', 'Path', oldpath);
55 oldpath := oldpath + ';';
56 i := 0;
57 while (Pos(';', oldpath) > 0) do begin
58 SetArrayLength(pathArr, i+1);
59 pathArr[i] := Copy(oldpath, 0, Pos(';', oldpath)-1);
60 oldpath := Copy(oldpath, Pos(';', oldpath)+1, Length(oldpath));
61 i := i + 1;
63 // Check if current directory matches app dir
64 if pathdir[d] = pathArr[i-1] then begin
65 // if uninstalling, remove dir from path
66 if IsUninstaller() = true then begin
67 continue;
68 // if installing, abort because dir was already in path
69 end else begin
70 abort;
71 end;
72 end;
74 // Add current directory to new path
75 if i = 1 then begin
76 newpath := pathArr[i-1];
77 end else begin
78 newpath := newpath + ';' + pathArr[i-1];
79 end;
80 end;
82 // Append app dir to path if not already included
83 if IsUninstaller() = false then
84 newpath := newpath + ';' + pathdir[d];
86 // Write new path
87 RegWriteStringValue(HKEY_CURRENT_USER, 'Environment', 'Path', newpath);
89 // Modify Win9x path
90 end else begin
92 // Convert to shortened dirname
93 pathdir[d] := GetShortName(pathdir[d]);
95 // If autoexec.bat exists, check if app dir already exists in path
96 aExecFile := 'C:\AUTOEXEC.BAT';
97 if FileExists(aExecFile) then begin
98 LoadStringsFromFile(aExecFile, aExecArr);
99 for i := 0 to GetArrayLength(aExecArr)-1 do begin
100 if IsUninstaller() = false then begin
101 // If app dir already exists while installing, abort add
102 if (Pos(pathdir[d], aExecArr[i]) > 0) then
103 abort;
104 end else begin
105 // If app dir exists and = what we originally set, then delete at uninstall
106 if aExecArr[i] = 'SET PATH=%PATH%;' + pathdir[d] then
107 aExecArr[i] := '';
108 end;
109 end;
110 end;
112 // If app dir not found, or autoexec.bat didn't exist, then (create and) append to current path
113 if IsUninstaller() = false then begin
114 SaveStringToFile(aExecFile, #13#10 + 'SET PATH=%PATH%;' + pathdir[d], True);
116 // If uninstalling, write the full autoexec out
117 end else begin
118 SaveStringsToFile(aExecFile, aExecArr, False);
119 end;
120 end;
122 // Write file to flag modifypath was selected
123 // Workaround since IsTaskSelected() cannot be called at uninstall and AppName and AppId cannot be "read" in Code section
124 if IsUninstaller() = false then
125 SaveStringToFile(ExpandConstant('{app}') + '\uninsTasks.txt', WizardSelectedTasks(False), False);
126 end;
127 end;
129 procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
131 appdir: String;
132 selectedTasks: String;
133 begin
134 appdir := ExpandConstant('{app}')
135 if CurUninstallStep = usUninstall then begin
136 if LoadStringFromFile(appdir + '\uninsTasks.txt', selectedTasks) then
137 if Pos('modifypath', selectedTasks) > 0 then
138 ModPath();
139 DeleteFile(appdir + '\uninsTasks.txt')
140 end;
141 end;
143 function NeedRestart(): Boolean;
144 begin
145 if IsTaskSelected('modifypath') and not UsingWinNT() then begin
146 Result := True;
147 end else begin
148 Result := False;
149 end;
150 end;