Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Fix board and library updates for CLI #1611

Merged
merged 2 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
### Fixed

- 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)
- 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)

## Version 0.5.0

Expand Down
64 changes: 55 additions & 9 deletions src/arduino/arduino.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import * as child_process from "child_process";
import * as fs from "fs";
import * as glob from "glob";
import * as os from "os";
Expand Down Expand Up @@ -129,6 +130,17 @@ export class ArduinoApp {
}
}

public getAdditionalUrls(): string[] {
// For better compatibility, merge urls both in user settings and arduino IDE preferences.
const settingsUrls = VscodeSettings.getInstance().additionalUrls;
let preferencesUrls = [];
const preferences = this._settings.preferences;
if (preferences && preferences.has("boardsmanager.additional.urls")) {
preferencesUrls = util.toStringArray(preferences.get("boardsmanager.additional.urls"));
}
return util.union(settingsUrls, preferencesUrls);
}

/**
* Set the Arduino preferences value.
* @param {string} key - The preference key
Expand Down Expand Up @@ -259,10 +271,17 @@ export class ArduinoApp {
arduinoChannel.info(`${packageName}${arch && ":" + arch}${version && ":" + version}`);
try {
if (this.useArduinoCli()) {
await util.spawn(this._settings.commandPath,
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : null });
if (updatingIndex) {
await this.spawnCli(
["core", "update-index"],
undefined,
{ channel: showOutput ? arduinoChannel.channel : null });
} else {
await this.spawnCli(
["core", "install", `${packageName}${arch && ":" + arch}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : null });
}
} else {
await util.spawn(this._settings.commandPath,
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`],
Expand Down Expand Up @@ -311,10 +330,17 @@ export class ArduinoApp {
}
try {
if (this.useArduinoCli()) {
await util.spawn(this._settings.commandPath,
["lib", "install", `${libName}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : undefined });
if (updatingIndex) {
this.spawnCli(
["lib", "update-index"],
undefined,
{ channel: showOutput ? arduinoChannel.channel : undefined });
} else {
await this.spawnCli(
["lib", "install", `${libName}${version && "@" + version}`],
undefined,
{ channel: showOutput ? arduinoChannel.channel : undefined });
}
} else {
await util.spawn(this._settings.commandPath,
["--install-library", `${libName}${version && ":" + version}`],
Expand Down Expand Up @@ -816,7 +842,12 @@ export class ArduinoApp {
arduinoChannel.channel.append(line);
});

return await util.spawn(
const run = (...args: any[]) =>
this.useArduinoCli() ?
this.spawnCli(...(args.slice(1))) :
util.spawn.apply(undefined, args);

return await run(
this._settings.commandPath,
args,
{ cwd: ArduinoWorkspace.rootPath },
Expand All @@ -836,4 +867,19 @@ export class ArduinoApp {
return false;
});
}

private spawnCli(
args: string[] = [],
options: child_process.SpawnOptions = {},
output?: {channel?: vscode.OutputChannel,
stdout?: (s: string) => void,
stderr?: (s: string) => void},
): Thenable<object> {
const additionalUrls = this.getAdditionalUrls();
return util.spawn(
this._settings.commandPath,
args.concat(["--additional-urls", additionalUrls.join(",")]),
options,
output);
}
}
17 changes: 3 additions & 14 deletions src/arduino/boardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class BoardManager {
this._platforms = [];
this._installedPlatforms = [];

const additionalUrls = this.getAdditionalUrls();
const additionalUrls = this._arduinoApp.getAdditionalUrls();
if (update) { // Update index files.
await this.setPreferenceUrls(additionalUrls);
await this._arduinoApp.initialize(true);
Expand Down Expand Up @@ -113,11 +113,11 @@ export class BoardManager {
}

public async updatePackageIndex(indexUri: string): Promise<boolean> {
let allUrls = this.getAdditionalUrls();
let allUrls = this._arduinoApp.getAdditionalUrls();
if (!(allUrls.indexOf(indexUri) >= 0)) {
allUrls = allUrls.concat(indexUri);
VscodeSettings.getInstance().updateAdditionalUrls(allUrls);
await this._arduinoApp.setPref("boardsmanager.additional.urls", this.getAdditionalUrls().join(","));
await this._arduinoApp.setPref("boardsmanager.additional.urls", this._arduinoApp.getAdditionalUrls().join(","));
}
return true;
}
Expand Down Expand Up @@ -527,17 +527,6 @@ export class BoardManager {
return normalizedUrl.pathname.substr(normalizedUrl.pathname.lastIndexOf("/") + 1);
}

private getAdditionalUrls(): string[] {
// For better compatibility, merge urls both in user settings and arduino IDE preferences.
const settingsUrls = VscodeSettings.getInstance().additionalUrls;
let preferencesUrls = [];
const preferences = this._settings.preferences;
if (preferences && preferences.has("boardsmanager.additional.urls")) {
preferencesUrls = util.toStringArray(preferences.get("boardsmanager.additional.urls"));
}
return util.union(settingsUrls, preferencesUrls);
}

private async setPreferenceUrls(additionalUrls: string[]) {
const settingsUrls = additionalUrls.join(",");
if (this._settings.preferences.get("boardsmanager.additional.urls") !== settingsUrls) {
Expand Down