-
Notifications
You must be signed in to change notification settings - Fork 17
/
Sempare.Template.RCGenerator.dpr
173 lines (152 loc) · 4.43 KB
/
Sempare.Template.RCGenerator.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
program Sempare.Template.RCGenerator;
{$I 'Sempare.Template.Compiler.inc'}
{$APPTYPE CONSOLE}
{$R *.res}
uses
Sempare.Template,
System.Generics.Collections,
System.Types, // for inline hint
System.Classes,
System.IOUtils,
System.SysUtils;
type
THelperClass = class
class function RCName(const AName: string): string; static;
class function EscapeBackslash(const AName: string): string; static;
end;
procedure ScanDirectory(const ADirectory: string; const AExt: TList<string>; const AFiles: TList<string>);
begin
TDirectory.GetFiles(ADirectory,
function(const APath: string; const SearchRec: TSearchRec): Boolean
var
LExt: string;
begin
for LExt in AExt do
begin
if string(SearchRec.Name).ToLower.EndsWith(LExt.ToLower) then
begin
AFiles.Add(TPath.Combine(APath, SearchRec.Name));
break;
end;
end;
exit(false);
end);
TDirectory.GetDirectories(ADirectory,
function(const APath: string; const SearchRec: TSearchRec): Boolean
begin
ScanDirectory(TPath.Combine(APath, SearchRec.Name), AExt, AFiles);
exit(false);
end);
end;
procedure NormaliseInput(var ATempalteRelPath: string; const AFiles: TList<string>);
var
i: integer;
begin
if not ATempalteRelPath.EndsWith(TPath.DirectorySeparatorChar) then
ATempalteRelPath := ATempalteRelPath + TPath.DirectorySeparatorChar;
for i := 0 to AFiles.Count - 1 do
begin
AFiles[i] := ExtractRelativePath(ATempalteRelPath, AFiles[i]);
end;
end;
procedure WriteRC(const AFilename: string; const AFiles: TList<string>);
type
TData = record
Files: TList<string>;
end;
TUseStream = {$IFDEF SUPPORT_BUFFERED_STREAM}TBufferedFileStream{$ELSE}TFileStream{$ENDIF};
var
LStream: TUseStream;
LData: TData;
begin
LData.Files := AFiles;
LStream := TUseStream.Create(AFilename, fmCreate);
try
TTemplateRegistry.Instance.Eval('sempare_template_rcgenerator_tpl', LData, LStream);
finally
LStream.Free;
end;
end;
var
DEFAULT_EXTS: array [0 .. 8] of string;
procedure main;
var
LRCFilename: string;
LTemplatePath: string;
LTempalteRelPath: string;
LExt: TList<string>;
LFiles: TList<string>;
i: integer;
LLoadStrategy: TArray<TTemplateLoadStrategy>;
begin
if ParamCount < 2 then
begin
writeln(TPath.GetFilename(ParamStr(0)) + ' <rcfilename> <templatepath> [<ext>+]');
writeln;
writeln('<rcfilename> is the path to the filename that will be generated');
writeln('<templatepath> is the path to the directory listing all the templates');
writeln('<ext>+ is one one or more extensions to be included. By default: ' + string.Join(', ', DEFAULT_EXTS));
writeln('');
exit;
end;
DEFAULT_EXTS[0] := '.bmp';
DEFAULT_EXTS[1] := '.gif';
DEFAULT_EXTS[2] := '.ico';
DEFAULT_EXTS[3] := '.jpg';
DEFAULT_EXTS[4] := '.jpeg';
DEFAULT_EXTS[5] := '.png';
DEFAULT_EXTS[6] := '.tpl';
DEFAULT_EXTS[7] := '.wbmp';
DEFAULT_EXTS[8] := '.webp';
LRCFilename := TPath.GetFullPath(ParamStr(1));
LTemplatePath := TPath.GetFullPath(ParamStr(2));
LTempalteRelPath := TPath.GetDirectoryName(LRCFilename);
LFiles := nil;
LExt := nil;
SetLength(LLoadStrategy, 1);
LLoadStrategy[0] := tlsLoadResource;
TTemplateRegistry.Instance.LoadStrategy := LLoadStrategy;
TTemplateRegistry.Instance.Context.Functions.AddFunctions(THelperClass);
try
LFiles := TList<string>.Create;
LExt := TList<string>.Create;
LExt.AddRange(DEFAULT_EXTS);
if ParamCount > 2 then
begin
LExt.Clear;
for i := 3 to ParamCount do
begin
LExt.Add(ParamStr(i));
end;
end;
ScanDirectory(LTemplatePath, LExt, LFiles);
NormaliseInput(LTempalteRelPath, LFiles);
WriteRC(LRCFilename, LFiles);
finally
LExt.Free;
LFiles.Free;
end;
end;
{ THelperClass }
class function THelperClass.EscapeBackslash(const AName: string): string;
begin
exit(AName.Replace(TPath.DirectorySeparatorChar, TPath.DirectorySeparatorChar + TPath.DirectorySeparatorChar, [rfReplaceAll]));
end;
class function THelperClass.RCName(const AName: string): string;
var
LParts: TArray<string>;
begin
result := AName;
LParts := result.Split([TPath.DirectorySeparatorChar]);
result := string.Join(TPath.DirectorySeparatorChar, LParts, 1, length(LParts));
result := result.ToUpper;
result := result.Replace('.', '_', [rfReplaceAll]);
end;
begin
try
main;
except
on E: Exception do
writeln(E.ClassName, ': ', E.Message);
end;
end.