-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathassembly.ts
More file actions
276 lines (238 loc) · 6.79 KB
/
assembly.ts
File metadata and controls
276 lines (238 loc) · 6.79 KB
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
import * as jsii from '@jsii/spec';
import { ClassType } from './class';
import { Dependency } from './dependency';
import { EnumType } from './enum';
import { ModuleLike } from './module-like';
import { InterfaceType } from './interface';
import { Submodule } from './submodule';
import { Type } from './type';
import { TypeSystem } from './type-system';
export class Assembly extends ModuleLike {
private _typeCache?: { [fqn: string]: Type };
private _submoduleCache?: { [fqn: string]: Submodule };
private _dependencyCache?: { [name: string]: Dependency };
public constructor(system: TypeSystem, public readonly spec: jsii.Assembly) {
super(system);
}
public get fqn(): string {
return this.spec.name;
}
/**
* The version of the spec schema
*/
public get schema(): jsii.SchemaVersion {
return this.spec.schema;
}
/**
* The name of the assembly
*/
public get name(): string {
return this.spec.name;
}
/**
* Description of the assembly, maps to "description" from package.json
* This is required since some package managers (like Maven) require it.
*/
public get description(): string {
return this.spec.description;
}
/**
* The metadata associated with the assembly, if any.
*/
public get metadata(): { readonly [key: string]: any } | undefined {
return this.spec.metadata;
}
/**
* The url to the project homepage. Maps to "homepage" from package.json.
*/
public get homepage(): string {
return this.spec.homepage;
}
/**
* The module repository, maps to "repository" from package.json
* This is required since some package managers (like Maven) require it.
*/
public get repository(): {
readonly type: string;
readonly url: string;
readonly directory?: string;
} {
return this.spec.repository;
}
/**
* The main author of this package.
*/
public get author(): jsii.Person {
return this.spec.author;
}
/**
* Additional contributors to this package.
*/
public get contributors(): readonly jsii.Person[] {
return this.spec.contributors ?? [];
}
/**
* A fingerprint that can be used to determine if the specification has changed.
*/
public get fingerprint(): string {
return this.spec.fingerprint;
}
/**
* The version of the assembly
*/
public get version(): string {
return this.spec.version;
}
/**
* The SPDX name of the license this assembly is distributed on.
*/
public get license(): string {
return this.spec.license;
}
/**
* A map of target name to configuration, which is used when generating packages for
* various languages.
*/
public get targets() {
return this.spec.targets;
}
/**
* Dependencies on other assemblies (with semver), the key is the JSII assembly name.
*/
public get dependencies(): readonly Dependency[] {
return Object.keys(this._dependencies).map(
(name) => this._dependencies[name],
);
}
public findDependency(name: string) {
const dep = this._dependencies[name];
if (!dep) {
throw new Error(`Dependency ${name} not found for assembly ${this.name}`);
}
return dep;
}
/**
* List if bundled dependencies (these are not expected to be jsii assemblies).
*/
public get bundled(): { readonly [module: string]: string } {
return this.spec.bundled ?? {};
}
/**
* The top-level readme document for this assembly (if any).
*/
public get readme() {
return this.spec.readme;
}
public get submodules(): readonly Submodule[] {
const { submodules } = this._types;
return Object.values(submodules);
}
/**
* All types in the assembly
*/
public get types(): readonly Type[] {
const { types } = this._types;
return Object.values(types);
}
public findType(fqn: string) {
const type = this.tryFindType(fqn);
if (!type) {
throw new Error(`Type '${fqn}' not found in assembly ${this.name}`);
}
return type;
}
/**
* Validate an assembly after loading
*
* If the assembly was loaded without validation, call this to validate
* it after all. Throws an exception if validation fails.
*/
public validate() {
jsii.validateAssembly(this.spec);
}
private get _dependencies() {
if (!this._dependencyCache) {
this._dependencyCache = {};
if (this.spec.dependencies) {
for (const name of Object.keys(this.spec.dependencies)) {
this._dependencyCache[name] = new Dependency(
this.system,
name,
this.spec.dependencies[name],
);
}
}
}
return this._dependencyCache;
}
private get _types() {
if (!this._typeCache || !this._submoduleCache) {
this._typeCache = {};
const submodules: { [fullName: string]: SubmoduleMap } = {};
const ts = this.spec.types ?? {};
for (const fqn of Object.keys(ts)) {
const typeSpec = ts[fqn];
let submodule = typeSpec.namespace;
while (submodule != null && `${this.spec.name}.${submodule}` in ts) {
submodule = ts[`${this.spec.name}.${submodule}`].namespace;
}
let type: Type;
switch (typeSpec.kind) {
case jsii.TypeKind.Class:
type = new ClassType(this.system, this, typeSpec);
break;
case jsii.TypeKind.Interface:
type = new InterfaceType(this.system, this, typeSpec);
break;
case jsii.TypeKind.Enum:
type = new EnumType(this.system, this, typeSpec);
break;
default:
throw new Error('Unknown type kind');
}
if (submodule != null) {
const [root, ...parts] = submodule.split('.');
let container = (submodules[root] = submodules[root] ?? {
submodules: {},
types: [],
});
for (const part of parts) {
container = container.submodules[part] = container.submodules[
part
] ?? { submodules: {}, types: [] };
}
container.types.push(type);
} else {
this._typeCache[fqn] = type;
}
}
this._submoduleCache = {};
for (const [name, map] of Object.entries(submodules)) {
this._submoduleCache[name] = makeSubmodule(
this.system,
map,
`${this.name}.${name}`,
);
}
}
return { types: this._typeCache, submodules: this._submoduleCache };
}
}
interface SubmoduleMap {
readonly submodules: { [fullName: string]: SubmoduleMap };
readonly types: Type[];
}
function makeSubmodule(
system: TypeSystem,
map: SubmoduleMap,
fullName: string,
): Submodule {
return new Submodule(
system,
fullName,
Object.entries(map.submodules).map(([name, subMap]) =>
makeSubmodule(system, subMap, `${fullName}.${name}`),
),
map.types,
);
}