Skip to content

Commit 1021e75

Browse files
committed
RunWithDocker get port and start service
1 parent 35809e9 commit 1021e75

File tree

4 files changed

+152
-44
lines changed

4 files changed

+152
-44
lines changed

Src/Command/Command.Runner.pas

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,61 @@
33
interface
44

55
uses
6-
DOSCommand, OpenToolApi.CommandMessage;
6+
DOSCommand, OpenToolApi.CommandMessage, UtilityFunctions, System.Classes;
77

8-
function Runner(APath, ACommand: string): Integer; overload;
9-
function Runner(ACommand: string): Integer; overload;
8+
type
9+
TRunnerReturn = record
10+
ExitCode: Integer;
11+
Output: string;
12+
constructor Create(AExitCode: Integer; AOutput: string);
13+
end;
14+
15+
function Runner(APath, ACommand: string): TRunnerReturn; overload;
16+
function Runner(ACommand: string): TRunnerReturn; overload;
1017

1118
implementation
1219

1320
uses
1421
System.SysUtils, Vcl.Forms;
1522

16-
var
17-
FMonitorLock: TObject;
18-
19-
function DoRunner(APath, ACommand: string): Integer;
23+
function DoRunner(APath, ACommand: string): TRunnerReturn;
2024
var
2125
LDosCommand: TDosCommand;
2226
begin
23-
//System.TMonitor.Enter(FMonitorLock);
27+
LDosCommand := TDosCommand.Create(nil);
2428
try
25-
LDosCommand := TDosCommand.Create(nil);
26-
try
27-
// LDosCommand.InputToOutput := False;
28-
LDosCommand.CurrentDir := APath;
29-
LDosCommand.CommandLine := ACommand;
30-
LDosCommand.Execute;
31-
32-
while LDosCommand.IsRunning do
33-
begin
34-
Application.ProcessMessages;
35-
end;
36-
37-
Result := LDosCommand.ExitCode;
38-
finally
39-
LDosCommand.Free;
29+
LDosCommand.InputToOutput := True;
30+
LDosCommand.CurrentDir := APath;
31+
LDosCommand.CommandLine := ACommand;
32+
LDosCommand.Execute;
33+
34+
while LDosCommand.IsRunning do
35+
begin
36+
Application.ProcessMessages;
4037
end;
38+
39+
Result := TRunnerReturn.Create(LDosCommand.ExitCode, LDosCommand.Lines.Text);
4140
finally
42-
// System.TMonitor.Exit(FMonitorLock);
41+
LDosCommand.Free;
4342
end;
4443
end;
4544

46-
function Runner(ACommand: string): Integer;
45+
function Runner(ACommand: string): TRunnerReturn;
4746
begin
48-
Result := DoRunner('C:/', ACommand);
47+
Result := DoRunner(ExtractFilePath(ActiveProject.FileName), ACommand);
4948
end;
5049

51-
function Runner(APath, ACommand: string): Integer;
50+
function Runner(APath, ACommand: string): TRunnerReturn;
5251
begin
5352
Result := DoRunner(APath, ACommand);
5453
end;
5554

55+
{ TRunnerReturn }
56+
57+
constructor TRunnerReturn.Create(AExitCode: Integer; AOutput: string);
58+
begin
59+
ExitCode := AExitCode;
60+
Output := AOutput;
61+
end;
62+
5663
end.

Src/Constants/Constants.Version.pas

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
interface
44

5+
uses
6+
SysUtils, Winapi.Windows;
7+
58
type
69

10+
TSemanticVersion = record
11+
Major: integer;
12+
Minor: integer;
13+
Patch: integer;
14+
function ToString: string;
15+
constructor Create(AMajor: integer; AMinor: integer; APatch: integer);
16+
end;
17+
718
TVersion = class
819
private
920
FName: string;
1021
FSemantic: string;
22+
FIDEVersion: TSemanticVersion;
23+
function GetIDEVersion: TSemanticVersion;
1124
class var FInstance: TVersion;
1225
public
1326
class function GetInstance: TVersion;
@@ -19,24 +32,66 @@ TVersion = class
1932

2033
implementation
2134

35+
uses
36+
ToolsAPI;
37+
38+
{ TSemanticVersion }
39+
40+
constructor TSemanticVersion.Create(AMajor, AMinor, APatch: integer);
41+
begin
42+
Major := AMajor;
43+
Minor := AMinor;
44+
Patch := APatch;
45+
end;
46+
47+
function TSemanticVersion.ToString: string;
48+
begin
49+
Result := Major.ToString + '.' + Minor.ToString + '.' + Patch.ToString;
50+
end;
51+
2252
{ TVersion }
2353

2454
constructor TVersion.Create;
2555
begin
26-
// {$IFDEF CONDITIONALEXPRESSIONS}
56+
FIDEVersion := GetIDEVersion;
57+
2758
{$IFDEF VER330}
28-
FName := 'Rio';
29-
FSemantic := '10.3.0';
59+
FName := 'rio';
60+
61+
if FIDEVersion.Minor.ToString.StartsWith('32') then
62+
FSemantic := '10.3.1';
63+
64+
if FIDEVersion.Minor.ToString.StartsWith('32') then
65+
FSemantic := '10.3.0';
3066
{$ENDIF}
31-
{$IFDEF VER331}
32-
FName := 'Rio';
33-
FSemantic := '10.3.1'
67+
{$IFDEF VER320}
68+
FName := 'tokyo';
69+
FSemantic := '10.2.3'
3470
{$ENDIF}
35-
// {$IF RTLVersion >= 14.0}
36-
// {$DEFINE HAS_ERROUTPUT}
37-
// {$IFEND}
38-
// {$ENDIF}
39-
// end;
71+
end;
72+
73+
function TVersion.GetIDEVersion: TSemanticVersion;
74+
var
75+
VerInfoSize: Cardinal;
76+
VerValueSize: Cardinal;
77+
Dummy: Cardinal;
78+
PVerInfo: Pointer;
79+
PVerValue: PVSFixedFileInfo;
80+
LFileName: string;
81+
begin
82+
LFileName := ParamStr(0);
83+
VerInfoSize := GetFileVersionInfoSize(PChar(LFileName), Dummy);
84+
GetMem(PVerInfo, VerInfoSize);
85+
try
86+
if GetFileVersionInfo(PChar(LFileName), 0, VerInfoSize, PVerInfo) then
87+
if VerQueryValue(PVerInfo, '\', Pointer(PVerValue), VerValueSize) then
88+
with PVerValue^ do
89+
begin
90+
Result := TSemanticVersion.Create(HiWord(dwFileVersionMS), HiWord(dwFileVersionLS), LoWord(dwFileVersionLS));
91+
end;
92+
finally
93+
FreeMem(PVerInfo, VerInfoSize);
94+
end;
4095
end;
4196

4297
class function TVersion.GetInstance: TVersion;

Src/Docker/Docker.RunWithDocker.pas

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,69 @@
22

33
interface
44

5+
uses
6+
UtilityFunctions, SysUtils;
7+
58
type
69
TRunWithDocker = class
10+
const
11+
COMMAND_ERROR = 1;
12+
COMMAND_SUCESS = 0;
713
public
814
procedure Execute;
915
end;
1016

1117
implementation
1218

1319
uses
14-
Vcl.Dialogs, Command.Runner, Constants.Version;
20+
Vcl.Dialogs, Command.Runner, Constants.Version, System.Classes;
1521

1622
{ TRunWithDocker }
1723

1824
procedure TRunWithDocker.Execute;
25+
var
26+
LLine, LContainerVersion, LContainerName: string;
27+
LReturn: TRunnerReturn;
28+
LPAServerPort: Integer;
29+
LHasPAServerPort: Boolean;
30+
LPorts: TArray<string>;
1931
begin
20-
// Runner('dir');
21-
Runner('docker ps');
32+
if not(ActiveProject.CurrentPlatform = 'Linux64') then
33+
raise Exception.Create('Plataform not suported');
34+
35+
LContainerVersion := TVersion.GetInstance.Semantic + '-' + TVersion.GetInstance.Name;
36+
LContainerName := ExtractFileName(ActiveProject.FileName).Split(['.'])[0] + '_' + LContainerVersion;
37+
38+
LReturn := Runner('docker start ' + LContainerName);
39+
40+
if LReturn.ExitCode = COMMAND_ERROR then
41+
LReturn := Runner('docker run -tP --name ' + LContainerName + ' hashload/delphi-dev:' +
42+
LContainerVersion);
43+
44+
if LReturn.ExitCode = COMMAND_SUCESS then
45+
begin
46+
LReturn := Runner('docker ps --filter name=teste_10.3.0-rio --format "{{.Ports}}"');
47+
48+
LPorts := LReturn.Output.Split([',']);
49+
for LLine in LPorts do
50+
begin
51+
LHasPAServerPort := False;
52+
if LLine.Contains('->64211/tcp') then
53+
begin
54+
LPAServerPort := LLine.Substring(LLine.IndexOf(':') + 1, LLine.IndexOf('->') - LLine.IndexOf(':') - 1)
55+
.ToInteger;
56+
LHasPAServerPort := True;
57+
Break;
58+
end;
59+
end;
60+
61+
if not LHasPAServerPort then
62+
raise Exception.Create('PAServer not found');
63+
end;
64+
65+
if LReturn.ExitCode = COMMAND_ERROR then
66+
raise Exception.Create(LReturn.Output);
67+
2268
ShowMessage('Version ' + TVersion.GetInstance.Semantic);
2369
end;
2470

Src/wrapper/Wrapper.DockerCompose.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ function DockerComposePS(APath: string): Boolean;
2727

2828
function DockerComposeUp(APath: string): Boolean;
2929
begin
30-
Result := Runner(DOCKER_COMPOSE_UP_D, APath) = 0;
31-
Sleep(100);
32-
TCommandCmd.GetInstance.Run(DOCKER_COMPOSE_LOGS_F, APath);
30+
// Result := Runner(DOCKER_COMPOSE_UP_D, APath) = 0;
31+
// Sleep(100); 8
32+
// TCommandCmd.GetInstance.Run(DOCKER_COMPOSE_LOGS_F, APath);
3333
end;
3434

3535
function DockerComposeDown(APath: string): Boolean;

0 commit comments

Comments
 (0)