1 | unit UExternalTool;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, process;
|
---|
9 |
|
---|
10 | var
|
---|
11 | ClearToolPath: string;
|
---|
12 | OMakePath: string;
|
---|
13 | SubstPath: string;
|
---|
14 | RationalRosePath: string;
|
---|
15 | SuperTracerPath: string;
|
---|
16 | BeyondComparePath: string;
|
---|
17 |
|
---|
18 | const
|
---|
19 | SVNPath: string = 'c:\Program Files\TortoiseSVN\bin\svn.exe';
|
---|
20 | SVNAdminPath: string = 'c:\Program Files\TortoiseSVN\bin\svnadmin.exe';
|
---|
21 |
|
---|
22 | function ExecuteProcessOut(Executable: string; Parameters: array of string;
|
---|
23 | CurrentDir: string = ''): string;
|
---|
24 | function ClearTool(Parameters: array of string; CurrentDir: string = ''): string;
|
---|
25 | procedure ExecuteNoWait(AExecutable: string; Params: array of string;
|
---|
26 | Maximized: Boolean = false; CurrentDir: string = '');
|
---|
27 | function SVN(Parameters: array of string; CurrentDir: string = ''): string;
|
---|
28 |
|
---|
29 |
|
---|
30 | implementation
|
---|
31 |
|
---|
32 | function ExecuteProcessOut(Executable: string; Parameters: array of string;
|
---|
33 | CurrentDir: string = ''): string;
|
---|
34 | var
|
---|
35 | Process: TProcess;
|
---|
36 | I: Integer;
|
---|
37 | Cmd: string;
|
---|
38 | Buffer: string;
|
---|
39 | Count: Integer;
|
---|
40 | begin
|
---|
41 | Result := '';
|
---|
42 | try
|
---|
43 | Process := TProcess.Create(nil);
|
---|
44 | Process.CurrentDirectory := CurrentDir;
|
---|
45 | Process.Executable := Executable;
|
---|
46 | Process.ShowWindow := swoHIDE;
|
---|
47 | Process.StartupOptions := [suoUseShowWindow];
|
---|
48 | for I := 0 to Length(Parameters) - 1 do
|
---|
49 | Process.Parameters.Add(Parameters[I]);
|
---|
50 | for I := 0 to GetEnvironmentVariableCount - 1 do
|
---|
51 | Process.Environment.Add(GetEnvironmentString(I));
|
---|
52 | Process.Options := [poUsePipes, poNoConsole];
|
---|
53 | Process.Parameters.LineBreak := ' ';
|
---|
54 | Process.Execute;
|
---|
55 |
|
---|
56 | while Process.Running or (Process.Output.NumBytesAvailable > 0) or
|
---|
57 | (Process.Stderr.NumBytesAvailable > 0) do
|
---|
58 | begin
|
---|
59 | if Process.Output.NumBytesAvailable > 0 then begin
|
---|
60 | SetLength(Buffer, 1000);
|
---|
61 | Count := Process.Output.Read(Buffer[1], Length(Buffer));
|
---|
62 | SetLength(Buffer, Count);
|
---|
63 | Result := Result + Buffer;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | if Process.Stderr.NumBytesAvailable > 0 then begin
|
---|
67 | SetLength(Buffer, 1000);
|
---|
68 | Count := Process.Stderr.Read(Buffer[1], Length(Buffer));
|
---|
69 | SetLength(Buffer, Count);
|
---|
70 | Result := Result + Buffer;
|
---|
71 | end;
|
---|
72 | Sleep(1);
|
---|
73 | end;
|
---|
74 |
|
---|
75 | if Process.ExitCode <> 0 then begin
|
---|
76 | Cmd := Process.Executable + ' ' + Process.Parameters.Text;
|
---|
77 | raise Exception.Create('Execution of "' + Cmd + '" failed with exit code ' + IntToStr(Process.ExitCode) + ' error: ' + Result);
|
---|
78 | end;
|
---|
79 | finally
|
---|
80 | Process.Free;
|
---|
81 | end;
|
---|
82 | end;
|
---|
83 |
|
---|
84 | function ClearTool(Parameters: array of string; CurrentDir: string = ''): string;
|
---|
85 | begin
|
---|
86 | Result := Trim(ExecuteProcessOut(ClearToolPath, Parameters, CurrentDir));
|
---|
87 | end;
|
---|
88 |
|
---|
89 | function SVN(Parameters: array of string; CurrentDir: string = ''): string;
|
---|
90 | begin
|
---|
91 | Result := Trim(ExecuteProcessOut(SVNPath, Parameters, CurrentDir));
|
---|
92 | end;
|
---|
93 |
|
---|
94 | procedure ExecuteNoWait(AExecutable: string; Params: array of string;
|
---|
95 | Maximized: Boolean = false; CurrentDir: string = '');
|
---|
96 | var
|
---|
97 | I: Integer;
|
---|
98 | begin
|
---|
99 | with TProcess.Create(nil) do
|
---|
100 | try
|
---|
101 | Executable := AExecutable;
|
---|
102 | CurrentDirectory := CurrentDir;
|
---|
103 | for I := 0 to Length(Params) - 1 do
|
---|
104 | Parameters.Add(Params[I]);
|
---|
105 | if Maximized then ShowWindow := swoMaximize;
|
---|
106 | Options := [poNoConsole];
|
---|
107 | Execute;
|
---|
108 | finally
|
---|
109 | Free;
|
---|
110 | end;
|
---|
111 | end;
|
---|
112 |
|
---|
113 |
|
---|
114 | end.
|
---|
115 |
|
---|