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

Show all supported boards and set default build directory #1425

Merged
merged 3 commits into from
Jan 19, 2022
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
6 changes: 6 additions & 0 deletions src/arduino/boardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -228,6 +229,11 @@ export class BoardManager {
// });
if (addedPlatform.name === plat.name) {
addedPlatform.versions.push(plat.version);
// 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;
}
}
} else {
plat.versions = [plat.version];
Expand Down
41 changes: 41 additions & 0 deletions src/common/sharedUtilities/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 5 additions & 18 deletions src/deviceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
},
});
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/views/app/components/BoardItemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
platform: any;
Expand Down
2 changes: 1 addition & 1 deletion src/views/app/components/BoardManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
Expand Down
3 changes: 2 additions & 1 deletion src/views/app/components/LibraryItemView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -133,7 +134,7 @@ export default class LibraryItemView extends React.Component<ILibraryProps, ILib
)
}
{
lib.versions && lib.versions.length && util.versionCompare(lib.versions[0], lib.version) > 0 && (
lib.versions && lib.versions.length && versionCompare(lib.versions[0], lib.version) > 0 && (
<Button className="operation-btn" onClick={() => this.installLibrary(lib.name, lib.versions[0])}>Update</Button>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/views/app/components/LibraryManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<any> {
Expand Down
3 changes: 2 additions & 1 deletion src/views/app/reducers/boardManagerReducer.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 { versionCompare } from "../../../common/sharedUtilities/utils";
import * as actions from "../actions";
import * as util from "../utils/util";

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/views/app/reducers/libraryManagerReducer.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 { versionCompare } from "../../../common/sharedUtilities/utils";
import * as actions from "../actions";
import * as util from "../utils/util";

Expand Down Expand Up @@ -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,
Expand Down
37 changes: 0 additions & 37 deletions src/views/app/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/views/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"outDir": "out",
"alwaysStrict": true,
"sourceMap": true,
"rootDir": "."
"rootDirs": [".", "../common/sharedUtilities"]
},
"exclude": [
"node_modules"
Expand Down