From c9574324f53cf2aec5dc2dc449b33dbf332859d6 Mon Sep 17 00:00:00 2001 From: Ben McMorran Date: Tue, 18 Jan 2022 15:49:09 -0800 Subject: [PATCH 1/3] Show all supported board types in board manager --- src/arduino/boardManager.ts | 6 +++ src/common/sharedUtilities/utils.ts | 41 +++++++++++++++++++ src/views/app/components/BoardItemView.tsx | 2 +- src/views/app/components/BoardManager.tsx | 2 +- src/views/app/components/LibraryItemView.tsx | 3 +- src/views/app/components/LibraryManager.tsx | 2 +- src/views/app/reducers/boardManagerReducer.ts | 3 +- .../app/reducers/libraryManagerReducer.ts | 3 +- src/views/app/utils/util.ts | 37 ----------------- src/views/tsconfig.json | 2 +- 10 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 src/common/sharedUtilities/utils.ts diff --git a/src/arduino/boardManager.ts b/src/arduino/boardManager.ts index 3c8389af..480b4104 100644 --- a/src/arduino/boardManager.ts +++ b/src/arduino/boardManager.ts @@ -9,6 +9,7 @@ import * as util from "../common/util"; import * as constants from "../common/constants"; import { arduinoChannel } from "../common/outputChannel"; +import { versionCompare } from "../common/sharedUtilities/utils"; import { DeviceContext } from "../deviceContext"; import { ArduinoApp } from "./arduino"; import { IArduinoSettings } from "./arduinoSettings"; @@ -228,6 +229,11 @@ export class BoardManager { // }); if (addedPlatform.name === plat.name) { addedPlatform.versions.push(plat.version); + // Check if this is the latest version. Packages typically support more boards in later versions. + addedPlatform.versions.sort(versionCompare); + if (plat.version === addedPlatform.versions[addedPlatform.versions.length - 1]) { + addedPlatform.boards = plat.boards; + } } } else { plat.versions = [plat.version]; diff --git a/src/common/sharedUtilities/utils.ts b/src/common/sharedUtilities/utils.ts new file mode 100644 index 00000000..dd614163 --- /dev/null +++ b/src/common/sharedUtilities/utils.ts @@ -0,0 +1,41 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT license. + +// This file should work in both VS Code and browser contexts. + +export function versionCompare(a, b) { + return versionArrayCompare(a.split("."), b.split(".")); +} + +function versionArrayCompare(a: any[], b: any[]) { + for (let i = 0; i < Math.min(a.length, b.length); i++) { + const na = parseInt(a[i], 10); + const nb = parseInt(b[i], 10); + if (!isNaN(na) && !isNaN(nb)) { + if (na > nb) { + return 1; + } else if (nb > na) { + return -1; + } + const compare = (isNaN(a[i]) ? -1 : 1) - (isNaN(b[i]) ? -1 : 1); + if (compare !== 0) { + return compare; + } + } else if (!isNaN(na) && isNaN(nb)) { + return 1; + } else if (isNaN(na) && !isNaN(nb)) { + return -1; + } else { + const re = /-|\+/; + const subA = a[i].split(re); + const subB = b[i].split(re); + if (subA.length > 1 || subB.length > 1 || subA[0] !== a[i] || subB[0] !== b[i]) { + const compare = versionArrayCompare(subA, subB); + if (compare !== 0) { + return compare; + } + } + } + } + return a.length - b.length; +} diff --git a/src/views/app/components/BoardItemView.tsx b/src/views/app/components/BoardItemView.tsx index 3a6e59ee..38c087eb 100644 --- a/src/views/app/components/BoardItemView.tsx +++ b/src/views/app/components/BoardItemView.tsx @@ -3,8 +3,8 @@ import * as React from "react"; import { Button, DropdownButton, MenuItem } from "react-bootstrap"; +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as API from "../actions/api"; -import { versionCompare } from "../utils/util"; interface IBoardProps extends React.Props { platform: any; diff --git a/src/views/app/components/BoardManager.tsx b/src/views/app/components/BoardManager.tsx index d367642e..3b9dd927 100644 --- a/src/views/app/components/BoardManager.tsx +++ b/src/views/app/components/BoardManager.tsx @@ -5,9 +5,9 @@ import * as React from "react"; import { Button, DropdownButton, MenuItem } from "react-bootstrap"; import { connect } from "react-redux"; import SearchInput, { createFilter } from "react-search-input"; +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as actions from "../actions"; import * as API from "../actions/api"; -import { versionCompare } from "../utils/util"; import BoardItemView from "./BoardItemView"; interface IBoardManagerProps extends React.Props { diff --git a/src/views/app/components/LibraryItemView.tsx b/src/views/app/components/LibraryItemView.tsx index 47a46465..3ad7fcbd 100644 --- a/src/views/app/components/LibraryItemView.tsx +++ b/src/views/app/components/LibraryItemView.tsx @@ -3,6 +3,7 @@ import * as React from "react"; import { Button, DropdownButton, MenuItem } from "react-bootstrap"; +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as API from "../actions/api"; import * as util from "../utils/util"; @@ -133,7 +134,7 @@ export default class LibraryItemView extends React.Component 0 && ( + lib.versions && lib.versions.length && versionCompare(lib.versions[0], lib.version) > 0 && ( ) } diff --git a/src/views/app/components/LibraryManager.tsx b/src/views/app/components/LibraryManager.tsx index 864ab326..0bf6bc33 100644 --- a/src/views/app/components/LibraryManager.tsx +++ b/src/views/app/components/LibraryManager.tsx @@ -6,8 +6,8 @@ import { Button, Checkbox, DropdownButton, MenuItem } from "react-bootstrap"; import * as ReactList from "react-list"; import { connect } from "react-redux"; import SearchInput, { createFilter } from "react-search-input"; +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as actions from "../actions"; -import { versionCompare } from "../utils/util"; import LibraryItemView from "./LibraryItemView"; interface ILibraryManagerProps extends React.Props { diff --git a/src/views/app/reducers/boardManagerReducer.ts b/src/views/app/reducers/boardManagerReducer.ts index e4dafa48..4c95ea58 100644 --- a/src/views/app/reducers/boardManagerReducer.ts +++ b/src/views/app/reducers/boardManagerReducer.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as actions from "../actions"; import * as util from "../utils/util"; @@ -28,7 +29,7 @@ export default function boardManagerReducer(state = initalState, action) { const categories = util.parseGroups(action.platforms, "category"); // Sorting versions in descending order. action.platforms.forEach((element) => { - element.versions = element.versions.sort(util.versionCompare).reverse(); + element.versions = element.versions.sort(versionCompare).reverse(); }); return { ...state, diff --git a/src/views/app/reducers/libraryManagerReducer.ts b/src/views/app/reducers/libraryManagerReducer.ts index 00ef0a7a..83427bac 100644 --- a/src/views/app/reducers/libraryManagerReducer.ts +++ b/src/views/app/reducers/libraryManagerReducer.ts @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +import { versionCompare } from "../../../common/sharedUtilities/utils"; import * as actions from "../actions"; import * as util from "../utils/util"; @@ -30,7 +31,7 @@ export default function libraryManagerReducer(state = initalState, action) { // Sorting versions in descending order. // for loop is faster than forEach iterator. for (const element of action.libraries) { - element.versions = element.versions ? element.versions.sort(util.versionCompare).reverse() : element.versions; + element.versions = element.versions ? element.versions.sort(versionCompare).reverse() : element.versions; } return { ...state, diff --git a/src/views/app/utils/util.ts b/src/views/app/utils/util.ts index 5f822de0..584e8a3e 100644 --- a/src/views/app/utils/util.ts +++ b/src/views/app/utils/util.ts @@ -20,43 +20,6 @@ export function parseGroups(sourceArray: any[], key): any[] { return Object.keys(groups); } -export function versionCompare(a, b) { - return versionArrayCompare(a.split("."), b.split(".")); -} - -function versionArrayCompare(a: any[], b: any[]) { - for (let i = 0; i < Math.min(a.length, b.length); i++) { - const na = parseInt(a[i], 10); - const nb = parseInt(b[i], 10); - if (!isNaN(na) && !isNaN(nb)) { - if (na > nb) { - return 1; - } else if (nb > na) { - return -1; - } - const compare = (isNaN(a[i]) ? -1 : 1) - (isNaN(b[i]) ? -1 : 1); - if (compare !== 0) { - return compare; - } - } else if (!isNaN(na) && isNaN(nb)) { - return 1; - } else if (isNaN(na) && !isNaN(nb)) { - return -1; - } else { - const re = /-|\+/; - const subA = a[i].split(re); - const subB = b[i].split(re); - if (subA.length > 1 || subB.length > 1 || subA[0] !== a[i] || subB[0] !== b[i]) { - const compare = versionArrayCompare(subA, subB); - if (compare !== 0) { - return compare; - } - } - } - } - return a.length - b.length; -} - export function shallowEqual(objA, objB, keys?: any[]) { if (Object.is(objA, objB)) { return true; diff --git a/src/views/tsconfig.json b/src/views/tsconfig.json index 9def2d1d..b488f699 100644 --- a/src/views/tsconfig.json +++ b/src/views/tsconfig.json @@ -7,7 +7,7 @@ "outDir": "out", "alwaysStrict": true, "sourceMap": true, - "rootDir": "." + "rootDirs": [".", "../common/sharedUtilities"] }, "exclude": [ "node_modules" From 453fdb35fd735cbe41bd6aa52e3f0d57234a1b00 Mon Sep 17 00:00:00 2001 From: Ben McMorran Date: Tue, 18 Jan 2022 16:17:37 -0800 Subject: [PATCH 2/3] Set default build directory and better sketch name --- src/deviceContext.ts | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/deviceContext.ts b/src/deviceContext.ts index 0cfaa358..34bd9807 100644 --- a/src/deviceContext.ts +++ b/src/deviceContext.ts @@ -312,29 +312,14 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { .then(async (fileUris) => { if (fileUris.length === 0) { let newSketchFileName = await vscode.window.showInputBox({ - value: "my-sketch.ino", + value: "sketch.ino", prompt: "No sketch (*.ino) found in workspace, please provide a name", placeHolder: "Sketch file name (*.ino or *.cpp)", validateInput: (value) => { - /* TODO (EW, 2020-02-18): - * is 'c' actually allowed? Also found on within other files. - * And the regular expression doesn't need the internal groups. - * The outer group can be an anonymous group. - * And \w doesn't match dashes - so any sketch containing dashes - * will not be found. - * The correct expression therefore would be something like this: - * - * /^[\w\-]+\.(?:ino|cpp)$/ - * - * I'd recommend to define such regular expressions (including) - * line splitting etc.) at the global constants file. - * This is true for any hard coded paths (like the snippets below) - * as well. - */ - if (value && /^\w+\.((ino)|(cpp)|c)$/.test(value.trim())) { + if (value && /^[\w-]+\.(?:ino|cpp)$/.test(value.trim())) { return null; } else { - return "Invalid sketch file name. Should be *.ino/*.cpp/*.c"; + return "Invalid sketch file name. Should be *.ino/*.cpp"; } }, }); @@ -343,6 +328,8 @@ export class DeviceContext implements IDeviceContext, vscode.Disposable { const snippets = fs.readFileSync(path.join(this.extensionPath, "snippets", "sample.ino")); fs.writeFileSync(path.join(ArduinoWorkspace.rootPath, newSketchFileName), snippets); this.sketch = newSketchFileName; + // Set a build directory in new configurations to avoid warnings about slow builds. + this.output = "build"; // Open the new sketch file. const textDocument = await vscode.workspace.openTextDocument(path.join(ArduinoWorkspace.rootPath, newSketchFileName)); vscode.window.showTextDocument(textDocument, vscode.ViewColumn.One, true); From 68b8f23b60be842fae65f00496614f63b0cd64a7 Mon Sep 17 00:00:00 2001 From: Ben McMorran Date: Tue, 18 Jan 2022 16:35:18 -0800 Subject: [PATCH 3/3] Update comment --- src/arduino/boardManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/arduino/boardManager.ts b/src/arduino/boardManager.ts index 480b4104..196e4edb 100644 --- a/src/arduino/boardManager.ts +++ b/src/arduino/boardManager.ts @@ -229,7 +229,7 @@ export class BoardManager { // }); if (addedPlatform.name === plat.name) { addedPlatform.versions.push(plat.version); - // Check if this is the latest version. Packages typically support more boards in later versions. + // Check if this is the latest version. Platforms typically support more boards in later versions. addedPlatform.versions.sort(versionCompare); if (plat.version === addedPlatform.versions[addedPlatform.versions.length - 1]) { addedPlatform.boards = plat.boards;