-
Notifications
You must be signed in to change notification settings - Fork 12
/
ToDsl.java
287 lines (255 loc) · 9.45 KB
/
ToDsl.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package org.ndx.aadarchi.base.enhancers;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.inject.Inject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.vfs2.FileObject;
import org.apache.deltaspike.core.api.config.ConfigProperty;
import org.ndx.aadarchi.base.AgileArchitectureException;
import org.ndx.aadarchi.base.ModelEnhancer;
import org.ndx.aadarchi.base.OutputBuilder;
import org.ndx.aadarchi.base.ViewEnhancer;
import com.structurizr.Workspace;
import com.structurizr.model.Component;
import com.structurizr.model.Container;
import com.structurizr.model.Element;
import com.structurizr.model.Model;
import com.structurizr.model.Person;
import com.structurizr.model.Relationship;
import com.structurizr.model.SoftwareSystem;
import com.structurizr.view.ComponentView;
import com.structurizr.view.ContainerView;
import com.structurizr.view.SystemContextView;
import com.structurizr.view.SystemLandscapeView;
import com.structurizr.view.View;
import com.structurizr.view.ViewSet;
/**
* Enhancer that generate a structurizr dsl file The dsl file is used to
* generate structurizr diagrams of the project architecture This dsl file will
* be located in target/structurizr/workspace.dsl
*
* @author jason-sycz feat nicolas-delsaux
*
*/
@com.structurizr.annotation.Component(technology = "Java, CDI")
public class ToDsl implements ModelEnhancer, ViewEnhancer {
public static class UnableToBuildDslException extends AgileArchitectureException {
public UnableToBuildDslException(String message, Throwable cause) {
super(message, cause);
}
}
String architecture = null;
String relations = "";
@Inject Logger logger;
@Inject
@ConfigProperty(name = ModelElementKeys.PREFIX + "todsl.enabled", defaultValue = "false")
boolean toDslEnabled;
@Inject
@ConfigProperty(name = ModelElementKeys.PREFIX + "todsl.target", defaultValue = "${project.build.directory}/structurizr/workspace.dsl")
private FileObject dslTargetFile;
@Override
public boolean isParallel() {
return false;
}
@Override
public int priority() {
return Integer.MAX_VALUE - 1;
}
/**
* Convert this set of properties into Structurizr DSL properties
* @param indentCount indent iteration
* @param properties dsl properties to map
*/
private String propertiesToDsl(int indentCount, Map<String, String> properties) {
if(properties.isEmpty())
return "";
String tabs = StringUtils.repeat('\t', indentCount);
return properties.entrySet().stream()
.map(entry -> String.format("%s\"%s\" \"%s\"", tabs+"\t", entry.getKey(), entry.getValue()))
.collect(Collectors.joining("\n", tabs+"properties {\n", "\n"+tabs+"}\n"));
}
@Override
public boolean startVisit(Workspace workspace, OutputBuilder builder) {
if (toDslEnabled) {
architecture = String.format("workspace \"%s\" {\n", workspace.getName());
architecture+= propertiesToDsl(1, workspace.getProperties());
}
return toDslEnabled;
}
@Override
public boolean startVisit(Model model) {
architecture = architecture + "\tmodel {\n";
if (!model.getPeople().isEmpty()) {
architecture += personSetToDsl(model.getPeople());
}
if (!model.getRelationships().isEmpty()) {
relations += relationshipSetToDsl(model.getRelationships());
}
return true;
}
@Override
public boolean startVisit(SoftwareSystem softwareSystem) {
architecture += String.format("\t\t%s = softwareSystem \"%s\" {\n", asVariableName(softwareSystem),
softwareSystem.getName());
architecture += propertiesToDsl(3, softwareSystem.getProperties());
if (!softwareSystem.getRelationships().isEmpty()) {
relations += relationshipSetToDsl(softwareSystem.getRelationships());
}
return true;
}
@Override
public boolean startVisit(Container container) {
architecture += String.format("\t\t\t%s = container \"%s\" {\n", asVariableName(container),
container.getName());
architecture += propertiesToDsl(4, container.getProperties());
if (!container.getRelationships().isEmpty()) {
relations += relationshipSetToDsl(container.getRelationships());
}
return true;
}
@Override
public boolean startVisit(Component component) {
architecture += String.format("\t\t\t\t%s = component \"%s\" {\n", asVariableName(component),
component.getName());
architecture += propertiesToDsl(5, component.getProperties());
if (!component.getRelationships().isEmpty()) {
relations += relationshipSetToDsl(component.getRelationships());
}
return true;
}
@Override
public void endVisit(Component component, OutputBuilder builder) {
architecture += "\t\t\t\t}\n";
}
@Override
public void endVisit(Container container, OutputBuilder builder) {
architecture += "\t\t\t}\n";
}
@Override
public void endVisit(SoftwareSystem softwareSystem, OutputBuilder builder) {
architecture += "\t\t}\n";
}
@Override
public void endVisit(Model model, OutputBuilder builder) {
architecture += relations + "\t}\n";
}
@Override
public void endVisit(Workspace workspace, OutputBuilder outputBuilder) {
if (toDslEnabled) {
architecture += "}";
try {
StringBuilder builder = new StringBuilder(architecture);
dslTargetFile.getParent().createFolder();
try(OutputStream outputStream = dslTargetFile.getContent().getOutputStream()) {
IOUtils.write(builder, outputStream, "UTF-8");
} finally {
dslTargetFile.getContent().close();
}
} catch (IOException e) {
throw new UnableToBuildDslException(
String.format("Unable to build dsl file %s", dslTargetFile), e);
}
}
}
private String personSetToDsl(Set<Person> person) {
return person.stream().map(this::personToDsl).collect(Collectors.joining("\n", "", "\n"));
}
private String personToDsl(Person person) {
String description = "";
if (person.getDescription() != null)
description = person.getDescription();
String personText = String.format("\t\t%s = person \"%s\" \"%s\"", Objects.requireNonNull(asVariableName(person)),
Objects.requireNonNull(person.getName()), description);
if(!person.getProperties().isEmpty()) {
personText += " {\n";
personText += propertiesToDsl(3, person.getProperties());
personText += "\t\t}";
}
return personText;
}
private String relationshipSetToDsl(Set<Relationship> relationships) {
return relationships.stream().map(this::relationshipToDsl).collect(Collectors.joining("\n", "", "\n"));
}
private String relationshipToDsl(Relationship relationship) {
String description = "";
if (relationship.getDescription() != null)
description = relationship.getDescription();
String technology = "";
if (relationship.getTechnology() != null)
technology = relationship.getTechnology();
String relationshipText = String.format("\t\t%s -> %s \"%s\" \"%s\"",
asVariableName(Objects.requireNonNull(relationship.getSource())),
asVariableName(Objects.requireNonNull(relationship.getDestination())), description, technology);
if(!relationship.getProperties().isEmpty()) {
relationshipText += " {\n";
relationshipText += propertiesToDsl(3, relationship.getProperties());
relationshipText += "}";
}
return relationshipText;
}
private String asVariableName(Element element) {
return String.format("%s_%s", element.getName().replaceAll("[^A-Za-z0-9]", "_"), element.getId());
}
@Override
public boolean startVisit(ViewSet viewset) {
if (toDslEnabled) {
try {
InputStream toDslDefaultView = Objects.requireNonNull(getClass().getResourceAsStream("ToDSL.default.views.dsl"));
String viewsDeclaration = IOUtils.toString(toDslDefaultView, StandardCharsets.UTF_8);
architecture += "\tviews {\n";
architecture += viewsDeclaration;
} catch (IOException e) {
throw new UnableToBuildDslException("Unable to read pseudo-standard view header file", e);
}
}
return toDslEnabled;
}
@Override
public boolean startVisit(View s) {
return true;
}
@Override
public void endVisit(View s, OutputBuilder builder) {
String PLACEHOLDER = "\t\t\tWARNING: Include and autolayout can't be infered from static view content, so you'll have to set that yourself\n\t\t}\n";
if(s instanceof ComponentView) {
ComponentView c = (ComponentView) s;
architecture += String.format("\t\tcomponent \"%s\" \"%s\" \"%s\" {\n"+PLACEHOLDER,
asVariableName(c.getContainer()),
c.getKey(),
c.getDescription());
} else if(s instanceof ContainerView) {
ContainerView c = (ContainerView) s;
architecture += String.format("\t\tcontainer \"%s\" \"%s\" \"%s\" {\n"+PLACEHOLDER,
asVariableName(c.getSoftwareSystem()),
c.getKey(),
c.getDescription());
} else if(s instanceof SystemContextView) {
SystemContextView c = (SystemContextView) s;
architecture += String.format("\t\tsystemContext \"%s\" \"%s\" \"%s\" {\n"+PLACEHOLDER,
asVariableName(c.getSoftwareSystem()),
c.getKey(),
c.getDescription());
} else if(s instanceof SystemLandscapeView) {
SystemLandscapeView c = (SystemLandscapeView) s;
architecture += String.format("\t\tsystemLandscape \"%s\" \"%s\" {\n"+PLACEHOLDER,
c.getKey(),
c.getDescription());
} else {
logger.warning(String.format("View to DSL isn't yet supported for given view, so view %s is ignored", s));
}
}
@Override
public void endVisit(ViewSet viewset, OutputBuilder builder) {
architecture += "\n";
}
}