Skip to content

Commit c4d6ca7

Browse files
committed
Add new Compilers.UAutoDetect unit to project
Implements a static class that can detect and register Delphi compilers present on the user's system that are not yet registered with CodeSnip.
1 parent 9209e64 commit c4d6ca7

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

Src/CodeSnip.dpr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,8 @@ uses
370370
UXMLDocConsts in 'UXMLDocConsts.pas',
371371
UXMLDocHelper in 'UXMLDocHelper.pas',
372372
UXMLDocumentEx in 'UXMLDocumentEx.pas',
373-
FmDeleteUserDBDlg in 'FmDeleteUserDBDlg.pas' {DeleteUserDBDlg};
373+
FmDeleteUserDBDlg in 'FmDeleteUserDBDlg.pas' {DeleteUserDBDlg},
374+
Compilers.UAutoDetect in 'Compilers.UAutoDetect.pas';
374375

375376
// Include resources
376377
{$Resource ExternalObj.tlb} // Type library file

Src/CodeSnip.dproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@
575575
<DCCReference Include="FmDeleteUserDBDlg.pas">
576576
<Form>DeleteUserDBDlg</Form>
577577
</DCCReference>
578+
<DCCReference Include="Compilers.UAutoDetect.pas"/>
578579
<None Include="CodeSnip.todo"/>
579580
<BuildConfiguration Include="Base">
580581
<Key>Base</Key>

Src/Compilers.UAutoDetect.pas

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at https://mozilla.org/MPL/2.0/
5+
*
6+
* Copyright (C) 2022, Peter Johnson (gravatar.com/delphidabbler).
7+
*
8+
* Implements a static class that can detect and register Delphi compilers
9+
* present on the user's system that are not yet registered with CodeSnip.
10+
}
11+
12+
13+
unit Compilers.UAutoDetect;
14+
15+
interface
16+
17+
uses
18+
Compilers.UGlobals,
19+
Compilers.UCompilers,
20+
UBaseObjects;
21+
22+
type
23+
TCompilerAutoDetect = class(TNoConstructObject)
24+
public
25+
type
26+
TCallback = reference to procedure (Compiler: ICompiler);
27+
strict private
28+
class procedure DoCallback(const Callback: TCallback;
29+
Compiler: ICompiler);
30+
public
31+
class procedure RegisterCompilers(Compilers: ICompilers;
32+
const Callback: TCallback = nil); overload;
33+
class procedure RegisterSpecificCompilers(AllCompilers: ICompilers;
34+
const RegList: TCompilerList; const Callback: TCallback = nil);
35+
class procedure ListRegisterableCompilers(Compilers: ICompilers;
36+
const Registerable: TCompilerList);
37+
end;
38+
39+
implementation
40+
41+
uses
42+
SysUtils;
43+
44+
{ TCompilerAutoDetect }
45+
46+
class procedure TCompilerAutoDetect.DoCallback(
47+
const Callback: TCallback; Compiler: ICompiler);
48+
begin
49+
if Assigned(Callback) then
50+
Callback(Compiler);
51+
end;
52+
53+
class procedure TCompilerAutoDetect.ListRegisterableCompilers(
54+
Compilers: ICompilers; const Registerable: TCompilerList);
55+
var
56+
Compiler: ICompiler;
57+
begin
58+
Registerable.Clear;
59+
for Compiler in Compilers do
60+
begin
61+
if not Supports(Compiler, ICompilerAutoDetect) then
62+
Continue; // compiler can't be auto-detected/registered
63+
if not (Compiler as ICompilerAutoDetect).IsInstalled then
64+
Continue; // compiler is not installed on the user's system
65+
if Compiler.IsAvailable then
66+
Continue; // compiler installed & already registered for use by CodeSnip
67+
if not (Compiler as ICompilerAutoDetect).GetCanAutoInstall then
68+
Continue; // user has excluded this compiler from being auto-registered
69+
// We get here then we have an installed, un-registered, auto-detectable
70+
// compiler with permission to register
71+
Registerable.Add(Compiler);
72+
end;
73+
end;
74+
75+
class procedure TCompilerAutoDetect.RegisterSpecificCompilers(
76+
AllCompilers: ICompilers; const RegList: TCompilerList;
77+
const Callback: TCallback);
78+
var
79+
Compiler: ICompiler;
80+
begin
81+
for Compiler in AllCompilers do
82+
begin
83+
if RegList.IndexOf(Compiler) >= 0 then
84+
begin
85+
Assert(Supports(Compiler, ICompilerAutoDetect), ClassName +
86+
'.RegisterCompilers: Compiler does not support ICompilerAutoDetect');
87+
if (Compiler as ICompilerAutoDetect).DetectExeFile then
88+
DoCallback(Callback, Compiler);
89+
end;
90+
end;
91+
end;
92+
93+
class procedure TCompilerAutoDetect.RegisterCompilers(Compilers: ICompilers;
94+
const Callback: TCallback);
95+
var
96+
Registerable: TCompilerList;
97+
Compiler: ICompiler;
98+
begin
99+
Registerable := TCompilerList.Create;
100+
try
101+
ListRegisterableCompilers(Compilers, Registerable);
102+
for Compiler in Registerable do
103+
begin
104+
Assert(Supports(Compiler, ICompilerAutoDetect), ClassName +
105+
'.RegisterCompilers: Compiler does not support ICompilerAutoDetect');
106+
if (Compiler as ICompilerAutoDetect).DetectExeFile then
107+
DoCallback(Callback, Compiler);
108+
end;
109+
finally
110+
Registerable.Free;
111+
end;
112+
end;
113+
114+
end.
115+

0 commit comments

Comments
 (0)