Skip to content

Commit

Permalink
Improve devcontainer quick pick
Browse files Browse the repository at this point in the history
  • Loading branch information
3timeslazy committed Sep 26, 2024
1 parent c82a610 commit 7253846
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ dist
node_modules
.vscode-test/
*.vsix

*.tsbuildinfo
x
53 changes: 32 additions & 21 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,30 +86,13 @@ async function openContainer(recreate: boolean = false) {
return;
}

const picks = new Map<string, vscode.Uri>([]);
containerFiles.forEach(uri => {
const short = uri.path.replace(workspace.uri.path, "");
picks.set(short, uri);
});

let pick: vscode.Uri | undefined;
if (picks.size === 1) {
pick = [...picks.values()][0];
}
if (picks.size > 1) {
pick = await vscode.window.showQuickPick([...picks.keys()]).then(chosen => {
if (!chosen) {
return;
}
return picks.get(chosen);
});
}
if (!pick) {
const config = await pickConfig(workspace, containerFiles);
if (!config) {
return;
}

await upDevpod({
configPath: pick.path.replace(workspace.uri.path, ""),
configPath: config.path.replace(workspace.uri.path, ""),
workspaceFolder: workspace.uri.fsPath,
recreate: recreate,
});
Expand All @@ -123,7 +106,7 @@ async function openContainer(recreate: boolean = false) {
}
const devpodHost = `${devpod.id}.devpod`;

const customizations = parseCustomizations(pick.fsPath);
const customizations = parseCustomizations(config.fsPath);
const exts = customizations.extensions;
const installExtArgs = [];
const registryExts = [];
Expand Down Expand Up @@ -163,6 +146,34 @@ async function openContainer(recreate: boolean = false) {
redirectToDevpod(devpod.id);
}

async function pickConfig(workspace: vscode.WorkspaceFolder, configs: vscode.Uri[]) {
const picks: Record<string, vscode.Uri> = {};
configs.forEach(config => {
const short = config.path.replace(workspace.uri.path, "");
picks[short] = config;
})
const picksLenght = Object.keys(picks).length;
if (picksLenght === 1) {
return Object.values(picks)[0];
}
if (picksLenght > 1) {
const options = Object.keys(picks)
.sort()
.map(pick => ({
label: /devcontainer\/(.+)\//.exec(pick)?.[1] as string,
description: pick,
}));
const pick = await vscode.window.showQuickPick(options, { placeHolder: "Select a devcontainer.json file" });
if (!pick) {
return;
}

return picks[pick.description];
}

return;
}

function redirectToDevpod(id: string) {
const devpodHost = `${id}.devpod`;
const workdir = findWorkDir(devpodHost);
Expand Down

0 comments on commit 7253846

Please sign in to comment.