Skip to content

Commit

Permalink
Fix #35, Board type default will crash project creation
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
  • Loading branch information
paulober committed Aug 12, 2024
1 parent ce3ae5a commit 08c8f07
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 33 deletions.
79 changes: 54 additions & 25 deletions src/webview/newProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import {
} from "../utils/examplesUtil.mjs";

export const NINJA_AUTO_INSTALL_DISABLED = false;
// process.platform === "linux" && process.arch === "arm64";
// process.platform === "linux" && process.arch === "arm64";

export const picotoolVersion = "2.0.0";
export const openOCDVersion = "0.12.0+dev";
Expand Down Expand Up @@ -165,10 +165,7 @@ enum Debugger {
swd = "SWD (Pi host)",
}

async function enumToBoard(
e: BoardType,
sdkPath: string
): Promise<string> {
async function enumToBoard(e: BoardType, sdkPath: string): Promise<string> {
const quickPickItems: string[] = [];
let board;
switch (e) {
Expand Down Expand Up @@ -1501,11 +1498,24 @@ export class NewProjectPanel {
<div>
<label for="sel-board-type" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Board type</label>
<select id="sel-board-type" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500">
<option id="sel-${BoardType.default}" value="${BoardType.default}">Default</option>
<option id="sel-${BoardType.pico}" value="${BoardType.pico}">Pico</option>
<option id="sel-${BoardType.picoW}" value="${BoardType.picoW}">Pico W</option>
<option id="sel-${BoardType.pico2}" value="${BoardType.pico2}" selected>Pico 2</option>
<option id="sel-${BoardType.other}" value="${BoardType.other}">Other</option>
${
// show the default option only if a use has the option to create base on an example
this._examples.length > 0
? `<option id="sel-${BoardType.default}" value="${BoardType.default}">Default</option>`
: ""
}
<option id="sel-${BoardType.pico}" value="${
BoardType.pico
}">Pico</option>
<option id="sel-${BoardType.picoW}" value="${
BoardType.picoW
}">Pico W</option>
<option id="sel-${BoardType.pico2}" value="${
BoardType.pico2
}" selected>Pico 2</option>
<option id="sel-${BoardType.other}" value="${
BoardType.other
}">Other</option>
</select>
</div>
</div>`
Expand Down Expand Up @@ -1885,14 +1895,18 @@ export class NewProjectPanel {
options: ExecOptions
): Promise<number | null> {
return new Promise<number | null>(resolve => {
const generatorProcess = exec(command, options, (error, stdout, stderr) => {
this._logger.debug(stdout);
this._logger.info(stderr);
if (error) {
this._logger.error(`Generator Process error: ${error.message}`);
resolve(null); // indicate error
const generatorProcess = exec(
command,
options,
(error, stdout, stderr) => {
this._logger.debug(stdout);
this._logger.info(stderr);
if (error) {
this._logger.error(`Generator Process error: ${error.message}`);
resolve(null); // indicate error
}
}
});
);

generatorProcess.on("exit", code => {
// Resolve with exit code or -1 if code is undefined
Expand Down Expand Up @@ -1940,18 +1954,37 @@ export class NewProjectPanel {
: ""
}${isWindows ? ";" : ":"}${customEnv[isWindows ? "Path" : "PATH"]}`;

// convert the selected board type to a vaild option
// for the project generator
let boardTypeFromEnum = "";
if ("boardType" in options) {
try {
boardTypeFromEnum = await enumToBoard(
options.boardType === BoardType.default
? BoardType.pico
: options.boardType,
options.toolchainAndSDK.sdkPath
);
} catch (error) {
await window.showErrorMessage(
"Unknown board type: " + options.boardType
);
return;

Check warning on line 1972 in src/webview/newProjectPanel.mts

View workflow job for this annotation

GitHub Actions / build

Expected newline before return statement
}
}

const basicNewProjectOptions: string[] =
"consoleOptions" in options
? [
await enumToBoard(options.boardType, options.toolchainAndSDK.sdkPath),
boardTypeFromEnum,
...options.consoleOptions.map(option => enumToParam(option)),
!options.consoleOptions.includes(ConsoleOption.consoleOverUART)
? "-nouart"
: "",
]
: "boardType" in options && options.boardType !== BoardType.default
? [await enumToBoard(options.boardType, options.toolchainAndSDK.sdkPath)]
: [];
? [boardTypeFromEnum]
: [];
if (!("boardType" in options) && this._isProjectImport) {
try {
const cmakel = await readFile(
Expand All @@ -1971,11 +2004,7 @@ export class NewProjectPanel {
} catch {
/* ignore */
}
} else if (
("boardType" in options) &&
isExampleBased &&
"name" in options
) {
} else if ("boardType" in options && isExampleBased && "name" in options) {
if (options.boardType === BoardType.default) {
if (options.name.includes("picow") || options.name.includes("pico_w")) {
basicNewProjectOptions.push(`-board pico_w`);
Expand Down
25 changes: 20 additions & 5 deletions web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,26 @@ var isPicoWireless = false;
var selectedIndex = getIndexByValue(toolchainSelector, result.toolchainVersion);

if (result.riscvToolchainVersion === "NONE") {
document.getElementById("sel-pico2").hidden = true;
if (document.getElementById('sel-board-type').selectedIndex > 1) {
document.getElementById('sel-board-type').selectedIndex = 0;
document.getElementById("sel-pico2").disabled = true;
const boardTypeSelector = document.getElementById('sel-board-type');

if (boardTypeSelector && boardTypeSelector.value.includes("pico2")) {
// first element could be hidden
//document.getElementById('sel-board-type').selectedIndex = 0;

// select first not hidden option
for (let i = 0; i < boardTypeSelector.options.length; i++) {
const option = boardTypeSelector.options[i];

// Check if the option is not hidden
if (option.style.display !== 'none' && option.hidden === false) {
boardTypeSelector.selectedIndex = i;
break;
}
}
}
} else {
document.getElementById("sel-pico2").hidden = false;
document.getElementById("sel-pico2").disabled = false;
}

var riscv = document.getElementById("sel-riscv").checked;
Expand Down Expand Up @@ -569,8 +583,9 @@ var isPicoWireless = false;
document.getElementById('btn-create').addEventListener('click', submitBtnClick);
const selectBoardTypeElement = document.getElementById('sel-board-type');
if (selectBoardTypeElement) {
selectBoardTypeElement.addEventListener('change', function () {
selectBoardTypeElement.addEventListener('change', function (event) {
try {
// TODO: fix not very future proof for different model naming
const isPicoWireless = selectBoardTypeElement.value.endsWith('w');

if (!isPicoWireless) {
Expand Down
48 changes: 48 additions & 0 deletions web/nav.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ window.toggleCreateFromExampleMode = function (forceOn, forceOff) {
const examplesList = document.getElementById('examples-list');
const projectNameGrid = document.getElementById('project-name-grid');
const projectNameDropdownButton = document.getElementById('project-name-dropdown-button');
const defaultBoardTypeOption = document.getElementById('sel-default');

if (isExampleMode && (forceOn === undefined || !forceOn) && (forceOff === undefined || forceOff)) {
if (createFromExampleBtn) {
Expand All @@ -121,6 +122,29 @@ window.toggleCreateFromExampleMode = function (forceOn, forceOff) {
//projectNameInput.required = true;
}

if (defaultBoardTypeOption) {
defaultBoardTypeOption.hidden = true;
defaultBoardTypeOption.disabled = true;

// if selected switch selection to first not hidden option
if (defaultBoardTypeOption.selected) {
const boardTypeSelector = document.getElementById('sel-board-type');

if (boardTypeSelector) {
// select first not hidden option
for (let i = 0; i < boardTypeSelector.options.length; i++) {
const option = boardTypeSelector.options[i];

// Check if the option is not hidden
if (option.style.display !== 'none' && option.hidden === false) {
boardTypeSelector.selectedIndex = i;
break;
}
}
}
}
}

if (projectNameInput) {
// old datalist approach: projectNameInput.setAttribute('list', undefined);
// remove keyup event listener from projectNameInput if it exists
Expand Down Expand Up @@ -154,6 +178,12 @@ window.toggleCreateFromExampleMode = function (forceOn, forceOff) {
//projectNameInput.setAttribute('list', "examples-list");
projectNameInput.setAttribute('placeholder', 'Select an example');

if (defaultBoardTypeOption) {
defaultBoardTypeOption.hidden = false;
defaultBoardTypeOption.disabled = false;
defaultBoardTypeOption.selected = true;
}

window.removeExampleItems = window.removeExampleItems || function () {
if (examplesList !== null) {
// clear ul
Expand Down Expand Up @@ -303,6 +333,24 @@ window.onload = function () {
createFromExampleBtn.classList.add('hidden');
}
toggleCreateFromExampleMode(true);
} else {
// hide if not force from example
const defaultBoardTypeOption = document.getElementById('sel-default');
if (defaultBoardTypeOption) {
defaultBoardTypeOption.hidden = true;
defaultBoardTypeOption.disabled = true;
}
}

// TODO: maybe can remove if sel-pico2 disable is moved into state restore
const sdkSelector = document.getElementById('sel-pico-sdk');
if (sdkSelector) {
if (parseInt(sdkSelector.value.split(".")[0]) < 2) {
const selPico2 = document.getElementById('sel-pico2');
if (selPico2) {
selPico2.disabled = true;
}
}
}

const burgerMenu = document.getElementById("burger-menu");
Expand Down
14 changes: 11 additions & 3 deletions web/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class State {
cppCodeGen;
cppRttiCodeGen;
cppExceptionsCodeGen;
selRiscv;
debuggerSelection;

// special ui only state
Expand All @@ -47,14 +48,18 @@ function restoreState(state) {
/*if (state.projectLocation) {
document.getElementById('inp-project-location').value = state.projectLocation;
}*/
// TODO: currently must be restorted before board type because otherwise
// the change of board type would trigger the change listener sending version bundle message
// this would undisable the pico2 board type and it does not get disabled again in the loading
// So maybe restore the board type disable state after restoring the state to avaoid these conflicts
if (state.selectedSDK) {
document.getElementById('sel-pico-sdk').value = state.selectedSDK;
}
if (state.boardType) {
document.getElementById('sel-board-type').value = state.boardType;
// trigger change event to update the ui based on the selected board
document.getElementById('sel-board-type').dispatchEvent(new Event('change'));
}
if (state.selectedSDK) {
document.getElementById('sel-pico-sdk').value = state.selectedSDK;
}
if (state.selectedToolchain) {
document.getElementById('sel-toolchain').value = state.selectedToolchain;
}
Expand Down Expand Up @@ -381,6 +386,9 @@ function setupStateSystem(vscode) {
case "cpp-exceptions-code-gen-cblist":
state.cppExceptionsCodeGen = checkbox.checked;
break;
case "sel-riscv":
state.selRiscv = checkbox.checked;
break;
}

vscode.setState(state);
Expand Down

0 comments on commit 08c8f07

Please sign in to comment.