|
1 | 1 | // @ts-check
|
2 |
| -// The links to the downloads as of today (02.09.) are the followings: |
3 |
| -// In order to get the latest nightly build for your platform use the following links replacing <DATE> with the current date, using the format YYYYMMDD (i.e for 2019/Aug/06 use 20190806 ) |
4 |
| -// Linux 64 bit: https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-<DATE>_Linux_64bit.tar.gz |
5 |
| -// Linux ARM 64 bit: https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-<DATE>_Linux_ARM64.tar.gz |
6 |
| -// Windows 64 bit: https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-<DATE>_Windows_64bit.zip |
7 |
| -// Mac OSX: https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-<DATE>_macOS_64bit.tar.gz |
8 |
| -// [...] |
9 |
| -// 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 |
10 | 2 |
|
11 |
| -(() => { |
12 |
| - |
13 |
| - const DEFAULT_VERSION = '0.13.0'; // require('moment')().format('YYYYMMDD'); |
| 3 | +(async () => { |
14 | 4 |
|
| 5 | + const fs = require('fs'); |
15 | 6 | const path = require('path');
|
| 7 | + const temp = require('temp'); |
16 | 8 | const shell = require('shelljs');
|
| 9 | + const semver = require('semver'); |
| 10 | + const moment = require('moment'); |
17 | 11 | const downloader = require('./downloader');
|
18 | 12 |
|
19 |
| - const yargs = require('yargs') |
20 |
| - .option('cli-version', { |
21 |
| - alias: 'cv', |
22 |
| - default: DEFAULT_VERSION, |
23 |
| - describe: `The version of the 'arduino-cli' to download, or 'nightly-latest'. Defaults to ${DEFAULT_VERSION}.` |
24 |
| - }) |
25 |
| - .option('force-download', { |
26 |
| - alias: 'fd', |
27 |
| - default: false, |
28 |
| - describe: `If set, this script force downloads the 'arduino-cli' even if it already exists on the file system.` |
29 |
| - }) |
30 |
| - .version(false).parse(); |
31 |
| - |
32 |
| - const version = yargs['cli-version']; |
33 |
| - const force = yargs['force-download']; |
34 |
| - const { platform, arch } = process; |
| 13 | + const version = (() => { |
| 14 | + const pkg = require(path.join(__dirname, '..', 'package.json')); |
| 15 | + if (!pkg) { |
| 16 | + return undefined; |
| 17 | + } |
35 | 18 |
|
36 |
| - const build = path.join(__dirname, '..', 'build'); |
37 |
| - const cli = path.join(build, `arduino-cli${platform === 'win32' ? '.exe' : ''}`); |
38 |
| - |
39 |
| - const suffix = (() => { |
40 |
| - switch (platform) { |
41 |
| - case 'darwin': return 'macOS_64bit.tar.gz'; |
42 |
| - case 'win32': return 'Windows_64bit.zip'; |
43 |
| - case 'linux': { |
44 |
| - switch (arch) { |
45 |
| - case 'arm': return 'Linux_ARMv7.tar.gz'; |
46 |
| - case 'arm64': return 'Linux_ARM64.tar.gz'; |
47 |
| - case 'x64': return 'Linux_64bit.tar.gz'; |
48 |
| - default: return undefined; |
49 |
| - } |
50 |
| - } |
51 |
| - default: return undefined; |
| 19 | + const { arduino } = pkg; |
| 20 | + if (!arduino) { |
| 21 | + return undefined; |
52 | 22 | }
|
| 23 | + |
| 24 | + const { cli } = arduino; |
| 25 | + if (!cli) { |
| 26 | + return undefined; |
| 27 | + } |
| 28 | + |
| 29 | + const { version } = cli; |
| 30 | + return version; |
53 | 31 | })();
|
54 |
| - if (!suffix) { |
55 |
| - shell.echo(`The CLI is not available for ${platform} ${arch}.`); |
| 32 | + |
| 33 | + if (!version) { |
| 34 | + shell.echo(`Could not retrieve CLI version info from the 'package.json'.`); |
56 | 35 | shell.exit(1);
|
57 | 36 | }
|
58 | 37 |
|
59 |
| - const url = `https://downloads.arduino.cc/arduino-cli${version.startsWith('nightly-') ? '/nightly' : ''}/arduino-cli_${version}_${suffix}`; |
60 |
| - downloader.downloadUnzipFile(url, cli, 'arduino-cli', force); |
| 38 | + const { platform, arch } = process; |
| 39 | + const buildFolder = path.join(__dirname, '..', 'build'); |
| 40 | + const cliName = `arduino-cli${platform === 'win32' ? '.exe' : ''}`; |
| 41 | + const destinationPath = path.join(buildFolder, cliName); |
| 42 | + |
| 43 | + if (typeof version === 'string') { |
| 44 | + const suffix = (() => { |
| 45 | + switch (platform) { |
| 46 | + case 'darwin': return 'macOS_64bit.tar.gz'; |
| 47 | + case 'win32': return 'Windows_64bit.zip'; |
| 48 | + case 'linux': { |
| 49 | + switch (arch) { |
| 50 | + case 'arm': return 'Linux_ARMv7.tar.gz'; |
| 51 | + case 'arm64': return 'Linux_ARM64.tar.gz'; |
| 52 | + case 'x64': return 'Linux_64bit.tar.gz'; |
| 53 | + default: return undefined; |
| 54 | + } |
| 55 | + } |
| 56 | + default: return undefined; |
| 57 | + } |
| 58 | + })(); |
| 59 | + if (!suffix) { |
| 60 | + shell.echo(`The CLI is not available for ${platform} ${arch}.`); |
| 61 | + shell.exit(1); |
| 62 | + } |
| 63 | + if (semver.valid(version)) { |
| 64 | + const url = `https://downloads.arduino.cc/arduino-cli/arduino-cli_${version}_${suffix}`; |
| 65 | + shell.echo(`📦 Identified released version of the CLI. Downloading version ${version} from '${url}'`); |
| 66 | + await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
| 67 | + } else if (moment(version, 'YYYYMMDD', true).isValid()) { |
| 68 | + const url = `https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-${version}_${suffix}`; |
| 69 | + shell.echo(`🌙 Identified nightly version of the CLI. Downloading version ${version} from '${url}'`); |
| 70 | + await downloader.downloadUnzipFile(url, destinationPath, 'arduino-cli'); |
| 71 | + } else { |
| 72 | + shell.echo(`🔥 Could not interpret 'version': ${version}`); |
| 73 | + shell.exit(1); |
| 74 | + } |
| 75 | + } else { |
| 76 | + |
| 77 | + // We assume an object with `owner`, `repo`, commitish?` properties. |
| 78 | + const { owner, repo, commitish } = version; |
| 79 | + if (!owner) { |
| 80 | + shell.echo(`Could not retrieve 'owner' from ${JSON.stringify(version)}`); |
| 81 | + shell.exit(1); |
| 82 | + } |
| 83 | + if (!repo) { |
| 84 | + shell.echo(`Could not retrieve 'repo' from ${JSON.stringify(version)}`); |
| 85 | + shell.exit(1); |
| 86 | + } |
| 87 | + const url = `https://github.com/${owner}/${repo}.git`; |
| 88 | + shell.echo(`Building CLI from ${url}. Commitish: ${commitish ? commitish : 'HEAD'}`); |
| 89 | + |
| 90 | + if (fs.existsSync(destinationPath)) { |
| 91 | + shell.echo(`Skipping the CLI build because it already exists: ${destinationPath}`); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (shell.mkdir('-p', buildFolder).code !== 0) { |
| 96 | + shell.echo('Could not create build folder.'); |
| 97 | + shell.exit(1); |
| 98 | + } |
| 99 | + |
| 100 | + const tempRepoPath = temp.mkdirSync(); |
| 101 | + shell.echo(`>>> Cloning CLI source to ${tempRepoPath}...`); |
| 102 | + if (shell.exec(`git clone ${url} ${tempRepoPath}`).code !== 0) { |
| 103 | + shell.exit(1); |
| 104 | + } |
| 105 | + shell.echo('<<< Cloned CLI repo.') |
| 106 | + |
| 107 | + if (commitish) { |
| 108 | + shell.echo(`>>> Checking out ${commitish}...`); |
| 109 | + if (shell.exec(`git -C ${tempRepoPath} checkout ${commitish}`).code !== 0) { |
| 110 | + shell.exit(1); |
| 111 | + } |
| 112 | + shell.echo(`<<< Checked out ${commitish}.`); |
| 113 | + } |
| 114 | + |
| 115 | + shell.echo(`>>> Building the CLI...`); |
| 116 | + if (shell.exec('go build', { cwd: tempRepoPath }).code !== 0) { |
| 117 | + shell.exit(1); |
| 118 | + } |
| 119 | + shell.echo('<<< CLI build done.') |
| 120 | + |
| 121 | + if (!fs.existsSync(path.join(tempRepoPath, cliName))) { |
| 122 | + shell.echo(`Could not find the CLI at ${path.join(tempRepoPath, cliName)}.`); |
| 123 | + shell.exit(1); |
| 124 | + } |
| 125 | + |
| 126 | + const builtCliPath = path.join(tempRepoPath, cliName); |
| 127 | + shell.echo(`>>> Copying CLI from ${builtCliPath} to ${destinationPath}...`); |
| 128 | + if (shell.cp(builtCliPath, destinationPath).code !== 0) { |
| 129 | + shell.exit(1); |
| 130 | + } |
| 131 | + shell.echo(`<<< Copied the CLI.`); |
| 132 | + |
| 133 | + shell.echo('<<< Verifying CLI...'); |
| 134 | + if (!fs.existsSync(destinationPath)) { |
| 135 | + shell.exit(1); |
| 136 | + } |
| 137 | + shell.echo('>>> Verified CLI.'); |
| 138 | + |
| 139 | + } |
61 | 140 |
|
62 | 141 | })();
|
0 commit comments