@agilewallaby/c4-model 2.0.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -9
- package/src/archetype.d.ts +30 -0
- package/src/buildModel.d.ts +20 -0
- package/src/component.d.ts +2 -1
- package/src/container.d.ts +5 -4
- package/src/core.d.ts +9 -4
- package/src/generateDiagrams.d.ts +1 -1
- package/src/index.d.ts +2 -0
- package/src/index.js +813 -9
- package/src/model.d.ts +9 -18
- package/src/person.d.ts +3 -2
- package/src/softwareSystem.d.ts +6 -5
- package/src/structurizrDslWriter.d.ts +3 -0
- package/src/component.js +0 -12
- package/src/component.js.map +0 -1
- package/src/container.js +0 -57
- package/src/container.js.map +0 -1
- package/src/core.js +0 -60
- package/src/core.js.map +0 -1
- package/src/generateDiagrams.js +0 -47
- package/src/generateDiagrams.js.map +0 -1
- package/src/index.js.map +0 -1
- package/src/model.js +0 -121
- package/src/model.js.map +0 -1
- package/src/person.js +0 -12
- package/src/person.js.map +0 -1
- package/src/softwareSystem.js +0 -57
- package/src/softwareSystem.js.map +0 -1
- package/src/structurizrDslWriter.js +0 -162
- package/src/structurizrDslWriter.js.map +0 -1
- package/src/views.js +0 -73
- package/src/views.js.map +0 -1
package/src/index.js
CHANGED
|
@@ -1,9 +1,813 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
// libs/c4-model/src/archetype.ts
|
|
2
|
+
var ElementArchetype = class {
|
|
3
|
+
constructor(name, elementKind, definition, parent) {
|
|
4
|
+
this.name = name;
|
|
5
|
+
this.elementKind = elementKind;
|
|
6
|
+
this.parent = parent;
|
|
7
|
+
this.ownDescription = definition?.description;
|
|
8
|
+
this.ownTechnology = definition?.technology;
|
|
9
|
+
this.ownTags = definition?.tags ?? [];
|
|
10
|
+
this.description = this.ownDescription ?? parent?.description;
|
|
11
|
+
this.technology = this.ownTechnology ?? parent?.technology;
|
|
12
|
+
this.tags = [...parent?.tags ?? [], ...this.ownTags];
|
|
13
|
+
}
|
|
14
|
+
ownDescription;
|
|
15
|
+
ownTechnology;
|
|
16
|
+
ownTags;
|
|
17
|
+
description;
|
|
18
|
+
technology;
|
|
19
|
+
tags;
|
|
20
|
+
};
|
|
21
|
+
var RelationshipArchetype = class {
|
|
22
|
+
constructor(name, definition, parent) {
|
|
23
|
+
this.name = name;
|
|
24
|
+
this.parent = parent;
|
|
25
|
+
this.ownDescription = definition?.description;
|
|
26
|
+
this.ownTechnology = definition?.technology;
|
|
27
|
+
this.ownTags = definition?.tags ?? [];
|
|
28
|
+
this.description = this.ownDescription ?? parent?.description;
|
|
29
|
+
this.technology = this.ownTechnology ?? parent?.technology;
|
|
30
|
+
this.tags = [...parent?.tags ?? [], ...this.ownTags];
|
|
31
|
+
}
|
|
32
|
+
ownDescription;
|
|
33
|
+
ownTechnology;
|
|
34
|
+
ownTags;
|
|
35
|
+
description;
|
|
36
|
+
technology;
|
|
37
|
+
tags;
|
|
38
|
+
};
|
|
39
|
+
function mergeArchetypeWithOverride(archetype, override) {
|
|
40
|
+
return {
|
|
41
|
+
description: override?.description ?? archetype.description,
|
|
42
|
+
technology: override?.technology ?? archetype.technology,
|
|
43
|
+
tags: [...archetype.tags, ...override?.tags ?? []]
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// libs/c4-model/src/core.ts
|
|
48
|
+
import { camelCase } from "change-case";
|
|
49
|
+
var Element = class {
|
|
50
|
+
constructor(name, defaultTags = [], definition, archetype, overrideDefinition) {
|
|
51
|
+
this.name = name;
|
|
52
|
+
this.description = definition?.description;
|
|
53
|
+
this.tags = (definition?.tags ?? []).concat(["Element"]).concat(defaultTags);
|
|
54
|
+
this.archetype = archetype;
|
|
55
|
+
this.overrideDefinition = overrideDefinition;
|
|
56
|
+
}
|
|
57
|
+
description;
|
|
58
|
+
tags;
|
|
59
|
+
archetype;
|
|
60
|
+
overrideDefinition;
|
|
61
|
+
_relationships = [];
|
|
62
|
+
get canonicalName() {
|
|
63
|
+
return camelCase(this.name);
|
|
64
|
+
}
|
|
65
|
+
uses(otherElement, archetypeOrDef, override) {
|
|
66
|
+
let definition;
|
|
67
|
+
let archetype;
|
|
68
|
+
if (archetypeOrDef instanceof RelationshipArchetype) {
|
|
69
|
+
archetype = archetypeOrDef;
|
|
70
|
+
definition = mergeArchetypeWithOverride(archetypeOrDef, override);
|
|
71
|
+
} else {
|
|
72
|
+
definition = archetypeOrDef;
|
|
73
|
+
}
|
|
74
|
+
const relationship = new Relationship(this, otherElement, definition, archetype, override);
|
|
75
|
+
this._relationships.push(relationship);
|
|
76
|
+
}
|
|
77
|
+
get relationships() {
|
|
78
|
+
return this._relationships;
|
|
79
|
+
}
|
|
80
|
+
getRelationshipsInHierarchy() {
|
|
81
|
+
return this._relationships.concat(this.getChildElements().flatMap((element) => element.getRelationshipsInHierarchy()));
|
|
82
|
+
}
|
|
83
|
+
getChildElementNames(path2) {
|
|
84
|
+
const result = Array.from(this.getChildElements()).flatMap((reference) => {
|
|
85
|
+
const currentPath = `${path2 ? path2 : "" + this.name}.${reference.name}`;
|
|
86
|
+
return [currentPath, ...reference.getChildElementNames(currentPath)];
|
|
87
|
+
});
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
var TechnicalElement = class extends Element {
|
|
92
|
+
technology;
|
|
93
|
+
constructor(name, defaultTags = [], definition, archetype, overrideDefinition) {
|
|
94
|
+
super(name, defaultTags, definition, archetype, overrideDefinition);
|
|
95
|
+
this.technology = definition?.technology;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var Relationship = class {
|
|
99
|
+
constructor(source, destination, definition, archetype, overrideDefinition) {
|
|
100
|
+
this.source = source;
|
|
101
|
+
this.destination = destination;
|
|
102
|
+
this.description = definition?.description;
|
|
103
|
+
this.technology = definition?.technology;
|
|
104
|
+
this.tags = (definition?.tags ?? []).concat(["Relationship"]);
|
|
105
|
+
this.archetype = archetype;
|
|
106
|
+
this.overrideDefinition = overrideDefinition;
|
|
107
|
+
}
|
|
108
|
+
description;
|
|
109
|
+
tags;
|
|
110
|
+
technology;
|
|
111
|
+
archetype;
|
|
112
|
+
overrideDefinition;
|
|
113
|
+
};
|
|
114
|
+
var Group = class {
|
|
115
|
+
constructor(name) {
|
|
116
|
+
this.name = name;
|
|
117
|
+
}
|
|
118
|
+
// TODO: Implement this in some useful way?
|
|
119
|
+
// public addToGroup(groupCollection: string, groupMember: T) {}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// libs/c4-model/src/component.ts
|
|
123
|
+
var Component = class extends TechnicalElement {
|
|
124
|
+
constructor(name, definition, archetype, overrideDefinition) {
|
|
125
|
+
super(name, ["Component"], definition, archetype, overrideDefinition);
|
|
126
|
+
this.name = name;
|
|
127
|
+
}
|
|
128
|
+
getChildElements() {
|
|
129
|
+
return [];
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// libs/c4-model/src/container.ts
|
|
134
|
+
var ContainerGroup = class extends Group {
|
|
135
|
+
constructor(name, container) {
|
|
136
|
+
super(name);
|
|
137
|
+
this.name = name;
|
|
138
|
+
this.container = container;
|
|
139
|
+
}
|
|
140
|
+
_components = /* @__PURE__ */ new Map();
|
|
141
|
+
defineComponent(name, archetypeOrDef, override) {
|
|
142
|
+
const component = this.container.defineComponent(name, archetypeOrDef, override);
|
|
143
|
+
this._components.set(name, component);
|
|
144
|
+
return component;
|
|
145
|
+
}
|
|
146
|
+
getComponents() {
|
|
147
|
+
return Array.from(this._components.values());
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
var Container = class extends TechnicalElement {
|
|
151
|
+
constructor(name, definition, archetype, overrideDefinition) {
|
|
152
|
+
super(name, ["Container"], definition, archetype, overrideDefinition);
|
|
153
|
+
this.name = name;
|
|
154
|
+
}
|
|
155
|
+
_components = /* @__PURE__ */ new Map();
|
|
156
|
+
_groups = /* @__PURE__ */ new Map();
|
|
157
|
+
defineComponent(name, archetypeOrDef, override) {
|
|
158
|
+
if (this._components.has(name)) {
|
|
159
|
+
throw Error(`A Component named '${name}' is defined elsewhere in this Container. A Component can be defined only once.`);
|
|
160
|
+
}
|
|
161
|
+
let definition;
|
|
162
|
+
let archetype;
|
|
163
|
+
if (archetypeOrDef instanceof ElementArchetype) {
|
|
164
|
+
archetype = archetypeOrDef;
|
|
165
|
+
definition = mergeArchetypeWithOverride(archetypeOrDef, override);
|
|
166
|
+
} else {
|
|
167
|
+
definition = archetypeOrDef;
|
|
168
|
+
}
|
|
169
|
+
const component = new Component(name, definition, archetype, override);
|
|
170
|
+
this._components.set(name, component);
|
|
171
|
+
return component;
|
|
172
|
+
}
|
|
173
|
+
addGroup(groupName) {
|
|
174
|
+
let group = this._groups.get(groupName);
|
|
175
|
+
if (!group) {
|
|
176
|
+
group = new ContainerGroup(groupName, this);
|
|
177
|
+
this._groups.set(groupName, group);
|
|
178
|
+
}
|
|
179
|
+
return group;
|
|
180
|
+
}
|
|
181
|
+
getGroups() {
|
|
182
|
+
return Array.from(this._groups.values());
|
|
183
|
+
}
|
|
184
|
+
getComponentsNotInGroups() {
|
|
185
|
+
const componentsInGroups = this.getGroups().flatMap((group) => group.getComponents());
|
|
186
|
+
return Array.from(this._components.values()).filter((component) => !componentsInGroups.includes(component));
|
|
187
|
+
}
|
|
188
|
+
getChildElements() {
|
|
189
|
+
return Array.from(this._components.values());
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
// libs/c4-model/src/softwareSystem.ts
|
|
194
|
+
var SoftwareSystemGroup = class extends Group {
|
|
195
|
+
constructor(name, softwareSystem) {
|
|
196
|
+
super(name);
|
|
197
|
+
this.name = name;
|
|
198
|
+
this.softwareSystem = softwareSystem;
|
|
199
|
+
}
|
|
200
|
+
_containers = /* @__PURE__ */ new Map();
|
|
201
|
+
defineContainer(name, archetypeOrDef, override) {
|
|
202
|
+
const container = this.softwareSystem.defineContainer(name, archetypeOrDef, override);
|
|
203
|
+
this._containers.set(name, container);
|
|
204
|
+
return container;
|
|
205
|
+
}
|
|
206
|
+
getContainers() {
|
|
207
|
+
return Array.from(this._containers.values());
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
var SoftwareSystem = class extends Element {
|
|
211
|
+
constructor(name, definition, archetype, overrideDefinition) {
|
|
212
|
+
super(name, ["Software System"], definition, archetype, overrideDefinition);
|
|
213
|
+
this.name = name;
|
|
214
|
+
}
|
|
215
|
+
_containers = /* @__PURE__ */ new Map();
|
|
216
|
+
_groups = /* @__PURE__ */ new Map();
|
|
217
|
+
defineContainer(name, archetypeOrDef, override) {
|
|
218
|
+
if (this._containers.has(name)) {
|
|
219
|
+
throw Error(`A Container named '${name}' is defined elsewhere in this SoftwareSystem. A Container can be defined only once.`);
|
|
220
|
+
}
|
|
221
|
+
let definition;
|
|
222
|
+
let archetype;
|
|
223
|
+
if (archetypeOrDef instanceof ElementArchetype) {
|
|
224
|
+
archetype = archetypeOrDef;
|
|
225
|
+
definition = mergeArchetypeWithOverride(archetypeOrDef, override);
|
|
226
|
+
} else {
|
|
227
|
+
definition = archetypeOrDef;
|
|
228
|
+
}
|
|
229
|
+
const container = new Container(name, definition, archetype, override);
|
|
230
|
+
this._containers.set(name, container);
|
|
231
|
+
return container;
|
|
232
|
+
}
|
|
233
|
+
addGroup(groupName) {
|
|
234
|
+
let group = this._groups.get(groupName);
|
|
235
|
+
if (!group) {
|
|
236
|
+
group = new SoftwareSystemGroup(groupName, this);
|
|
237
|
+
this._groups.set(groupName, group);
|
|
238
|
+
}
|
|
239
|
+
return group;
|
|
240
|
+
}
|
|
241
|
+
getGroups() {
|
|
242
|
+
return Array.from(this._groups.values());
|
|
243
|
+
}
|
|
244
|
+
getChildElements() {
|
|
245
|
+
return Array.from(this._containers.values());
|
|
246
|
+
}
|
|
247
|
+
getContainersNotInGroups() {
|
|
248
|
+
const containersInGroups = Array.from(this._groups.values()).flatMap((group) => group.getContainers());
|
|
249
|
+
return Array.from(this._containers.values()).filter((container) => !containersInGroups.includes(container));
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// libs/c4-model/src/person.ts
|
|
254
|
+
var Person = class extends Element {
|
|
255
|
+
constructor(name, definition, archetype, overrideDefinition) {
|
|
256
|
+
super(name, ["Person"], definition, archetype, overrideDefinition);
|
|
257
|
+
this.name = name;
|
|
258
|
+
}
|
|
259
|
+
getChildElements() {
|
|
260
|
+
return [];
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
// libs/c4-model/src/model.ts
|
|
265
|
+
var ModelGroup = class extends Group {
|
|
266
|
+
constructor(name, model) {
|
|
267
|
+
super(name);
|
|
268
|
+
this.name = name;
|
|
269
|
+
this.model = model;
|
|
270
|
+
}
|
|
271
|
+
softwareSystems = /* @__PURE__ */ new Map();
|
|
272
|
+
people = /* @__PURE__ */ new Map();
|
|
273
|
+
defineSoftwareSystem(name, archetypeOrDef, override) {
|
|
274
|
+
const softwareSystem = this.model.defineSoftwareSystem(name, archetypeOrDef, override);
|
|
275
|
+
this.softwareSystems.set(name, softwareSystem);
|
|
276
|
+
return softwareSystem;
|
|
277
|
+
}
|
|
278
|
+
definePerson(name, archetypeOrDef, override) {
|
|
279
|
+
const person = this.model.definePerson(name, archetypeOrDef, override);
|
|
280
|
+
this.people.set(name, person);
|
|
281
|
+
return person;
|
|
282
|
+
}
|
|
283
|
+
getSoftwareSystems() {
|
|
284
|
+
return Array.from(this.softwareSystems.values());
|
|
285
|
+
}
|
|
286
|
+
getPeople() {
|
|
287
|
+
return Array.from(this.people.values());
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
var Model = class {
|
|
291
|
+
constructor(name) {
|
|
292
|
+
this.name = name;
|
|
293
|
+
}
|
|
294
|
+
softwareSystems = /* @__PURE__ */ new Map();
|
|
295
|
+
people = /* @__PURE__ */ new Map();
|
|
296
|
+
groups = /* @__PURE__ */ new Map();
|
|
297
|
+
defineSoftwareSystem(name, archetypeOrDef, override) {
|
|
298
|
+
if (this.softwareSystems.has(name)) {
|
|
299
|
+
throw Error(`A SoftwareSystem named '${name}' is defined elsewhere in this Model. A SoftwareSystem can be defined only once.`);
|
|
300
|
+
}
|
|
301
|
+
let definition;
|
|
302
|
+
let archetype;
|
|
303
|
+
if (archetypeOrDef instanceof ElementArchetype) {
|
|
304
|
+
archetype = archetypeOrDef;
|
|
305
|
+
definition = mergeArchetypeWithOverride(archetypeOrDef, override);
|
|
306
|
+
} else {
|
|
307
|
+
definition = archetypeOrDef;
|
|
308
|
+
}
|
|
309
|
+
const system = new SoftwareSystem(name, definition, archetype, override);
|
|
310
|
+
this.softwareSystems.set(name, system);
|
|
311
|
+
return system;
|
|
312
|
+
}
|
|
313
|
+
// TODO:Should be a Group<SoftwareSystem | Person> if that is added back in
|
|
314
|
+
addGroup(groupName) {
|
|
315
|
+
let group = this.groups.get(groupName);
|
|
316
|
+
if (!group) {
|
|
317
|
+
group = new ModelGroup(groupName, this);
|
|
318
|
+
this.groups.set(groupName, group);
|
|
319
|
+
}
|
|
320
|
+
return group;
|
|
321
|
+
}
|
|
322
|
+
definePerson(name, archetypeOrDef, override) {
|
|
323
|
+
if (this.people.has(name)) {
|
|
324
|
+
throw Error(`A Person named '${name}' is defined elsewhere in this Model. A Person can be defined only once.`);
|
|
325
|
+
}
|
|
326
|
+
let definition;
|
|
327
|
+
let archetype;
|
|
328
|
+
if (archetypeOrDef instanceof ElementArchetype) {
|
|
329
|
+
archetype = archetypeOrDef;
|
|
330
|
+
definition = mergeArchetypeWithOverride(archetypeOrDef, override);
|
|
331
|
+
} else {
|
|
332
|
+
definition = archetypeOrDef;
|
|
333
|
+
}
|
|
334
|
+
const person = new Person(name, definition, archetype, override);
|
|
335
|
+
this.people.set(name, person);
|
|
336
|
+
return person;
|
|
337
|
+
}
|
|
338
|
+
validate() {
|
|
339
|
+
}
|
|
340
|
+
getPeople() {
|
|
341
|
+
return Array.from(this.people.values());
|
|
342
|
+
}
|
|
343
|
+
getSoftwareSystems() {
|
|
344
|
+
return Array.from(this.softwareSystems.values());
|
|
345
|
+
}
|
|
346
|
+
getPeopleNotInGroups() {
|
|
347
|
+
const peopleInGroups = Array.from(this.groups.values()).flatMap((group) => group.getPeople());
|
|
348
|
+
return Array.from(this.people.values()).filter((person) => !peopleInGroups.includes(person));
|
|
349
|
+
}
|
|
350
|
+
getSoftwareSystemsNotInGroups() {
|
|
351
|
+
const systemsInGroups = Array.from(this.groups.values()).flatMap((group) => group.getSoftwareSystems());
|
|
352
|
+
return Array.from(this.softwareSystems.values()).filter((system) => !systemsInGroups.includes(system));
|
|
353
|
+
}
|
|
354
|
+
getGroups() {
|
|
355
|
+
return Array.from(this.groups.values());
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// libs/c4-model/src/views.ts
|
|
360
|
+
var View = class {
|
|
361
|
+
constructor(key, viewDefinition) {
|
|
362
|
+
this.key = key;
|
|
363
|
+
this.description = viewDefinition.description;
|
|
364
|
+
this.subject = viewDefinition.subject;
|
|
365
|
+
this.title = viewDefinition.title;
|
|
366
|
+
}
|
|
367
|
+
subject;
|
|
368
|
+
description;
|
|
369
|
+
title;
|
|
370
|
+
_scopes = [];
|
|
371
|
+
includeAll() {
|
|
372
|
+
this._scopes.push("include *");
|
|
373
|
+
}
|
|
374
|
+
includeElement(element) {
|
|
375
|
+
this._scopes.push(`include ${element.canonicalName}`);
|
|
376
|
+
}
|
|
377
|
+
includeExpression(expression) {
|
|
378
|
+
this._scopes.push(`include ${expression}`);
|
|
379
|
+
}
|
|
380
|
+
excludeAll() {
|
|
381
|
+
this._scopes.push("exclude *");
|
|
382
|
+
}
|
|
383
|
+
excludeElement(element) {
|
|
384
|
+
this._scopes.push(`exclude ${element.canonicalName}`);
|
|
385
|
+
}
|
|
386
|
+
excludeExpression(expression) {
|
|
387
|
+
this._scopes.push(`exclude ${expression}`);
|
|
388
|
+
}
|
|
389
|
+
get scopes() {
|
|
390
|
+
return this._scopes;
|
|
391
|
+
}
|
|
392
|
+
};
|
|
393
|
+
var Views = class {
|
|
394
|
+
_systemLandscapeViews = /* @__PURE__ */ new Map();
|
|
395
|
+
_systemContextViews = /* @__PURE__ */ new Map();
|
|
396
|
+
_containerViews = /* @__PURE__ */ new Map();
|
|
397
|
+
_componentViews = /* @__PURE__ */ new Map();
|
|
398
|
+
addSystemLandscapeView(key, definition) {
|
|
399
|
+
const view = new View(key, { subject: void 0, description: definition.description, title: definition.title });
|
|
400
|
+
this._systemLandscapeViews.set(key, view);
|
|
401
|
+
return view;
|
|
402
|
+
}
|
|
403
|
+
addSystemContextView(key, definition) {
|
|
404
|
+
const view = new View(key, definition);
|
|
405
|
+
this._systemContextViews.set(key, view);
|
|
406
|
+
return view;
|
|
407
|
+
}
|
|
408
|
+
addContainerView(key, definition) {
|
|
409
|
+
const view = new View(key, definition);
|
|
410
|
+
this._containerViews.set(key, view);
|
|
411
|
+
return view;
|
|
412
|
+
}
|
|
413
|
+
addComponentView(key, definition) {
|
|
414
|
+
const view = new View(key, definition);
|
|
415
|
+
this._componentViews.set(key, view);
|
|
416
|
+
return view;
|
|
417
|
+
}
|
|
418
|
+
get systemLandscapeViews() {
|
|
419
|
+
return Array.from(this._systemLandscapeViews.values());
|
|
420
|
+
}
|
|
421
|
+
get systemContextViews() {
|
|
422
|
+
return Array.from(this._systemContextViews.values());
|
|
423
|
+
}
|
|
424
|
+
get containerViews() {
|
|
425
|
+
return Array.from(this._containerViews.values());
|
|
426
|
+
}
|
|
427
|
+
get componentViews() {
|
|
428
|
+
return Array.from(this._componentViews.values());
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
// libs/c4-model/src/structurizrDslWriter.ts
|
|
433
|
+
var INDENT_SIZE = 2;
|
|
434
|
+
var StructurizrDSLWriter = class {
|
|
435
|
+
constructor(model, views) {
|
|
436
|
+
this.model = model;
|
|
437
|
+
this.views = views;
|
|
438
|
+
}
|
|
439
|
+
collectArchetypes() {
|
|
440
|
+
const elementSet = /* @__PURE__ */ new Set();
|
|
441
|
+
const relationshipSet = /* @__PURE__ */ new Set();
|
|
442
|
+
const collectFromElement = (element) => {
|
|
443
|
+
if (element.archetype) {
|
|
444
|
+
let arch = element.archetype;
|
|
445
|
+
while (arch) {
|
|
446
|
+
elementSet.add(arch);
|
|
447
|
+
arch = arch.parent;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
element.relationships.forEach((rel) => {
|
|
451
|
+
if (rel.archetype) {
|
|
452
|
+
let arch = rel.archetype;
|
|
453
|
+
while (arch) {
|
|
454
|
+
relationshipSet.add(arch);
|
|
455
|
+
arch = arch.parent;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
element.getChildElements().forEach(collectFromElement);
|
|
460
|
+
};
|
|
461
|
+
const allElements = [...this.model.getPeople(), ...this.model.getSoftwareSystems()];
|
|
462
|
+
allElements.forEach(collectFromElement);
|
|
463
|
+
const sortArchetypes = (set) => {
|
|
464
|
+
const sorted = [];
|
|
465
|
+
const visited = /* @__PURE__ */ new Set();
|
|
466
|
+
const visit = (arch) => {
|
|
467
|
+
if (visited.has(arch)) return;
|
|
468
|
+
if (arch.parent && set.has(arch.parent)) visit(arch.parent);
|
|
469
|
+
visited.add(arch);
|
|
470
|
+
sorted.push(arch);
|
|
471
|
+
};
|
|
472
|
+
set.forEach(visit);
|
|
473
|
+
return sorted;
|
|
474
|
+
};
|
|
475
|
+
return {
|
|
476
|
+
elementArchetypes: sortArchetypes(elementSet),
|
|
477
|
+
relationshipArchetypes: sortArchetypes(relationshipSet)
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
writeArchetypes(level) {
|
|
481
|
+
const { elementArchetypes, relationshipArchetypes } = this.collectArchetypes();
|
|
482
|
+
if (elementArchetypes.length === 0 && relationshipArchetypes.length === 0) return "";
|
|
483
|
+
let dsl = this.writeLine(`archetypes {`, level);
|
|
484
|
+
for (const arch of elementArchetypes) {
|
|
485
|
+
const baseType = arch.parent ? arch.parent.name : arch.elementKind;
|
|
486
|
+
let inner = "";
|
|
487
|
+
if (arch.ownDescription) {
|
|
488
|
+
inner += this.writeLine(`description "${arch.ownDescription}"`, level + 2);
|
|
489
|
+
}
|
|
490
|
+
if (arch.ownTechnology) {
|
|
491
|
+
inner += this.writeLine(`technology "${arch.ownTechnology}"`, level + 2);
|
|
492
|
+
}
|
|
493
|
+
if (arch.ownTags.length > 0) {
|
|
494
|
+
inner += this.writeLine(`tags ${arch.ownTags.map((t) => `"${t}"`).join(" ")}`, level + 2);
|
|
495
|
+
}
|
|
496
|
+
if (inner) {
|
|
497
|
+
dsl += this.writeLine(`${arch.name} = ${baseType} {`, level + 1);
|
|
498
|
+
dsl += inner;
|
|
499
|
+
dsl += this.writeLine(`}`, level + 1);
|
|
500
|
+
} else {
|
|
501
|
+
dsl += this.writeLine(`${arch.name} = ${baseType} {}`, level + 1);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
for (const arch of relationshipArchetypes) {
|
|
505
|
+
const arrow = arch.parent ? `--${arch.parent.name}->` : `->`;
|
|
506
|
+
let inner = "";
|
|
507
|
+
if (arch.ownDescription) {
|
|
508
|
+
inner += this.writeLine(`description "${arch.ownDescription}"`, level + 2);
|
|
509
|
+
}
|
|
510
|
+
if (arch.ownTechnology) {
|
|
511
|
+
inner += this.writeLine(`technology "${arch.ownTechnology}"`, level + 2);
|
|
512
|
+
}
|
|
513
|
+
if (arch.ownTags.length > 0) {
|
|
514
|
+
inner += this.writeLine(`tags ${arch.ownTags.map((t) => `"${t}"`).join(" ")}`, level + 2);
|
|
515
|
+
}
|
|
516
|
+
if (inner) {
|
|
517
|
+
dsl += this.writeLine(`${arch.name} = ${arrow} {`, level + 1);
|
|
518
|
+
dsl += inner;
|
|
519
|
+
dsl += this.writeLine(`}`, level + 1);
|
|
520
|
+
} else {
|
|
521
|
+
dsl += this.writeLine(`${arch.name} = ${arrow} {}`, level + 1);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
dsl += this.writeLine(`}`, level);
|
|
525
|
+
return dsl;
|
|
526
|
+
}
|
|
527
|
+
writeElement(elementType, element, level, closeElement = true) {
|
|
528
|
+
let elementDsl = "";
|
|
529
|
+
const type = element.archetype ? element.archetype.name : elementType;
|
|
530
|
+
elementDsl += this.writeLine(`${element.canonicalName} = ${type} "${element.name}" {`, level);
|
|
531
|
+
if (element.archetype) {
|
|
532
|
+
const ovr = element.overrideDefinition;
|
|
533
|
+
if (ovr?.description) {
|
|
534
|
+
elementDsl += this.writeLine(`description "${ovr.description}"`, level + 1);
|
|
535
|
+
}
|
|
536
|
+
if (ovr?.tags && ovr.tags.length > 0) {
|
|
537
|
+
elementDsl += this.writeLine(`tags ${ovr.tags.map((tag) => `"${tag}"`).join(" ")}`, level + 1);
|
|
538
|
+
}
|
|
539
|
+
if (ovr && "technology" in ovr && ovr.technology) {
|
|
540
|
+
elementDsl += this.writeLine(`technology "${ovr.technology}"`, level + 1);
|
|
541
|
+
}
|
|
542
|
+
} else {
|
|
543
|
+
if (element.description) {
|
|
544
|
+
elementDsl += this.writeLine(`description "${element.description}"`, level + 1);
|
|
545
|
+
}
|
|
546
|
+
elementDsl += this.writeLine(`tags ${element.tags.map((tag) => `"${tag}"`).join(" ")}`, level + 1);
|
|
547
|
+
}
|
|
548
|
+
if (closeElement) {
|
|
549
|
+
elementDsl += this.writeLine(`}`, level);
|
|
550
|
+
}
|
|
551
|
+
return elementDsl;
|
|
552
|
+
}
|
|
553
|
+
writeComponent(component, level) {
|
|
554
|
+
let componentDsl = "";
|
|
555
|
+
componentDsl += this.writeElement("component", component, level, false);
|
|
556
|
+
if (!component.archetype && component.technology) {
|
|
557
|
+
componentDsl += this.writeLine(`technology "${component.technology}"`, level + 1);
|
|
558
|
+
}
|
|
559
|
+
componentDsl += this.writeLine(`}`, level);
|
|
560
|
+
return componentDsl;
|
|
561
|
+
}
|
|
562
|
+
writeContainerGroup(group, level) {
|
|
563
|
+
let containerGroupDsl = "";
|
|
564
|
+
containerGroupDsl += this.writeLine(`${group.name} = group "${group.name}" {`, level);
|
|
565
|
+
group.getComponents().forEach((component) => {
|
|
566
|
+
containerGroupDsl += this.writeComponent(component, level + 1);
|
|
567
|
+
});
|
|
568
|
+
containerGroupDsl += this.writeLine(`}`, level);
|
|
569
|
+
return containerGroupDsl;
|
|
570
|
+
}
|
|
571
|
+
writeContainer(container, level) {
|
|
572
|
+
let containerDsl = "";
|
|
573
|
+
containerDsl += this.writeElement("container", container, level, false);
|
|
574
|
+
if (!container.archetype && container.technology) {
|
|
575
|
+
containerDsl += this.writeLine(`technology "${container.technology}"`, level + 1);
|
|
576
|
+
}
|
|
577
|
+
container.getComponentsNotInGroups().forEach((component) => {
|
|
578
|
+
containerDsl += this.writeComponent(component, level + 1);
|
|
579
|
+
});
|
|
580
|
+
container.getGroups().forEach((group) => {
|
|
581
|
+
containerDsl += this.writeContainerGroup(group, level + 1);
|
|
582
|
+
});
|
|
583
|
+
containerDsl += this.writeLine(`}`, level);
|
|
584
|
+
return containerDsl;
|
|
585
|
+
}
|
|
586
|
+
writeSoftwareSystemGroup(group, level) {
|
|
587
|
+
let softwareSystemGroupDsl = "";
|
|
588
|
+
softwareSystemGroupDsl += this.writeLine(`${group.name} = group "${group.name}" {`, level);
|
|
589
|
+
group.getContainers().forEach((container) => {
|
|
590
|
+
softwareSystemGroupDsl += this.writeContainer(container, level + 1);
|
|
591
|
+
});
|
|
592
|
+
softwareSystemGroupDsl += this.writeLine(`}`, level);
|
|
593
|
+
return softwareSystemGroupDsl;
|
|
594
|
+
}
|
|
595
|
+
writeSoftwareSystem(softwareSystem, level) {
|
|
596
|
+
let softwareSystemDsl = "";
|
|
597
|
+
softwareSystemDsl += this.writeElement("softwareSystem", softwareSystem, level, false);
|
|
598
|
+
softwareSystem.getContainersNotInGroups().forEach((container) => {
|
|
599
|
+
softwareSystemDsl += this.writeContainer(container, level + 1);
|
|
600
|
+
});
|
|
601
|
+
softwareSystem.getGroups().forEach((group) => {
|
|
602
|
+
softwareSystemDsl += this.writeSoftwareSystemGroup(group, level + 1);
|
|
603
|
+
});
|
|
604
|
+
softwareSystemDsl += this.writeLine(`}`, level);
|
|
605
|
+
return softwareSystemDsl;
|
|
606
|
+
}
|
|
607
|
+
writeRelationship(relationship, level) {
|
|
608
|
+
let dsl = "";
|
|
609
|
+
if (relationship.archetype) {
|
|
610
|
+
const arrow = `--${relationship.archetype.name}->`;
|
|
611
|
+
const ovr = relationship.overrideDefinition;
|
|
612
|
+
const desc = ovr?.description ?? relationship.description ?? "uses";
|
|
613
|
+
dsl += this.writeLine(
|
|
614
|
+
`${relationship.source.canonicalName} ${arrow} ${relationship.destination.canonicalName} "${desc}" {`,
|
|
615
|
+
level
|
|
616
|
+
);
|
|
617
|
+
if (ovr?.technology) {
|
|
618
|
+
dsl += this.writeLine(`technology "${ovr.technology}"`, level + 1);
|
|
619
|
+
}
|
|
620
|
+
if (ovr?.tags && ovr.tags.length > 0) {
|
|
621
|
+
dsl += this.writeLine(`tags ${ovr.tags.map((tag) => `"${tag}"`).join(" ")}`, level + 1);
|
|
622
|
+
}
|
|
623
|
+
dsl += this.writeLine(`}`, level);
|
|
624
|
+
} else {
|
|
625
|
+
const tech = relationship.technology ? ` "${relationship.technology}"` : "";
|
|
626
|
+
dsl += this.writeLine(
|
|
627
|
+
`${relationship.source.canonicalName} -> ${relationship.destination.canonicalName} "${relationship.description ?? "uses"}"${tech} {`,
|
|
628
|
+
level
|
|
629
|
+
);
|
|
630
|
+
dsl += this.writeLine(`tags ${relationship.tags.map((tag) => `"${tag}"`).join(" ")}`, level + 1);
|
|
631
|
+
dsl += this.writeLine(`}`, level);
|
|
632
|
+
}
|
|
633
|
+
return dsl;
|
|
634
|
+
}
|
|
635
|
+
writeRelationships(elements, level) {
|
|
636
|
+
let relationshipsDsl = "";
|
|
637
|
+
elements.forEach((element) => {
|
|
638
|
+
element.getRelationshipsInHierarchy().forEach((relationship) => {
|
|
639
|
+
relationshipsDsl += this.writeRelationship(relationship, level);
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
return relationshipsDsl;
|
|
643
|
+
}
|
|
644
|
+
writeModelGroup(group, level) {
|
|
645
|
+
let modelGroupDsl = "";
|
|
646
|
+
modelGroupDsl += this.writeLine(`${group.name} = group "${group.name}" {`, level);
|
|
647
|
+
group.getPeople().forEach((person) => {
|
|
648
|
+
modelGroupDsl += this.writeElement("person", person, level + 1);
|
|
649
|
+
});
|
|
650
|
+
group.getSoftwareSystems().forEach((softwareSystem) => {
|
|
651
|
+
modelGroupDsl += this.writeSoftwareSystem(softwareSystem, level + 1);
|
|
652
|
+
});
|
|
653
|
+
modelGroupDsl += this.writeLine(`}`, level);
|
|
654
|
+
return modelGroupDsl;
|
|
655
|
+
}
|
|
656
|
+
writeModel(model, level) {
|
|
657
|
+
let modelDsl = "";
|
|
658
|
+
modelDsl += this.writeLine(`model {`, level);
|
|
659
|
+
modelDsl += this.writeArchetypes(level + 1);
|
|
660
|
+
modelDsl += this.writeLine("// Elements", level + 1);
|
|
661
|
+
model.getPeopleNotInGroups().forEach((person) => {
|
|
662
|
+
modelDsl += this.writeElement("person", person, level + 1);
|
|
663
|
+
});
|
|
664
|
+
model.getSoftwareSystemsNotInGroups().forEach((softwareSystem) => {
|
|
665
|
+
modelDsl += this.writeSoftwareSystem(softwareSystem, level + 1);
|
|
666
|
+
});
|
|
667
|
+
model.getGroups().forEach((group) => {
|
|
668
|
+
modelDsl += this.writeModelGroup(group, level + 1);
|
|
669
|
+
});
|
|
670
|
+
modelDsl += this.writeLine("// Relationships", level + 1);
|
|
671
|
+
modelDsl += this.writeRelationships(model.getPeople().concat(model.getSoftwareSystems()), level + 1);
|
|
672
|
+
modelDsl += this.writeLine(`}`, level);
|
|
673
|
+
return modelDsl;
|
|
674
|
+
}
|
|
675
|
+
writeView(view, viewType, level) {
|
|
676
|
+
let viewDsl = this.writeLine(`${viewType}${view.subject ? ' "' + view.subject.canonicalName + '"' : ""} "${view.key}" {`, level);
|
|
677
|
+
viewDsl += this.writeLine(`description "${view.description}"`, level + 1);
|
|
678
|
+
if (view.title) {
|
|
679
|
+
viewDsl += this.writeLine(`title "${view.title}"`, level + 1);
|
|
680
|
+
}
|
|
681
|
+
view.scopes.forEach((scope) => {
|
|
682
|
+
viewDsl += this.writeLine(`${scope}`, level + 1);
|
|
683
|
+
});
|
|
684
|
+
viewDsl += this.writeLine(`}`, level);
|
|
685
|
+
return viewDsl;
|
|
686
|
+
}
|
|
687
|
+
writeViews(views, level) {
|
|
688
|
+
let viewDsl = "";
|
|
689
|
+
viewDsl += this.writeLine(`views {`, level);
|
|
690
|
+
viewDsl += this.writeLine("// System Landscape Views", level + 1);
|
|
691
|
+
views.systemLandscapeViews.forEach((view) => {
|
|
692
|
+
viewDsl += this.writeView(view, "systemLandscape", level + 1);
|
|
693
|
+
});
|
|
694
|
+
viewDsl += this.writeLine("// System Context Views", level + 1);
|
|
695
|
+
views.systemContextViews.forEach((view) => {
|
|
696
|
+
viewDsl += this.writeView(view, "systemContext", level + 1);
|
|
697
|
+
});
|
|
698
|
+
viewDsl += this.writeLine("// Container Views", level + 1);
|
|
699
|
+
views.containerViews.forEach((view) => {
|
|
700
|
+
viewDsl += this.writeView(view, "container", level + 1);
|
|
701
|
+
});
|
|
702
|
+
viewDsl += this.writeLine("// Component Views", level + 1);
|
|
703
|
+
views.componentViews.forEach((view) => {
|
|
704
|
+
viewDsl += this.writeView(view, "component", level + 1);
|
|
705
|
+
});
|
|
706
|
+
viewDsl += this.writeLine(`theme default`, level + 1);
|
|
707
|
+
viewDsl += this.writeLine(`}`, level);
|
|
708
|
+
return viewDsl;
|
|
709
|
+
}
|
|
710
|
+
write() {
|
|
711
|
+
let dsl = "";
|
|
712
|
+
this.model.validate();
|
|
713
|
+
dsl += this.writeLine(`workspace "${this.model.name}" {`, 0);
|
|
714
|
+
dsl += this.writeModel(this.model, 1);
|
|
715
|
+
dsl += this.writeViews(this.views, 1);
|
|
716
|
+
dsl += this.writeLine(`}`, 0);
|
|
717
|
+
return dsl;
|
|
718
|
+
}
|
|
719
|
+
writeLine(line, level) {
|
|
720
|
+
const indent = " ".repeat(level * INDENT_SIZE);
|
|
721
|
+
return `${indent}${line}
|
|
722
|
+
`;
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
|
|
726
|
+
// libs/c4-model/src/buildModel.ts
|
|
727
|
+
import { glob } from "glob";
|
|
728
|
+
import { join, dirname } from "path";
|
|
729
|
+
import { fileURLToPath } from "url";
|
|
730
|
+
var __dirname = dirname(fileURLToPath(import.meta.url));
|
|
731
|
+
async function buildModelWithCatalog(options = {}) {
|
|
732
|
+
const { modelName = "model", modules: explicitModules } = options;
|
|
733
|
+
const model = new Model(modelName);
|
|
734
|
+
let c4Modules;
|
|
735
|
+
if (explicitModules) {
|
|
736
|
+
c4Modules = [...explicitModules];
|
|
737
|
+
} else {
|
|
738
|
+
const { globPath = "c4.dsl.ts", searchRoot = __dirname } = options;
|
|
739
|
+
const result = await glob(`**/${globPath}`, { cwd: searchRoot });
|
|
740
|
+
if (result.length === 0) {
|
|
741
|
+
throw new Error(`No ${globPath} files found`);
|
|
742
|
+
}
|
|
743
|
+
const imported = await Promise.all(result.map((file) => import(join(searchRoot, file))));
|
|
744
|
+
c4Modules = imported.filter((m) => m.c4Module).map((m) => m.c4Module);
|
|
745
|
+
}
|
|
746
|
+
if (c4Modules.length === 0) {
|
|
747
|
+
throw new Error("No c4Module instances found");
|
|
748
|
+
}
|
|
749
|
+
const registrations = [];
|
|
750
|
+
const rootCatalog = {};
|
|
751
|
+
for (const instance of c4Modules) {
|
|
752
|
+
const local = instance.registerDefinitions(model);
|
|
753
|
+
rootCatalog[instance.key] = local;
|
|
754
|
+
registrations.push({ instance, key: instance.key, local });
|
|
755
|
+
}
|
|
756
|
+
for (const { instance, key, local } of registrations) {
|
|
757
|
+
const dependencies = Object.fromEntries(Object.entries(rootCatalog).filter(([k]) => k !== key));
|
|
758
|
+
instance.buildRelationships(local, dependencies);
|
|
759
|
+
}
|
|
760
|
+
return { model, catalog: rootCatalog };
|
|
761
|
+
}
|
|
762
|
+
async function buildModel(options = {}) {
|
|
763
|
+
return (await buildModelWithCatalog(options)).model;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
// libs/c4-model/src/generateDiagrams.ts
|
|
767
|
+
import * as fs from "fs";
|
|
768
|
+
import * as os from "os";
|
|
769
|
+
import * as path from "path";
|
|
770
|
+
import { GenericContainer, Wait } from "testcontainers";
|
|
771
|
+
async function generateDiagrams(options) {
|
|
772
|
+
const { views: viewsFactory, outputDir, ...buildOptions } = options;
|
|
773
|
+
const { model, catalog } = await buildModelWithCatalog(buildOptions);
|
|
774
|
+
const views = viewsFactory(catalog);
|
|
775
|
+
const dsl = new StructurizrDSLWriter(model, views).write();
|
|
776
|
+
const tmpDir = await fs.promises.mkdtemp(path.join(fs.realpathSync(os.tmpdir()), "c4-diagrams-"));
|
|
777
|
+
await fs.promises.writeFile(path.join(tmpDir, "workspace.dsl"), dsl, "utf8");
|
|
778
|
+
await new GenericContainer("structurizr/structurizr").withBindMounts([{ source: tmpDir, target: "/workspace", mode: "rw" }]).withCommand(["export", "-w", "/workspace/workspace.dsl", "-f", "mermaid", "-o", "/workspace"]).withWaitStrategy(Wait.forOneShotStartup()).start();
|
|
779
|
+
await fs.promises.mkdir(outputDir, { recursive: true });
|
|
780
|
+
const tmpFiles = await fs.promises.readdir(tmpDir);
|
|
781
|
+
const mmdFiles = tmpFiles.filter((f) => f.endsWith(".mmd"));
|
|
782
|
+
for (const file of mmdFiles) {
|
|
783
|
+
await fs.promises.copyFile(path.join(tmpDir, file), path.join(outputDir, file));
|
|
784
|
+
}
|
|
785
|
+
const generatedFiles = mmdFiles.map((f) => path.join(outputDir, f));
|
|
786
|
+
for (const file of mmdFiles) {
|
|
787
|
+
const baseName = path.basename(file, ".mmd");
|
|
788
|
+
const pngFile = `${baseName}.png`;
|
|
789
|
+
await new GenericContainer("minlag/mermaid-cli").withBindMounts([{ source: tmpDir, target: "/data", mode: "rw" }]).withCommand(["-i", `/data/${file}`, "-o", `/data/${pngFile}`]).withWaitStrategy(Wait.forOneShotStartup()).start();
|
|
790
|
+
await fs.promises.copyFile(path.join(tmpDir, pngFile), path.join(outputDir, pngFile));
|
|
791
|
+
generatedFiles.push(path.join(outputDir, pngFile));
|
|
792
|
+
}
|
|
793
|
+
return generatedFiles;
|
|
794
|
+
}
|
|
795
|
+
export {
|
|
796
|
+
Component,
|
|
797
|
+
Container,
|
|
798
|
+
ContainerGroup,
|
|
799
|
+
ElementArchetype,
|
|
800
|
+
Model,
|
|
801
|
+
ModelGroup,
|
|
802
|
+
Person,
|
|
803
|
+
RelationshipArchetype,
|
|
804
|
+
SoftwareSystem,
|
|
805
|
+
SoftwareSystemGroup,
|
|
806
|
+
StructurizrDSLWriter,
|
|
807
|
+
View,
|
|
808
|
+
Views,
|
|
809
|
+
buildModel,
|
|
810
|
+
buildModelWithCatalog,
|
|
811
|
+
generateDiagrams,
|
|
812
|
+
mergeArchetypeWithOverride
|
|
813
|
+
};
|