Skip to content

Commit

Permalink
Prepare command to get App Variants
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed May 15, 2024
1 parent 6a2306c commit 9f7841f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"when": "view == mainView && viewItem == functionalTests && ledgerDevTools.showSelectTestUseCase",
"group": "inline"
},
{
"command": "selectVariant",
"when": "view == mainView && viewItem == selectVariant",
"group": "inline"
},
{
"command": "buildUseCase",
"when": "view == mainView && viewItem == buildUseCase",
Expand Down Expand Up @@ -98,6 +103,13 @@
"category": "Ledger",
"tooltip": "Select the device you want to build your app for."
},
{
"command": "selectVariant",
"title": "Select variant",
"category": "Ledger",
"tooltip": "Select the app variant you want to build.",
"icon": "$(variable)"
},
{
"command": "buildUseCase",
"title": "Select build use case",
Expand Down
69 changes: 69 additions & 0 deletions src/appSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export interface App {
// If the app manifest has build use cases (optional) section they are parsed here
buildUseCases?: BuildUseCase[];
selectedBuildUseCase?: BuildUseCase;
// Supported variants
variants?: string[];
selectedVariant?: string;
}

let appList: App[] = [];
Expand All @@ -68,6 +71,9 @@ export const onTestUseCaseSelected: vscode.Event<void> = testUseCaseSelected.eve
let useCaseSelectedEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter<string>();
export const onUseCaseSelectedEvent: vscode.Event<string> = useCaseSelectedEmitter.event;

let variantSelectedEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter<string>();
export const onVariantSelectedEvent: vscode.Event<string> = variantSelectedEmitter.event;

export function getSelectedBuidUseCase(): string {
if (selectedApp && selectedApp.selectedBuildUseCase) {
console.log("LEDGER: getSelectedBuidUseCase: " + selectedApp.selectedBuildUseCase.name);
Expand Down Expand Up @@ -107,6 +113,32 @@ export function showBuildUseCase() {
}
}

export function setVariant(name: string) {
if (selectedApp && selectedApp?.variants) {
selectedApp.selectedVariant = name;
}
}

export function showVariant() {
if (selectedApp) {
if (selectedApp.variants) {
const items: string[] = [];
for (let variant of selectedApp.variants) {
items.push(variant);
}
const result = vscode.window.showQuickPick(items, {
placeHolder: "Please select a variant",
onDidSelectItem: (item) => {
setVariant(item.toString());
variantSelectedEmitter.fire(item.toString());
},
});
return result;
}
return "";
}
}

function detectAppType(appFolder: vscode.Uri): [AppType?, string?] {
const searchPatterns = APP_DETECTION_FILES.map((file) => path.join(appFolder.fsPath, `**/${file}`).replace(/\\/g, "/"));
const makefileOrToml = fg.sync(searchPatterns, { onlyFiles: true, deep: 2 });
Expand Down Expand Up @@ -151,6 +183,7 @@ export function findAppInFolder(folderUri: vscode.Uri): App | undefined {
let compatibleDevices: LedgerDevice[] = ["Nano S", "Nano S Plus", "Nano X", "Stax", "Flex"];
let testsUseCases = undefined;
let buildUseCases = undefined;
let variants = undefined;
let buildDirPath = "./";

let found = true;
Expand Down Expand Up @@ -199,6 +232,7 @@ export function findAppInFolder(folderUri: vscode.Uri): App | undefined {
found = false;
break;
}
variants = getAppVariants(folderStr.replace(/^file?:\/\//, ''));
} catch (error) {
let err = new Error();
if (!(error instanceof Error)) {
Expand Down Expand Up @@ -229,6 +263,8 @@ export function findAppInFolder(folderUri: vscode.Uri): App | undefined {
builtTestDependencies: false,
buildUseCases: buildUseCases,
selectedBuildUseCase: buildUseCases ? buildUseCases[0] : undefined,
variants: variants,
selectedVariant: variants ? variants[0] : "",
};
}

Expand Down Expand Up @@ -402,6 +438,39 @@ function getAppName(appdir: string): string {
return cp.execSync(cleanCmd, optionsExecSync).toString().trim();
}

// define a function that converts a string to hex
function stringToHex(str: string) {
let hex = '';
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
const hexValue = charCode.toString(16);

// Pad with zeros to ensure two-digit representation
hex += hexValue.padStart(2, '0');
}
return hex;
};

// Get the app variants (for C apps)
function getAppVariants(appdir: string): string[] {
let optionsExecSync: cp.ExecSyncOptions = { stdio: "pipe", encoding: "utf-8" };
// If platform is windows, set shell to powershell for cp exec.
if (platform === "win32") {
let shell: string = "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
optionsExecSync.shell = shell;
}

const conf = vscode.workspace.getConfiguration("ledgerDevTools");
const image = conf.get<string>("dockerImage") || "";

// BOLOS_SDK value doesn't impact the APPNAME
let cleanCmd:string = `docker run --rm -v '${appdir}:/app' ${image} bash -c "BOLOS_SDK=/opt/stax-secure-sdk make listvariants | grep VARIANTS | cut -d' ' -f3-"`;
let result = cp.execSync(cleanCmd, optionsExecSync).toString().trim();

return result.split(" ");
}


// Type guard function to check if a string is a valid app language
function isValidLanguage(value: string): AppLanguage {
if (!validLanguages.includes(value as AppLanguage)) {
Expand Down
18 changes: 18 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
onUseCaseSelectedEvent,
getAndBuildAppTestsDependencies,
getSelectedBuidUseCase,
onVariantSelectedEvent,
showVariant,
} from "./appSelector";

let outputChannel: vscode.OutputChannel;
Expand Down Expand Up @@ -71,6 +73,16 @@ export function activate(context: vscode.ExtensionContext) {
})
);

// Event listener for variant selection.
// This event is fired when the user selects a build variant
context.subscriptions.push(
onVariantSelectedEvent((data) => {
// taskProvider.generateTasks();
// statusBarManager.updateBuildUseCaseItem(data);
treeProvider.updateDynamicLabels();
})
);

// Event listener for useCase selection.
// This event is fired when the user selects a build useCase
context.subscriptions.push(
Expand Down Expand Up @@ -106,6 +118,12 @@ export function activate(context: vscode.ExtensionContext) {
})
);

context.subscriptions.push(
vscode.commands.registerCommand("selectVariant", () => {
showVariant();
})
);

context.subscriptions.push(
vscode.commands.registerCommand("buildUseCase", () => {
showBuildUseCase();
Expand Down
23 changes: 23 additions & 0 deletions src/treeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export class TreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
let selectAppItem = this.data.find((item) => item.label && item.label.toString().startsWith("Select app"));
let functionalTestsItem = this.data.find((item) => item.label && item.label.toString().startsWith("Functional"));
let buidUseCaseItem = this.data.find((item) => item.label && item.label.toString().startsWith("Build"));
let selectVariantItem = this.data.find((item) => item.label && item.label.toString().startsWith("Select variant"));

if (selectAppItem) {
selectAppItem.label = `Select app [${currentApp.folderName}]`;
Expand All @@ -204,6 +205,13 @@ export class TreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
buidUseCaseItem.label = `Build`;
}
}
if (selectVariantItem) {
if (currentApp.selectedVariant) {
selectVariantItem.label = `Select variant [${currentApp.selectedVariant}]`;
} else {
selectVariantItem.label = `Select variant`;
}
}
} else {
// Remove all tree items. The welcome view will be displayed instead.
this.data = [];
Expand All @@ -215,6 +223,7 @@ export class TreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
// Check select app and select target items don't already exist
const selectAppItem = this.data.find((item) => item.label && item.label.toString().startsWith("Select app"));
const selectTargetItem = this.data.find((item) => item.label && item.label.toString().startsWith("Select target"));
const selectVariantItem = this.data.find((item) => item.label && item.label.toString().startsWith("Select variant"));

if (!selectAppItem) {
let selectApp = new TreeItem("Select app");
Expand Down Expand Up @@ -242,6 +251,20 @@ export class TreeDataProvider implements vscode.TreeDataProvider<TreeItem> {
console.log("Ledger: Adding selectTarget to tree");
this.data.push(selectTarget);
}

if (!selectVariantItem) {
let selectVariant = new TreeItem("Select variant");
selectVariant.contextValue = "selectVariant";
selectVariant.setDefault();
selectVariant.tooltip = "Select the variant to build";
selectVariant.command = {
command: "selectVariant",
title: "Select target",
arguments: [],
};
console.log("Ledger: Adding selectVariant to tree");
this.data.push(selectVariant);
}
}
}

Expand Down

0 comments on commit 9f7841f

Please sign in to comment.