-
Notifications
You must be signed in to change notification settings - Fork 243
/
infuse.ts
271 lines (233 loc) · 8.54 KB
/
infuse.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
import * as spec from '@jsii/spec';
import * as fs from 'fs-extra';
import * as path from 'path';
import {
loadAssemblies,
replaceAssembly,
loadAllDefaultTablets,
LoadedAssembly,
allTypeScriptSnippets,
} from '../jsii/assemblies';
import { renderMetadataline, TypeScriptSnippet } from '../snippet';
import { SnippetSelector, mean, meanLength, shortest, longest } from '../snippet-selectors';
import { snippetKey } from '../tablets/key';
import { LanguageTablet, TranslatedSnippet, DEFAULT_TABLET_NAME } from '../tablets/tablets';
import { isDefined, mkDict, indexBy } from '../util';
export interface InfuseResult {
readonly coverageResults: Record<string, InfuseTypes>;
}
export interface InfuseTypes {
readonly types: number;
readonly typesWithInsertedExamples: number;
}
export interface InfuseOptions {
readonly logFile?: string;
/**
* Where to read additional translations
*/
readonly cacheFromFile?: string;
/**
* In addition to the implicit tablets, also write all added examples to this additional output tablet
*/
readonly cacheToFile?: string;
}
export const DEFAULT_INFUSION_RESULTS_NAME = 'infusion-results.html';
const ADDITIONAL_SELECTORS: Record<string, SnippetSelector> = { meanLength, shortest, longest };
class DefaultRecord<A> {
public readonly index: Record<string, A[]> = {};
public add(key: string, value: A) {
if (!this.index[key]) {
this.index[key] = [];
}
this.index[key].push(value);
}
}
/**
* Infuse will analyze the snippets in a set of tablets, and update the assembly to add
* examples to types that don't have any yet, based on snippets that use the given type.
*/
export async function infuse(assemblyLocations: string[], options?: InfuseOptions): Promise<InfuseResult> {
let stream: fs.WriteStream | undefined = undefined;
if (options?.logFile) {
// Create stream for html file and insert some styling
stream = fs.createWriteStream(options.logFile, { encoding: 'utf-8' });
startFile(stream);
}
// Load tablet file and assemblies
const assemblies = await loadAssemblies(assemblyLocations, false);
const defaultTablets = await loadAllDefaultTablets(assemblies);
const availableTranslations = new LanguageTablet();
if (options?.cacheFromFile) {
availableTranslations.addTablet(await LanguageTablet.fromOptionalFile(options.cacheFromFile));
}
availableTranslations.addTablets(...Object.values(defaultTablets));
const { translationsByFqn, originalsByKey } = await availableSnippetsPerFqn(assemblies, availableTranslations);
const additionalOutputTablet = options?.cacheToFile
? await LanguageTablet.fromOptionalFile(options?.cacheToFile)
: new LanguageTablet();
const coverageResults = mkDict(
await Promise.all(
assemblies.map(async ({ assembly, directory }) => {
stream?.write(`<h1>${assembly.name}</h1>\n`);
const implicitTablet = defaultTablets[directory];
if (!implicitTablet) {
throw new Error(`No tablet found for ${directory}`);
}
let insertedExamples = 0;
const filteredTypes = filterForTypesWithoutExamples(assembly.types ?? {});
for (const [typeFqn, type] of Object.entries(filteredTypes)) {
const available = translationsByFqn[typeFqn];
if (!available) {
continue;
}
const example = pickBestExample(typeFqn, available, stream);
const original = originalsByKey[example.key];
insertExample(example, original, type, [implicitTablet, additionalOutputTablet]);
insertedExamples++;
}
if (insertedExamples > 0) {
// Save the updated assembly and implicit tablets
// eslint-disable-next-line no-await-in-loop
await Promise.all([
replaceAssembly(assembly, directory),
implicitTablet.save(path.join(directory, DEFAULT_TABLET_NAME)),
]);
}
return [
directory,
{
types: Object.keys(filteredTypes).length,
typesWithInsertedExamples: insertedExamples,
} as InfuseTypes,
] as const;
}),
),
);
stream?.close();
// If we copied examples onto different types, we'll also have inserted new snippets
// with different keys into the tablet. We must now write the updated tablet somewhere.
if (options?.cacheToFile) {
await additionalOutputTablet.save(options.cacheToFile);
}
return {
coverageResults: coverageResults,
};
}
function pickBestExample(typeFqn: string, choices: TranslatedSnippet[], logStream?: fs.WriteStream) {
const meanResult = mean(choices);
if (logStream) {
const selected = Object.entries(ADDITIONAL_SELECTORS).map(([name, fn]) => [name, fn(choices)] as const);
const selectedFromSelector = {
...makeDict(selected),
mean: meanResult,
};
logOutput(logStream, typeFqn, createHtmlEntry(selectedFromSelector));
}
return meanResult;
}
function startFile(stream: fs.WriteStream) {
stream.write('<style>\n');
stream.write('h2 { color: blue; clear: both; }\n');
stream.write('h1 { color: red; clear: both; }\n');
stream.write(
'div { float: left; height: 31em; width: 22em; overflow: auto; margin: 1em; background-color: #ddd; }\n',
);
stream.write(
'pre { float: left; height: 30em; width: 25em; overflow: auto; padding: 0.5em; background-color: #ddd; }\n',
);
stream.write('</style>\n');
}
function createHtmlEntry(results: Record<string, TranslatedSnippet>): Record<string, string[]> {
const entry = new DefaultRecord<string>();
for (const [key, value] of Object.entries(results)) {
entry.add(value.originalSource.source, key);
}
return entry.index;
}
function logOutput(stream: fs.WriteStream | undefined, typeFqn: string, algorithmMap: Record<string, string[]>) {
stream?.write(`<h2>${typeFqn}</h2>\n`);
for (const [key, value] of Object.entries(algorithmMap)) {
stream?.write(`<div class="snippet"><h3>${value.toString()}</h3>\n<pre>${key}</pre>\n</div>\n`);
}
for (let i = 0; i < 4 - Object.keys(algorithmMap).length; i++) {
stream?.write('<div class="padding"></div>\n');
}
}
function filterForTypesWithoutExamples(types: { [fqn: string]: spec.Type }): Record<string, spec.Type> {
const filteredTypes: Record<string, spec.Type> = {};
for (const [typeFqn, type] of Object.entries(types)) {
// Ignore Interfaces that contain only properties
if (type.kind === spec.TypeKind.Interface && !type.datatype) {
continue;
}
// Already has example
if (type.docs?.example !== undefined) {
continue;
}
filteredTypes[typeFqn] = type;
}
return filteredTypes;
}
/**
* Insert an example into the docs of a type, and insert it back into the tablet under a new key
*/
function insertExample(
example: TranslatedSnippet,
original: TypeScriptSnippet | undefined,
type: spec.Type,
tablets: LanguageTablet[],
): void {
const parameters = {
...original?.parameters,
infused: '',
};
// exampleMetadata should always be nonempty since we always have a parameter.
const exampleMetadata = renderMetadataline(parameters) ?? '';
if (type.docs) {
type.docs.example = example.originalSource.source;
type.docs.custom = { ...type.docs.custom, exampleMetadata };
} else {
type.docs = {
example: example.originalSource.source,
custom: { exampleMetadata },
};
}
for (const tablet of tablets) {
tablet.addSnippet(
example.withLocation({
api: { api: 'type', fqn: type.fqn },
field: { field: 'example' },
}),
);
}
}
/**
* Return a map of FQN -> snippet keys that exercise that FQN.
*
* For a snippet to qualify, it must both:
*
* a) be current (i.e.: exist in the input assemblies)
* b) have been analyzed (i.e.: exist in one of the translated tablets)
*
* Returns a map of fqns to a list of keys that represent snippets that include the fqn.
*/
async function availableSnippetsPerFqn(asms: readonly LoadedAssembly[], translationsTablet: LanguageTablet) {
const ret = new DefaultRecord<TranslatedSnippet>();
const originalsByKey = indexBy(await allTypeScriptSnippets(asms), snippetKey);
const translations = Object.keys(originalsByKey)
.map((key) => translationsTablet.tryGetSnippet(key))
.filter(isDefined);
for (const trans of translations) {
for (const fqn of trans.snippet.fqnsReferenced ?? []) {
ret.add(fqn, trans);
}
}
return { originalsByKey, translationsByFqn: ret.index };
}
function makeDict<A>(xs: Array<readonly [string, A]>): Record<string, A> {
const ret: Record<string, A> = {};
for (const [str, a] of xs) {
ret[str] = a;
}
return ret;
}