This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathindex.js
292 lines (256 loc) · 7.53 KB
/
index.js
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
const fs = require("fs");
const path = require("path");
const glob = require("glob");
const toml = require("toml");
const ejs = require("ejs");
const tv4 = require("tv4");
const base = path.join(__dirname, "..");
const dest = path.join(base, "out");
const schema = require("./schema.json");
const btoa = b => Buffer.from(b).toString("base64");
const basePackages = fs
.readFileSync(path.join(base, "packages.txt"), "utf8")
.split(/\r?\n/)
.filter(x => !/^#|^\s*$/.test(x));
basePackages.sort();
console.log(basePackages);
const aptKeys = {};
const aptKeyUrls = {};
const aptRepos = {};
const packages = {};
// TODO: Move this to a configuration file instead of having it here.
const env = {
APT_OPTIONS: '-o debug::nolocking=true -o dir::cache=/tmp/apt/cache -o dir::state=/tmp/apt/state -o dir::etc::sourcelist=/tmp/apt/sources/sources.list',
CPATH: '/home/runner/.apt/usr/include:/home/runner/.apt/usr/include/x86_64-linux-gnu',
CPPPATH: '/home/runner/.apt/usr/include:/home/runner/.apt/usr/include/x86_64-linux-gnu',
DISPLAY: ':0',
HOME: '/home/runner',
INCLUDE_PATH: '/home/runner/.apt/usr/include:/home/runner/.apt/usr/include/x86_64-linux-gnu',
LANG: 'en_US.UTF-8',
LC_ALL: 'en_US.UTF-8',
LD_LIBRARY_PATH: '/home/runner/.apt/usr/lib/x86_64-linux-gnu:/home/runner/.apt/usr/lib/i386-linux-gnu:/usr/local/lib:/home/runner/.apt/usr/lib',
LIBRARY_PATH: '/home/runner/.apt/usr/lib/x86_64-linux-gnu:/home/runner/.apt/usr/lib/i386-linux-gnu:/usr/local/lib:/home/runner/.apt/usr/lib',
PATH: '/home/runner/.nix-profile/bin:/usr/local/go/bin:/opt/virtualenvs/python3/bin:/usr/GNUstep/System/Tools:/usr/GNUstep/Local/Tools:/home/runner/.apt/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
NIX_SSL_CERT_FILE: '/etc/ssl/certs/ca-certificates.crt',
NIX_PATH: '/home/runner/.nix-defexpr/channels',
NIX_PROFILES: '/nix/var/nix/profiles/default /home/runner/.nix-profile',
PKG_CONFIG_PATH: '/home/runner/.apt/usr/lib/x86_64-linux-gnu/pkgconfig:/home/runner/.apt/usr/lib/i386-linux-gnu/pkgconfig:/home/runner/.apt/usr/lib/pkgconfig:/home/runner/.apt/usr/share/pkgconfig',
PYTHONPATH: '/opt/virtualenvs/python3/lib/python3.8/site-packages',
USER: 'runner',
VIRTUAL_ENV: '/opt/virtualenvs/python3',
};
const languageIds = glob
.sync(path.join(base, "languages", "*.toml"))
.map(filename => path.basename(filename, '.toml'))
.sort();
const languageInfo = languageIds
.map(languageId => {
const filepath = path.join(base, "languages", `${languageId}.toml`);
const info = toml.parse(
fs
.readFileSync(filepath, 'utf8')
.replace(/\r\n/g, '\n')
);
const result = tv4.validateMultiple(info, schema);
if (!result.valid) {
console.log(`Warning: ${filepath} schema invalid.`);
for (const error of result.errors) {
console.log(`-> ${error.message} @ ${error.dataPath}`);
}
process.exit(1);
}
info.id = languageId;
return info;
}).reduce((result, info) => {
result[info.id] = info;
return result;
}, {});
for (const childLanguage of Object.values(languageInfo)) {
if (!childLanguage.languages) {
continue;
}
const queue = [].concat(childLanguage.languages);
while (queue.length) {
const parentLanguage = languageInfo[queue.shift()];
if (!parentLanguage.childLanguages) {
parentLanguage.childLanguages = new Set();
}
if (parentLanguage.childLanguages.has(childLanguage.id)) {
continue;
}
parentLanguage.childLanguages.add(childLanguage.id);
if (parentLanguage.languages) {
queue.push(...parentLanguage.languages);
}
}
}
const languages = [];
const handledLanguages = {};
function handleLanguage(language, dependency = false) {
if (language.id in handledLanguages) {
return;
}
let name = language.name;
let names = [name, language.id];
if (language.aliases) {
names = names.concat(language.aliases);
};
language.names = [...new Set(names)];
language.popularity = language.popularity || 2;
for (const parentLanguageId of (language.languages || [])) {
handleLanguage(languageInfo[parentLanguageId]);
}
if (!language.versionCommand) {
let flag = "--version";
if (["lua"].indexOf(language.name) !== -1) {
flag = "-v";
}
if (["go"].indexOf(language.name) !== -1) {
flag = "version";
}
if (["java"].indexOf(language.name) !== -1) {
flag = "-version";
}
if (language.compile) {
language.versionCommand = [language.compile.command[0], flag];
} else if (language.run) {
language.versionCommand = [language.run.command[0], flag];
} else {
language.versionCommand = ["/bin/false"];
}
}
if (!("tests" in language)) {
console.log(`Warning ${name} has no tests.`);
language.tests = {};
}
const languageIds = [language.id, ...(language.childLanguages || [])];
if (language.aptKeys) {
for (let p of language.aptKeys) {
if (!aptKeys[p]) {
aptKeys[p] = [];
}
aptKeys[p].push(...languageIds);
}
}
if (language.aptKeyUrls) {
for (let p of language.aptKeyUrls) {
if (!aptKeyUrls[p]) {
aptKeyUrls[p] = [];
}
aptKeyUrls[p].push(...languageIds);
}
}
if (language.aptRepos) {
for (let p of language.aptRepos) {
if (!aptRepos[p]) {
aptRepos[p] = [];
}
aptRepos[p].push(...languageIds);
}
}
if (language.packages) {
for (let p of language.packages) {
if (basePackages.indexOf(p) != -1) continue;
if (!packages[p]) {
packages[p] = [];
}
packages[p].push(...languageIds);
}
}
languages.push(language);
handledLanguages[language.id] = true;
}
for (const languageId of languageIds ) {
handleLanguage(languageInfo[languageId]);
}
// Ensure this is stable-sorted.
const lbypop = JSON.parse(JSON.stringify(languages))
.map((value, index) => [index, value])
.sort(([aIndex, aValue], [bIndex, bValue]) => {
if (aValue.popularity == bValue.popularity) {
return aIndex - bIndex;
}
return bValue.popularity - aValue.popularity;
})
.map(([, value]) => value);
let lpad = (s, n) => s + new Array(n - s.length).fill(" ").join("");
let ctx = {
basePackages,
languageInfo,
languages,
btoa,
lbypop,
packages,
aptRepos,
aptKeys,
aptKeyUrls,
lpad,
env,
c: a => a.map((s) => {
if (/^(\S*|`[^`]+`)$/.test(s)) return s;
return `'${s.replace(/[$'\\]/g, (m) => '\\' + m)}'`;
}).join(" ")
};
let objects = {
"test.sh": "tests.ejs",
"self-test": "self-test.ejs",
"phase0.sh": "phase0.ejs",
"Dockerfile": "Dockerfile.ejs",
"Dockerfile.splice": "Dockerfile.splice.ejs",
"languages.d": "languages.d.ejs",
"languages.json": "languages.json.ejs",
"run-project": "run-project.ejs",
"run-language-server": "run-language-server.ejs",
"detect-language": "detect-language.ejs",
"polygott-survey": "versions.ejs",
"polygott-lang-setup": "lang-setup.ejs",
"polygott-x11-vnc": "x11-vnc.ejs",
};
for (let target in objects) {
let tp = path.join(dest, target);
fs.writeFileSync(
tp,
ejs.compile(
fs
.readFileSync(path.join(__dirname, objects[target]), "utf8")
.replace(/\r\n/g, '\n')
)(
ctx
),
"utf8"
);
fs.chmodSync(path.join(dest, target), 0o755);
}
const perLangScripts = [
'self-test',
'phase2',
];
for (const perLangScript of perLangScripts) {
const dirname = path.join(dest, `share/polygott/${perLangScript}.d`);
fs.mkdirSync(dirname, { recursive: true, mode: 0o755 });
for (const lang of languages) {
const scriptPath = path.join(dirname, lang.id);
fs.writeFileSync(
scriptPath,
ejs.compile(
fs
.readFileSync(
path.join(__dirname, `${perLangScript}-per-lang.ejs`),
'utf8',
)
.replace(/\r\n/g, '\n')
)(
{
...ctx,
lang,
},
),
'utf8'
);
fs.chmodSync(scriptPath, 0o755);
}
}
// Local Variables:
// indent-tabs-mode: t
// js-indent-level: 8
// End: