-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcross-model-package-manager.ts
335 lines (294 loc) · 13.5 KB
/
cross-model-package-manager.ts
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/********************************************************************************
* Copyright (c) 2023 CrossBreeze.
********************************************************************************/
import { Disposable, DocumentState, LangiumDocument, MultiMap } from 'langium';
// eslint-disable-next-line import/no-unresolved
import { SystemInfo, SystemUpdatedEvent, SystemUpdateListener } from '@crossbreeze/protocol';
import { PackageJson } from 'type-fest';
import { CancellationToken, WorkspaceFolder } from 'vscode-languageserver';
import { URI, Utils as UriUtils } from 'vscode-uri';
import { CrossModelSharedServices } from './cross-model-module.js';
import { QUALIFIED_ID_SEPARATOR } from './cross-model-naming.js';
import { PackageAstNodeDescription } from './cross-model-scope.js';
import { Utils } from './util/uri-util.js';
/** Constant for representing an unknown project ID. */
export const UNKNOWN_PROJECT_ID = 'unknown';
/** Constant for representing an unknown project reference. */
export const UNKNOWN_PROJECT_REFERENCE = 'unknown';
export const PACKAGE_JSON = 'package.json';
export function isPackageUri(uri: URI): boolean {
return UriUtils.basename(uri) === PACKAGE_JSON;
}
export function isPackageLockUri(uri: URI): boolean {
return UriUtils.basename(uri).endsWith('package-lock.json');
}
/**
* Creates a unique id for the given data.
*
* @param name package name
* @param version version
* @returns unique id
*/
export function createPackageId(name?: string, version = '0.0.0'): string {
return name === undefined ? UNKNOWN_PROJECT_ID : name + '@' + version;
}
/**
* Creates the package reference name for the given package. This name is used to export the nodes in this package and
* thus will be used in the references and serialization.
*
* @param packageJson package.json data
* @returns package reference name
*/
export function createPackageReferenceName(packageJson?: PackageJson): string {
// we prefer the our custom-introduced alias if it is specified, otherwise we will fall back on the package name
// we do not care about the package version as we do not allow to install multiple versions of the same package
const name = (packageJson?.['alias'] as string | undefined) || packageJson?.name || UNKNOWN_PROJECT_ID;
// ensure we only have characters that are supported by our ID rule in the grammar and still look good to the user
return name.split(' ').join('_').split(QUALIFIED_ID_SEPARATOR).join('-');
}
export function isUnknownPackage(packageId: string): boolean {
return packageId === UNKNOWN_PROJECT_ID;
}
/**
* Information derived from the package.json containing all data necessary within the crossmodel system.
*/
export class PackageInfo {
constructor(
/** URI of the 'package.json' file. */
readonly uri: URI,
/** Data parsed from the 'package.json' file. */
readonly packageJson?: PackageJson,
/** URI of the directory in which the 'package.json' file is located. */
readonly directory: URI = UriUtils.dirname(uri),
/** Identifier for the package. */
readonly id = createPackageId(packageJson?.name, packageJson?.version),
/** Package name used in references and serialization. */
readonly referenceName = createPackageReferenceName(packageJson),
/** A list of all direct dependencies of this package. */
readonly dependencies = Object.entries(packageJson?.dependencies || {}).map(dep => createPackageId(dep[0], dep[1])),
/** True if this is an unknown package, not having a name. */
readonly isUnknown = isUnknownPackage(id)
) {}
}
/**
* A manager that builds up a system of packages on top of a given workspace.
* Directories with a 'package.json' file represent a closed system that can only reference itself.
* However, dependencies may be explicitly given as part of the 'package.json' in which case other systems may become visible/referable.
*/
export class CrossModelPackageManager {
protected uriToPackage = new Map<string, PackageInfo>();
protected idToPackage = new MultiMap<string, PackageInfo>();
protected readonly updateListeners: SystemUpdateListener[] = [];
constructor(
protected shared: CrossModelSharedServices,
protected fileSystemProvider = shared.workspace.FileSystemProvider,
protected textDocuments = shared.workspace.TextDocuments,
protected langiumDocuments = shared.workspace.LangiumDocuments,
protected documentBuilder = shared.workspace.DocumentBuilder,
protected logger = shared.logger.ClientLogger
) {
this.documentBuilder.onUpdate((changed, deleted) => this.onBuildUpdate(changed, deleted));
this.documentBuilder.onBuildPhase(DocumentState.Parsed, (docs, token) => this.documentParsed(docs, token));
}
/**
* Initializes this package manager for the given folders by parsing all package.json files available and
* building up the dependencies between the packages.
*/
async initialize(folders: WorkspaceFolder[]): Promise<void> {
const initializations: Promise<void>[] = [];
for (const folder of folders) {
initializations.push(this.initializePackages(URI.parse(folder.uri)));
}
await Promise.all(initializations);
}
protected async initializePackages(folderPath: URI): Promise<void> {
const content = await this.fileSystemProvider.readDirectory(folderPath);
await Promise.all(
content.map(async entry => {
if (entry.isDirectory) {
await this.initializePackages(entry.uri);
} else if (entry.isFile && isPackageUri(entry.uri)) {
const text = await this.fileSystemProvider.readFile(entry.uri);
await this.updatePackage(entry.uri, text);
}
})
);
}
getPackageIdByUri(uri?: URI): string {
return this.getPackageInfoByURI(uri)?.id || UNKNOWN_PROJECT_ID;
}
getPackageIdByDocument(doc: LangiumDocument): string {
return this.getPackageInfoByDocument(doc)?.id || UNKNOWN_PROJECT_ID;
}
getPackageInfoByDocument(doc?: LangiumDocument): PackageInfo | undefined {
if (!doc) {
return undefined;
}
// during document parsing we store the package URI in the document
const packageUri = (doc as any)['packageUri'] as URI | undefined;
return this.getPackageInfoByURI(packageUri ?? doc.uri);
}
getPackageInfos(): PackageInfo[] {
return Array.from(this.uriToPackage.values());
}
getPackageInfoByURI(uri?: URI): PackageInfo | undefined {
if (!uri) {
return;
}
// see if we have a hit directly on a 'package.json' (faster)
const packageInfo = this.uriToPackage.get(uri.toString());
if (packageInfo) {
return packageInfo;
}
// find closest package info based on the given URI
// we prefer longer path names as we are deeper in the nested hierarchy
const packageInfos = [...this.uriToPackage.values()]
.filter(info => Utils.isChildOf(info.directory, uri))
.sort((left, right) => right.directory.fsPath.length - left.directory.fsPath.length);
return packageInfos[0];
}
isVisible(sourcePackageId: string, targetPackageId: string): boolean {
// an unknown package cannot see anything
return !isUnknownPackage(sourcePackageId) && this.getVisiblePackageIds(sourcePackageId).includes(targetPackageId);
}
protected getAllDependencies(packageId: string): string[] {
return this.idToPackage.get(packageId).flatMap(info => info.dependencies);
}
protected getVisiblePackageIds(sourcePackage: string, includeSource = false, visited: string[] = []): string[] {
// recursively get all visible package ids by going down the package dependencies starting from the source package
if (visited.includes(sourcePackage)) {
return [];
}
visited.push(sourcePackage);
const visible = includeSource ? [sourcePackage] : [];
this.getAllDependencies(sourcePackage).forEach(dependency => visible.push(...this.getVisiblePackageIds(dependency, true, visited)));
return visible;
}
protected async onBuildUpdate(changed: URI[], deleted: URI[]): Promise<void> {
// convert 'package.json' updates to document updates
// - remove 'package.json' updates and track necessary changes
// - build all text documents that are within updated packages
const affectedPackages: string[] = [];
const changedPackages = getAndRemovePackageUris(changed);
for (const changedPackage of changedPackages) {
affectedPackages.push(...(await this.updatePackage(changedPackage)));
}
const deletedPackages = getAndRemovePackageUris(deleted);
for (const deletedPackage of deletedPackages) {
affectedPackages.push(...this.deletePackage(deletedPackage));
}
if (affectedPackages.length > 0) {
// add transitive affected packages
this.idToPackage
.values()
.filter(info => affectedPackages.some(affected => info.dependencies.includes(affected)))
.forEach(info => affectedPackages.push(info.id));
this.langiumDocuments.all
.filter(doc => affectedPackages.includes(this.getPackageIdByDocument(doc)))
.forEach(doc => {
this.langiumDocuments.invalidateDocument(doc.uri);
changed.push(doc.uri);
});
}
}
protected addPackage(uri: URI, packageJson: PackageJson): string[] {
const packageInfo = new PackageInfo(uri, packageJson);
if (!packageInfo.isUnknown) {
this.uriToPackage.set(packageInfo.uri.toString(), packageInfo);
// remove existing entry if there is already one for this package
const existing = this.idToPackage.get(packageInfo.id).find(info => info.uri.toString() === uri.toString());
if (existing) {
this.idToPackage.delete(packageInfo.id, existing);
}
// warn if other package with same ID (but different URI) is registered
const sameId = this.idToPackage.get(packageInfo.id).find(info => info.uri.toString() !== uri.toString());
if (sameId) {
this.logger.warn('A package with the same id was already registered.');
}
this.idToPackage.add(packageInfo.id, packageInfo);
this.emitUpdate({ system: this.convertPackageInfoToSystemInfo(packageInfo), reason: 'added' });
return [packageInfo.id];
}
return [];
}
protected deletePackage(uri: URI): string[] {
const packageInfo = this.uriToPackage.get(uri.toString());
if (packageInfo && !packageInfo?.isUnknown) {
this.uriToPackage.delete(uri.toString());
if (this.idToPackage.delete(packageInfo.id, packageInfo)) {
this.emitUpdate({ system: this.convertPackageInfoToSystemInfo(packageInfo), reason: 'removed' });
}
return [packageInfo.id];
}
return [];
}
protected async updatePackage(uri: URI, text = this.shared.workspace.TextDocuments.get(uri.toString())?.getText()): Promise<string[]> {
const documentText = text ?? (await this.shared.workspace.FileSystemProvider.readFile(uri));
const newPackageJson = parsePackageJson(documentText);
if (!newPackageJson) {
return [];
}
const toUpdate = [];
const existingPackageInfo = this.uriToPackage.get(uri.toString());
const newPackageId = createPackageId(newPackageJson.name, newPackageJson.version);
if (existingPackageInfo && existingPackageInfo?.id !== newPackageId) {
toUpdate.push(...this.deletePackage(uri));
}
toUpdate.push(...this.addPackage(uri, newPackageJson));
return toUpdate;
}
protected async emitUpdate(event: SystemUpdatedEvent): Promise<void> {
await Promise.all(this.updateListeners.map(listener => listener(event)));
}
onUpdate(callback: SystemUpdateListener): Disposable {
this.updateListeners.push(callback);
return Disposable.create(() => {
const index = this.updateListeners.indexOf(callback);
if (index >= 0) {
this.updateListeners.splice(index, 1);
}
});
}
protected documentParsed(built: LangiumDocument[], _cancelToken: CancellationToken): void {
// we only do this so we can quickly find the package info for a given document
for (const doc of built) {
(doc as any)['packageUri'] = this.getPackageInfoByURI(doc.uri)?.uri;
}
}
convertPackageInfoToSystemInfo(packageInfo: PackageInfo): SystemInfo {
const packageId = packageInfo.id;
const directory = UriUtils.dirname(packageInfo.uri);
return {
id: packageInfo.id,
name: packageInfo.packageJson?.name ?? UriUtils.basename(directory) ?? 'Unknown',
directory: directory.fsPath,
packageFilePath: packageInfo.uri.fsPath,
modelFilePaths: this.shared.workspace.IndexManager.allElements()
.filter(desc => desc instanceof PackageAstNodeDescription && desc.packageId === packageId)
.map(desc => desc.documentUri.fsPath)
.distinct()
.toArray()
};
}
}
function getAndRemovePackageUris(uris: URI[]): URI[] {
const packages: URI[] = [];
uris.forEach((uri, idx) => {
if (isPackageUri(uri)) {
packages.push(...uris.splice(idx, 1));
} else if (isPackageLockUri(uri)) {
uris.splice(idx, 1);
}
});
return packages;
}
function parsePackageJson(text?: string): PackageJson | undefined {
if (!text) {
return;
}
try {
return JSON.parse(text) as PackageJson;
} catch (error) {
return undefined;
}
}