-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTreeTransformer.pas
109 lines (97 loc) · 3.04 KB
/
TreeTransformer.pas
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
unit TreeTransformer;
interface
uses
System.SysUtils, System.Classes,
dwsExprs, dwsComp, dwsJSONConnector, dwsFunctions;
type
TDataModule1 = class(TDataModule)
DelphiWebScript1: TDelphiWebScript;
TreeTransformer: TdwsUnit;
dwsJSONLibModule1: TdwsJSONLibModule;
function DelphiWebScript1NeedUnit(const unitName: string; var unitSource:
string): IdwsUnit;
private
procedure DoTransform(const prog: IDwsProgram; const filename: string; const onSuccess, onErr: TProc<string>);
{ Private declarations }
public
{ Public declarations }
procedure RunTransform(const script, path, mask: string; recurse: boolean;
const onSuccess, onErr: TProc<string>);
end;
var
DataModule1: TDataModule1;
implementation
uses
System.Types,
Vcl.Dialogs,
System.IOUtils,
DFMJSON,
dwsErrors,
dwsJson;
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
{ TDataModule1 }
procedure TDataModule1.RunTransform(const script, path, mask: string; recurse: boolean;
const onSuccess, onErr: TProc<string>);
var
files: TStringDynArray;
filename: string;
prog: IDwsProgram;
option: TSearchOption;
begin
prog := DelphiWebScript1.Compile(script);
if prog.Msgs.HasErrors then
begin
onErr(prog.Msgs.AsInfo);
Exit;
end;
if recurse then
option := TSearchOption.soAllDirectories
else option := TSearchOption.soTopDirectoryOnly;
files := TDirectory.GetFiles(path, mask, option);
for filename in files do
DoTransform(prog, filename, onSuccess, onErr);
end;
procedure TDataModule1.DoTransform(const prog: IDwsProgram; const filename: string; const onSuccess, onErr: TProc<string>);
var
exec: IdwsProgramExecution;
dfm: TdwsJSONObject;
begin
try
try
dfm := DFMJSON.Dfm2JSON(filename);
except on EParserError do
try
dfm := DFMJSON.DfmBin2JSON(filename)
except on E: Exception do
begin
if assigned(onErr) then
onErr(format('Error while parsing %s. Exception %s: %s', [filename, e.ClassName, e.Message]));
exit;
end;
end;
end;
exec := prog.BeginNewExecution;
exec.Info.ValueAsVariant['DFM'] := BoxedJSONValue(dfm);
exec.RunProgram(0);
dfm := (IInterface(exec.Info.ValueAsVariant['DFM']) as IBoxedJSONValue).Value as TdwsJSONObject;
exec.EndProgram;
SaveJSON2Dfm(dfm, filename);
if assigned(onSuccess) then
onSuccess(format('Successfully converted %s.', [filename]));
except on E: Exception do
if assigned(onErr) then
onErr(format('Error while converting %s. Exception %s: %s', [filename, e.ClassName, e.Message]));
end;
end;
function TDataModule1.DelphiWebScript1NeedUnit(const unitName: string; var
unitSource: string): IdwsUnit;
var
rtlPath, filename: string;
begin
rtlPath := TPath.Combine(ExtractFilePath(ParamStr(0)), 'RTL');
filename := TPath.Combine(rtlPath, unitName + '.dws');
if FileExists(filename) then
unitSource := TFile.ReadAllText(filename);
end;
end.