git.iko.soy/typeable/pandoc
on 1.6 help sign in
178 lines 5.6 kB history raw
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//
8// Modified 10/18/2008 by John MacFarlane to set path in HKCU instead of HKLM if
9// user does not have admin privileges. Also correctly handle the case where
10// oldpath is empty.
11//
12// Script Function:
13// Enable modification of system path directly from Inno Setup installers
14//
15// Instructions:
16// Copy modpath.iss to the same directory as your setup script
17//
18// Add this statement to your [Setup] section
19// ChangesEnvironment=yes
20//
21// Add this statement to your [Tasks] section
22// You can change the Description or Flags, but the Name must be modifypath
23// Name: modifypath; Description: &Add application directory to your system path; Flags: unchecked
24//
25// Add the following to the end of your [Code] section
26// setArrayLength must specify the total number of dirs to be added
27// Dir[0] contains first directory, Dir[1] contains second, etc.
28// function ModPathDir(): TArrayOfString;
29// var
30// Dir: TArrayOfString;
31// begin
32// setArrayLength(Dir, 1)
33// Dir[0] := ExpandConstant('{app}');
34// Result := Dir;
35// end;
36// #include "modpath.iss"
37// ----------------------------------------------------------------------------
39procedure ModPath();
40var
41 oldpath: String;
42 newpath: String;
43 pathArr: TArrayOfString;
44 aExecFile: String;
45 aExecArr: TArrayOfString;
46 i, d: Integer;
47 pathdir: TArrayOfString;
48 reg: Integer;
49 regkey: String;
50begin
52 // Get array of new directories and act on each individually
53 pathdir := ModPathDir();
54 for d := 0 to GetArrayLength(pathdir)-1 do begin
56 // Modify WinNT path
57 if UsingWinNT() = true then begin
59 // Select HKLM or HKCU depending on whether user has admin privileges
60 if (IsAdminLoggedOn or IsPowerUserLoggedOn) then begin
61 reg := HKEY_LOCAL_MACHINE;
62 regkey := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
63 end else begin
64 reg := HKEY_CURRENT_USER;
65 regkey := 'Environment';
66 end;
68 // Get current path, split into an array
69 RegQueryStringValue(reg, regkey, 'Path', oldpath);
70 oldpath := oldpath + ';';
71 i := 0;
72 while (Pos(';', oldpath) > 0) do begin
73 SetArrayLength(pathArr, i+1);
74 pathArr[i] := Copy(oldpath, 0, Pos(';', oldpath)-1);
75 oldpath := Copy(oldpath, Pos(';', oldpath)+1, Length(oldpath));
76 i := i + 1;
78 // Check if current directory matches app dir
79 if pathdir[d] = pathArr[i-1] then begin
80 // if uninstalling, remove dir from path
81 if IsUninstaller() = true then begin
82 continue;
83 // if installing, abort because dir was already in path
84 end else begin
85 abort;
86 end;
87 end;
89 // Add current directory to new path
90 if i = 1 then begin
91 newpath := pathArr[i-1];
92 end else begin
93 newpath := newpath + ';' + pathArr[i-1];
94 end;
95 end;
97 // Append app dir to path if not already included
98 if IsUninstaller() = false then begin
99 if newpath = '' then begin
100 newpath := pathdir[d];
101 end else begin
102 newpath := newpath + ';' + pathdir[d];
103 end;
104 end;
106 // Write new path
107 RegWriteStringValue(reg, regkey, 'Path', newpath);
109 // Modify Win9x path
110 end else begin
112 // Convert to shortened dirname
113 pathdir[d] := GetShortName(pathdir[d]);
115 // If autoexec.bat exists, check if app dir already exists in path
116 aExecFile := 'C:\AUTOEXEC.BAT';
117 if FileExists(aExecFile) then begin
118 LoadStringsFromFile(aExecFile, aExecArr);
119 for i := 0 to GetArrayLength(aExecArr)-1 do begin
120 if IsUninstaller() = false then begin
121 // If app dir already exists while installing, abort add
122 if (Pos(pathdir[d], aExecArr[i]) > 0) then
123 abort;
124 end else begin
125 // If app dir exists and = what we originally set, then delete at uninstall
126 if aExecArr[i] = 'SET PATH=%PATH%;' + pathdir[d] then
127 aExecArr[i] := '';
128 end;
129 end;
130 end;
132 // If app dir not found, or autoexec.bat didn't exist, then (create and) append to current path
133 if IsUninstaller() = false then begin
134 SaveStringToFile(aExecFile, #13#10 + 'SET PATH=%PATH%;' + pathdir[d], True);
136 // If uninstalling, write the full autoexec out
137 end else begin
138 SaveStringsToFile(aExecFile, aExecArr, False);
139 end;
140 end;
142 // Write file to flag modifypath was selected
143 // Workaround since IsTaskSelected() cannot be called at uninstall and AppName and AppId cannot be "read" in Code section
144 if IsUninstaller() = false then
145 SaveStringToFile(ExpandConstant('{app}') + '\uninsTasks.txt', WizardSelectedTasks(False), False);
146 end;
147end;
149procedure CurStepChanged(CurStep: TSetupStep);
150begin
151 if CurStep = ssPostInstall then
152 if IsTaskSelected('modifypath') then
153 ModPath();
154end;
156procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
157var
158 appdir: String;
159 selectedTasks: String;
160begin
161 appdir := ExpandConstant('{app}')
162 if CurUninstallStep = usUninstall then begin
163 if LoadStringFromFile(appdir + '\uninsTasks.txt', selectedTasks) then
164 if Pos('modifypath', selectedTasks) > 0 then
165 ModPath();
166 DeleteFile(appdir + '\uninsTasks.txt')
167 end;
168end;
170function NeedRestart(): Boolean;
171begin
172 if IsTaskSelected('modifypath') and not UsingWinNT() then begin
173 Result := True;
174 end else begin
175 Result := False;
176 end;
177end;