Skip to content

Commit

Permalink
VSC: add runner state object and integrate VS Code user configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed May 27, 2024
1 parent 9b01e34 commit 8bd43c9
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 39 deletions.
4 changes: 4 additions & 0 deletions .tokeignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
build/
target/

examples/

*.md
*.svg

*.json
19 changes: 18 additions & 1 deletion integration/vscode-ext/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,24 @@
"command": "devinit.render-template",
"title": "Devinit: Render template into current file"
}
]
],
"configuration": {
"title": "Devinit",
"properties": {
"devinit.environment.executablePath": {
"markdownDescription": "Specifies the path to the `devinit` executable; leave empty to read from PATH.",
"type": "string",
"default": "",
"order": 0
},
"devinit.environment.configurationFile": {
"markdownDescription": "Specifies the path to the configuration file (i.e. the `devinitrc.yml` file by default); leave empty to use system default locations. These locations include:\n- `$HOME/.devinit/devinitrc.yml`\n- `$HOME/.config/devinit/devinitrc.yml`.",
"type": "string",
"default": "",
"order": 0
}
}
}
},
"engines": {
"vscode": "^1.76.0"
Expand Down
77 changes: 43 additions & 34 deletions integration/vscode-ext/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,57 @@
*/

import * as vscode from "vscode";
import { Runner, RunnerOutputType, RunnerSubcommandVariant } from "./runner";
import { RunnerState } from "./runnerState";

/**
* Static extension activation set-up function
* @param context VS Code extension API context
*/
export async function activate(context: vscode.ExtensionContext) {
// initialise runner state; update it if user configuration changes
const runnerState = new RunnerState();
vscode.workspace.onDidChangeConfiguration(_ => {
runnerState.updateUserConfigProperties();
});

context.subscriptions.push(
vscode.commands.registerCommand("devinit.render-template", () => {
let runner = new Runner()
.setConfigPath("/home/jack/Developer/Utilities/devinit/examples/devinitrc.yml")
.setExecPath("/home/jack/Developer/Utilities/devinit/build/devinit")
.setSubcommand(RunnerSubcommandVariant.List)
console.log(
runner.run((stdout, stderr) => {
// command returned succesfully
vscode.window.showInformationMessage(stdout);
vscode.window.showInformationMessage(stderr);
}, (error) => {
// command returned with errors
vscode.window.showErrorMessage(`${error}`);
})
);



runner = new Runner()
.setConfigPath("/home/jack/Developer/Utilities/devinit/examples/devinitrc.yml")
.setExecPath("/home/jack/Developer/Utilities/devinit/build/devinit")
.setSubcommand(RunnerSubcommandVariant.File)
.setOutputType(RunnerOutputType.ListVars)
.setTemplateName("c_cpp_header")
console.log(
runner.run((stdout, stderr) => {
// command returned succesfully
vscode.window.showInformationMessage(stdout);
vscode.window.showInformationMessage(stderr);
}, (error) => {
// command returned with errors
vscode.window.showErrorMessage(`${error}`);
})
);
console.log(runnerState.buildRunner().run());


// let runner = new Runner()
// .setConfigPath("/home/jack/Developer/Utilities/devinit/examples/devinitrc.yml")
// .setExecPath("/home/jack/Developer/Utilities/devinit/build/devinit")
// .setSubcommand(RunnerSubcommandVariant.List)
// console.log(
// runner.run((stdout, stderr) => {
// // command returned succesfully
// vscode.window.showInformationMessage(stdout);
// vscode.window.showInformationMessage(stderr);
// }, (error) => {
// // command returned with errors
// vscode.window.showErrorMessage(`${error}`);
// })
// );



// runner = new Runner()
// .setConfigPath("/home/jack/Developer/Utilities/devinit/examples/devinitrc.yml")
// .setExecPath("/home/jack/Developer/Utilities/devinit/build/devinit")
// .setSubcommand(RunnerSubcommandVariant.File)
// .setOutputType(RunnerOutputType.ListVars)
// .setTemplateName("c_cpp_header")
// console.log(
// runner.run((stdout, stderr) => {
// // command returned succesfully
// vscode.window.showInformationMessage(stdout);
// vscode.window.showInformationMessage(stderr);
// }, (error) => {
// // command returned with errors
// vscode.window.showErrorMessage(`${error}`);
// })
// );
}),
);
}
12 changes: 8 additions & 4 deletions integration/vscode-ext/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ export class Runner {
* @returns The executed command as a verbatim string, for diagnostics
*/
public run(
then: (stdout: string, stderr: string) => void,
err: (reason: string) => void
then?: (stdout: string, stderr: string) => void,
err?: (reason: string) => void
): string {
let cmd = `"${this.execPath}" ${this.buildArgs().join(" ")}`;

child_process.exec(
cmd,
(error, stdout, stderr) => {
if (error !== null) {
err(error.message);
if (err !== undefined) {
err(error.message);
}
} else {
then(stdout, stderr);
if (then !== undefined) {
then(stdout, stderr);
}
}
}
);
Expand Down
58 changes: 58 additions & 0 deletions integration/vscode-ext/src/runnerState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

import { Runner } from "./runner";
import * as userConfig from "./userConfig";

/**
* A state object from which multiple command runners can be created to use shared configuration.
*/
export class RunnerState {
/**
* Path to `devinit` - may just be the term 'devinit' itself, if we are using PATH.
*/
private execPath!: string;

/**
* Path to the `devinitrc.yml` config file to read, or undefined to use system defaults
*/
private configPath: string | undefined;

/**
* Initialise the runner state object
*/
public constructor() {
this.updateUserConfigProperties();
}

/**
* Update the properties in the runner state which are based on the user's VS Code configuration.
* This should be done when config changes (via listening to `vscode.workspace.onDidChangeConfiguration()`)
*/
public updateUserConfigProperties() {
let execPath = userConfig.getExecutablePath();
this.execPath = (execPath.length > 0) ? execPath : "devinit";

let configPath = userConfig.getConfigPath();
this.configPath = (configPath.length > 0) ? configPath : undefined;
}

/**
* Build a Runner object from the shared configuration in this state object.
* @returns New command runner instance
*/
public buildRunner(): Runner {
let runner = new Runner()
.setExecPath(this.execPath);

if (this.configPath !== undefined) {
runner.setConfigPath(this.configPath);
}

return runner;
}
}
20 changes: 20 additions & 0 deletions integration/vscode-ext/src/userConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

import * as vscode from "vscode";

export function getExecutablePath(): string {
return getConfig().get<string>("devinit.environment.executablePath")!;
}

export function getConfigPath(): string {
return getConfig().get<string>("devinit.environment.configurationFile")!;
}

function getConfig() {
return vscode.workspace.getConfiguration();
}

0 comments on commit 8bd43c9

Please sign in to comment.