Skip to content

Commit 5f2cf62

Browse files
authored
Merge pull request microsoft#1611 from microsoft/dev/bemcmorr/improve-arduino-cli
Fix board and library updates for CLI
2 parents 979e301 + c84cc34 commit 5f2cf62

File tree

3 files changed

+59
-23
lines changed

3 files changed

+59
-23
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
1313
### Fixed
1414

1515
- When using the bundled Arduino CLI, the extension will no longer attempt to make the CLI executable if it is already executable. Additionally, any errors that occur while attempting to make the CLI executable are shown in a notification. [#1601](https://github.com/microsoft/vscode-arduino/pull/1601)
16+
- Refreshing the index in the board manager or library manager now works correctly with the Arduino CLI, and additional URLs from the `arduino.additionalUrls` setting are passed to the Arduino CLI. [#1611](https://github.com/microsoft/vscode-arduino/pull/1611)
1617

1718
## Version 0.5.0
1819

src/arduino/arduino.ts

+55-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

4+
import * as child_process from "child_process";
45
import * as fs from "fs";
56
import * as glob from "glob";
67
import * as os from "os";
@@ -129,6 +130,17 @@ export class ArduinoApp {
129130
}
130131
}
131132

133+
public getAdditionalUrls(): string[] {
134+
// For better compatibility, merge urls both in user settings and arduino IDE preferences.
135+
const settingsUrls = VscodeSettings.getInstance().additionalUrls;
136+
let preferencesUrls = [];
137+
const preferences = this._settings.preferences;
138+
if (preferences && preferences.has("boardsmanager.additional.urls")) {
139+
preferencesUrls = util.toStringArray(preferences.get("boardsmanager.additional.urls"));
140+
}
141+
return util.union(settingsUrls, preferencesUrls);
142+
}
143+
132144
/**
133145
* Set the Arduino preferences value.
134146
* @param {string} key - The preference key
@@ -259,10 +271,17 @@ export class ArduinoApp {
259271
arduinoChannel.info(`${packageName}${arch && ":" + arch}${version && ":" + version}`);
260272
try {
261273
if (this.useArduinoCli()) {
262-
await util.spawn(this._settings.commandPath,
263-
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
264-
undefined,
265-
{ channel: showOutput ? arduinoChannel.channel : null });
274+
if (updatingIndex) {
275+
await this.spawnCli(
276+
["core", "update-index"],
277+
undefined,
278+
{ channel: showOutput ? arduinoChannel.channel : null });
279+
} else {
280+
await this.spawnCli(
281+
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
282+
undefined,
283+
{ channel: showOutput ? arduinoChannel.channel : null });
284+
}
266285
} else {
267286
await util.spawn(this._settings.commandPath,
268287
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`],
@@ -311,10 +330,17 @@ export class ArduinoApp {
311330
}
312331
try {
313332
if (this.useArduinoCli()) {
314-
await util.spawn(this._settings.commandPath,
315-
["lib", "install", `${libName}${version && "@" + version}`],
316-
undefined,
317-
{ channel: showOutput ? arduinoChannel.channel : undefined });
333+
if (updatingIndex) {
334+
this.spawnCli(
335+
["lib", "update-index"],
336+
undefined,
337+
{ channel: showOutput ? arduinoChannel.channel : undefined });
338+
} else {
339+
await this.spawnCli(
340+
["lib", "install", `${libName}${version && "@" + version}`],
341+
undefined,
342+
{ channel: showOutput ? arduinoChannel.channel : undefined });
343+
}
318344
} else {
319345
await util.spawn(this._settings.commandPath,
320346
["--install-library", `${libName}${version && ":" + version}`],
@@ -816,7 +842,12 @@ export class ArduinoApp {
816842
arduinoChannel.channel.append(line);
817843
});
818844

819-
return await util.spawn(
845+
const run = (...args: any[]) =>
846+
this.useArduinoCli() ?
847+
this.spawnCli(...(args.slice(1))) :
848+
util.spawn.apply(undefined, args);
849+
850+
return await run(
820851
this._settings.commandPath,
821852
args,
822853
{ cwd: ArduinoWorkspace.rootPath },
@@ -836,4 +867,19 @@ export class ArduinoApp {
836867
return false;
837868
});
838869
}
870+
871+
private spawnCli(
872+
args: string[] = [],
873+
options: child_process.SpawnOptions = {},
874+
output?: {channel?: vscode.OutputChannel,
875+
stdout?: (s: string) => void,
876+
stderr?: (s: string) => void},
877+
): Thenable<object> {
878+
const additionalUrls = this.getAdditionalUrls();
879+
return util.spawn(
880+
this._settings.commandPath,
881+
args.concat(["--additional-urls", additionalUrls.join(",")]),
882+
options,
883+
output);
884+
}
839885
}

src/arduino/boardManager.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class BoardManager {
4747
this._platforms = [];
4848
this._installedPlatforms = [];
4949

50-
const additionalUrls = this.getAdditionalUrls();
50+
const additionalUrls = this._arduinoApp.getAdditionalUrls();
5151
if (update) { // Update index files.
5252
await this.setPreferenceUrls(additionalUrls);
5353
await this._arduinoApp.initialize(true);
@@ -113,11 +113,11 @@ export class BoardManager {
113113
}
114114

115115
public async updatePackageIndex(indexUri: string): Promise<boolean> {
116-
let allUrls = this.getAdditionalUrls();
116+
let allUrls = this._arduinoApp.getAdditionalUrls();
117117
if (!(allUrls.indexOf(indexUri) >= 0)) {
118118
allUrls = allUrls.concat(indexUri);
119119
VscodeSettings.getInstance().updateAdditionalUrls(allUrls);
120-
await this._arduinoApp.setPref("boardsmanager.additional.urls", this.getAdditionalUrls().join(","));
120+
await this._arduinoApp.setPref("boardsmanager.additional.urls", this._arduinoApp.getAdditionalUrls().join(","));
121121
}
122122
return true;
123123
}
@@ -527,17 +527,6 @@ export class BoardManager {
527527
return normalizedUrl.pathname.substr(normalizedUrl.pathname.lastIndexOf("/") + 1);
528528
}
529529

530-
private getAdditionalUrls(): string[] {
531-
// For better compatibility, merge urls both in user settings and arduino IDE preferences.
532-
const settingsUrls = VscodeSettings.getInstance().additionalUrls;
533-
let preferencesUrls = [];
534-
const preferences = this._settings.preferences;
535-
if (preferences && preferences.has("boardsmanager.additional.urls")) {
536-
preferencesUrls = util.toStringArray(preferences.get("boardsmanager.additional.urls"));
537-
}
538-
return util.union(settingsUrls, preferencesUrls);
539-
}
540-
541530
private async setPreferenceUrls(additionalUrls: string[]) {
542531
const settingsUrls = additionalUrls.join(",");
543532
if (this._settings.preferences.get("boardsmanager.additional.urls") !== settingsUrls) {

0 commit comments

Comments
 (0)