Skip to content

Commit 496eb87

Browse files
committed
Added docker call cmd
1 parent 320fe61 commit 496eb87

File tree

11 files changed

+140
-76
lines changed

11 files changed

+140
-76
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ modules/
6666
# Castalia statistics file (since XE7 Castalia is distributed with Delphi)
6767
*.stat
6868
*.cbk
69+
modules

DelphiDocker.dpk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ package DelphiDocker;
3030

3131
requires
3232
rtl,
33-
designide,
34-
boss_ide;
33+
designide;
3534

3635
contains
3736
OpenToolApi.Register in 'Src\OpenToolApi\OpenToolApi.Register.pas',
@@ -44,6 +43,7 @@ contains
4443
Wrapper.Docker in 'Src\wrapper\Wrapper.Docker.pas',
4544
Wrapper.DockerCompose in 'Src\wrapper\Wrapper.DockerCompose.pas',
4645
Command.Cmd in 'Src\Command\Command.Cmd.pas',
47-
Command.Message in 'Src\Command\Command.Message.pas';
46+
Command.Message in 'Src\Command\Command.Message.pas',
47+
Docker.Utils in 'Src\Docker\Docker.Utils.pas';
4848

4949
end.

DelphiDocker.dproj

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
<DCC_RemoteDebug>false</DCC_RemoteDebug>
9696
<VerInfo_IncludeVerInfo>true</VerInfo_IncludeVerInfo>
9797
<VerInfo_Locale>1033</VerInfo_Locale>
98-
<Debugger_HostApplication>E:\tools\Embarcadero\Studio\20.0\bin\bds.exe</Debugger_HostApplication>
98+
<Debugger_HostApplication>E:\tools\Embarcadero\Studio\19.0\bin\bds.exe</Debugger_HostApplication>
9999
</PropertyGroup>
100100
<PropertyGroup Condition="'$(Cfg_2)'!=''">
101101
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
@@ -113,7 +113,6 @@
113113
</DelphiCompile>
114114
<DCCReference Include="rtl.dcp"/>
115115
<DCCReference Include="designide.dcp"/>
116-
<DCCReference Include="boss_ide.dcp"/>
117116
<DCCReference Include="Src\OpenToolApi\OpenToolApi.Register.pas"/>
118117
<DCCReference Include="Src\OpenToolApi\OpenToolApi.ToolBarItem.pas">
119118
<Form>ToolBarItem</Form>
@@ -129,6 +128,7 @@
129128
<DCCReference Include="Src\wrapper\Wrapper.DockerCompose.pas"/>
130129
<DCCReference Include="Src\Command\Command.Cmd.pas"/>
131130
<DCCReference Include="Src\Command\Command.Message.pas"/>
131+
<DCCReference Include="Src\Docker\Docker.Utils.pas"/>
132132
<BuildConfiguration Include="Release">
133133
<Key>Cfg_2</Key>
134134
<CfgParent>Base</CfgParent>
@@ -150,11 +150,8 @@
150150
<Source Name="MainSource">DelphiDocker.dpk</Source>
151151
</Source>
152152
<Excluded_Packages>
153-
<Excluded_Packages Name="$(BDSBIN)\DataExplorerDBXPluginEnt260.bpl">DBExpress Enterprise Data Explorer Integration</Excluded_Packages>
154-
<Excluded_Packages Name="$(BDSBIN)\bcboffice2k260.bpl">Embarcadero C++Builder Office 2000 Servers Package</Excluded_Packages>
155-
<Excluded_Packages Name="$(BDSBIN)\bcbofficexp260.bpl">Embarcadero C++Builder Office XP Servers Package</Excluded_Packages>
156-
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k260.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
157-
<Excluded_Packages Name="$(BDSBIN)\dclofficexp260.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
153+
<Excluded_Packages Name="$(BDSBIN)\dcloffice2k250.bpl">Microsoft Office 2000 Sample Automation Server Wrapper Components</Excluded_Packages>
154+
<Excluded_Packages Name="$(BDSBIN)\dclofficexp250.bpl">Microsoft Office XP Sample Automation Server Wrapper Components</Excluded_Packages>
158155
</Excluded_Packages>
159156
</Delphi.Personality>
160157
<Deployment Version="3">

Src/Command/Command.Runner.pas

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,36 @@ implementation
1212
uses
1313
System.SysUtils, Vcl.Forms;
1414

15+
var
16+
FMonitorLock: TObject;
17+
1518
function Runner(ACommand, APath: string): Cardinal;
1619
var
1720
LDosCommand: TDosCommand;
1821
begin
19-
LDosCommand := TDosCommand.Create(nil);
22+
System.TMonitor.Enter(FMonitorLock);
2023
try
21-
LDosCommand.OnNewLine := procedure(ASender: TObject; const ANewLine: string; AOutputType: TOutputType)
22-
begin
23-
// TCommandMessage.GetInstance.WriteLn(ANewLine);
24-
end;
25-
26-
LDosCommand.OnTerminated := procedure(ASender: TObject)
24+
LDosCommand := TDosCommand.Create(nil);
25+
try
26+
LDosCommand.InputToOutput := False;
27+
LDosCommand.CurrentDir := APath;
28+
LDosCommand.CommandLine := ACommand;
29+
LDosCommand.Execute;
30+
31+
while LDosCommand.IsRunning do
2732
begin
28-
// ASender.Free;
33+
Application.ProcessMessages;
34+
Sleep(0);
2935
end;
3036

31-
LDosCommand.InputToOutput := False;
32-
LDosCommand.CurrentDir := APath;
33-
LDosCommand.CommandLine := ACommand;
34-
LDosCommand.Execute;
37+
Result := LDosCommand.ExitCode
3538

36-
while LDosCommand.IsRunning do
37-
begin
38-
Application.ProcessMessages;
39-
Sleep(0);
39+
finally
40+
LDosCommand.Free;
4041
end;
41-
42-
Result := LDosCommand.ExitCode
43-
4442
finally
45-
LDosCommand.Free;
43+
System.TMonitor.Exit(FMonitorLock);
4644
end;
4745
end;
4846

49-
50-
5147
end.

Src/Docker/Docker.Utils.pas

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
unit Docker.Utils;
2+
3+
interface
4+
5+
function DockerIsRunning: Boolean;
6+
function DockerIsInstalled: Boolean;
7+
8+
implementation
9+
10+
uses
11+
Winapi.TlHelp32, Winapi.Windows, System.SysUtils;
12+
13+
function processExists(exeFileName: string): Boolean;
14+
var
15+
ContinueLoop: BOOL;
16+
FSnapshotHandle: THandle;
17+
FProcessEntry32: TProcessEntry32;
18+
begin
19+
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
20+
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
21+
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
22+
Result := False;
23+
while Integer(ContinueLoop) <> 0 do
24+
begin
25+
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = UpperCase(exeFileName)) or
26+
(UpperCase(FProcessEntry32.szExeFile) = UpperCase(exeFileName))) then
27+
begin
28+
Result := True;
29+
end;
30+
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
31+
end;
32+
CloseHandle(FSnapshotHandle);
33+
end;
34+
35+
function DockerIsRunning: Boolean;
36+
begin
37+
processExists('dockerd.exe')
38+
end;
39+
40+
function DockerIsInstalled: Boolean;
41+
begin
42+
Result := processExists('where docker');
43+
end;
44+
45+
46+
end.

Src/OpenToolApi/OpenToolApi.Register.pas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ procedure AddToolBarDocker;
3232
try
3333
LServices.AddImages(TToolBarItem.GetInstance.ImageListDocker);
3434

35-
UtilityFunctions.CreateMenuItem('RunWithDocker', 'Run With Docker', 'Run', TToolBarItem.GetInstance.teste, nil,
35+
UtilityFunctions.CreateMenuItem('RunWithDocker', 'Run With Docker', 'Run',
36+
TToolBarItem.GetInstance.RunWithDocker, nil,
3637
False, True, 'Alt+F9');
3738

3839
finally

Src/OpenToolApi/OpenToolApi.ToolBarItem.dfm

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,4 @@ object ToolBarItem: TToolBarItem
145145
0000000000000000000000000000000000000000000000000000000000000000
146146
000000000000}
147147
end
148-
object ActionListDocker: TActionList
149-
Images = ImageListDocker
150-
OnExecute = ActionListDockerExecute
151-
OnUpdate = ActionListDockerUpdate
152-
Left = 40
153-
Top = 40
154-
object ToolBarItemDocker: TAction
155-
Category = 'Run'
156-
Caption = 'Run With Docker'
157-
ImageIndex = 0
158-
end
159-
end
160-
object MainMenuDocker: TMainMenu
161-
Left = 128
162-
Top = 88
163-
object Este21: TMenuItem
164-
Action = ToolBarItemDocker
165-
end
166-
end
167148
end

Src/OpenToolApi/OpenToolApi.ToolBarItem.pas

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,28 @@ interface
88
type
99
TToolBarItem = class(TDataModule)
1010
ImageListDocker: TImageList;
11-
ActionListDocker: TActionList;
12-
ToolBarItemDocker: TAction;
13-
MainMenuDocker: TMainMenu;
14-
Este21: TMenuItem;
15-
procedure ActionListDockerExecute(Action: TBasicAction; var Handled: Boolean);
16-
procedure ActionListDockerUpdate(Action: TBasicAction; var Handled: Boolean);
1711
private
12+
FImageStartIndex: Integer;
1813
class var FInstance: TToolBarItem;
1914
public
2015
class function GetInstance: TToolBarItem;
21-
class procedure teste(ASender:TObject);
16+
class procedure RunWithDocker(ASender: TObject);
2217
class procedure Release;
2318
constructor Create; reintroduce;
2419
end;
2520

2621
implementation
2722

2823
uses
29-
ToolsAPI, Vcl.Dialogs, Wrapper.DockerCompose, UtilityFunctions;
24+
ToolsAPI, Vcl.Dialogs, Wrapper.DockerCompose, UtilityFunctions, Wrapper.Docker;
3025

3126
{%CLASSGROUP 'Vcl.Controls.TControl'}
3227
{$R *.dfm}
3328

34-
procedure TToolBarItem.ActionListDockerExecute(Action: TBasicAction; var Handled: Boolean);
35-
begin
36-
ShowMessage('Rodando com docker');
37-
end;
38-
39-
procedure TToolBarItem.ActionListDockerUpdate(Action: TBasicAction; var Handled: Boolean);
40-
begin
41-
// ShowMessage('Update');
42-
end;
43-
4429
constructor TToolBarItem.Create;
4530
begin
4631
inherited Create(nil);
32+
FImageStartIndex := NativeServices.AddImages(ImageListDocker, 'docker');
4733
end;
4834

4935
class function TToolBarItem.GetInstance: TToolBarItem;
@@ -60,15 +46,33 @@ class procedure TToolBarItem.Release;
6046
FInstance.Free;
6147
end;
6248

63-
class procedure TToolBarItem.teste(ASender: TObject);
49+
class procedure TToolBarItem.RunWithDocker(ASender: TObject);
6450
var
65-
LDebugger: IOTADebuggerServices;
51+
LDebugger: IOTADebuggerServices;
52+
LMainMenu: TMainMenu;
53+
LMenuItem: TMenuItem;
6654
begin
67-
DockerComposeUp(ExtractFilePath(ActiveProject.FileName));
55+
if not DoDockerPreFlight then
56+
Exit;
57+
58+
59+
60+
61+
if not DockerComposeUp(ExtractFilePath(ActiveProject.FileName)) then
62+
begin
63+
MessageDlg('Can''t start docker compose or docker not started, verify output messages.',
64+
TMsgDlgType.mtError,
65+
[TMsgDlgBtn.mbOK],
66+
1);
67+
Exit;
68+
end;
69+
70+
LMainMenu := NativeServices.MainMenu;
6871

69-
ActiveProject.ProjectBuilder.BuildProject(TOTACompileMode.cmOTABuild, True, True);
72+
LMenuItem := LMainMenu.Items.Find('Run').Find('Run');
73+
if Assigned(LMenuItem) then
74+
LMenuItem.Action.Execute;
7075

71-
// (BorlandIDEServices as IOTADebuggerServices).CreateProcess('E:\Projetos\_playground\dosCommandTest\Win32\Debug\Project3.exe', '');
7276
end;
7377

7478
initialization

Src/OpenToolApi/UtilityFunctions.pas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@
103103
strShortCut : String) : TMenuItem;
104104
Procedure PatchActionShortcuts(Sender : TObject);
105105

106+
function NativeServices: INTAServices;
107+
106108
Implementation
107109

108110
Uses
@@ -1129,6 +1131,12 @@ function FindMenuItem(strParentMenu : String): TMenuItem;
11291131
{$ENDIF}
11301132
End;
11311133

1134+
1135+
function NativeServices: INTAServices;
1136+
begin
1137+
Result := (BorlandIDEServices as INTAServices);
1138+
end;
1139+
11321140
(** Creates an object list for storing all action reference so they can be
11331141
removed from the IDE. **)
11341142
Initialization

Src/wrapper/Wrapper.Docker.pas

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,38 @@
22

33
interface
44

5+
function DockerIsRunning: Boolean;
6+
function DockerIsInstalled: Boolean;
57

8+
function DoDockerPreFlight: Boolean;
69

710
implementation
811

12+
uses
13+
Command.Runner, System.SysUtils;
14+
15+
const
16+
Docker = 'docker';
17+
DOCKER_PS = Docker + ' ps';
18+
WHERE_DOCKER = 'where docker.exe';
19+
20+
function DockerIsRunning: Boolean;
21+
begin
22+
Result := Runner(DOCKER_PS) = 0;
23+
end;
24+
25+
function DockerIsInstalled: Boolean;
26+
begin
27+
Result := Runner(WHERE_DOCKER) = 0;
28+
end;
29+
30+
function DoDockerPreFlight: Boolean;
31+
begin
32+
if not DockerIsInstalled then
33+
raise Exception.Create('Docker is not installed on your system.');
34+
35+
if not DockerIsRunning then
36+
raise Exception.Create('Docker is not started.');
37+
end;
38+
939
end.

0 commit comments

Comments
 (0)