-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-binaries.js
More file actions
119 lines (105 loc) · 3.78 KB
/
get-binaries.js
File metadata and controls
119 lines (105 loc) · 3.78 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
import fs from "fs";
import os from "os";
import path, { dirname } from "path";
import { fileURLToPath } from "url";
import { execSync } from "child_process";
import { argv, exit } from "process";
const __dirname = dirname(fileURLToPath(import.meta.url));
const rubyBuilderUrl = 'https://github.com/ruby/ruby-builder/releases/expanded_assets/toolcache';
const rvmExecPath = execSync('which rvm').toString().trim();
if (!fs.existsSync(rvmExecPath)) {
console.error("RVM is not found!");
exit(1);
}
const rvmRubiesPath = path.join(path.dirname(rvmExecPath), '../rubies');
const tarPath = path.join(__dirname, 'public');
const prefix = argv.length > 2 ? argv[2] : `${os.arch()}-${os.platform()}`;
const metadataPath = `${__dirname}/public/${prefix}-metadata.json`;
const getVersions = async () => {
const rubyBuilderData = await (await fetch(rubyBuilderUrl, {
headers: {
'user-agent': 'curl/8.11.0'
}
})).text();
let versions = [];
{
const hrefRegex = new RegExp(`href="[-\\w/]+?\\/((ruby-3|jruby-|truffleruby-)[.\\d]+)-ubuntu-24.04.tar.gz"`, 'g');
// @ts-ignore
var matches = [
...("" + rubyBuilderData).matchAll(hrefRegex),
];
for (const match of matches) {
if (!versions.includes(match[1])) {
versions.push(match[1]);
}
}
versions = sortSemver(versions).reverse();
// remove minor versions
versions = versions.filter((x, i) => versions.findIndex(y => y.startsWith(x.substring(0, x.indexOf('-') + 4))) == i);
}
fs.writeFileSync(
metadataPath,
JSON.stringify({
date: new Date().toISOString(),
gcc: execSync('gcc --version').toString().split('\n')[0],
machine: [os.platform(), os.arch()].join('-'),
prefix,
versions,
}, null, 2)
);
};
const installVersions = async () => {
const desiredVersions = JSON.parse(fs.readFileSync(metadataPath)).versions;
const installedVersions = fs.readdirSync(rvmRubiesPath);
const missingVersions = desiredVersions.filter(version => !installedVersions.includes(version));
missingVersions.forEach(version => {
try {
execSync(`rvm install ${version} -C "--enable-load-relative,--disable-install-doc"`, { stdio: 'inherit' });
console.log(`Successfully installed Ruby ${version}`);
} catch (error) {
console.error(`Failed to install Ruby ${version}:`, error.message);
}
});
}
const packVersions = async () => {
const desiredVersions = JSON.parse(fs.readFileSync(metadataPath)).versions;
desiredVersions.forEach(version => {
const rubyDir = path.join(rvmRubiesPath, `${version}`);
const fileName = `${prefix}-${version}.tar.gz`;
const archivePath = path.join(tarPath, fileName);
if (fs.existsSync(rubyDir) && !fs.existsSync(archivePath)) {
try {
console.log(`Compressing Ruby ${version}...`);
execSync(`tar -czf ${archivePath} -C ${rvmRubiesPath} ${version}`, { stdio: 'inherit' });
console.log(`Compressed to ${archivePath}`);
} catch (error) {
console.error(`Failed to compress Ruby ${version}:`, error.message);
}
}
});
fs.readdirSync(tarPath)
.filter(file => file.endsWith('.tar.gz') && file.startsWith(prefix + '-'))
.forEach(file => {
if (!desiredVersions.includes(file.replace(prefix + '-', '').replace(/\.tar\.gz$/, ''))) {
fs.unlinkSync(path.join(tarPath, file));
console.log(`Deleted outdated archive: ${file}`);
}
});
}
// https://stackoverflow.com/a/40201629/3908409
/**
* @param {string[]} arr
*/
function sortSemver(arr) {
return arr
.map((a) => a.replace(/\d+/g, (n) => +n + 100000 + ""))
.sort()
.map((a) => a.replace(/\d+/g, (n) => +n - 100000 + ""));
}
async function main() {
await getVersions();
await installVersions();
await packVersions();
console.log("builder tasks completed")
}
main();