From 3cfb1450c00e0ac6b96b6ce7c6505c38ba22ed35 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Wed, 20 Nov 2019 18:40:28 +0100 Subject: [PATCH 1/2] Only pick arduino-cli from the PATH if it's more recent --- arduino-ide-extension/package.json | 5 +- arduino-ide-extension/src/node/arduino-cli.ts | 69 ++++++++++--------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/arduino-ide-extension/package.json b/arduino-ide-extension/package.json index 3294eeb4..efa4996e 100644 --- a/arduino-ide-extension/package.json +++ b/arduino-ide-extension/package.json @@ -33,7 +33,8 @@ "string-natural-compare": "^2.0.3", "tree-kill": "^1.2.1", "upath": "^1.1.2", - "which": "^1.3.1" + "which": "^1.3.1", + "semver": "^6.3.0" }, "scripts": { "prepare": "yarn download-cli && yarn download-ls && yarn run clean && yarn run build", @@ -77,4 +78,4 @@ "frontendElectron": "lib/electron-browser/electron-arduino-menu-module" } ] -} \ No newline at end of file +} diff --git a/arduino-ide-extension/src/node/arduino-cli.ts b/arduino-ide-extension/src/node/arduino-cli.ts index f170be38..2939d13a 100644 --- a/arduino-ide-extension/src/node/arduino-cli.ts +++ b/arduino-ide-extension/src/node/arduino-cli.ts @@ -1,7 +1,8 @@ import * as os from 'os'; import * as which from 'which'; +import * as semver from 'semver'; import { spawn } from 'child_process'; -import { join, delimiter } from 'path'; +import { join } from 'path'; import { injectable, inject } from 'inversify'; import { ILogger } from '@theia/core'; import { FileUri } from '@theia/core/lib/node/file-uri'; @@ -13,43 +14,49 @@ export class ArduinoCli { @inject(ILogger) protected logger: ILogger; + private execPath: string | undefined; + async getExecPath(): Promise { - const build = join(__dirname, '..', '..', 'build'); - return new Promise((resolve, reject) => { - which(`arduino-cli${os.platform() === 'win32' ? '.exe' : ''}`, { path: `${process.env.PATH}${delimiter}${build}` }, (err, path) => { - if (err) { - reject(err); - return; - } - resolve(path); + if (this.execPath) { + return this.execPath; + } + const version = /\d+\.\d+\.\d+/; + const cli = `arduino-cli${os.platform() === 'win32' ? '.exe' : ''}`; + const buildCli = join(__dirname, '..', '..', 'build', cli); + const buildVersion = await this.spawn(`"${buildCli}"`, ['version']); + const buildShortVersion = (buildVersion.match(version) || [])[0]; + this.execPath = buildCli; + try { + const pathCli = await new Promise((resolve, reject) => { + which(cli, (error, path) => { + if (error) { + reject(error); + return; + } + resolve(path); + }); }); - }); + if (!pathCli) { + return buildCli; + } + const pathVersion = await this.spawn(`"${pathCli}"`, ['version']); + const pathShortVersion = (pathVersion.match(version) || [])[0]; + if (semver.gt(pathShortVersion, buildShortVersion)) { + this.execPath = pathCli; + return pathCli; + } + } catch (error) { + this.logger.warn(`Could not check for Arduino CLI in $PATH, using embedded CLI instead:`, error); + // Any errors here should be safe to ignore, e.g.: + // - Could not search for CLI in $PATH + // - Could not get version of CLI in $PATH + } + return buildCli; } async getVersion(): Promise { const execPath = await this.getExecPath(); return this.spawn(`"${execPath}"`, ['version']); - return new Promise((resolve, reject) => { - const buffers: Buffer[] = []; - const cp = spawn(`"${execPath}"`, ['version'], { windowsHide: true, shell: true }); - cp.stdout.on('data', (b: Buffer) => buffers.push(b)); - cp.on('error', error => reject(error)); - cp.on('exit', (code, signal) => { - if (code === 0) { - const result = Buffer.concat(buffers).toString('utf8').trim() - resolve(result); - return; - } - if (signal) { - reject(new Error(`Process exited with signal: ${signal}`)); - return; - } - if (code) { - reject(new Error(`Process exited with exit code: ${code}`)); - return; - } - }); - }); } async getDefaultConfig(): Promise { From 125ee70fa391b403fc51d90477e42ec29c39a0f3 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Thu, 21 Nov 2019 10:16:06 +0000 Subject: [PATCH 2/2] Sort package.json dependencies alphabetically --- arduino-ide-extension/package.json | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/arduino-ide-extension/package.json b/arduino-ide-extension/package.json index efa4996e..e21daa9f 100644 --- a/arduino-ide-extension/package.json +++ b/arduino-ide-extension/package.json @@ -10,31 +10,31 @@ "@grpc/grpc-js": "^0.4.0", "@theia/application-package": "next", "@theia/core": "next", + "@theia/cpp": "next", "@theia/editor": "next", "@theia/filesystem": "next", "@theia/git": "next", "@theia/languages": "next", "@theia/markers": "next", "@theia/monaco": "next", - "@theia/outline-view": "next", - "@theia/workspace": "next", "@theia/navigator": "next", - "@theia/terminal": "next", + "@theia/outline-view": "next", "@theia/search-in-workspace": "next", - "@theia/cpp": "next", - "@types/ps-tree": "^1.1.0", - "@types/which": "^1.3.1", - "@types/react-select": "^3.0.0", + "@theia/terminal": "next", + "@theia/workspace": "next", "@types/google-protobuf": "^3.7.1", + "@types/ps-tree": "^1.1.0", + "@types/react-select": "^3.0.0", + "@types/which": "^1.3.1", "css-element-queries": "^1.2.0", - "react-select": "^3.0.4", "p-queue": "^5.0.0", "ps-tree": "^1.2.0", + "react-select": "^3.0.4", + "semver": "^6.3.0", "string-natural-compare": "^2.0.3", "tree-kill": "^1.2.1", "upath": "^1.1.2", - "which": "^1.3.1", - "semver": "^6.3.0" + "which": "^1.3.1" }, "scripts": { "prepare": "yarn download-cli && yarn download-ls && yarn run clean && yarn run build",