1 | unit Project;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Dialogs, DOM, XMLWrite, XMLRead, FileUtil,
|
---|
7 | Generics.Collections;
|
---|
8 |
|
---|
9 | const
|
---|
10 | ProjectExt = '.tppr';
|
---|
11 |
|
---|
12 | type
|
---|
13 | TProject = class;
|
---|
14 |
|
---|
15 | TProjectPackage = class
|
---|
16 | Name: string;
|
---|
17 | end;
|
---|
18 |
|
---|
19 | { TProjectPackages }
|
---|
20 |
|
---|
21 | TProjectPackages = class(TObjectList<TProjectPackage>)
|
---|
22 | Parent: TProject;
|
---|
23 | procedure Load;
|
---|
24 | procedure Save;
|
---|
25 | procedure SaveToXMLNode(Node: TDOMNode);
|
---|
26 | procedure LoadFromXMLNode(Node: TDOMNode);
|
---|
27 | end;
|
---|
28 |
|
---|
29 | TProjectBuildConfig = class
|
---|
30 | Name: string;
|
---|
31 | end;
|
---|
32 |
|
---|
33 | TProjectBuildConfigs = class(TObjectList<TProjectBuildConfig>)
|
---|
34 | end;
|
---|
35 |
|
---|
36 | { TProjectFile }
|
---|
37 |
|
---|
38 | TProjectFile = class
|
---|
39 | private
|
---|
40 | FModified: Boolean;
|
---|
41 | function GetAbsoluteFileName: string;
|
---|
42 | procedure SetModified(const AValue: Boolean);
|
---|
43 | public
|
---|
44 | FileName: string;
|
---|
45 | Source: TStringList;
|
---|
46 | Parent: TProject;
|
---|
47 | constructor Create;
|
---|
48 | destructor Destroy; override;
|
---|
49 | procedure SaveToXMLNode(Node: TDOMNode);
|
---|
50 | procedure LoadFromXMLNode(Node: TDOMNode);
|
---|
51 | procedure Load;
|
---|
52 | procedure Save;
|
---|
53 | property Modified: Boolean read FModified write SetModified;
|
---|
54 | property AbsoluteFileName: string read GetAbsoluteFileName;
|
---|
55 | end;
|
---|
56 |
|
---|
57 | { TProjectFiles }
|
---|
58 |
|
---|
59 | TProjectFiles = class(TObjectList<TProjectFile>)
|
---|
60 | Parent: TProject;
|
---|
61 | procedure DumpFileList(Files: TStringList);
|
---|
62 | procedure LoadFromList(Files: TStringList);
|
---|
63 | procedure Load;
|
---|
64 | procedure Save;
|
---|
65 | procedure SaveToXMLNode(Node: TDOMNode);
|
---|
66 | procedure LoadFromXMLNode(Node: TDOMNode);
|
---|
67 | function SearchFile(FileName: string): TProjectFile;
|
---|
68 | function AddFile(FileName: string): TProjectFile;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | TProject = class
|
---|
72 | private
|
---|
73 | FModified: Boolean;
|
---|
74 | FOnChange: TNotifyEvent;
|
---|
75 | procedure SetModified(AValue: Boolean);
|
---|
76 | public
|
---|
77 | FileName: string;
|
---|
78 | Files: TProjectFiles;
|
---|
79 | Packages: TProjectPackages;
|
---|
80 | BuildConfigs: TProjectBuildConfigs;
|
---|
81 | MainSource: TProjectFile;
|
---|
82 | procedure LoadFromFile(FileName: string);
|
---|
83 | procedure SaveToFile(FileName: string);
|
---|
84 | procedure Load;
|
---|
85 | procedure Save;
|
---|
86 | procedure Clear;
|
---|
87 | constructor Create;
|
---|
88 | destructor Destroy; override;
|
---|
89 | property Modified: Boolean read FModified write SetModified;
|
---|
90 | property OnChange: TNotifyEvent read FOnChange write FOnChange;
|
---|
91 | end;
|
---|
92 |
|
---|
93 | { TProjectTemplate }
|
---|
94 |
|
---|
95 | TProjectTemplate = class
|
---|
96 | Name: string;
|
---|
97 | Description: TStringList;
|
---|
98 | IsProject: Boolean;
|
---|
99 | procedure InitProject(var Project: TProject); virtual;
|
---|
100 | constructor Create; virtual;
|
---|
101 | destructor Destroy; override;
|
---|
102 | end;
|
---|
103 |
|
---|
104 | { TProjectTemplates }
|
---|
105 |
|
---|
106 | TProjectTemplates = class(TObjectList<TProjectTemplate>)
|
---|
107 | procedure AddTemplate(Template: TProjectTemplate);
|
---|
108 | end;
|
---|
109 |
|
---|
110 | resourcestring
|
---|
111 | SNewProject = 'New project';
|
---|
112 |
|
---|
113 |
|
---|
114 | implementation
|
---|
115 |
|
---|
116 | { TProjectTemplates }
|
---|
117 |
|
---|
118 | procedure TProjectTemplates.AddTemplate(Template: TProjectTemplate);
|
---|
119 | begin
|
---|
120 | Add(Template);
|
---|
121 | end;
|
---|
122 |
|
---|
123 | { TProjectTemplate }
|
---|
124 |
|
---|
125 | procedure TProjectTemplate.InitProject(var Project: TProject);
|
---|
126 | begin
|
---|
127 | Project.FileName := SNewProject;
|
---|
128 | end;
|
---|
129 |
|
---|
130 | constructor TProjectTemplate.Create;
|
---|
131 | begin
|
---|
132 | Description := TStringList.Create;
|
---|
133 | end;
|
---|
134 |
|
---|
135 | destructor TProjectTemplate.Destroy;
|
---|
136 | begin
|
---|
137 | FreeAndNil(Description);
|
---|
138 | inherited;
|
---|
139 | end;
|
---|
140 |
|
---|
141 | { TProjectPackages }
|
---|
142 |
|
---|
143 | procedure TProjectPackages.Load;
|
---|
144 | begin
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TProjectPackages.Save;
|
---|
148 | begin
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TProjectPackages.SaveToXMLNode(Node: TDOMNode);
|
---|
152 | begin
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TProjectPackages.LoadFromXMLNode(Node: TDOMNode);
|
---|
156 | begin
|
---|
157 | end;
|
---|
158 |
|
---|
159 | { TProjectGroup }
|
---|
160 |
|
---|
161 | procedure TProjectFiles.DumpFileList(Files: TStringList);
|
---|
162 | var
|
---|
163 | I: Integer;
|
---|
164 | begin
|
---|
165 | Files.Clear;
|
---|
166 | for I := 0 to Count - 1 do
|
---|
167 | Files.Add(TProjectFile(Items[I]).AbsoluteFileName);
|
---|
168 | end;
|
---|
169 |
|
---|
170 | procedure TProjectFiles.LoadFromList(Files: TStringList);
|
---|
171 | var
|
---|
172 | I: Integer;
|
---|
173 | NewFile: TProjectFile;
|
---|
174 | FileName: string;
|
---|
175 | begin
|
---|
176 | Clear;
|
---|
177 | for I := 0 to Files.Count - 1 do begin
|
---|
178 | FileName := Files[I];
|
---|
179 | if Copy(FileName, 1, Length(ExtractFileDir(Parent.FileName))) =
|
---|
180 | ExtractFileDir(Parent.FileName) then
|
---|
181 | System.Delete(FileName, 1, Length(ExtractFileDir(Parent.FileName)) + 1);
|
---|
182 | NewFile := AddFile(FileName);
|
---|
183 | NewFile.Load;
|
---|
184 | end;
|
---|
185 | end;
|
---|
186 |
|
---|
187 | procedure TProjectFiles.Load;
|
---|
188 | var
|
---|
189 | I: Integer;
|
---|
190 | begin
|
---|
191 | for I := 0 to Count - 1 do
|
---|
192 | TProjectFile(Items[I]).Load;
|
---|
193 | end;
|
---|
194 |
|
---|
195 | procedure TProjectFiles.Save;
|
---|
196 | var
|
---|
197 | I: Integer;
|
---|
198 | begin
|
---|
199 | for I := 0 to Count - 1 do
|
---|
200 | TProjectFile(Items[I]).Save;
|
---|
201 | end;
|
---|
202 |
|
---|
203 | procedure TProjectFiles.SaveToXMLNode(Node: TDOMNode);
|
---|
204 | var
|
---|
205 | I: Integer;
|
---|
206 | NewNode: TDOMNode;
|
---|
207 | begin
|
---|
208 | with Node do
|
---|
209 | for I := 0 to Count - 1 do begin
|
---|
210 | NewNode := OwnerDocument.CreateElement('File');
|
---|
211 | TProjectFile(Items[I]).SaveToXMLNode(NewNode);
|
---|
212 | AppendChild(NewNode);
|
---|
213 | end
|
---|
214 | end;
|
---|
215 |
|
---|
216 | procedure TProjectFiles.LoadFromXMLNode(Node: TDOMNode);
|
---|
217 | var
|
---|
218 | NewNode: TDomNode;
|
---|
219 | NewFile: TProjectFile;
|
---|
220 | begin
|
---|
221 | NewNode := Node.FirstChild;
|
---|
222 | while Assigned(NewNode) do begin
|
---|
223 | if NewNode.NodeName = 'File' then begin
|
---|
224 | NewFile := TProjectFile.Create;
|
---|
225 | NewFile.Parent := Parent;
|
---|
226 | NewFile.LoadFromXMLNode(NewNode);
|
---|
227 | Add(NewFile);
|
---|
228 | end;
|
---|
229 | NewNode := NewNode.NextSibling;
|
---|
230 | end;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | function TProjectFiles.SearchFile(FileName: string): TProjectFile;
|
---|
234 | var
|
---|
235 | I: Integer;
|
---|
236 | ParentDir: string;
|
---|
237 | begin
|
---|
238 | ParentDir := ExtractFileDir(Parent.FileName);
|
---|
239 | if Copy(FileName, 1, Length(ParentDir)) = ParentDir then
|
---|
240 | System.Delete(FileName, 1, Length(ParentDir) + 1);
|
---|
241 | I := 0;
|
---|
242 | while (I < Count) and (TProjectFile(Items[I]).FileName <> FileName) do Inc(I);
|
---|
243 | if I < Count then Result := TProjectFile(Items[I])
|
---|
244 | else Result := nil;
|
---|
245 | end;
|
---|
246 |
|
---|
247 | function TProjectFiles.AddFile(FileName: string): TProjectFile;
|
---|
248 | begin
|
---|
249 | Result := TProjectFile.Create;
|
---|
250 | Result.Parent := Parent;
|
---|
251 | Result.FileName := FileName;
|
---|
252 | Add(Result);
|
---|
253 | end;
|
---|
254 |
|
---|
255 | { TProjectFile }
|
---|
256 |
|
---|
257 | procedure TProjectFile.SetModified(const AValue: Boolean);
|
---|
258 | begin
|
---|
259 | if FModified = AValue then Exit;
|
---|
260 | FModified := True;
|
---|
261 | if AValue then Parent.Modified := True;
|
---|
262 | end;
|
---|
263 |
|
---|
264 | function TProjectFile.GetAbsoluteFileName: string;
|
---|
265 | begin
|
---|
266 | Result := ExtractFileDir(Parent.FileName) + DirectorySeparator + FileName;
|
---|
267 | end;
|
---|
268 |
|
---|
269 | constructor TProjectFile.Create;
|
---|
270 | begin
|
---|
271 | Source := TStringList.Create;
|
---|
272 | end;
|
---|
273 |
|
---|
274 | destructor TProjectFile.Destroy;
|
---|
275 | begin
|
---|
276 | FreeAndNil(Source);
|
---|
277 | inherited;
|
---|
278 | end;
|
---|
279 |
|
---|
280 | procedure TProjectFile.SaveToXMLNode(Node: TDOMNode);
|
---|
281 | var
|
---|
282 | NewNode: TDOMNode;
|
---|
283 | begin
|
---|
284 | with Node do begin
|
---|
285 | NewNode := OwnerDocument.CreateElement('FileName');
|
---|
286 | NewNode.TextContent := UTF8Decode(FileName);
|
---|
287 | AppendChild(NewNode);
|
---|
288 | end;
|
---|
289 | Save;
|
---|
290 | end;
|
---|
291 |
|
---|
292 | procedure TProjectFile.LoadFromXMLNode(Node: TDOMNode);
|
---|
293 | var
|
---|
294 | NewNode: TDOMNode;
|
---|
295 | begin
|
---|
296 | with Node do begin
|
---|
297 | NewNode := FindNode('FileName');
|
---|
298 | if Assigned(NewNode) then
|
---|
299 | FileName := UTF8Encode(NewNode.TextContent);
|
---|
300 | end;
|
---|
301 | if FileExists(AbsoluteFileName) then Load;
|
---|
302 | end;
|
---|
303 |
|
---|
304 | procedure TProjectFile.Load;
|
---|
305 | begin
|
---|
306 | Source.LoadFromFile(AbsoluteFileName);
|
---|
307 | end;
|
---|
308 |
|
---|
309 | procedure TProjectFile.Save;
|
---|
310 | begin
|
---|
311 | Source.SaveToFile(AbsoluteFileName);
|
---|
312 | end;
|
---|
313 |
|
---|
314 | { TProject }
|
---|
315 |
|
---|
316 | procedure TProject.SetModified(AValue: Boolean);
|
---|
317 | begin
|
---|
318 | if FModified = AValue then Exit;
|
---|
319 | FModified := AValue;
|
---|
320 | if Assigned(FOnChange) then FOnChange(Self);
|
---|
321 | end;
|
---|
322 |
|
---|
323 | procedure TProject.LoadFromFile(FileName: string);
|
---|
324 | var
|
---|
325 | Doc: TXMLDocument;
|
---|
326 | NewNode: TDOMNode;
|
---|
327 | NewSubNode: TDOMNode;
|
---|
328 | I: Integer;
|
---|
329 | Text: string;
|
---|
330 | MainSourceName: string;
|
---|
331 | begin
|
---|
332 | try
|
---|
333 | Clear;
|
---|
334 | Self.FileName := FileName;
|
---|
335 | ReadXMLFile(Doc, UTF8Decode(FileName));
|
---|
336 |
|
---|
337 | NewNode := Doc.DocumentElement.FindNode('Files');
|
---|
338 | if Assigned(NewNode) then
|
---|
339 | Files.LoadFromXMLNode(NewNode);
|
---|
340 |
|
---|
341 | NewNode := Doc.DocumentElement.FindNode('MainSource');
|
---|
342 | if Assigned(NewNode) then begin
|
---|
343 | MainSourceName := UTF8Encode(NewNode.TextContent);
|
---|
344 | MainSource := Files.SearchFile(MainSourceName);
|
---|
345 | end;
|
---|
346 |
|
---|
347 | NewNode := Doc.DocumentElement.FindNode('Packages');
|
---|
348 | if Assigned(NewNode) then
|
---|
349 | Packages.LoadFromXMLNode(NewNode);
|
---|
350 | finally
|
---|
351 | Doc.Free;
|
---|
352 | end;
|
---|
353 | FModified := False;
|
---|
354 | end;
|
---|
355 |
|
---|
356 | procedure TProject.SaveToFile(FileName: string);
|
---|
357 | var
|
---|
358 | Doc: TXMLDocument;
|
---|
359 | RootNode: TDOMNode;
|
---|
360 | NewNode: TDOMNode;
|
---|
361 | NewSubNode: TDOMNode;
|
---|
362 | I: Integer;
|
---|
363 | Text: string;
|
---|
364 | begin
|
---|
365 | Doc := TXMLDocument.Create;
|
---|
366 | Self.FileName := FileName;
|
---|
367 | with Doc do try
|
---|
368 | RootNode := CreateElement('Project');
|
---|
369 | AppendChild(RootNode);
|
---|
370 | with RootNode do begin
|
---|
371 | NewNode := CreateElement('MainSource');
|
---|
372 | if Assigned(MainSource) then
|
---|
373 | NewNode.TextContent := UTF8Decode(MainSource.FileName);
|
---|
374 | AppendChild(NewNode);
|
---|
375 |
|
---|
376 | NewNode := CreateElement('Files');
|
---|
377 | Files.SaveToXMLNode(NewNode);
|
---|
378 | AppendChild(NewNode);
|
---|
379 |
|
---|
380 | NewNode := CreateElement('Packages');
|
---|
381 | Packages.SaveToXMLNode(NewNode);
|
---|
382 | AppendChild(NewNode);
|
---|
383 | end;
|
---|
384 | WriteXMLFile(Doc, UTF8Decode(FileName));
|
---|
385 | finally
|
---|
386 | Free;
|
---|
387 | end;
|
---|
388 | FModified := False;
|
---|
389 | end;
|
---|
390 |
|
---|
391 | procedure TProject.Load;
|
---|
392 | begin
|
---|
393 | Files.Load;
|
---|
394 | Packages.Load;
|
---|
395 | end;
|
---|
396 |
|
---|
397 | procedure TProject.Save;
|
---|
398 | var
|
---|
399 | I: Integer;
|
---|
400 | begin
|
---|
401 | Files.Save;
|
---|
402 | Packages.Save;
|
---|
403 | for I := 0 to Files.Count - 1 do
|
---|
404 | TProjectFile(Files[I]).Modified := False;
|
---|
405 | Modified := False;
|
---|
406 | end;
|
---|
407 |
|
---|
408 | procedure TProject.Clear;
|
---|
409 | begin
|
---|
410 | Files.Clear;
|
---|
411 | Packages.Clear;
|
---|
412 | MainSource := nil;
|
---|
413 | end;
|
---|
414 |
|
---|
415 | constructor TProject.Create;
|
---|
416 | begin
|
---|
417 | Files := TProjectFiles.Create;
|
---|
418 | Files.Parent := Self;
|
---|
419 | Packages := TProjectPackages.Create;
|
---|
420 | Packages.Parent := Self;
|
---|
421 | end;
|
---|
422 |
|
---|
423 | destructor TProject.Destroy;
|
---|
424 | begin
|
---|
425 | FreeAndNil(Files);
|
---|
426 | FreeAndNil(Packages);
|
---|
427 | FreeAndNil(BuildConfigs);
|
---|
428 | inherited;
|
---|
429 | end;
|
---|
430 |
|
---|
431 | end.
|
---|
432 |
|
---|