-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathtablets.ts
More file actions
318 lines (270 loc) · 8.81 KB
/
tablets.ts
File metadata and controls
318 lines (270 loc) · 8.81 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
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
import { existsSync, promises as fs } from 'fs';
import * as path from 'path';
import * as zlib from 'zlib';
import { TargetLanguage } from '../languages';
import * as logging from '../logging';
import { TypeScriptSnippet, SnippetLocation, completeSource } from '../snippet';
import { mapValues, Mutable } from '../util';
import { snippetKey } from './key';
import { TabletSchema, TranslatedSnippetSchema, ORIGINAL_SNIPPET_KEY } from './schema';
// eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires
const TOOL_VERSION = require('../../package.json').version;
/**
* The default name of the tablet file
*/
export const DEFAULT_TABLET_NAME = '.jsii.tabl.json';
/**
* The default name of the compressed tablet file
*/
export const DEFAULT_TABLET_NAME_COMPRESSED = '.jsii.tabl.json.gz';
export const CURRENT_SCHEMA_VERSION = '2';
/**
* A tablet containing various snippets in multiple languages
*/
export class LanguageTablet {
/**
* Load a tablet from a file
*/
public static async fromFile(filename: string) {
const ret = new LanguageTablet();
await ret.load(filename);
return ret;
}
/**
* Load a tablet from a file that may not exist
*
* Will return an empty tablet if the file does not exist
*/
public static async fromOptionalFile(filename: string) {
const ret = new LanguageTablet();
if (existsSync(filename)) {
try {
await ret.load(filename);
} catch (e: any) {
logging.warn(`${filename}: ${e}`);
}
}
return ret;
}
/**
* Whether or not the LanguageTablet was loaded with a compressed source.
* This gets used to determine if it should be compressed when saved.
*/
public compressedSource = false;
private readonly snippets: Record<string, TranslatedSnippet> = {};
/**
* Add one or more snippets to this tablet
*/
public addSnippets(...snippets: TranslatedSnippet[]) {
for (const snippet of snippets) {
const existingSnippet = this.snippets[snippet.key];
this.snippets[snippet.key] = existingSnippet ? existingSnippet.mergeTranslations(snippet) : snippet;
}
}
/**
* Add one snippet to this tablet
*
* @deprecated use addSnippets instead
*/
public addSnippet(snippet: TranslatedSnippet) {
this.addSnippets(snippet);
}
public get snippetKeys() {
return Object.keys(this.snippets);
}
/**
* Add all snippets from the given tablets into this one
*/
public addTablets(...tablets: LanguageTablet[]) {
for (const tablet of tablets) {
for (const snippet of Object.values(tablet.snippets)) {
this.addSnippet(snippet);
}
}
}
/**
* Add all snippets from the given tablet into this one
*
* @deprecated Use `addTablets()` instead.
*/
public addTablet(tablet: LanguageTablet) {
this.addTablets(tablet);
}
public tryGetSnippet(key: string): TranslatedSnippet | undefined {
return this.snippets[key];
}
/**
* Look up a single translation of a source snippet
*
* @deprecated Use `lookupTranslationBySource` instead.
*/
public lookup(typeScriptSource: TypeScriptSnippet, language: TargetLanguage): Translation | undefined {
return this.lookupTranslationBySource(typeScriptSource, language);
}
/**
* Look up a single translation of a source snippet
*/
public lookupTranslationBySource(
typeScriptSource: TypeScriptSnippet,
language: TargetLanguage,
): Translation | undefined {
const snippet = this.snippets[snippetKey(typeScriptSource)];
return snippet?.get(language);
}
/**
* Lookup the translated verion of a TypeScript snippet
*/
public lookupBySource(typeScriptSource: TypeScriptSnippet): TranslatedSnippet | undefined {
return this.snippets[snippetKey(typeScriptSource)];
}
/**
* Load the tablet from a file. Will automatically detect if the file is
* compressed and decompress accordingly.
*/
public async load(filename: string) {
let data = await fs.readFile(filename);
// Gzip objects start with 1f 8b 08
if (data[0] === 0x1f && data[1] === 0x8b && data[2] === 0x08) {
// This is a gz object, so we decompress it now...
data = zlib.gunzipSync(data);
this.compressedSource = true;
}
const obj: TabletSchema = JSON.parse(data.toString('utf-8'));
if (!obj.toolVersion || !obj.snippets) {
throw new Error(`File '${filename}' does not seem to be a Tablet file`);
}
if (obj.version !== CURRENT_SCHEMA_VERSION) {
// If we're ever changing the schema version in a backwards incompatible way,
// do upconversion here.
throw new Error(
`Tablet file '${filename}' has schema version '${obj.version}', this program expects '${CURRENT_SCHEMA_VERSION}'`,
);
}
Object.assign(this.snippets, mapValues(obj.snippets, TranslatedSnippet.fromSchema));
}
public get count() {
return Object.keys(this.snippets).length;
}
public get translatedSnippets() {
return Object.values(this.snippets);
}
/**
* Saves the tablet schema to a file. If the compress option is passed, then
* the schema will be gzipped before writing to the file.
*/
public async save(filename: string, compress = false) {
await fs.mkdir(path.dirname(filename), { recursive: true });
let schema = Buffer.from(JSON.stringify(this.toSchema(), null, 2));
if (compress) {
schema = zlib.gzipSync(schema);
}
await fs.writeFile(filename, schema);
}
private toSchema(): TabletSchema {
return {
version: CURRENT_SCHEMA_VERSION,
toolVersion: TOOL_VERSION,
snippets: mapValues(this.snippets, (s) => s.snippet),
};
}
}
/**
* Mutable operations on an underlying TranslatedSnippetSchema
*/
export class TranslatedSnippet {
public static fromSchema(schema: TranslatedSnippetSchema) {
if (!schema.translations[ORIGINAL_SNIPPET_KEY]) {
throw new Error(`Input schema must have '${ORIGINAL_SNIPPET_KEY}' key set in translations`);
}
return new TranslatedSnippet(schema);
}
public static fromTypeScript(original: TypeScriptSnippet, didCompile?: boolean) {
return new TranslatedSnippet({
translations: {
[ORIGINAL_SNIPPET_KEY]: { source: original.visibleSource, version: '0' },
},
didCompile: didCompile,
location: original.location,
fullSource: completeSource(original),
});
}
public readonly snippet: TranslatedSnippetSchema;
private readonly _snippet: Mutable<TranslatedSnippetSchema>;
private _key?: string;
private constructor(snippet: TranslatedSnippetSchema) {
this._snippet = { ...snippet };
this.snippet = this._snippet;
}
public get key() {
if (this._key === undefined) {
this._key = snippetKey(this.asTypescriptSnippet());
}
return this._key;
}
public get originalSource(): Translation {
return {
source: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,
language: 'typescript',
didCompile: this.snippet.didCompile,
};
}
public addTranslation(language: TargetLanguage, translation: string, version: string): Translation {
this.snippet.translations[language] = { source: translation, version };
return {
source: translation,
language,
didCompile: this.snippet.didCompile,
};
}
public fqnsReferenced() {
return this._snippet.fqnsReferenced ?? [];
}
public addSyntaxKindCounter(syntaxKindCounter: Record<string, number>) {
if (!this._snippet.syntaxKindCounter) {
this._snippet.syntaxKindCounter = {};
}
for (const [key, value] of Object.entries(syntaxKindCounter)) {
const x = this._snippet.syntaxKindCounter[key] ?? 0;
this._snippet.syntaxKindCounter[key] = value + x;
}
}
public get languages(): TargetLanguage[] {
return Object.keys(this.snippet.translations).filter((x) => x !== ORIGINAL_SNIPPET_KEY) as TargetLanguage[];
}
public get(language: TargetLanguage): Translation | undefined {
const t = this.snippet.translations[language];
return t && { source: t.source, language, didCompile: this.snippet.didCompile };
}
public mergeTranslations(other: TranslatedSnippet) {
return new TranslatedSnippet({
...this.snippet,
translations: { ...this.snippet.translations, ...other.snippet.translations },
});
}
public withFingerprint(fp: string) {
return new TranslatedSnippet({
...this.snippet,
fqnsFingerprint: fp,
});
}
public withLocation(location: SnippetLocation) {
return new TranslatedSnippet({
...this.snippet,
location,
});
}
public toJSON() {
return this._snippet;
}
private asTypescriptSnippet(): TypeScriptSnippet {
return {
visibleSource: this.snippet.translations[ORIGINAL_SNIPPET_KEY].source,
location: this.snippet.location,
};
}
}
export interface Translation {
source: string;
language: string;
didCompile?: boolean;
}