Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom task provider for cmake #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/tasks/execution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import * as vscode from "vscode";
import {IPty, IBasePtyForkOptions} from "node-pty";
import * as path from 'path';

//@ts-ignore
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
const moduleName = path.join(vscode.env.appRoot, "node_modules.asar", "node-pty");
const spawn: typeof import('node-pty').spawn = requireFunc(moduleName).spawn;

import { CMakeClient } from "../cmake/cmake";

class CMakeBuildTerminal implements vscode.Pseudoterminal {
private terminal: IPty | undefined;
private closeEmitter: vscode.EventEmitter<
number | void
> = new vscode.EventEmitter();
private writeEmitter: vscode.EventEmitter<string> = new vscode.EventEmitter();

constructor(private client: CMakeClient, private targets?: string[]) {}

onDidWrite: vscode.Event<string> = this.writeEmitter.event;
onDidClose: vscode.Event<number | void> = this.closeEmitter.event;

open(initialDimensions: vscode.TerminalDimensions | undefined): void {
let options: IBasePtyForkOptions = {};
if (initialDimensions) {
options.cols = initialDimensions.columns;
options.rows = initialDimensions.rows;
}

this.terminal = spawn(
this.client.cmakeExecutable,
this.client.getBuildArguments(this.targets),
options
);

this.terminal.onExit((data) => this.closeEmitter.fire(data.exitCode));
this.terminal.onData((data) => this.writeEmitter.fire(data));
}

close(): void {
if (this.terminal) {
this.terminal.kill();
}
}

setDimensions(dim: vscode.TerminalDimensions) {
if (this.terminal) {
this.terminal.resize(dim.columns, dim.rows);
}
}
}

export { CMakeBuildTerminal };
141 changes: 141 additions & 0 deletions src/tasks/taskProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import * as vscode from "vscode";
import { CMakeClient } from "../cmake/cmake";
import { CMakeBuildTerminal } from "./execution";

interface CMakeTaskDefinition extends vscode.TaskDefinition {
type: string;
action: "configure" | "build";
target?: string;
}

class CMakeTaskProvider implements vscode.TaskProvider {
private clientTasks: Map<CMakeClient, vscode.Task[]> = new Map();
private workspaceClients: Map<string, CMakeClient> = new Map();

static TaskSource: string = "cmake";
static TaskType: string = "cmake";

private static generateClientTasks(client: CMakeClient) {
let tasks: vscode.Task[] = [];
tasks.push(
new vscode.Task(
{
type: CMakeTaskProvider.TaskType,
action: "configure"
} as CMakeTaskDefinition,
client.workspaceFolder,
`configure`,
CMakeTaskProvider.TaskSource,
new vscode.CustomExecution(() => Promise.reject())
)
);
tasks.push(
new vscode.Task(
{
type: "cmake",
action: "build",
target: "clean"
} as CMakeTaskDefinition,
client.workspaceFolder,
`clean`,
"cmake",
new vscode.CustomExecution(() => Promise.resolve(
new CMakeBuildTerminal(
client!,
["clean"]
)
))
)
);
client.targets.forEach((target) => {
tasks.push(
new vscode.Task(
{
type: "cmake",
action: "build",
target: target.name
} as CMakeTaskDefinition,
client.workspaceFolder,
`build ${target.name}`,
"cmake",
new vscode.CustomExecution(() => Promise.resolve(
new CMakeBuildTerminal(
client!,
[target.name]
)
))
)
);
});
return tasks;
}

addClient(client: CMakeClient) {
this.clientTasks.set(client, CMakeTaskProvider.generateClientTasks(client));
this.workspaceClients.set(client.workspaceFolder.uri.toString(), client);
}

deleteClient(client: CMakeClient) {
this.clientTasks.delete(client);
this.workspaceClients.delete(client.workspaceFolder.uri.toString());
}

changeClientModel(client: CMakeClient) {
this.clientTasks.set(client, CMakeTaskProvider.generateClientTasks(client));
}

provideTasks(
_token?: vscode.CancellationToken | undefined
): vscode.ProviderResult<vscode.Task[]> {
let tasks: vscode.Task[] = [];
for (const clientTasks of this.clientTasks.values()) {
tasks.push(...clientTasks);
}
return tasks;
}
resolveTask(
task: vscode.Task,
_token?: vscode.CancellationToken | undefined
): vscode.ProviderResult<vscode.Task> {
function isWorkspaceFolder(
scope?: vscode.WorkspaceFolder | vscode.TaskScope
): scope is vscode.WorkspaceFolder {
return typeof scope === "object";
}
let client: CMakeClient | undefined;
if (task.scope) {
if (isWorkspaceFolder(task.scope)) {
client = this.workspaceClients.get(task.scope.uri.toString());
} else if (task.scope === vscode.TaskScope.Workspace) {
client = this.workspaceClients.values().next().value;
}
}
if (client) {
let def: CMakeTaskDefinition = <any>task.definition;
let targets = [];
if (def.target) {
targets.push(def.target);
}
return new vscode.Task(
{
type: "cmake",
action: def.action,
target: def.target
} as CMakeTaskDefinition,
client.workspaceFolder,
"Build",
"cmake",
Promise.resolve(
new CMakeBuildTerminal(
client!,
targets
)
)
);
} else {
throw new Error(`Can't resolve client for Task ${task.name}`);
}
}
}

export { CMakeTaskProvider };