-
Notifications
You must be signed in to change notification settings - Fork 43
/
generate.ts
336 lines (266 loc) · 8.4 KB
/
generate.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
336
const basepath = "./lua/efmls-configs";
const supportedListMdFilepath = "./doc/SUPPORTED_LIST.md";
const supportedListJsonFilepath = "./doc/supported-list.json";
type DefaultConfiguration = {
languages: string[];
linters?: string[];
formatters?: string[];
alias?: string;
};
type DefaultConfigurationJsonData = {
defaults: DefaultConfiguration[];
};
type LuaFileMetadata = {
[key: string]: string | string[];
};
// Tracker
type ToolMetadata = {
name: string;
url: string;
};
type LanguageTool = {
linters: ToolMetadata[];
formatters: ToolMetadata[];
};
const languages = new Map<string, LanguageTool>();
/**
* Parse a custom frontmatter data for lua files
*
* @param {string} content
* @returns {{ [key: string]: string }}
*/
function parseFrontmatter(content: string): LuaFileMetadata {
const frontmatter: LuaFileMetadata = {};
const lines = content.split(/\r?\n/);
let inFrontmatter = false;
for (const line of lines) {
if (!inFrontmatter) {
if (line.trim() === "-- Metadata") {
inFrontmatter = true;
}
} else {
if (line.trim() === "") {
// End parsing
break;
}
const match = line.match(/^\s*--\s*(\S+):\s*(.*)/);
if (match) {
const key = match[1];
const value = match[2];
if (key === "languages") {
// Make comma separated values into an array
frontmatter[key] = value.split(",").map((v) => v.trim());
} else {
frontmatter[key] = value;
}
}
}
}
return frontmatter;
}
/**
* Capitalize the first word.
*
* @param {string} word
* @returns {string}
*/
function capitalize(word: string): string {
return word.charAt(0).toUpperCase() + word.slice(1);
}
/**
* Get a list of defaults from defaults.lua.
*
* @async
* @returns {Promise<string[][]>}
*/
async function getDefaults(): Promise<string[][]> {
const defaultFile = JSON.parse(
await Deno.readTextFile(`${basepath}/defaults.json`),
) as DefaultConfigurationJsonData;
const defaults: string[][] = [];
for (const def of defaultFile.defaults) {
const langs = def.alias ?? def.languages.map((lang) => capitalize(lang)).join("/");
const linters = def.linters?.map((linter) => "`" + linter + "`").join(",") ?? "";
const formatters = def.formatters?.map((formatter) => "`" + formatter + "`").join(",") ?? "";
defaults.push([langs, linters, formatters]);
}
return defaults;
}
/**
* Stringify defaults.
*
* @async
* @returns {Promise<string>}
*/
async function renderDefaults(): Promise<string> {
const defaults = await getDefaults();
let defString = "";
for (const def of defaults) {
defString += `| ${def[0]} | ${def[1]} | ${def[2]} |\n`;
}
return defString;
}
/**
* Generate linter data from linter files for languages.
*
* @async
* @returns {Promise<string[]>}
*/
async function generateLanguageLinters(): Promise<void> {
for await (const dirEntry of Deno.readDir(`${basepath}/linters`)) {
const linterName = dirEntry.name.split(".")[0];
const fileContents = await Deno.readTextFile(`${basepath}/linters/${dirEntry.name}`);
const frontmatter = parseFrontmatter(fileContents);
for (const lang of frontmatter.languages ?? []) {
const linters = languages.get(lang)?.linters ?? [];
languages.set(
lang,
{
...languages.get(lang),
linters: [...linters, { name: linterName, url: frontmatter.url as string }].sort((a, b) =>
a.name.localeCompare(b.name)
),
} as LanguageTool,
);
}
}
}
/**
* Generate formatter data from formatter files for languages.
*
* @async
* @returns {Promise<string[]>}
*/
async function generateLanguageFormatters(): Promise<void> {
for await (const dirEntry of Deno.readDir(`${basepath}/formatters`)) {
const formatterName = dirEntry.name.split(".")[0];
const fileContents = await Deno.readTextFile(`${basepath}/formatters/${dirEntry.name}`);
const frontmatter = parseFrontmatter(fileContents);
for (const lang of frontmatter.languages ?? []) {
const formatters = languages.get(lang)?.formatters ?? [];
languages.set(
lang,
{
...languages.get(lang),
formatters: [...formatters, { name: formatterName, url: frontmatter.url as string }].sort(
(a, b) => a.name.localeCompare(b.name),
),
} as LanguageTool,
);
}
}
}
/**
* Generate misc documentation
*
* @param {LanguageTool} misc
* @returns {string}
*/
function getRenderMiscLanguages(misc: LanguageTool): string {
let contents = "";
contents += `### Misc (Text, LaTeX, Markdown, RST, etc)\n\n`;
if (misc.linters) {
contents += `#### Linters\n\n`;
for (const linter of misc.linters) {
contents += `\`${linter.name}\` [${linter.url}](${linter.url})
\`\`\`lua
local ${linter.name} = require('efmls-configs.linters.${linter.name}')
\`\`\`
`;
}
}
if (misc.formatters) {
contents += `#### Formatters\n\n`;
for (const formatter of misc.formatters) {
contents += `\`${formatter.name}\` [${formatter.url}](${formatter.url})
\`\`\`lua
local ${formatter.name} = require('efmls-configs.formatters.${formatter.name}')
\`\`\`
`;
}
}
return contents;
}
/**
* Render language section.
*
* @returns {string}
*/
function renderLanguages(): string {
let languageString = "";
// Render misc languages first then the rest
languageString += getRenderMiscLanguages(languages.get("misc") as LanguageTool);
languages.delete("misc");
// Sort languages
const sortedLanguages = new Map([...languages.entries()].sort());
for (const [lang, tools] of sortedLanguages) {
languageString += `### ${capitalize(lang)}\n\n`;
if (tools.linters) {
languageString += `#### Linters\n\n`;
for (const linter of tools.linters) {
languageString += `\`${linter.name}\` [${linter.url}](${linter.url})
\`\`\`lua
local ${linter.name} = require('efmls-configs.linters.${linter.name}')
\`\`\`
`;
}
}
if (tools.formatters) {
languageString += `#### Formatters\n\n`;
for (const formatter of tools.formatters) {
languageString += `\`${formatter.name}\` [${formatter.url}](${formatter.url})
\`\`\`lua
local ${formatter.name} = require('efmls-configs.formatters.${formatter.name}')
\`\`\`
`;
}
}
}
return languageString;
}
async function generateSupportedList() {
let contents = `<!-- DO NOT MODIFY THIS FILE. AUTO-GENERATED USING \`deno run gen\` -->
# Supported Linters & Formatters
Please create an Pull Request if you see any mismatched documentation or typos.
## Default Configurations
Here is a list of languages that are automatically set and configured when you call \`languages()\`. For more information
check the docs: [\`:help efmls-configs-defaults\`](../README.md#default-configurations).
| **Filetype** | **Default Linter** | **Default Formatter** |
|--------------|--------------------|-----------------------|
`;
contents += await renderDefaults();
contents += `\n`;
contents += `## Languages\n\n`;
contents +=
`Below are the supported linters and formatters that are configured to run with efm-langserver. You can access
the table of contents of this document on the left hand corner of the file, in github (Or just a simple <kbd>Ctrl</kbd> + <kbd>F</kbd>
to search, <kbd>Cmd</kbd> + <kbd>F</kbd> if on Mac OS). See example code: [\`:help efmls-configs-setup\`](../README.md#setup).\n\n`;
contents += renderLanguages();
console.log("Generating SUPPORTED_LIST.md");
await Deno.writeTextFile(supportedListMdFilepath, contents);
console.log("Done!");
}
async function createOrUpdateLanguagesJson(): Promise<void> {
const sortedLanguages = new Map([...languages.entries()].sort());
const jsonLanguages: Record<string, { linters?: ToolMetadata[]; formatters?: ToolMetadata[] }> =
{};
for (const [lang, tools] of sortedLanguages) {
jsonLanguages[lang] = {};
if (tools.linters) {
jsonLanguages[lang].linters = tools.linters;
}
if (tools.formatters) {
jsonLanguages[lang].formatters = tools.formatters;
}
}
const jsonContent = JSON.stringify(jsonLanguages, null, 2);
console.log("Generating supported-list.json");
await Deno.writeTextFile(supportedListJsonFilepath, jsonContent);
console.log("Done!");
}
if (import.meta.main) {
await generateLanguageLinters();
await generateLanguageFormatters();
await createOrUpdateLanguagesJson();
await generateSupportedList();
}