|
| 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