Skip to content

Commit 60bf58a

Browse files
committed
Added download script for arduino-language-server
1 parent c2675ef commit 60bf58a

File tree

4 files changed

+148
-64
lines changed

4 files changed

+148
-64
lines changed

arduino-ide-extension/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
"which": "^1.3.1"
3434
},
3535
"scripts": {
36-
"prepare": "yarn download-cli && yarn run clean && yarn run build",
36+
"prepare": "yarn download-cli && yarn download-ls && yarn run clean && yarn run build",
3737
"clean": "rimraf lib",
3838
"download-cli": "node ./scripts/download-cli.js",
39+
"download-ls": "node ./scripts/download-ls.js",
3940
"generate-protocol": "node ./scripts/generate-protocol.js",
4041
"lint": "tslint -c ./tslint.json --project ./tsconfig.json",
4142
"build": "tsc && ncp ./src/node/cli-protocol/ ./lib/node/cli-protocol/ && yarn lint",

arduino-ide-extension/scripts/download-cli.js

+3-63
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,14 @@
88
// [...]
99
// redirecting to latest generated builds by replacing latest with the latest available build date, using the format YYYYMMDD (i.e for 2019/Aug/06 latest is replaced with 20190806
1010

11-
(async () => {
11+
(() => {
1212

1313
const DEFAULT_VERSION = 'latest'; // require('moment')().format('YYYYMMDD');
1414

1515
const os = require('os');
16-
const fs = require('fs');
1716
const path = require('path');
1817
const shell = require('shelljs');
19-
const download = require('download');
20-
const decompress = require('decompress');
21-
const unzip = require('decompress-unzip');
22-
const untargz = require('decompress-targz');
23-
24-
process.on('unhandledRejection', (reason, _) => {
25-
shell.echo(String(reason));
26-
shell.exit(1);
27-
throw reason;
28-
});
29-
process.on('uncaughtException', error => {
30-
shell.echo(String(error));
31-
shell.exit(1);
32-
throw error;
33-
});
18+
const downloader = require('./downloader');
3419

3520
const yargs = require('yargs')
3621
.option('cli-version', {
@@ -50,23 +35,8 @@
5035
const { platform, arch } = process;
5136

5237
const build = path.join(__dirname, '..', 'build');
53-
const downloads = path.join(__dirname, '..', 'downloads');
5438
const cli = path.join(build, `arduino-cli${os.platform() === 'win32' ? '.exe' : ''}`);
5539

56-
if (fs.existsSync(cli) && !force) {
57-
shell.echo(`The 'arduino-cli' already exists at ${cli}. Skipping download.`);
58-
shell.exit(0);
59-
}
60-
if (!fs.existsSync(build)) {
61-
if (shell.mkdir('-p', build).code !== 0) {
62-
shell.echo('Could not create new directory.');
63-
shell.exit(1);
64-
}
65-
}
66-
if (shell.rm('-rf', cli, downloads).code !== 0) {
67-
shell.exit(1);
68-
}
69-
7040
const suffix = (() => {
7141
switch (platform) {
7242
case 'darwin': return 'macOS_64bit.tar.gz';
@@ -87,36 +57,6 @@
8757
}
8858

8959
const url = `https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-${version}_${suffix}`;
90-
shell.echo(`>>> Downloading 'arduino-cli' from '${url}'...`);
91-
const data = await download(url);
92-
shell.echo(`<<< Download succeeded.`);
93-
shell.echo('>>> Decompressing CLI...');
94-
const files = await decompress(data, downloads, {
95-
plugins: [
96-
unzip(),
97-
untargz()
98-
]
99-
});
100-
if (files.length === 0) {
101-
shell.echo('Error ocurred when decompressing the CLI.');
102-
shell.exit(1);
103-
}
104-
const cliIndex = files.findIndex(f => f.path.startsWith('arduino-cli'));
105-
if (cliIndex === -1) {
106-
shell.echo('The downloaded artifact does not contains the CLI.');
107-
shell.exit(1);
108-
}
109-
shell.echo('<<< Decompressing succeeded.');
110-
111-
if (shell.mv('-f', path.join(downloads, files[cliIndex].path), cli).code !== 0) {
112-
shell.echo(`Could not move file to ${cli}.`);
113-
shell.exit(1);
114-
}
115-
if (!fs.existsSync(cli)) {
116-
shell.echo(`Could not find CLI at ${cli}.`);
117-
shell.exit(1);
118-
} else {
119-
shell.echo('Done.');
120-
}
60+
downloader.download(url, cli, 'arduino-cli', force);
12161

12262
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// @ts-check
2+
// The links to the downloads as of today (28.08.2019) are the following:
3+
// - https://downloads.arduino.cc/arduino-language-server/nightly/arduino-language-server_${SUFFIX}
4+
// - https://downloads.arduino.cc/arduino-language-server/clangd/clangd_${VERSION}_${SUFFIX}
5+
6+
(() => {
7+
8+
const DEFAULT_ALS_VERSION = 'nightly';
9+
const DEFAULT_CLANGD_VERSION = '8.0.1';
10+
11+
const os = require('os');
12+
const path = require('path');
13+
const shell = require('shelljs');
14+
const downloader = require('./downloader');
15+
16+
const yargs = require('yargs')
17+
.option('ls-version', {
18+
alias: 'lv',
19+
default: DEFAULT_ALS_VERSION,
20+
choices: ['nightly'],
21+
describe: `The version of the 'arduino-language-server' to download. Defaults to ${DEFAULT_ALS_VERSION}.`
22+
})
23+
.option('clangd-version', {
24+
alias: 'cv',
25+
default: DEFAULT_CLANGD_VERSION,
26+
choices: ['8.0.1'],
27+
describe: `The version of 'clangd' to download. Defaults to ${DEFAULT_CLANGD_VERSION}.`
28+
})
29+
.option('force-download', {
30+
alias: 'fd',
31+
default: false,
32+
describe: `If set, this script force downloads the 'arduino-language-server' even if it already exists on the file system.`
33+
})
34+
.version(false).parse();
35+
36+
const alsVersion = yargs['ls-version'];
37+
const clangdVersion = yargs['clangd-version']
38+
const force = yargs['force-download'];
39+
const { platform, arch } = process;
40+
41+
const build = path.join(__dirname, '..', 'build');
42+
const als = path.join(build, `arduino-language-server${os.platform() === 'win32' ? '.exe' : ''}`);
43+
const clangd = path.join(build, `clangd${os.platform() === 'win32' ? '.exe' : ''}`);
44+
45+
let alsSuffix, clangdSuffix;
46+
switch (platform) {
47+
case 'darwin':
48+
alsSuffix = 'Darwin_amd64.zip';
49+
clangdSuffix = 'macos.zip';
50+
break;
51+
case 'win32':
52+
alsSuffix = 'Windows_NT_amd64.zip';
53+
clangdSuffix = 'windows.zip';
54+
break;
55+
case 'linux':
56+
alsSuffix = 'Linux_amd64.zip';
57+
break;
58+
}
59+
if (!alsSuffix) {
60+
shell.echo(`The arduino-language-server is not available for ${platform} ${arch}.`);
61+
shell.exit(1);
62+
}
63+
64+
const alsUrl = `https://downloads.arduino.cc/arduino-language-server/${alsVersion === 'nightly' ? 'nightly/arduino-language-server' : 'arduino-language-server_' + alsVersion}_${alsSuffix}`;
65+
downloader.download(alsUrl, als, 'arduino-language-server', force);
66+
67+
if (clangdSuffix) {
68+
const clangdUrl = `https://downloads.arduino.cc/arduino-language-server/clangd/clangd_${clangdVersion}_${clangdSuffix}`;
69+
downloader.download(clangdUrl, clangd, 'clangd', force);
70+
}
71+
72+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const shell = require('shelljs');
4+
const download = require('download');
5+
const decompress = require('decompress');
6+
const unzip = require('decompress-unzip');
7+
const untargz = require('decompress-targz');
8+
9+
process.on('unhandledRejection', (reason, _) => {
10+
shell.echo(String(reason));
11+
shell.exit(1);
12+
throw reason;
13+
});
14+
process.on('uncaughtException', error => {
15+
shell.echo(String(error));
16+
shell.exit(1);
17+
throw error;
18+
});
19+
20+
exports.download = async (url, targetFile, filePrefix, force) => {
21+
const { platform, arch } = process;
22+
23+
if (fs.existsSync(targetFile) && !force) {
24+
shell.echo(`Skipping download because file already exists: ${targetFile}`);
25+
return;
26+
}
27+
if (!fs.existsSync(path.dirname(targetFile))) {
28+
if (shell.mkdir('-p', path.dirname(targetFile)).code !== 0) {
29+
shell.echo('Could not create new directory.');
30+
shell.exit(1);
31+
}
32+
}
33+
34+
const downloads = path.join(__dirname, '..', 'downloads');
35+
if (shell.rm('-rf', targetFile, downloads).code !== 0) {
36+
shell.exit(1);
37+
}
38+
39+
shell.echo(`>>> Downloading from '${url}'...`);
40+
const data = await download(url);
41+
shell.echo(`<<< Download succeeded.`);
42+
43+
shell.echo('>>> Decompressing...');
44+
const files = await decompress(data, downloads, {
45+
plugins: [
46+
unzip(),
47+
untargz()
48+
]
49+
});
50+
if (files.length === 0) {
51+
shell.echo('Error ocurred while decompressing the archive.');
52+
shell.exit(1);
53+
}
54+
const fileIndex = files.findIndex(f => f.path.startsWith(filePrefix));
55+
if (fileIndex === -1) {
56+
shell.echo(`The downloaded artifact does not contain any file with prefix ${filePrefix}.`);
57+
shell.exit(1);
58+
}
59+
shell.echo('<<< Decompressing succeeded.');
60+
61+
if (shell.mv('-f', path.join(downloads, files[fileIndex].path), targetFile).code !== 0) {
62+
shell.echo(`Could not move file to target path: ${targetFile}`);
63+
shell.exit(1);
64+
}
65+
if (!fs.existsSync(targetFile)) {
66+
shell.echo(`Could not find file: ${targetFile}`);
67+
shell.exit(1);
68+
} else {
69+
shell.echo(`Done: ${targetFile}`);
70+
}
71+
}

0 commit comments

Comments
 (0)