-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvironment.iss
More file actions
157 lines (126 loc) · 4.03 KB
/
environment.iss
File metadata and controls
157 lines (126 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
; environment.iss
; The code is kindly provided by Wojciech Mleczek in the answer on StackOverflow:
; https://stackoverflow.com/a/46609047/1240328
; Modified to be used at the current user level (if there are no administrator privileges) by georgy7 (https://github.com/georgy7/catframes/blob/main/installer/environment.iss).
[Code]
procedure SelectEnvKeys(var RootKey: integer; var EnvironmentKey: string);
begin
if IsAdminInstallMode() then
begin
RootKey := HKEY_LOCAL_MACHINE;
EnvironmentKey := 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
end
else
begin
RootKey := HKEY_CURRENT_USER;
EnvironmentKey := 'Environment';
end;
end;
function IsPathInList(Path: string; Paths: string): boolean;
var
X, Tail: string;
P: integer;
begin
Result := false;
Tail := Paths;
while Length(Tail) > 0 do
begin
P := Pos(';', Tail);
if P < 1 then
begin
X := Tail;
Tail := '';
end
else
begin
X := Copy(Tail, 1, P-1);
Tail := Copy(Tail, P+1, Length(Tail)-P);
end;
if SameStr(Uppercase(X), Uppercase(Path)) then
begin
Result := true;
break;
end;
end;
end;
function StartsWith(S, Head: string): boolean;
begin
Result := (1=Pos(Head, S));
end;
function EndsWith(S, Tail: string): boolean;
begin
Result := SameStr(Tail, Copy(S, Length(S)+1-Length(Tail), Length(Tail)));
end;
function WithoutPathInternal(S, Path: string): string;
var
Part: string;
I: integer;
begin
if SameStr(Uppercase(Path), Uppercase(S)) then Result := ''
else
begin
Result := S;
Part := ';'+Uppercase(Path)+';';
repeat
I := Pos(Part, Uppercase(Result));
Delete(Result, I, Length(Part)-1);
until 0=I;
Part := Uppercase(Path)+';';
if StartsWith(Uppercase(Result), Part) then
Delete(Result, 1, Length(Part));
Part := ';'+Uppercase(Path);
if EndsWith(Uppercase(Result), Part) then
Delete(Result, Length(Result)+1-Length(Part), Length(Part));
if StartsWith(Result, ';') then
Delete(Result, 1, 1);
if EndsWith(Result, ';') then
Delete(Result, Length(Result), 1);
end;
end;
function WithoutPath(S, Path: string): string;
begin
Result := WithoutPathInternal(S, Path);
if EndsWith(Path, '\') then
Result := WithoutPathInternal(Result, Copy(Path, 1, Length(Path)-1))
else
Result := WithoutPathInternal(Result, Path+'\');
end;
procedure EnvAddPath(Path: string);
var
RootKey: integer;
EnvironmentKey: string;
Paths: string;
begin
SelectEnvKeys(RootKey, EnvironmentKey);
{ Retrieve current path (use empty string if entry not exists) }
if not RegQueryStringValue(RootKey, EnvironmentKey, 'Path', Paths)
then Paths := '';
if IsPathInList(Path, Paths) then exit;
if 0 = Length(Paths) then
Paths := Path
else if EndsWith(Paths, ';') then
Paths := Paths + Path
else
Paths := Paths + ';'+ Path;
{ Overwrite (or create if missing) path environment variable }
if RegWriteStringValue(RootKey, EnvironmentKey, 'Path', Paths)
then Log(Format('The [%s] added to PATH: [%s]', [Path, Paths]))
else Log(Format('Error while adding the [%s] to PATH: [%s]', [Path, Paths]));
end;
procedure EnvRemovePath(Path: string);
var
RootKey: integer;
EnvironmentKey: string;
Paths: string;
begin
SelectEnvKeys(RootKey, EnvironmentKey);
{ Skip if registry entry not exists }
if not RegQueryStringValue(RootKey, EnvironmentKey, 'Path', Paths) then
exit;
if not IsPathInList(Path, Paths) then exit;
Paths := WithoutPath(Paths, Path);
{ Overwrite path environment variable }
if RegWriteStringValue(RootKey, EnvironmentKey, 'Path', Paths)
then Log(Format('The [%s] removed from PATH: [%s]', [Path, Paths]))
else Log(Format('Error while removing the [%s] from PATH: [%s]', [Path, Paths]));
end;