-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJSONExport.pas
216 lines (182 loc) · 5.3 KB
/
JSONExport.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
unit JSONExport;
interface
uses
System.Classes,
DelphiProject,
DelphiUnit;
type
TJSONExportOption = (JSONExportIntfUses, JSONExportImplUses);
TJSONExportOptions = set of TJSONExportOption;
TJSONExport = class
private
public
constructor Create;
destructor Destroy; override;
procedure ExportProjectToFile(Project: TDelphiProject; FileName: String; Options: TJSONExportOptions);
procedure ExportProjectToStrings(Project: TDelphiProject; Strings: TStrings; Options: TJSONExportOptions);
end;
implementation
uses
System.SysUtils,
JsonDataObjects;
const
cGraphId = 'F7F603A5-15E4-4B7B-9155-6B311F93344E';
cNodeType = '28324482-311B-4B60-8915-ED3F0D289195';
cIntfEdgeType = 'E087D745-F82B-4994-9876-BBE79C6BEBA8';
cImplEdgeType = 'A955057F-B3A2-4217-A47A-FD96A5463369';
constructor TJSONExport.Create;
begin
end;
destructor TJSONExport.Destroy;
begin
inherited;
end;
procedure TJSONExport.ExportProjectToFile(Project: TDelphiProject; FileName: String; Options: TJSONExportOptions);
var
Strings: TStringList;
begin
Strings:=TStringList.Create;
try
ExportProjectToStrings(Project, Strings, Options);
Strings.SaveToFile(FileName);
finally
FreeAndNil(Strings);
end;
end;
procedure ExportGraphToStrings(Project: TDelphiProject; Strings: TStrings; Options: TJSONExportOptions);
procedure ExportIntfUses(JSON: TJSONArray; U: TDelphiUnit; var EdgeId: Integer);
var
RefUnit: TDelphiUnit;
Edge: TJSONObject;
begin
for RefUnit in U.InterfaceUses do
if RefUnit.Parsed then
begin
Edge:=TJSONObject.Create;
Edge.S['id' ]:= IntToStr(EdgeId);
Edge.S['from' ]:= IntToStr(Integer(U));
Edge.S['to' ]:= IntToStr(Integer(RefUnit));
Edge.I['directed' ]:= 1;
Edge.S['name' ]:= 'Intf Ref';
Edge.S['type_id'] := cIntfEdgeType;
Edge.O['properties'] := TJSONObject.Create;
Edge.O['reference'] := nil;
JSON.Add(Edge);
Inc(EdgeId);
end;
end;
procedure ExportImplUses(JSON: TJSONArray; U: TDelphiUnit; var EdgeId: Integer);
var
RefUnit: TDelphiUnit;
Edge: TJSONObject;
begin
for RefUnit in U.ImplementationUses do
if RefUnit.Parsed then
begin
Edge:=TJSONObject.Create;
Edge.S['id' ]:= IntToStr(EdgeId);
Edge.S['from' ]:= IntToStr(Integer(U));
Edge.S['to' ]:= IntToStr(Integer(RefUnit));
Edge.I['directed' ]:= 1;
Edge.S['name' ]:= 'Impl Ref';
Edge.S['type_id'] := cImplEdgeType;
Edge.O['properties'] := TJSONObject.Create;
Edge.O['reference'] := nil;
JSON.Add(Edge);
Inc(EdgeId);
end;
end;
function ExportNodeTypes: TJSONArray;
var
NodeType: TJSONObject;
begin
Result :=TJSONArray.Create;
NodeType :=TJSONObject.Create;
NodeType.S['id'] := cNodeType;
NodeType.S['name'] := 'Delphi Unit';
NodeType.A['properties'] := TJSONArray.Create;
Result.Add(NodeType);
end;
function ExportEdgeTypes: TJSONArray;
var
IntfEdgeType: TJSONObject;
ImplEdgeType: TJSONObject;
begin
Result :=TJSONArray.Create;
IntfEdgeType :=TJSONObject.Create;
IntfEdgeType.S['id'] := cIntfEdgeType;
IntfEdgeType.S['name'] := 'Intf Ref';
IntfEdgeType.A['properties'] := TJSONArray.Create;
Result.Add(IntfEdgeType);
ImplEdgeType :=TJSONObject.Create;
ImplEdgeType.S['id'] := cImplEdgeType;
ImplEdgeType.S['name'] := 'Impl Ref';
ImplEdgeType.A['properties'] := TJSONArray.Create;
Result.Add(ImplEdgeType);
end;
function ExportNodes: TJSONArray;
function ExportNode(U: TDelphiUnit): TJSONObject;
begin
Result:=TJSONObject.Create;
Result.S['id' ] := IntToStr(Integer(U));
Result.S['type'] := 'Unit';
Result.S['type_id'] := cNodeType;
Result.S['name'] := U.Name;
Result.A['properties'] := TJSONArray.Create;
Result.O['reference'] := nil;
end;
var
U : TDelphiUnit;
begin
Result :=TJSONArray.Create;
for U in Project.Units.Values do
if U.Parsed then
Result.Add(ExportNode(U));
end;
function ExportEdges: TJSONArray;
var
U : TDelphiUnit;
EdgeId : Integer;
begin
Result :=TJSONArray.Create;
EdgeId:=0;
for U in Project.Units.Values do
if U.Parsed then
begin
if JSONExportIntfUses in Options then
ExportIntfUses(Result, U, EdgeId);
if JSONExportImplUses in Options then
ExportImplUses(Result, U, EdgeId);
end;
end;
var
JSON : TJSONObject;
Graph : TJSONObject;
begin
JSON := TJSONObject.Create;
try
Graph :=TJSONObject.Create;
Graph.S['id'] := cGraphId;
Graph.S['name'] := 'Unit Relations for ' + Project.MainUnit.Name;
Graph.I['status'] := 0;
Graph.A['nodeTypes'] := ExportNodeTypes;
Graph.A['nodes' ] := ExportNodes;
Graph.A['edgeTypes'] := ExportEdgeTypes;
Graph.A['edges' ] := ExportEdges;
JSON.O ['graph' ] := Graph;
JSON.SaveToLines( Strings );
finally
FreeAndNil(JSON);
end
end;
procedure TJSONExport.ExportProjectToStrings(Project: TDelphiProject; Strings: TStrings; Options: TJSONExportOptions);
begin
Strings.BeginUpdate;
try
Strings.Clear;
ExportGraphToStrings(Project, Strings, Options);
finally
Strings.EndUpdate;
end;
end;
end.