|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +(async () => { |
| 4 | + const fs = require('fs'); |
| 5 | + const path = require('path'); |
| 6 | + const temp = require('temp'); |
| 7 | + const shell = require('shelljs'); |
| 8 | + const semver = require('semver'); |
| 9 | + const downloader = require('./downloader'); |
| 10 | + |
| 11 | + const version = (() => { |
| 12 | + const pkg = require(path.join(__dirname, '..', 'package.json')); |
| 13 | + if (!pkg) { |
| 14 | + return undefined; |
| 15 | + } |
| 16 | + |
| 17 | + const { arduino } = pkg; |
| 18 | + if (!arduino) { |
| 19 | + return undefined; |
| 20 | + } |
| 21 | + |
| 22 | + const { fwuploader } = arduino; |
| 23 | + if (!fwuploader) { |
| 24 | + return undefined; |
| 25 | + } |
| 26 | + |
| 27 | + const { version } = fwuploader; |
| 28 | + return version; |
| 29 | + })(); |
| 30 | + |
| 31 | + if (!version) { |
| 32 | + shell.echo( |
| 33 | + `Could not retrieve Firmware Uploader version info from the 'package.json'.` |
| 34 | + ); |
| 35 | + shell.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + const { platform, arch } = process; |
| 39 | + const buildFolder = path.join(__dirname, '..', 'build'); |
| 40 | + const fwuploderName = `arduino-fwuploader${ |
| 41 | + platform === 'win32' ? '.exe' : '' |
| 42 | + }`; |
| 43 | + const destinationPath = path.join(buildFolder, fwuploderName); |
| 44 | + |
| 45 | + if (typeof version === 'string') { |
| 46 | + const suffix = (() => { |
| 47 | + switch (platform) { |
| 48 | + case 'darwin': |
| 49 | + return 'macOS_64bit.tar.gz'; |
| 50 | + case 'win32': |
| 51 | + return 'Windows_64bit.zip'; |
| 52 | + case 'linux': { |
| 53 | + switch (arch) { |
| 54 | + case 'arm': |
| 55 | + return 'Linux_ARMv7.tar.gz'; |
| 56 | + case 'arm64': |
| 57 | + return 'Linux_ARM64.tar.gz'; |
| 58 | + case 'x64': |
| 59 | + return 'Linux_64bit.tar.gz'; |
| 60 | + default: |
| 61 | + return undefined; |
| 62 | + } |
| 63 | + } |
| 64 | + default: |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + })(); |
| 68 | + if (!suffix) { |
| 69 | + shell.echo( |
| 70 | + `The Firmware Uploader is not available for ${platform} ${arch}.` |
| 71 | + ); |
| 72 | + shell.exit(1); |
| 73 | + } |
| 74 | + if (semver.valid(version)) { |
| 75 | + const url = `https://downloads.arduino.cc/arduino-fwuploader/arduino-fwuploader_${version}_${suffix}`; |
| 76 | + shell.echo( |
| 77 | + `📦 Identified released version of the Firmware Uploader. Downloading version ${version} from '${url}'` |
| 78 | + ); |
| 79 | + await downloader.downloadUnzipFile( |
| 80 | + url, |
| 81 | + destinationPath, |
| 82 | + 'arduino-fwuploader' |
| 83 | + ); |
| 84 | + } else { |
| 85 | + shell.echo(`🔥 Could not interpret 'version': ${version}`); |
| 86 | + shell.exit(1); |
| 87 | + } |
| 88 | + } else { |
| 89 | + // We assume an object with `owner`, `repo`, commitish?` properties. |
| 90 | + const { owner, repo, commitish } = version; |
| 91 | + if (!owner) { |
| 92 | + shell.echo(`Could not retrieve 'owner' from ${JSON.stringify(version)}`); |
| 93 | + shell.exit(1); |
| 94 | + } |
| 95 | + if (!repo) { |
| 96 | + shell.echo(`Could not retrieve 'repo' from ${JSON.stringify(version)}`); |
| 97 | + shell.exit(1); |
| 98 | + } |
| 99 | + const url = `https://github.com/${owner}/${repo}.git`; |
| 100 | + shell.echo( |
| 101 | + `Building Firmware Uploader from ${url}. Commitish: ${ |
| 102 | + commitish ? commitish : 'HEAD' |
| 103 | + }` |
| 104 | + ); |
| 105 | + |
| 106 | + if (fs.existsSync(destinationPath)) { |
| 107 | + shell.echo( |
| 108 | + `Skipping the Firmware Uploader build because it already exists: ${destinationPath}` |
| 109 | + ); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + if (shell.mkdir('-p', buildFolder).code !== 0) { |
| 114 | + shell.echo('Could not create build folder.'); |
| 115 | + shell.exit(1); |
| 116 | + } |
| 117 | + |
| 118 | + const tempRepoPath = temp.mkdirSync(); |
| 119 | + shell.echo(`>>> Cloning Firmware Uploader source to ${tempRepoPath}...`); |
| 120 | + if (shell.exec(`git clone ${url} ${tempRepoPath}`).code !== 0) { |
| 121 | + shell.exit(1); |
| 122 | + } |
| 123 | + shell.echo('<<< Cloned Firmware Uploader repo.'); |
| 124 | + |
| 125 | + if (commitish) { |
| 126 | + shell.echo(`>>> Checking out ${commitish}...`); |
| 127 | + if ( |
| 128 | + shell.exec(`git -C ${tempRepoPath} checkout ${commitish}`).code !== 0 |
| 129 | + ) { |
| 130 | + shell.exit(1); |
| 131 | + } |
| 132 | + shell.echo(`<<< Checked out ${commitish}.`); |
| 133 | + } |
| 134 | + |
| 135 | + shell.echo(`>>> Building the Firmware Uploader...`); |
| 136 | + if (shell.exec('go build', { cwd: tempRepoPath }).code !== 0) { |
| 137 | + shell.exit(1); |
| 138 | + } |
| 139 | + shell.echo('<<< Firmware Uploader build done.'); |
| 140 | + |
| 141 | + if (!fs.existsSync(path.join(tempRepoPath, fwuploderName))) { |
| 142 | + shell.echo( |
| 143 | + `Could not find the Firmware Uploader at ${path.join( |
| 144 | + tempRepoPath, |
| 145 | + fwuploderName |
| 146 | + )}.` |
| 147 | + ); |
| 148 | + shell.exit(1); |
| 149 | + } |
| 150 | + |
| 151 | + const builtFwUploaderPath = path.join(tempRepoPath, fwuploderName); |
| 152 | + shell.echo( |
| 153 | + `>>> Copying Firmware Uploader from ${builtFwUploaderPath} to ${destinationPath}...` |
| 154 | + ); |
| 155 | + if (shell.cp(builtFwUploaderPath, destinationPath).code !== 0) { |
| 156 | + shell.exit(1); |
| 157 | + } |
| 158 | + shell.echo(`<<< Copied the Firmware Uploader.`); |
| 159 | + |
| 160 | + shell.echo('<<< Verifying Firmware Uploader...'); |
| 161 | + if (!fs.existsSync(destinationPath)) { |
| 162 | + shell.exit(1); |
| 163 | + } |
| 164 | + shell.echo('>>> Verified Firmware Uploader.'); |
| 165 | + } |
| 166 | +})(); |
0 commit comments