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