source: trunk/IDE/Project.pas

Last change on this file was 75, checked in by chronos, 6 months ago
  • Modified: Removed U prefix from unit names.
  • Modified: Updated Common package.
File size: 9.5 KB
Line 
1unit Project;
2
3interface
4
5uses
6 Classes, SysUtils, Dialogs, DOM, XMLWrite, XMLRead, FileUtil,
7 Generics.Collections;
8
9const
10 ProjectExt = '.tppr';
11
12type
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
110resourcestring
111 SNewProject = 'New project';
112
113
114implementation
115
116{ TProjectTemplates }
117
118procedure TProjectTemplates.AddTemplate(Template: TProjectTemplate);
119begin
120 Add(Template);
121end;
122
123{ TProjectTemplate }
124
125procedure TProjectTemplate.InitProject(var Project: TProject);
126begin
127 Project.FileName := SNewProject;
128end;
129
130constructor TProjectTemplate.Create;
131begin
132 Description := TStringList.Create;
133end;
134
135destructor TProjectTemplate.Destroy;
136begin
137 FreeAndNil(Description);
138 inherited;
139end;
140
141{ TProjectPackages }
142
143procedure TProjectPackages.Load;
144begin
145end;
146
147procedure TProjectPackages.Save;
148begin
149end;
150
151procedure TProjectPackages.SaveToXMLNode(Node: TDOMNode);
152begin
153end;
154
155procedure TProjectPackages.LoadFromXMLNode(Node: TDOMNode);
156begin
157end;
158
159{ TProjectGroup }
160
161procedure TProjectFiles.DumpFileList(Files: TStringList);
162var
163 I: Integer;
164begin
165 Files.Clear;
166 for I := 0 to Count - 1 do
167 Files.Add(TProjectFile(Items[I]).AbsoluteFileName);
168end;
169
170procedure TProjectFiles.LoadFromList(Files: TStringList);
171var
172 I: Integer;
173 NewFile: TProjectFile;
174 FileName: string;
175begin
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;
185end;
186
187procedure TProjectFiles.Load;
188var
189 I: Integer;
190begin
191 for I := 0 to Count - 1 do
192 TProjectFile(Items[I]).Load;
193end;
194
195procedure TProjectFiles.Save;
196var
197 I: Integer;
198begin
199 for I := 0 to Count - 1 do
200 TProjectFile(Items[I]).Save;
201end;
202
203procedure TProjectFiles.SaveToXMLNode(Node: TDOMNode);
204var
205 I: Integer;
206 NewNode: TDOMNode;
207begin
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
214end;
215
216procedure TProjectFiles.LoadFromXMLNode(Node: TDOMNode);
217var
218 NewNode: TDomNode;
219 NewFile: TProjectFile;
220begin
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;
231end;
232
233function TProjectFiles.SearchFile(FileName: string): TProjectFile;
234var
235 I: Integer;
236 ParentDir: string;
237begin
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;
245end;
246
247function TProjectFiles.AddFile(FileName: string): TProjectFile;
248begin
249 Result := TProjectFile.Create;
250 Result.Parent := Parent;
251 Result.FileName := FileName;
252 Add(Result);
253end;
254
255{ TProjectFile }
256
257procedure TProjectFile.SetModified(const AValue: Boolean);
258begin
259 if FModified = AValue then Exit;
260 FModified := True;
261 if AValue then Parent.Modified := True;
262end;
263
264function TProjectFile.GetAbsoluteFileName: string;
265begin
266 Result := ExtractFileDir(Parent.FileName) + DirectorySeparator + FileName;
267end;
268
269constructor TProjectFile.Create;
270begin
271 Source := TStringList.Create;
272end;
273
274destructor TProjectFile.Destroy;
275begin
276 FreeAndNil(Source);
277 inherited;
278end;
279
280procedure TProjectFile.SaveToXMLNode(Node: TDOMNode);
281var
282 NewNode: TDOMNode;
283begin
284 with Node do begin
285 NewNode := OwnerDocument.CreateElement('FileName');
286 NewNode.TextContent := UTF8Decode(FileName);
287 AppendChild(NewNode);
288 end;
289 Save;
290end;
291
292procedure TProjectFile.LoadFromXMLNode(Node: TDOMNode);
293var
294 NewNode: TDOMNode;
295begin
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;
302end;
303
304procedure TProjectFile.Load;
305begin
306 Source.LoadFromFile(AbsoluteFileName);
307end;
308
309procedure TProjectFile.Save;
310begin
311 Source.SaveToFile(AbsoluteFileName);
312end;
313
314{ TProject }
315
316procedure TProject.SetModified(AValue: Boolean);
317begin
318 if FModified = AValue then Exit;
319 FModified := AValue;
320 if Assigned(FOnChange) then FOnChange(Self);
321end;
322
323procedure TProject.LoadFromFile(FileName: string);
324var
325 Doc: TXMLDocument;
326 NewNode: TDOMNode;
327 NewSubNode: TDOMNode;
328 I: Integer;
329 Text: string;
330 MainSourceName: string;
331begin
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;
354end;
355
356procedure TProject.SaveToFile(FileName: string);
357var
358 Doc: TXMLDocument;
359 RootNode: TDOMNode;
360 NewNode: TDOMNode;
361 NewSubNode: TDOMNode;
362 I: Integer;
363 Text: string;
364begin
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;
389end;
390
391procedure TProject.Load;
392begin
393 Files.Load;
394 Packages.Load;
395end;
396
397procedure TProject.Save;
398var
399 I: Integer;
400begin
401 Files.Save;
402 Packages.Save;
403 for I := 0 to Files.Count - 1 do
404 TProjectFile(Files[I]).Modified := False;
405 Modified := False;
406end;
407
408procedure TProject.Clear;
409begin
410 Files.Clear;
411 Packages.Clear;
412 MainSource := nil;
413end;
414
415constructor TProject.Create;
416begin
417 Files := TProjectFiles.Create;
418 Files.Parent := Self;
419 Packages := TProjectPackages.Create;
420 Packages.Parent := Self;
421end;
422
423destructor TProject.Destroy;
424begin
425 FreeAndNil(Files);
426 FreeAndNil(Packages);
427 FreeAndNil(BuildConfigs);
428 inherited;
429end;
430
431end.
432
Note: See TracBrowser for help on using the repository browser.