Skip to content

Commit

Permalink
auto-start autoproj watch in new workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
doudou committed Mar 8, 2018
1 parent 8e7bd5e commit 6b5a791
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/autoproj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,27 @@ export class Workspace
return p;
}

public readWatchPID(): Promise<number>
{
return new Promise((resolve, reject) => {
fs.readFile(path.join(this.root, '.autoproj', 'watch'),
(err, data) => {
if (err) {
reject(err);
}
else {
let pid = Number(data.toString());
if (isNaN(pid) || pid == 0) {
reject(new Error("invalid watch PID file"));
}
else {
resolve(pid);
}
}
})
})
}

// Private API, made public only for testing reasons
private redirectProcessToChannel(name, shortname, subprocess : Process)
{
Expand Down
9 changes: 7 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function handleNewWorkspaceFolder(
let { added, workspace } = workspaces.addFolder(path);
if (added && workspace) {
workspace.ensureSyskitContextAvailable().catch(() => {})

vscode.commands.executeCommand('workbench.action.tasks.runTask', `autoproj: ${workspace.name}: Watch`)
}
configManager.setupPackage(path).catch((reason) => {
vscode.window.showErrorMessage(reason.message);
Expand Down Expand Up @@ -52,7 +52,12 @@ function setupEvents(rockContext: context.Context, extensionContext: vscode.Exte
handleNewWorkspaceFolder(folder.uri.fsPath, rockContext, workspaces, configManager);
});
event.removed.forEach((folder) => {
workspaces.deleteFolder(folder.uri.fsPath);
let deletedWs = workspaces.deleteFolder(folder.uri.fsPath);
if (deletedWs) {
deletedWs.readWatchPID().
then((pid) => process.kill(pid)).
catch(() => {})
}
});
taskProvider.reloadTasks();
})
Expand Down
15 changes: 15 additions & 0 deletions src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class AutoprojProvider implements vscode.TaskProvider
{
workspaces : autoproj.Workspaces;

private _watchTasks: Map<string, vscode.Task>;
private _buildTasks: Map<string, vscode.Task>;
private _nodepsBuildTasks: Map<string, vscode.Task>;
private _forceBuildTasks: Map<string, vscode.Task>;
Expand Down Expand Up @@ -39,6 +40,12 @@ export class AutoprojProvider implements vscode.TaskProvider
['osdeps', '--color', ...args]);
}

private createWatchTask(name, ws, defs = {}, args : string[] = []) {
return this.createTask(name, ws,
{ mode: 'watch', ...defs },
['watch', '--show-events', ...args]);
}

private createBuildTask(name, ws, defs = {}, args : string[] = []) {
let task = this.createTask(name, ws,
{ mode: 'build', ...defs },
Expand Down Expand Up @@ -125,6 +132,11 @@ export class AutoprojProvider implements vscode.TaskProvider
return this.getCache(this._buildTasks, path);
}

public watchTask(path: string): vscode.Task
{
return this.getCache(this._watchTasks, path);
}

public forceBuildTask(path: string): vscode.Task
{
return this.getCache(this._forceBuildTasks, path);
Expand Down Expand Up @@ -166,6 +178,7 @@ export class AutoprojProvider implements vscode.TaskProvider
{
this._allTasks = [];

this._watchTasks = new Map<string, vscode.Task>();
this._buildTasks = new Map<string, vscode.Task>();
this._nodepsBuildTasks = new Map<string, vscode.Task>();
this._forceBuildTasks = new Map<string, vscode.Task>();
Expand All @@ -175,6 +188,8 @@ export class AutoprojProvider implements vscode.TaskProvider
this._updateConfigTasks = new Map<string, vscode.Task>();

this.workspaces.forEachWorkspace((ws) => {
this.addTask(ws.root, this.createWatchTask(`${ws.name}: Watch`, ws),
this._watchTasks);
this.addTask(ws.root, this.createBuildTask(`${ws.name}: Build`, ws),
this._buildTasks);
this.addTask(ws.root, this.createCheckoutTask(`${ws.name}: Checkout`, ws),
Expand Down
23 changes: 23 additions & 0 deletions test/autoproj.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,29 @@ describe("Autoproj helpers tests", function () {
new RegExp(`^Syskit background process for ${ws.root} has not been started`))
})
})

describe("readWatchPID", function() {
let workspace : autoproj.Workspace;

beforeEach(async function() {
let setup = new helpers.TestSetup();
let { mock, ws } = setup.createAndRegisterWorkspace('ws');
workspace = ws;
})
it ("errors if the watch file does not exist", async function() {
await helpers.assertThrowsAsync(workspace.readWatchPID(),
new RegExp("^ENOENT: no such file or directory"))
})
it ("errors if the watch file is empty", async function() {
helpers.mkfile('', 'ws', '.autoproj', 'watch');
await helpers.assertThrowsAsync(workspace.readWatchPID(),
new RegExp(`^invalid watch PID file$`))
})
it ("returns the PID if the file contains a number", async function() {
helpers.mkfile('1234', 'ws', '.autoproj', 'watch');
assert.strictEqual(1234, await workspace.readWatchPID())
})
})
})
describe("Workspaces", function () {
let workspaces: autoproj.Workspaces;
Expand Down
51 changes: 36 additions & 15 deletions test/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ describe("Task provider", function () {
let wsRoot = autoproj.findWorkspaceRoot(basePath) as string;
return autoproj.autoprojExePath(wsRoot);
}
function assertWatchTask(task: vscode.Task, path: string)
{
let process = autoprojExePath(path);
let args = ['watch', '--show-events'];
assertTask(task, process, args);
}
function assertBuildTask(task: vscode.Task, path: string, isPackage = true)
{
let process = autoprojExePath(path);
Expand Down Expand Up @@ -76,7 +82,7 @@ describe("Task provider", function () {
let args = ['update', '--progress=f', '-k', '--color', '--config'];
assertTask(task, process, args);
}
function assertAllTasks(path: string)
function assertAllPackageTasks(path: string)
{
let buildTask = subject.buildTask(path);
assert.notEqual(buildTask, undefined);
Expand All @@ -97,14 +103,18 @@ describe("Task provider", function () {
let checkoutTask = subject.checkoutTask(path);
assert.notEqual(checkoutTask, undefined);
assertCheckoutTask(checkoutTask, path);
}

function assertAllWorkspaceTasks(wsRoot: string) {
let watchTask = subject.watchTask(wsRoot);
assert.notEqual(watchTask, undefined);
assertWatchTask(watchTask, wsRoot);

let ws = workspaces.folderToWorkspace.get(path) as autoproj.Workspace;
let wsRoot = ws.root;
buildTask = subject.buildTask(wsRoot);
let buildTask = subject.buildTask(wsRoot);
assert.notEqual(buildTask, undefined);
assertBuildTask(buildTask, wsRoot, false);

checkoutTask = subject.checkoutTask(wsRoot);
let checkoutTask = subject.checkoutTask(wsRoot);
assert.notEqual(checkoutTask, undefined);
assertCheckoutTask(checkoutTask, wsRoot, false);

Expand All @@ -116,20 +126,22 @@ describe("Task provider", function () {
assert.notEqual(updateConfigTask, undefined);
assertUpdateConfigTask(updateConfigTask, wsRoot);

updateTask = subject.updateTask(wsRoot);
let updateTask = subject.updateTask(wsRoot);
assert.notEqual(updateTask, undefined);
assertUpdateTask(updateTask, wsRoot, false);
}

describe("in a non empty workspace", function () {
let wsOneRoot: string;
let wsTwoRoot: string;
let a: string;
let b: string;
let c: string;
let d: string;
let e: string;
beforeEach(function () {
helpers.mkdir('one');
helpers.mkdir('two');
wsOneRoot = helpers.mkdir('one');
wsTwoRoot = helpers.mkdir('two');
helpers.mkdir('one', '.autoproj');
helpers.mkdir('two', '.autoproj');
d = helpers.mkdir('one', 'autoproj');
Expand All @@ -150,13 +162,21 @@ describe("Task provider", function () {
workspaces.addFolder(e);
subject = new tasks.AutoprojProvider(workspaces);
})

it("is initalized with all tasks", function () {
let tasks = subject.provideTasks(null);
assert.equal(tasks.length, 25);

assertAllTasks(a);
assertAllTasks(b);
assertAllTasks(c);
assert.equal(tasks.length, 27);
})
it("is initalized with all workspace tasks", function () {
let tasks = subject.provideTasks(null);
assertAllWorkspaceTasks(wsOneRoot);
assertAllWorkspaceTasks(wsTwoRoot);
});
it("is initalized with all package tasks", function () {
let tasks = subject.provideTasks(null);
assertAllPackageTasks(a);
assertAllPackageTasks(b);
assertAllPackageTasks(c);
});
});

Expand All @@ -178,8 +198,9 @@ describe("Task provider", function () {
subject.reloadTasks();

let tasks = subject.provideTasks(null);
assert.equal(tasks.length, 10);
assertAllTasks(a);
assert.equal(tasks.length, 11);
assertAllWorkspaceTasks(helpers.fullPath());
assertAllPackageTasks(a);
})
});
});

0 comments on commit 6b5a791

Please sign in to comment.