forked from andersmelander/DWScriptStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamScript.FileSystemStructure.pas
More file actions
192 lines (158 loc) · 5.73 KB
/
amScript.FileSystemStructure.pas
File metadata and controls
192 lines (158 loc) · 5.73 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
unit amScript.FileSystemStructure;
(*
* Copyright © 2023 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,
amScript.FileSystemStructure.API,
amScript.Provider.API;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemObject
//
// -----------------------------------------------------------------------------
// Implements IScriptFileSystemObject
// Represents a file system object; Either a file or a folder.
// -----------------------------------------------------------------------------
type
TScriptFileSystemObject = class(TInterfacedObject, IScriptFileSystemObject)
strict private
FName: string;
// Parent link is a weak reference to avoid cyclic references keeping the object alive forever
[weak] FParent: IScriptFileSystemFolder;
private
// IScriptFileSystemObject
function GetParent: IScriptFileSystemFolder;
function GetName: string;
function GetPath: string;
strict protected
property Parent: IScriptFileSystemFolder read FParent;
property Name: string read FName;
public
constructor Create(const AName: string; AParent: IScriptFileSystemFolder = nil);
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFolder
//
// -----------------------------------------------------------------------------
// Implements IScriptFileSystemFolder
// A folder represents a collection of files and folders.
// -----------------------------------------------------------------------------
type
TScriptFileSystemFolder = class(TScriptFileSystemObject, IScriptFileSystemFolder)
strict private
FHasFolders: boolean;
FFolders: TArray<IScriptFileSystemFolder>;
FHasFiles: boolean;
FFiles: TArray<IScriptFileSystemFile>;
private
// ScriptFileSystemFolder
function GetFolders: TArray<IScriptFileSystemFolder>;
function GetFiles: TArray<IScriptFileSystemFile>;
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFile
//
// -----------------------------------------------------------------------------
// Implements IScriptFileSystemFile
// A file represents a single script unit.
// -----------------------------------------------------------------------------
type
TScriptFileSystemFile = class(TScriptFileSystemObject, IScriptFileSystemFile)
private
// ScriptFileSystemFile
function CreateScriptProvider: IScriptProvider;
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
implementation
uses
System.IOUtils,
System.SysUtils,
amScript.Provider;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemObject
//
// -----------------------------------------------------------------------------
constructor TScriptFileSystemObject.Create(const AName: string; AParent: IScriptFileSystemFolder);
begin
inherited Create;
FParent := AParent;
FName := AName;
end;
function TScriptFileSystemObject.GetName: string;
begin
Result := FName;
end;
function TScriptFileSystemObject.GetParent: IScriptFileSystemFolder;
begin
Result := FParent;
end;
function TScriptFileSystemObject.GetPath: string;
begin
if (Parent <> nil) then
Result := TPath.Combine(TScriptFileSystemObject(Parent).GetPath, Name)
else
Result := Name;
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFolder
//
// -----------------------------------------------------------------------------
function TScriptFileSystemFolder.GetFiles: TArray<IScriptFileSystemFile>;
begin
if (not FHasFiles) then
begin
FHasFiles := True;
var Filenames := TDirectory.GetFiles(GetPath, '*.pas',
function(const Path: string; const SearchRec: TSearchRec): Boolean
begin
var Attributes := TFile.IntegerToFileAttributes(SearchRec.Attr);
Result := ([TFileAttribute.faHidden, TFileAttribute.faSystem] * Attributes = []);
end);
SetLength(FFiles, Length(Filenames));
for var i := 0 to High(Filenames) do
FFiles[i] := TScriptFileSystemFile.Create(TPath.GetFileName(Filenames[i]), Self);
end;
Result := FFiles;
end;
function TScriptFileSystemFolder.GetFolders: TArray<IScriptFileSystemFolder>;
begin
if (not FHasFolders) then
begin
FHasFolders := True;
var Folders := TDirectory.GetDirectories(GetPath,
function(const Path: string; const SearchRec: TSearchRec): Boolean
begin
var Attributes := TFile.IntegerToFileAttributes(SearchRec.Attr);
Result := ([TFileAttribute.faHidden, TFileAttribute.faSystem] * Attributes = []);
end);
SetLength(FFolders, Length(Folders));
for var i := 0 to High(Folders) do
FFolders[i] := TScriptFileSystemFolder.Create(TPath.GetFileName(Folders[i]), Self);
end;
Result := FFolders;
end;
// -----------------------------------------------------------------------------
//
// TScriptFileSystemFile
//
// -----------------------------------------------------------------------------
function TScriptFileSystemFile.CreateScriptProvider: IScriptProvider;
begin
Result := TFileScriptProvider.Create(GetPath);
end;
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
end.