Skip to content

Commit

Permalink
move the auto-adding of the autoproj/ folder into the new folder even…
Browse files Browse the repository at this point in the history
…t code

Having it in the "Add package to workspace" code makes no sense. We
really want to auto-add when a new folder gets added. This ensures
a smooth-y transition to the Add Workspace command.
  • Loading branch information
doudou committed Feb 28, 2019
1 parent 9e8dc41 commit 3e06cb4
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 166 deletions.
86 changes: 45 additions & 41 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,48 @@ function assertWorkspaceNotEmpty(vscode: wrappers.VSCode)
throw new Error("Current workspace is empty");
}

function findInsertIndex(folders : vscode.WorkspaceFolder[], ws, name) : [boolean, number]
export function findAutoprojFolderIndex(folders : vscode.WorkspaceFolder[],
ws : { root: string }) : number | undefined
{
const configUri = vscode.Uri.file(pathjoin(ws.root, 'autoproj'));
let wsFound = false;
for (let i = 0; i < folders.length; ++i) {
let f = folders[i];
if (wsFound) {
if (!f.uri.path.startsWith(ws.root)) {
return [true, i];
}
if (name < f.name) {
return [true, i];
}
if (f.uri.fsPath == configUri.fsPath) {
return i;
}
else if (f.uri.fsPath == configUri.fsPath) {
wsFound = true;
}
}

export function addAutoprojFolder(vscodeW : wrappers.VSCode,
root: string, index: number | null = null) : boolean {

let wsname = basename(root);
let configUri = vscode.Uri.file(pathjoin(root, 'autoproj'));
let folder = { name: `autoproj (${wsname})`, uri: configUri };

if (index === null) {
index = vscodeW.workspaceFolders.length;
}
return vscodeW.updateWorkspaceFolders(index, null, folder);
}

function findInsertIndex(folders : vscode.WorkspaceFolder[], ws, name) : number
{
let configIndex = findAutoprojFolderIndex(folders, ws);
if (configIndex === undefined) {
return folders.length;
}

for (let i = configIndex + 1; i < folders.length; ++i) {
let f = folders[i];
if (!f.uri.path.startsWith(ws.root)) {
return i;
}
if (name < f.name) {
return i;
}
}
return [wsFound, folders.length];
return folders.length;
}

export class Commands
Expand Down Expand Up @@ -169,9 +192,9 @@ export class Commands
this._context.outputChannel.show();
}

async packagePickerChoices(): Promise<{ label, description, ws, config, pkg }[]>
async packagePickerChoices(): Promise<{ label, description, ws, pkg }[]>
{
let choices: { label, description, ws, config, pkg }[] = [];
let choices: { label, description, ws, pkg }[] = [];
let fsPathsObj = {};
const wsInfos: [autoproj.Workspace, Promise<autoproj.WorkspaceInfo>][] = [];

Expand All @@ -187,18 +210,16 @@ export class Commands
if (!fsPathsObj.hasOwnProperty(ws.root)) {
let name = `autoproj (${ws.name})`
choices.push({
label: name,
label: name, ws: ws,
description: `${ws.name} Build Configuration`,
config: true, ws: ws,
pkg: { name: name, srcdir: pathjoin(ws.root, 'autoproj') }
});
}
for (const aPkg of wsInfo.packages) {
if (!fsPathsObj.hasOwnProperty(aPkg[1].srcdir)) {
choices.push({
label: aPkg[1].name,
label: aPkg[1].name, ws: ws,
description: basename(wsInfo.path),
config: false, ws: ws,
pkg: aPkg[1]
});
}
Expand Down Expand Up @@ -232,16 +253,7 @@ export class Commands
paths.forEach((p) => {
let root = autoproj.findWorkspaceRoot(p.fsPath);
if (root) {
let wsname = basename(root);
let configUri = vscode.Uri.file(pathjoin(root, 'autoproj'));

let folder = { name: `autoproj (${wsname})`, uri: configUri };
const wsFolders = this._vscode.workspaceFolders;
let insertPosition = 0;
if (wsFolders) {
insertPosition = wsFolders.length;
}
this._vscode.updateWorkspaceFolders(insertPosition, null, folder);
addAutoprojFolder(this._vscode, root);
}
})
}
Expand All @@ -268,24 +280,16 @@ export class Commands

const name = selectedOption.pkg.name;
const wsFolders = this._vscode.workspaceFolders;
const ws = selectedOption.ws;
const configUri = vscode.Uri.file(pathjoin(ws.root, 'autoproj'));
const pkgUri = vscode.Uri.file(selectedOption.pkg.srcdir);

let insertPosition = 0;
let hasConfig = false;
if (wsFolders) {
[hasConfig, insertPosition] = findInsertIndex(wsFolders, ws, name);
}

let folders : any[] = [];
// Auto-add the autoproj folder if it's not there
if (!hasConfig && pkgUri != configUri) {
folders.push({ name: `autoproj (${ws.name})`, uri: configUri });
insertPosition = findInsertIndex(wsFolders, selectedOption.ws, name);
}

folders.push({ name: name, uri: pkgUri })
if (!this._vscode.updateWorkspaceFolders(insertPosition, null, ...folders)) {
let folder = { name: name, uri: vscode.Uri.file(selectedOption.pkg.srcdir) };
let success = this._vscode.updateWorkspaceFolders(
insertPosition, null, folder)
if (!success) {
this._vscode.showErrorMessage(
`Could not add folder: ${selectedOption.pkg.srcdir}`);
}
Expand Down
4 changes: 4 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class Context
return this._contextUpdatedEvent.event(callback);
}

public get vscode() : wrappers.VSCode {
return this._vscode;
}

public isWorkspaceEmpty() : boolean {
return this._vscode.workspaceFolders.length === 0;
}
Expand Down
107 changes: 10 additions & 97 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,101 +10,9 @@ import * as commands from './commands';
import * as packages from './packages';
import * as debug from './debug';
import * as config from './config';
import * as fs from 'fs';
import { join as joinpath } from 'path';
import * as snippets from './snippets';
import * as watcher from './watcher';
import * as path from 'path'

function watchManifest(ws: autoproj.Workspace, fileWatcher: watcher.FileWatcher)
{
let manifestPath = autoproj.installationManifestPath(ws.root);
try {
fileWatcher.startWatching(manifestPath, (filePath) => {
ws.reload().catch(err => {
let errMsg = `Could not load installation manifest: ${err.message}`
vscode.window.showErrorMessage(errMsg);
}
);
});
}
catch (err) {
vscode.window.showErrorMessage(err.message);
}
}

function unwatchManifest(ws: autoproj.Workspace, fileWatcher: watcher.FileWatcher)
{
let manifestPath = autoproj.installationManifestPath(ws.root);
try {
fileWatcher.stopWatching(autoproj.installationManifestPath(ws.root));
}
catch (err) {
vscode.window.showErrorMessage(err.message);
}
}

function handleNewWorkspaceFolder(
path: string,
rockContext : context.Context,
workspaces: autoproj.Workspaces, taskProvider: tasks.AutoprojProvider,
configManager: config.ConfigManager, fileWatcher: watcher.FileWatcher) : void
{
const { added, workspace } = workspaces.addFolder(path);
if (added && workspace) {
workspace.info().catch(err => {
let errMsg = `Could not load installation manifest: ${err.message}`
vscode.window.showErrorMessage(errMsg);
})
taskProvider.reloadTasks();
workspace.ensureSyskitContextAvailable().catch(() => {})
let watchTask = taskProvider.watchTask(workspace.root)
vscode.tasks.executeTask(watchTask).
then((taskExecution) => workspace.associateTask(taskExecution))
watchManifest(workspace, fileWatcher);
} else if (workspace) {
configManager.setupPackage(path).catch((reason) => {
vscode.window.showErrorMessage(reason.message);
});
}
}

function initializeWorkspacesFromVSCodeFolders(
rockContext: context.Context,
workspaces: autoproj.Workspaces, taskProvider: tasks.AutoprojProvider,
configManager: config.ConfigManager, fileWatcher: watcher.FileWatcher)
{
if (vscode.workspace.workspaceFolders != undefined) {
vscode.workspace.workspaceFolders.forEach((folder) => {
handleNewWorkspaceFolder(folder.uri.fsPath, rockContext,
workspaces, taskProvider, configManager, fileWatcher);
});
}
}

function setupEvents(rockContext: context.Context, extensionContext: vscode.ExtensionContext,
workspaces: autoproj.Workspaces, taskProvider: tasks.AutoprojProvider,
configManager: config.ConfigManager, fileWatcher: watcher.FileWatcher)
{
extensionContext.subscriptions.push(
vscode.workspace.onDidChangeWorkspaceFolders((event) => {
event.added.forEach((folder) => {
handleNewWorkspaceFolder(folder.uri.fsPath, rockContext,
workspaces, taskProvider, configManager, fileWatcher);
});
event.removed.forEach((folder) => {
let deletedWs = workspaces.deleteFolder(folder.uri.fsPath);
taskProvider.reloadTasks();
if (deletedWs) {
unwatchManifest(deletedWs, fileWatcher);
deletedWs.readWatchPID().
then((pid) => process.kill(pid, 'SIGINT')).
catch(() => {})
}
});
})
);
}
import { Manager as VSCodeWorkspaceManager } from './vscode_workspace_manager';

function applyConfiguration(configManager : config.ConfigManager,
workspaces : autoproj.Workspaces) : void {
Expand All @@ -125,6 +33,8 @@ export function activate(extensionContext: vscode.ExtensionContext) {
outputChannel);

let configManager = new config.ConfigManager(workspaces, vscodeWrapper);
let vscodeWorkspaceManager = new VSCodeWorkspaceManager(
rockContext, workspaces, autoprojTaskProvider, configManager, fileWatcher);
let rockCommands = new commands.Commands(rockContext, vscodeWrapper, configManager);

applyConfiguration(configManager, workspaces);
Expand All @@ -137,11 +47,14 @@ export function activate(extensionContext: vscode.ExtensionContext) {
extensionContext.subscriptions.push(
vscode.workspace.registerTaskProvider('autoproj', autoprojTaskProvider));

initializeWorkspacesFromVSCodeFolders(rockContext, workspaces, autoprojTaskProvider,
configManager, fileWatcher);
vscodeWorkspaceManager.initializeWorkspaces(vscodeWrapper.workspaceFolders);
autoprojTaskProvider.reloadTasks();
setupEvents(rockContext, extensionContext, workspaces,
autoprojTaskProvider, configManager, fileWatcher);
extensionContext.subscriptions.push(
vscode.workspace.onDidChangeWorkspaceFolders((event) => {
vscodeWorkspaceManager.handleWorkspaceChangeEvent(event)
})
)

rockCommands.register();

extensionContext.subscriptions.push(workspaces);
Expand Down
Loading

0 comments on commit 3e06cb4

Please sign in to comment.