Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 87 additions & 8 deletions CryptoLibProcessor.pas
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TCryptoLibProcessor = class
private
class procedure ProcessThisFolder(const AFolder, ACombinedPath: string;
const ALevel: Integer; ALogProcess: TLogProcess);
class function RewriteDirectives(const Text: string): string;
class procedure AddPathsToStrings(const APath: string; AList: TStrings);
class procedure InstallPathsToRegistry(const AVersion, APlatform: String;
PathList: TStrings);
Expand Down Expand Up @@ -78,7 +79,7 @@ class procedure TCryptoLibProcessor.BackupPaths(const AVersions,
begin
for VI := 0 to Length(AVersions) - 1 do
begin
for PI := 0 to Length(APlatforms) do
for PI := 0 to Length(APlatforms) - 1 do
begin
Registry := TRegistry.Create(KEY_READ);
try
Expand Down Expand Up @@ -126,7 +127,7 @@ class procedure TCryptoLibProcessor.InstallPath(const APath: string;
begin
for VI := 0 to Length(AVersions) - 1 do
begin
for PI := 0 to Length(APlatforms) do
for PI := 0 to Length(APlatforms) - 1 do
begin
Registry := TRegistry.Create(KEY_READ);
try
Expand Down Expand Up @@ -161,7 +162,7 @@ class procedure TCryptoLibProcessor.InstallPaths(const SourcePath: string;
AddPathsToStrings(SourcePath, TS);

for VI := 0 to Length(AVersions) - 1 do
for PI := 0 to Length(APlatforms) do
for PI := 0 to Length(APlatforms) - 1 do
InstallPathsToRegistry(AVersions[VI], APlatforms[PI], TS);

finally
Expand Down Expand Up @@ -215,27 +216,105 @@ class procedure TCryptoLibProcessor.InstallPathsToRegistry(const AVersion,
end;
end;

class function TCryptoLibProcessor.RewriteDirectives(const Text: string): string;

function StartsWithAt(const S, Prefix: string; Pos: Integer): Boolean;
begin
Result :=
(Pos > 0) and
(Pos + Prefix.Length - 1 <= S.Length) and
SameText(Copy(S, Pos, Prefix.Length), Prefix);
end;

function RewriteFileDirective(const Full, DirectivePrefix: string): string;
var
EndBrace: Integer;
Inner, CleanInner, FileNameOnly: string;
HasQuotes: Boolean;
begin
Result := Full;

EndBrace := Full.LastIndexOf('}');
if EndBrace < 0 then
Exit;

Inner := Full.Substring(DirectivePrefix.Length,
EndBrace - DirectivePrefix.Length).Trim;

HasQuotes :=
(Inner.Length >= 2) and
(Inner[1] = '''') and
(Inner[Inner.Length] = '''');

CleanInner := Inner;
if HasQuotes then
CleanInner := CleanInner.Trim(['''']);

FileNameOnly := TPath.GetFileName(CleanInner);

if HasQuotes then
Result := DirectivePrefix + '''' + FileNameOnly + '''}'
else
Result := DirectivePrefix + FileNameOnly + '}';
end;

var
i, EndPos: Integer;
Buf, Dir: string;

procedure TryRewriteDirective(const DirectivePrefix: string);
begin
if StartsWithAt(Buf, DirectivePrefix, i) then
begin
EndPos := Buf.IndexOf('}', i - 1);
if EndPos >= 0 then
begin
Dir := Buf.Substring(i - 1, EndPos - i + 2);
Buf := Buf.Remove(i - 1, Dir.Length)
.Insert(i - 1, RewriteFileDirective(Dir, DirectivePrefix));
end;
end;
end;

begin
Buf := Text;
i := 1;

while i <= Buf.Length do
begin
TryRewriteDirective('{$I ');
TryRewriteDirective('{$R ');
Inc(i);
end;

Result := Buf;
end;

class procedure TCryptoLibProcessor.ProcessThisFolder(const AFolder,
ACombinedPath: string; const ALevel: Integer; ALogProcess: TLogProcess);
var
lFiles, lFolders: TStringDynArray;
S, lName, lTargetName, fileContents, lInc: string;
S, lName, lTargetName, fileContents: string;
begin
if Assigned(ALogProcess) then
ALogProcess(' ' + StringOfChar('-', ALevel * 2) + ' Processing ' + AFolder);

lFolders := TDirectory.GetDirectories(AFolder);
lFiles := TDirectory.GetFiles(AFolder);

for S in lFiles do
begin
lName := TPath.GetFileName(S);
lTargetName := TPath.Combine(ACombinedPath, lName);
if TPath.GetExtension(S).ToLower = '.pas' then

if (TPath.GetExtension(S).ToLower = '.pas') or
(TPath.GetExtension(S).ToLower = '.inc') then
begin
fileContents := TFile.ReadAllText(S);
lInc := '{$I ' + DupeString('..\', ALevel) + 'Include\';
fileContents := fileContents.Replace(lInc, '{$I ', [rfReplaceAll]);
TFile.WriteAllText(lTargetName, fileContents);

fileContents := RewriteDirectives(fileContents);

TFile.WriteAllText(lTargetName, fileContents, TEncoding.UTF8);
end
else
begin
Expand Down
8 changes: 8 additions & 0 deletions MainForm2.pas
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ implementation
CRYPTO_LIB_GIT = 'https://github.com/Xor-el/CryptoLib4Pascal.git';
SIMPLE_BASE_LIB_GIT = 'https://github.com/Xor-el/SimpleBaseLib4Pascal.git';
QR_CODE_GIT = 'https://github.com/Xor-el/QRCodeGenLib4Pascal.git';
SOL_LIB_GIT = 'https://github.com/Xor-el/SolLib4Pascal.git';

HASH_LIB_PATH = 'HashLib4Pascal';
CRYPTO_LIB_PATH = 'CryptoLib4Pascal';
SIMPLE_BASE_LIB_PATH = 'SimpleBaseLib4Pascal';
QR_CODE_PATH = 'QRCodeGenLib4Pascal';
SOL_LIB_PATH = 'SolLib4Pascal';
COMBINED_PATH = 'CombinedCryptoLib';

HASH_LIB_SRC_PATH = 'HashLib4Pascal\HashLib\src';
CRYPTO_LIB_SRC_PATH = 'CryptoLib4Pascal\CryptoLib\src';
SIMPLE_BASE_LIB_SRC_PATH = 'SimpleBaseLib4Pascal\SimpleBaseLib\src';
SOL_LIB_SRC_PATH = 'SolLib4Pascal\SolLib\src';

procedure TMainFormNew.AddAllPathsToLibrary;
begin
Expand All @@ -136,6 +139,8 @@ procedure TMainFormNew.AddAllPathsToLibrary;
GetSelectedPlatforms);
TCryptoLibProcessor.InstallPaths(CRYPTO_LIB_SRC_PATH, GetSelectedVersions,
GetSelectedPlatforms);
TCryptoLibProcessor.InstallPaths(SOL_LIB_SRC_PATH, GetSelectedVersions,
GetSelectedPlatforms);
end;

procedure TMainFormNew.BrowseCryptoLibRootAccept(Sender: TObject);
Expand Down Expand Up @@ -180,6 +185,7 @@ procedure TMainFormNew.CloneUpdateActionExecute(Sender: TObject);
CloneOrUpdate(HASH_LIB_PATH, HASH_LIB_GIT);
CloneOrUpdate(CRYPTO_LIB_PATH, CRYPTO_LIB_GIT);
CloneOrUpdate(QR_CODE_PATH, QR_CODE_GIT);
CloneOrUpdate(SOL_LIB_PATH, SOL_LIB_GIT);
RunNextCommand;
end;

Expand Down Expand Up @@ -219,6 +225,8 @@ function TMainFormNew.CombineLibs: string;
HASH_LIB_SRC_PATH), Result, LogThis);
TCryptoLibProcessor.AddToCombinedLib(TPath.Combine(CryptoLibRootPath,
CRYPTO_LIB_SRC_PATH), Result, LogThis);
TCryptoLibProcessor.AddToCombinedLib(TPath.Combine(CryptoLibRootPath,
SOL_LIB_SRC_PATH), Result, LogThis);
end;

procedure TMainFormNew.CryptoLibsRootChange(Sender: TObject);
Expand Down