-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch.js
89 lines (77 loc) · 2.04 KB
/
fetch.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
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const request = require('request');
const PACKAGES = [
'tools-system',
'toolchain-icestorm',
'toolchain-iverilog'
];
const ARCHS = [
'linux_x86_64',
'linux_i686',
'linux_armv7l',
'linux_aarch64',
'windows_x86',
'windows_amd64',
'darwin'
];
const ARCHDIR_LOOKUP = {
linux_x86_64: 'linux-x64',
linux_i686: 'linux-x32',
linux_armv7l: 'linux-arm',
linux_aarch64: 'linux-arm64',
windows_x86: 'win32-x32',
windows_amd64: 'win32-x64',
darwin: 'darwin-x64'
};
function mkdir(dir) {
if (!dir.startsWith('/')) {
dir = path.join(__dirname, dir);
}
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
return dir;
}
function getLatestVersion(repo) {
return new Promise((resolve, reject) => {
request({
url: `${repo}/releases/latest`,
followRedirect: false
}, (err, res, body) => {
if (err) {
reject(err);
} else {
resolve(path.basename(res.headers.location).slice(1));
}
});
});
}
function downloadRelease(repo, version, file, dir) {
execSync(`wget ${repo}/releases/download/v${version}/${file} -P ${dir}`);
}
function extractTar(archive, dir) {
execSync(`tar -xf ${archive} -C ${dir}`);
}
function downloadLatest(org, prj, arch) {
const downloads = mkdir('downloads');
const prebuilds = mkdir('prebuilds');
const archdir = mkdir(path.join(prebuilds, ARCHDIR_LOOKUP[arch]));
const repo = `https://github.com/${org}/${prj}`;
return getLatestVersion(repo).then((version) => {
const file = `${prj}-${arch}-${version}.tar.gz`;
const tarFile = path.join(downloads, file);
downloadRelease(repo, version, file, downloads);
extractTar(tarFile, archdir);
});
}
function downloadLatestAllArchs(org, prj, archs) {
return Promise.all(archs.map((arch) => downloadLatest(org, prj, arch)));
}
function downloadPackages() {
return Promise.all(PACKAGES.map((pkg) => {
return downloadLatestAllArchs('FPGAwars', pkg, ARCHS);
}));
}
downloadPackages();