-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathamScript.Provider.pas
More file actions
306 lines (265 loc) · 8.18 KB
/
amScript.Provider.pas
File metadata and controls
306 lines (265 loc) · 8.18 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
unit amScript.Provider;
(*
* Copyright © 2012 Anders Melander
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*)
interface
uses
Classes,
dwsFileSystem,
amScript.Provider.API;
// -----------------------------------------------------------------------------
//
// TSimpleScriptProvider.
//
// -----------------------------------------------------------------------------
// Wraps a script contained in a string.
// -----------------------------------------------------------------------------
type
TSimpleScriptProvider = class(TInterfacedObject, IScriptProvider)
private
FName, FScript, FRoot: string;
protected
procedure SetScript(const Script: string); virtual;
function GetScript: string; virtual;
function GetScriptName: string;
function GetFolder: string; virtual;
function GetReadOnly: boolean; virtual;
function GetProtected: boolean; virtual;
function CreateFileSystem: IdwsFileSystem; virtual;
public
constructor Create(const AName, AScript: string; const ARoot: string = ''); overload;
constructor Create(const AName: string; AStream: TStream; const ARoot: string = ''); overload;
end;
// -----------------------------------------------------------------------------
//
// TStaticScriptProvider
//
// -----------------------------------------------------------------------------
// Wraps a read-only script contained in a string.
// -----------------------------------------------------------------------------
type
TStaticScriptProvider = class(TSimpleScriptProvider)
protected
procedure SetScript(const Script: string); override;
function GetReadOnly: boolean; override;
public
end;
// -----------------------------------------------------------------------------
//
// TBundleScriptProvider
//
// -----------------------------------------------------------------------------
// Wraps a script contained in a bundle (a zip file).
// -----------------------------------------------------------------------------
type
TBundleScriptProvider = class(TStaticScriptProvider)
private
FBundleFilename: string;
FAllowProtected: boolean;
protected
function CreateFileSystem: IdwsFileSystem; override;
function GetProtected: boolean; override;
function GetFolder: string; override;
public
constructor Create(const ABundleFilename, AName: string; AAllowProtected: boolean; AStream: TStream);
end;
// -----------------------------------------------------------------------------
//
// TFileScriptProvider
//
// -----------------------------------------------------------------------------
// Wraps a script contained in a file.
// -----------------------------------------------------------------------------
type
TFileScriptProvider = class(TInterfacedObject, IScriptProvider)
private
FFilename: string;
protected
// IScriptProvider
procedure SetScript(const Script: string);
function GetScript: string;
function GetScriptName: string;
function GetFolder: string;
function GetReadOnly: boolean;
function GetProtected: boolean;
function CreateFileSystem: IdwsFileSystem;
public
constructor Create(const AFilename: string);
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
implementation
uses
SysUtils,
IOUtils,
Dialogs,
Forms, Windows, Controls, // not necessary but included for inline expansion
amDialogs,
amEncoding,
amScript.Module,
amScript.FileSystem.API,
amScript.FileSystem;
// -----------------------------------------------------------------------------
//
// TSimpleScriptProvider
//
// -----------------------------------------------------------------------------
constructor TSimpleScriptProvider.Create(const AName, AScript: string; const ARoot: string);
begin
inherited Create;
FName := AName;
FScript := AScript;
FRoot := ARoot;
end;
constructor TSimpleScriptProvider.Create(const AName: string; AStream: TStream; const ARoot: string);
//var
// StreamReader: TStreamReader;
begin
inherited Create;
FName := AName;
FRoot := ARoot;
FScript := DecodeTextStream(AStream);
(*
StreamReader := TStreamReader.Create(AStream);
try
FScript := StreamReader.ReadToEnd;
finally
StreamReader.Free;
end;
*)
end;
function TSimpleScriptProvider.CreateFileSystem: IdwsFileSystem;
begin
Result := nil;
end;
function TSimpleScriptProvider.GetFolder: string;
begin
Result := FRoot;
end;
function TSimpleScriptProvider.GetProtected: boolean;
begin
Result := False;
end;
function TSimpleScriptProvider.GetReadOnly: boolean;
begin
Result := False;
end;
function TSimpleScriptProvider.GetScript: string;
begin
Result := FScript;
end;
function TSimpleScriptProvider.GetScriptName: string;
begin
Result := FName;
end;
procedure TSimpleScriptProvider.SetScript(const Script: string);
begin
FScript := Script;
end;
// -----------------------------------------------------------------------------
//
// TStaticScriptProvider
//
// -----------------------------------------------------------------------------
function TStaticScriptProvider.GetReadOnly: boolean;
begin
Result := True;
end;
procedure TStaticScriptProvider.SetScript(const Script: string);
begin
end;
// -----------------------------------------------------------------------------
//
// TBundleScriptProvider
//
// -----------------------------------------------------------------------------
constructor TBundleScriptProvider.Create(const ABundleFilename, AName: string; AAllowProtected: boolean; AStream: TStream);
begin
inherited Create(AName, AStream);
FBundleFilename := ABundleFilename;
FAllowProtected := AAllowProtected;
end;
function TBundleScriptProvider.CreateFileSystem: IdwsFileSystem;
var
FileSystem: IScriptFileSystem;
begin
Result := TScriptDelegatingFileSystem.Create(GetFolder, nil, FAllowProtected);
FileSystem := TScriptBundleFileSystem.Create(FBundleFilename, FAllowProtected);
(Result as IScriptDelegatingFileSystem).MountFileSystem(FileSystem);
end;
function TBundleScriptProvider.GetFolder: string;
begin
if (FBundleFilename <> '') then
Result := TPath.GetDirectoryName(FBundleFilename)
else
Result := '';
end;
function TBundleScriptProvider.GetProtected: boolean;
begin
Result := True;
end;
// -----------------------------------------------------------------------------
//
// TFileScriptProvider
//
// -----------------------------------------------------------------------------
constructor TFileScriptProvider.Create(const AFilename: string);
begin
inherited Create;
FFilename := AFilename;
end;
function TFileScriptProvider.CreateFileSystem: IdwsFileSystem;
begin
Result := TScriptRestrictedFileSystem.Create(GetFolder);
end;
function TFileScriptProvider.GetFolder: string;
begin
Result := TPath.GetDirectoryName(FFilename);
end;
function TFileScriptProvider.GetProtected: boolean;
begin
Result := False;
end;
function TFileScriptProvider.GetReadOnly: boolean;
begin
Result := (IOUtils.TFileAttribute.faReadOnly in IOUtils.TFile.GetAttributes(FFilename));
end;
function TFileScriptProvider.GetScript: string;
var
// StreamReader: TStreamReader;
Stream: TStream;
begin
Stream := TFileStream.Create(FFilename, fmOpenRead);
// StreamReader := TStreamReader.Create(FFilename);
try
// Result := StreamReader.ReadToEnd;
Result := DecodeTextStream(Stream);
finally
Stream.Free;
// StreamReader.Free;
end;
end;
function TFileScriptProvider.GetScriptName: string;
begin
Result := TPath.GetFileNameWithoutExtension(FFilename);
end;
procedure TFileScriptProvider.SetScript(const Script: string);
var
StreamWriter: TStreamWriter;
begin
StreamWriter := TStreamWriter.Create(FFilename);
try
StreamWriter.Write(Script);
finally
StreamWriter.Free;
end;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.