1 | unit USource;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, Contnrs, FileUtil, Dialogs, Forms,
|
---|
9 | DOM, XMLWrite, XMLRead;
|
---|
10 |
|
---|
11 | type
|
---|
12 | TSourceType = (stFPC, stIDE);
|
---|
13 |
|
---|
14 | { TSource }
|
---|
15 |
|
---|
16 | TSource = class
|
---|
17 | private
|
---|
18 | function GetDownloaded: Boolean;
|
---|
19 | public
|
---|
20 | Id: Integer;
|
---|
21 | ProjectName: string;
|
---|
22 | ProjectShortName: string;
|
---|
23 | ProjectType: string;
|
---|
24 | SubversionURL: string;
|
---|
25 | VersionNumber: string;
|
---|
26 | VersionType: string;
|
---|
27 | SourceType: TSourceType;
|
---|
28 | ExecutableFile: string;
|
---|
29 | property Downloaded: Boolean read GetDownloaded;
|
---|
30 | procedure Assign(Source: TSource);
|
---|
31 | procedure Download;
|
---|
32 | procedure Update;
|
---|
33 | procedure ExportTo(Path: string);
|
---|
34 | function GetExecutableFile: string;
|
---|
35 | function GetPath: string;
|
---|
36 | function GetName: string;
|
---|
37 | end;
|
---|
38 |
|
---|
39 | { TSourceList }
|
---|
40 |
|
---|
41 | TSourceList = class(TObjectList)
|
---|
42 | function FindById(Id: Integer): TSource;
|
---|
43 | procedure UpdateFormFile(FileName: string);
|
---|
44 | end;
|
---|
45 |
|
---|
46 | implementation
|
---|
47 |
|
---|
48 | uses
|
---|
49 | UMainForm, UOperationProgress;
|
---|
50 |
|
---|
51 | { TSource }
|
---|
52 |
|
---|
53 | function TSource.GetDownloaded: Boolean;
|
---|
54 | begin
|
---|
55 | Result := DirectoryExists(GetPath);
|
---|
56 | end;
|
---|
57 |
|
---|
58 | procedure TSource.Assign(Source: TSource);
|
---|
59 | begin
|
---|
60 | Id := Source.Id;
|
---|
61 | ProjectName := Source.ProjectName;
|
---|
62 | ProjectShortName := Source.ProjectShortName;
|
---|
63 | ProjectType := Source.ProjectType;
|
---|
64 | VersionNumber := Source.VersionNumber;
|
---|
65 | VersionType := Source.VersionType;
|
---|
66 | SubversionURL := Source.SubversionURL;
|
---|
67 | SourceType := Source.SourceType;
|
---|
68 | ExecutableFile := Source.ExecutableFile;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | procedure TSource.Download;
|
---|
72 | begin
|
---|
73 | OperationProgressForm.CommandLine := 'svn checkout "' + SubversionURL +
|
---|
74 | '" "' + GetPath + '"';
|
---|
75 | OperationProgressForm.ShowModal;
|
---|
76 | end;
|
---|
77 |
|
---|
78 | procedure TSource.Update;
|
---|
79 | begin
|
---|
80 | OperationProgressForm.CommandLine := 'svn update "' + GetPath + '"';
|
---|
81 | OperationProgressForm.ShowModal;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | procedure TSource.ExportTo(Path: string);
|
---|
85 | begin
|
---|
86 | OperationProgressForm.CommandLine := 'svn export --force "' + GetPath + '" "' +
|
---|
87 | Path + '"';
|
---|
88 | OperationProgressForm.ShowModal;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | function TSource.GetExecutableFile: string;
|
---|
92 | var
|
---|
93 | Postfix: string;
|
---|
94 | begin
|
---|
95 | PostFix := '';
|
---|
96 | {$IFDEF WINDOWS}
|
---|
97 | PostFix := '.exe';
|
---|
98 | {$ENDIF}
|
---|
99 | Result := ExecutableFile + Postfix;
|
---|
100 | end;
|
---|
101 |
|
---|
102 | function TSource.GetPath: string;
|
---|
103 | begin
|
---|
104 | Result := ExtractFileDir(Application.ExeName) + DirectorySeparator +
|
---|
105 | MainForm.SourceDir + DirectorySeparator + IntToStr(Id);
|
---|
106 | end;
|
---|
107 |
|
---|
108 | function TSource.GetName: string;
|
---|
109 | begin
|
---|
110 | Result := ProjectShortName + ' ' + VersionNumber +
|
---|
111 | ' (' + VersionType + ')';
|
---|
112 | end;
|
---|
113 |
|
---|
114 | { TSourceList }
|
---|
115 |
|
---|
116 | function TSourceList.FindById(Id: Integer): TSource;
|
---|
117 | var
|
---|
118 | I: Integer;
|
---|
119 | begin
|
---|
120 | I := 0;
|
---|
121 | while (I < Count) and (TSource(Items[I]).Id <> Id) do Inc(I);
|
---|
122 | if I < Count then Result := TSource(Items[I])
|
---|
123 | else Result := nil;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | procedure TSourceList.UpdateFormFile(FileName: string);
|
---|
127 | var
|
---|
128 | Doc: TXMLDocument;
|
---|
129 | NewNode: TDOMNode;
|
---|
130 | NewSubNode: TDOMNode;
|
---|
131 | I: Integer;
|
---|
132 | NewSource: TSource;
|
---|
133 | Child: TDOMNode;
|
---|
134 | ExistedSource: TSource;
|
---|
135 | begin
|
---|
136 | if FileExists(FileName) then
|
---|
137 | try
|
---|
138 | ReadXMLFile(Doc, UTF8Decode(FileName));
|
---|
139 |
|
---|
140 | NewNode := Doc.DocumentElement.FindNode('Items');
|
---|
141 | Child := NewNode.FirstChild;
|
---|
142 | while Assigned(Child) do
|
---|
143 | with Child do begin
|
---|
144 | try
|
---|
145 | NewSource := TSource.Create;
|
---|
146 | with NewSource do begin
|
---|
147 | NewSubNode := Child.FindNode('ProjectName');
|
---|
148 | if Assigned(NewSubNode) then
|
---|
149 | ProjectName := UTF8Encode(string(NewSubNode.TextContent));
|
---|
150 | NewSubNode := Child.FindNode('ProjectShortName');
|
---|
151 | if Assigned(NewSubNode) then
|
---|
152 | ProjectShortName := UTF8Encode(string(NewSubNode.TextContent));
|
---|
153 | NewSubNode := Child.FindNode('ProjectType');
|
---|
154 | if Assigned(NewSubNode) then
|
---|
155 | ProjectType := UTF8Encode(string(NewSubNode.TextContent));
|
---|
156 | NewSubNode := Child.FindNode('Id');
|
---|
157 | if Assigned(NewSubNode) then
|
---|
158 | Id := StrToInt(NewSubNode.TextContent);
|
---|
159 | NewSubNode := Child.FindNode('SubversionURL');
|
---|
160 | if Assigned(NewSubNode) then
|
---|
161 | SubversionURL := UTF8Encode(string(NewSubNode.TextContent));
|
---|
162 | NewSubNode := Child.FindNode('VersionNumber');
|
---|
163 | if Assigned(NewSubNode) then
|
---|
164 | VersionNumber := UTF8Encode(string(NewSubNode.TextContent));
|
---|
165 | NewSubNode := Child.FindNode('VersionType');
|
---|
166 | if Assigned(NewSubNode) then
|
---|
167 | VersionType := UTF8Encode(string(NewSubNode.TextContent));
|
---|
168 | NewSubNode := Child.FindNode('ExecutableFile');
|
---|
169 | if Assigned(NewSubNode) then
|
---|
170 | ExecutableFile := UTF8Encode(string(NewSubNode.TextContent));
|
---|
171 | NewSubNode := Child.FindNode('SourceType');
|
---|
172 | if Assigned(NewSubNode) then
|
---|
173 | SourceType := TSourceType(StrToInt(NewSubNode.TextContent));
|
---|
174 | end;
|
---|
175 | ExistedSource := FindById(NewSource.Id);
|
---|
176 | if Assigned(ExistedSource) then begin
|
---|
177 | ExistedSource.Assign(NewSource);
|
---|
178 | end else begin
|
---|
179 | Add(NewSource);
|
---|
180 | NewSource := nil;
|
---|
181 | end;
|
---|
182 | finally
|
---|
183 | NewSource.Free;
|
---|
184 | end;
|
---|
185 | Child := Child.NextSibling;
|
---|
186 | end;
|
---|
187 | finally
|
---|
188 | Doc.Free;
|
---|
189 | end;
|
---|
190 | end;
|
---|
191 |
|
---|
192 | end.
|
---|
193 |
|
---|