source: trunk/Units/VCS.pas

Last change on this file was 24, checked in by chronos, 12 days ago
  • Fixed: Test cases executed with FormTests form from Common package.
File size: 6.4 KB
Line 
1unit VCS;
2
3interface
4
5uses
6 Classes, SysUtils, LazFileUtils, Generics.Collections;
7
8type
9
10 { TLogItem }
11
12 TLogItem = class
13 Author: string;
14 Actions: string;
15 Time: TDateTime;
16 Revision: string;
17 Messages: TStringList;
18 ChangedFiles: TStringList;
19 constructor Create;
20 destructor Destroy; override;
21 end;
22
23 TLogItems = class(TObjectList<TLogItem>)
24 end;
25
26 TFileStatusState = (fssNonVersioned, fssAdded, fssRemoved, fssModified, fssNotModified);
27
28 { TFileStatus }
29
30 TFileStatus = class
31 FileName: string;
32 Version: string;
33 Author: string;
34 Time: TDateTime;
35 State: TFileStatusState;
36 procedure Assign(Source: TFileStatus);
37 end;
38
39 { TFileStatuses }
40
41 TFileStatuses = class(TObjectList<TFileStatus>)
42 function SearchByName(Name: string): TFileStatus;
43 end;
44
45 { TWorkingCopy }
46
47 TWorkingCopy = class
48 private
49 FPath: string;
50 FRepositoryURL: string;
51 procedure SetPath(AValue: string);
52 procedure SetRepositoryURL(AValue: string);
53 protected
54 EnvVars: TStringList;
55 procedure ExecuteProcess(Command: string; Parameters: array of string); virtual;
56 function GetNext(var Text: string; Separator: string): string;
57 public
58 UserName: string;
59 Password: string;
60 Email: string;
61 SilentExecution: Boolean;
62 ExecutionOutput: TStringList;
63 procedure Checkout; virtual;
64 procedure Update; virtual;
65 procedure CleanUp; virtual;
66 procedure Commit(Message: string); virtual;
67 procedure Move(Source, Dest: string); virtual;
68 procedure Merge; virtual;
69 procedure Refresh; virtual;
70 procedure Add(FileName: string); virtual;
71 procedure Remove(FileName: string); virtual;
72 procedure GetLog(FileName: string; Log: TLogItems); virtual;
73 procedure GetStatus(FileName: string; Status: TFileStatuses); virtual;
74 constructor Create;
75 destructor Destroy; override;
76 property RepositoryURL: string read FRepositoryURL write SetRepositoryURL;
77 property Path: string read FPath write SetPath;
78 end;
79
80 { TRepository }
81
82 TRepository = class
83 protected
84 procedure ExecuteProcess(Command: string; Parameters: array of string); virtual;
85 public
86 ExecutionOutput: TStringList;
87 Path: string;
88 SilentExecution: Boolean;
89 procedure Init; virtual;
90 constructor Create;
91 destructor Destroy; override;
92 end;
93
94function URLFromDirectory(DirName: string; Relative: Boolean): string;
95
96const
97 FileStatusStateText: array[TFileStatusState] of string = ('Not versioned',
98 'Added', 'Removed', 'Modified', 'Normal');
99
100
101implementation
102
103uses
104 FormConsole;
105
106function URLFromDirectory(DirName: string; Relative: Boolean): string;
107begin
108 Result := DirName;
109 if Relative then Result := GetCurrentDir + DirectorySeparator + Result;
110 Result := 'file:///' + StringReplace(Result, DirectorySeparator, '/', [rfReplaceAll]);
111end;
112
113{ TFileStatus }
114
115procedure TFileStatus.Assign(Source: TFileStatus);
116begin
117 FileName := Source.FileName;
118 Version := Source.Version;
119 Time := Source.Time;
120 Author := Source.Author;
121 State := Source.State;
122end;
123
124{ TFileStatuses }
125
126function TFileStatuses.SearchByName(Name: string): TFileStatus;
127var
128 I: Integer;
129begin
130 I := 0;
131 while (I < Count) and (TFileStatus(Items[I]).FileName <> Name) do Inc(I);
132 if I < Count then Result := TFileStatus(Items[I])
133 else Result := nil;
134end;
135
136{ TRepository }
137
138procedure TRepository.ExecuteProcess(Command: string;
139 Parameters: array of string);
140var
141 FormConsole: TFormConsole;
142begin
143 FormConsole := TFormConsole.Create(nil);
144 try
145 FormConsole.Executable := Command;
146 FormConsole.Parameters.Clear;
147 FormConsole.Parameters.AddStrings(Parameters);
148 if DirectoryExists(Path) then FormConsole.WorkingDir := Path
149 else FormConsole.WorkingDir := '';
150 if SilentExecution then FormConsole.Perform
151 else FormConsole.ShowModal;
152 ExecutionOutput.Assign(FormConsole.Log);
153 finally
154 FormConsole.Free;
155 end;
156end;
157
158procedure TRepository.Init;
159begin
160end;
161
162constructor TRepository.Create;
163begin
164 ExecutionOutput := TStringList.Create;
165 Path := '';
166end;
167
168destructor TRepository.Destroy;
169begin
170 FreeAndNil(ExecutionOutput);
171 inherited;
172end;
173
174{ TLogItem }
175
176constructor TLogItem.Create;
177begin
178 Messages := TStringList.Create;
179 ChangedFiles := TStringList.Create;
180end;
181
182destructor TLogItem.Destroy;
183begin
184 FreeAndNil(Messages);
185 FreeAndNil(ChangedFiles);
186 inherited;
187end;
188
189{ TWorkingCopy }
190
191procedure TWorkingCopy.SetPath(AValue: string);
192begin
193 if FPath = AValue then Exit;
194 FPath := AValue;
195 Refresh;
196end;
197
198procedure TWorkingCopy.SetRepositoryURL(AValue: string);
199begin
200 if FRepositoryURL = AValue then Exit;
201 FRepositoryURL := AValue;
202end;
203
204procedure TWorkingCopy.ExecuteProcess(Command: string; Parameters: array of string);
205var
206 FormConsole: TFormConsole;
207begin
208 FormConsole := TFormConsole.Create(nil);
209 try
210 FormConsole.Executable := Command;
211 FormConsole.EnvironmentVariables.Assign(EnvVars);
212 FormConsole.Parameters.Clear;
213 FormConsole.Parameters.AddStrings(Parameters);
214 if DirectoryExists(Path) then FormConsole.WorkingDir := Path
215 else FormConsole.WorkingDir := '';
216 if SilentExecution then FormConsole.Perform
217 else FormConsole.ShowModal;
218 ExecutionOutput.Assign(FormConsole.Log);
219 finally
220 FormConsole.Free;
221 end;
222end;
223
224function TWorkingCopy.GetNext(var Text: string; Separator: string): string;
225begin
226 if Pos(Separator, Text) > 0 then begin
227 Result := Copy(Text, 1, Pos(Separator, Text) - 1);
228 Delete(Text, 1, Pos(Separator, Text) + Length(Separator));
229 end else begin
230 Result := Text;
231 Text := '';
232 end;
233end;
234
235procedure TWorkingCopy.Checkout;
236begin
237end;
238
239procedure TWorkingCopy.Update;
240begin
241end;
242
243procedure TWorkingCopy.CleanUp;
244begin
245end;
246
247procedure TWorkingCopy.Commit(Message: string);
248begin
249end;
250
251procedure TWorkingCopy.Move(Source, Dest: string);
252begin
253end;
254
255procedure TWorkingCopy.Merge;
256begin
257end;
258
259procedure TWorkingCopy.Refresh;
260begin
261end;
262
263procedure TWorkingCopy.Add(FileName: string);
264begin
265end;
266
267procedure TWorkingCopy.Remove(FileName: string);
268begin
269end;
270
271procedure TWorkingCopy.GetLog(FileName: string; Log: TLogItems);
272begin
273 Log.Clear;
274end;
275
276procedure TWorkingCopy.GetStatus(FileName: string; Status: TFileStatuses);
277begin
278 Status.Clear;
279end;
280
281constructor TWorkingCopy.Create;
282begin
283 ExecutionOutput := TStringList.Create;
284 EnvVars := TStringList.Create;
285 FPath := '';
286 FRepositoryURL := '';
287end;
288
289destructor TWorkingCopy.Destroy;
290begin
291 FreeAndNil(EnvVars);
292 FreeAndNil(ExecutionOutput);
293 inherited;
294end;
295
296end.
297
Note: See TracBrowser for help on using the repository browser.