Skip to content

Commit

Permalink
refactor add package to workspace command
Browse files Browse the repository at this point in the history
  • Loading branch information
g-arjones committed Mar 15, 2018
1 parent 4dacefe commit 31d08c2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 39 deletions.
73 changes: 34 additions & 39 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,46 +148,39 @@ export class Commands
this._context.outputChannel.show();
}

packagePickerChoices(): Promise<{ label, description, pkg }[]>
async packagePickerChoices(): Promise<{ label, description, pkg }[]>
{
let choices: { label, description, pkg }[] = [];
function addChoice(aPkg: autoproj.Package, ws: autoproj.WorkspaceInfo)
{
const choice = {
label: aPkg.name,
description: basename(ws.path),
pkg: aPkg
let fsPathsObj = {};
const wsInfos: Promise<autoproj.WorkspaceInfo>[] = [];

this._context.workspaces.forEachWorkspace((ws) => wsInfos.push(ws.info()));
if (this._vscode.workspaceFolders) {
for (const folder of this._vscode.workspaceFolders) {
fsPathsObj[folder.uri.fsPath] = true;
}
choices.push(choice);
}

return new Promise<{ label, description, pkg }[]>((resolve, reject) => {
let pending: any[] = [];
this._context.workspaces.forEachWorkspace((ws) => {
pending.push(ws.info().then((wsInfo) => {
for (const aPkg of wsInfo.packages) {
if (!this._vscode.workspaceFolders
|| !this._vscode.workspaceFolders.find((item) => item.uri.fsPath == aPkg[1].srcdir))
{
addChoice(aPkg[1], wsInfo);
}
}
},
(err) => {
reject(new Error(`Could not load installation manifest: ${err.message}`));
}));
});
Promise.all(pending).then(_ => {
choices.sort((a, b) => {
if (a.pkg.name < b.pkg.name)
return -1;
if (a.pkg.name > b.pkg.name)
return 1;
return 0;
})
resolve(choices);
})
});
for (const wsInfoP of wsInfos) {
try {
const wsInfo = await wsInfoP;
for (const aPkg of wsInfo.packages) {
if (!fsPathsObj.hasOwnProperty(aPkg[1].srcdir)) {
choices.push({
label: aPkg[1].name,
description: basename(wsInfo.path),
pkg: aPkg[1]
});
}
}
}
catch (err) {
throw new Error(
`Could not load installation manifest: ${err.message}`);
}
}
choices.sort((a, b) =>
a.pkg.name < b.pkg.name ? -1 : a.pkg.name > b.pkg.name ? 1 : 0);
return choices;
}
async addPackageToWorkspace()
{
Expand All @@ -208,9 +201,11 @@ export class Commands
if (selectedOption) {
const folder = { uri: vscode.Uri.file(selectedOption.pkg.srcdir) };
const wsFolders = this._vscode.workspaceFolders;
const added = this._vscode.updateWorkspaceFolders(wsFolders ? wsFolders.length : 0, null, folder);
if (!added) {
this._vscode.showErrorMessage(`Could not add folder: ${selectedOption.pkg.srcdir}`);
const start = wsFolders ? wsFolders.length : 0;

if (!this._vscode.updateWorkspaceFolders(start, null, folder)) {
this._vscode.showErrorMessage(
`Could not add folder: ${selectedOption.pkg.srcdir}`);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ describe("Commands", function () {
callback((cb) => cb(mockWs.object));
})
it("throws if installation manifest loading fails", async function () {
mockWrapper.setup(x => x.workspaceFolders).returns(() => undefined);
mockWs.setup(x => x.info()).returns(() => Promise.reject('test'));
await helpers.assertThrowsAsync(subject.packagePickerChoices(),
/Could not load installation manifest/)
Expand Down

0 comments on commit 31d08c2

Please sign in to comment.