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

Commit 244936c

Browse files
Add autoUpdateIndexFiles option in settings (#178)
* Add autoUpdateIndexFiles option in settings * improve settings description text * Rephrase settings description
1 parent 2463f9d commit 244936c

11 files changed

+185
-106
lines changed

html/app/actions/api.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ function postHTTP(url, postData) {
1414
return window.fetch(request);
1515
}
1616

17-
export function getBoardPackages() {
18-
return window.fetch("/api/boardpackages").then((response) => response.json());
17+
export function getBoardPackages(update) {
18+
return window.fetch(`/api/boardpackages?update=${update}`).then((response) => response.json());
1919
}
2020

2121
export function installBoard(packageName, arch, version) {
@@ -39,8 +39,8 @@ export function openLink(link) {
3939
}).then((response) => response.json());
4040
}
4141

42-
export function getLibraries() {
43-
return window.fetch("/api/libraries").then((response) => response.json());
42+
export function getLibraries(update) {
43+
return window.fetch(`/api/libraries?update=${update}`).then((response) => response.json());
4444
}
4545

4646
export function installLibrary(libraryName, version) {

html/app/actions/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ export function uninstallLibraryFailure(libraryName, errorMessage) {
149149
};
150150
}
151151

152-
export function getBoardPackages(dispatch) {
152+
export function getBoardPackages(dispatch, update: boolean) {
153153
dispatch(boardPackagesRequest());
154-
API.getBoardPackages().then((response) => {
154+
API.getBoardPackages(update).then((response) => {
155155
const { platforms } = <any>response;
156156
dispatch(boardPackagesSuccess(JSONHelper.retrocycle(platforms)));
157157
}).catch((error) => {
@@ -183,9 +183,9 @@ export function uninstallBoard(dispatch, boardName, packagePath, callback?: Func
183183
});
184184
}
185185

186-
export function getLibraries(dispatch, callback?: Function) {
186+
export function getLibraries(dispatch, update: boolean, callback?: Function) {
187187
dispatch(librariesRequest());
188-
API.getLibraries().then((response) => {
188+
API.getLibraries(update).then((response) => {
189189
const { libraries } = <any>response;
190190
dispatch(librariesSuccess(libraries));
191191
if (callback) {

html/app/components/BoardManager.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ const mapStateToProps = (state) => {
4545

4646
const mapDispatchToProps = (dispatch) => {
4747
return {
48-
loadBoardPackages: () => actions.getBoardPackages(dispatch),
48+
loadBoardPackages: () => actions.getBoardPackages(dispatch, true),
4949
installBoard: (boardName, packageName, arch, version) => actions.installBoard(dispatch, boardName, packageName, arch, version, () => {
50-
actions.getBoardPackages(dispatch);
50+
actions.getBoardPackages(dispatch, false);
5151
}),
5252
uninstallBoard: (boardName, packagePath) => actions.uninstallBoard(dispatch, boardName, packagePath, () => {
53-
actions.getBoardPackages(dispatch);
53+
actions.getBoardPackages(dispatch, false);
5454
}),
5555
};
5656
};

html/app/components/LibraryManager.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ const mapStateToProps = (store) => {
4545

4646
const mapDispatchToProps = (dispatch) => {
4747
return {
48-
loadLibraries: () => actions.getLibraries(dispatch),
48+
loadLibraries: () => actions.getLibraries(dispatch, true),
4949
installLibrary: (libraryName, version, callback) => actions.installLibrary(dispatch, libraryName, version, (error) => {
5050
if (!error) {
5151
// Refresh library manager view
52-
actions.getLibraries(dispatch, callback);
52+
actions.getLibraries(dispatch, false, callback);
5353
} else {
5454
callback();
5555
}
5656
}),
5757
uninstallLibrary: (libraryName, libraryPath, callback) => actions.uninstallLibrary(dispatch, libraryName, libraryPath, (error) => {
5858
if (!error) {
5959
// Refresh library manager view
60-
actions.getLibraries(dispatch, callback);
60+
actions.getLibraries(dispatch, false, callback);
6161
} else {
6262
callback();
6363
}

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
"info",
127127
"verbose"
128128
]
129+
},
130+
"arduino.autoUpdateIndexFiles": {
131+
"type": "boolean",
132+
"default": false,
133+
"description": "Controls auto update of package_index.json and library_index.json index files. If enabled, each time when you open Boards Manager/Libraries Manager, download latest index files first. Otherwise, using index files cached on local disk for Boards Manager/Libraries Manager."
129134
}
130135
}
131136
},

src/arduino/arduino.ts

+49-12
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ export class ArduinoApp {
4141
if (force || !util.fileExistsSync(path.join(this._settings.packagePath, "package_index.json"))) {
4242
try {
4343
// Use the dummy package to initialize the Arduino IDE
44-
await this.installBoard("dummy", "dummy", "", false);
44+
await this.installBoard("dummy", "", "", true);
4545
} catch (ex) {
4646
}
4747
}
4848
}
4949

5050
/**
5151
* Initialize the arduino library.
52+
* @param {boolean} force - Whether force refresh library index file
5253
*/
53-
public async initializeLibrary() {
54-
if (!util.fileExistsSync(path.join(this._settings.packagePath, "library_index.json"))) {
54+
public async initializeLibrary(force: boolean = false) {
55+
if (force || !util.fileExistsSync(path.join(this._settings.packagePath, "library_index.json"))) {
5556
try {
5657
// Use the dummy library to initialize the Arduino IDE
57-
await this.installLibrary("dummy", "", false);
58+
await this.installLibrary("dummy", "", true);
5859
} catch (ex) {
5960
}
6061
}
@@ -177,17 +178,35 @@ export class ArduinoApp {
177178
/**
178179
* Install arduino board package based on package name and platform hardware architecture.
179180
*/
180-
public async installBoard(packageName: string, arch: string, version: string = "", showOutput: boolean = true) {
181+
public async installBoard(packageName: string, arch: string = "", version: string = "", showOutput: boolean = true) {
181182
arduinoChannel.show();
182-
arduinoChannel.start(`Install package - ${packageName}...`);
183+
const updatingIndex = packageName === "dummy" && !arch && !version;
184+
if (updatingIndex) {
185+
arduinoChannel.start(`Update package index files...`);
186+
} else {
187+
arduinoChannel.start(`Install package - ${packageName}...`);
188+
}
183189
try {
184190
await util.spawn(this._settings.commandPath,
185191
showOutput ? arduinoChannel.channel : null,
186-
["--install-boards", `${packageName}:${arch}${version && ":" + version}`]);
192+
["--install-boards", `${packageName}${arch && ":" + arch}${version && ":" + version}`]);
187193

188-
arduinoChannel.end(`Installed board package - ${packageName}${os.EOL}`);
194+
if (updatingIndex) {
195+
arduinoChannel.end("Updated package index files.");
196+
} else {
197+
arduinoChannel.end(`Installed board package - ${packageName}${os.EOL}`);
198+
}
189199
} catch (error) {
190-
arduinoChannel.error(`Exit with code=${error.code}${os.EOL}`);
200+
// If a platform with the same version is already installed, nothing is installed and program exits with exit code 1
201+
if (error.code === 1) {
202+
if (updatingIndex) {
203+
arduinoChannel.end("Updated package index files.");
204+
} else {
205+
arduinoChannel.end(`Installed board package - ${packageName}${os.EOL}`);
206+
}
207+
} else {
208+
arduinoChannel.error(`Exit with code=${error.code}${os.EOL}`);
209+
}
191210
}
192211
}
193212

@@ -199,15 +218,33 @@ export class ArduinoApp {
199218

200219
public async installLibrary(libName: string, version: string = "", showOutput: boolean = true) {
201220
arduinoChannel.show();
202-
arduinoChannel.start(`Install library - ${libName}`);
221+
const updatingIndex = (libName === "dummy" && !version);
222+
if (updatingIndex) {
223+
arduinoChannel.start("Update library index files...");
224+
} else {
225+
arduinoChannel.start(`Install library - ${libName}`);
226+
}
203227
try {
204228
await util.spawn(this._settings.commandPath,
205229
showOutput ? arduinoChannel.channel : null,
206230
["--install-library", `${libName}${version && ":" + version}`]);
207231

208-
arduinoChannel.end(`Installed library - ${libName}${os.EOL}`);
232+
if (updatingIndex) {
233+
arduinoChannel.end("Updated library index files.");
234+
} else {
235+
arduinoChannel.end(`Installed library - ${libName}${os.EOL}`);
236+
}
209237
} catch (error) {
210-
arduinoChannel.error(`Exit with code=${error.code}${os.EOL}`);
238+
// If a library with the same version is already installed, nothing is installed and program exits with exit code 1
239+
if (error.code === 1) {
240+
if (updatingIndex) {
241+
arduinoChannel.end("Updated library index files.");
242+
} else {
243+
arduinoChannel.end(`Installed library - ${libName}${os.EOL}`);
244+
}
245+
} else {
246+
arduinoChannel.error(`Exit with code=${error.code}${os.EOL}`);
247+
}
211248
}
212249
}
213250

src/arduino/arduinoContentProvider.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import { ArduinoApp } from "./arduino";
1111
import { BoardManager } from "./boardManager";
1212
import { LibraryManager } from "./libraryManager";
1313
import LocalWebServer from "./localWebServer";
14+
import { IArduinoSettings } from "./settings";
1415

1516
export class ArduinoContentProvider implements vscode.TextDocumentContentProvider {
1617
private _webserver: LocalWebServer;
1718
private _onDidChange = new vscode.EventEmitter<vscode.Uri>();
1819

1920
constructor(
21+
private _settings: IArduinoSettings,
2022
private _arduinoApp: ArduinoApp,
2123
private _boardManager: BoardManager,
2224
private _libraryManager: LibraryManager,
@@ -86,7 +88,8 @@ export class ArduinoContentProvider implements vscode.TextDocumentContentProvide
8688
}
8789

8890
public async getBoardPackages(req, res) {
89-
await this._boardManager.loadPackages();
91+
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
92+
await this._boardManager.loadPackages(update);
9093
return res.json({
9194
platforms: JSONHelper.decycle(this._boardManager.platforms, undefined),
9295
});
@@ -142,7 +145,8 @@ export class ArduinoContentProvider implements vscode.TextDocumentContentProvide
142145
}
143146

144147
public async getLibraries(req, res) {
145-
await this._libraryManager.loadLibraries();
148+
const update = (this._settings.autoUpdateIndexFiles && req.query.update === "true");
149+
await this._libraryManager.loadLibraries(update);
146150
return res.json({
147151
libraries: this._libraryManager.libraries,
148152
});

0 commit comments

Comments
 (0)