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

tools: Add automatic provider selection option #91

Merged
merged 2 commits into from
Jul 22, 2024
Merged
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
52 changes: 41 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@
"properties": {
"edacation.toolProvider": {
"type": "string",
"markdownDescription": "Specifies the tool provider used to run Yosys and Nextpnr. See the [docs](https://github.com/EDAcation/vscode-edacation/blob/main/docs/tool-provider.md) for more information.",
"default": "native-managed",
"markdownDescription": "Specifies the tool provider used to run Yosys and Nextpnr. [Learn more](https://github.com/EDAcation/vscode-edacation/blob/main/docs/tool-provider.md)",
"default": "auto",
"enum": [
"auto",
"native-managed",
"native-host",
"web"
],
"enumItemLabels": [
"Automatic",
"Native (Managed)",
"Native (Host)",
"Web"
],
"enumDescriptions": [
"Let EDAcation choose which provider to use. Native providers are used where possible, and pre-installed tools are preferred.",
"Let EDAcation install and manage native Yosys & Nextpnr tools on your system. Only available on VSCode for Desktop on certain platforms.",
"Use native Yosys & Nextpnr tools that are already present on the system. Requires said tools to be available in PATH. Only available on VSCode for Desktop.",
"Use WebAssembly versions of Yosys & Nextpnr. This option requires an active internet connection. Slower, but available on all platforms and environments."
Expand Down Expand Up @@ -364,6 +367,7 @@
"@types/vscode": "1.73.0",
"@types/vscode-webview": "^1.57.4",
"@types/webpack-env": "^1.18.4",
"@types/which": "^3.0.4",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"@vscode/test-web": "^0.0.54",
Expand Down Expand Up @@ -395,6 +399,7 @@
"path-browserify": "^1.0.1",
"tar-fs": "^3.0.6",
"vue": "^3.4.27",
"which": "^4.0.0",
"yosys2digitaljs": "^0.8.0"
}
}
2 changes: 2 additions & 0 deletions src/common/node-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type ModuleFS = typeof import('fs');
export type ModuleOS = typeof import('os');
export type ModuleProcess = typeof import('process');
export type ModuleStream = typeof import('stream');
export type ModuleWhich = typeof import('which');
export type ModuleWorkerThreads = typeof import('worker_threads');
export type ModuleZLib = typeof import('zlib');

Expand All @@ -36,5 +37,6 @@ export const fs = () => requireModule('fs') as ModuleFS;
export const os = () => requireModule('os') as ModuleOS;
export const process = () => requireModule('process') as ModuleProcess;
export const stream = () => requireModule('stream') as ModuleStream;
export const which = () => requireModule('which') as ModuleWhich;
export const workerThreads = () => requireModule('worker_threads') as ModuleWorkerThreads;
export const zlib = () => requireModule('zlib') as ModuleZLib;
2 changes: 1 addition & 1 deletion src/extension/tasks/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class TerminalMessageEmitter {
this.messageEvent.event(callback);
}

private fire(message: TerminalMessage) {
protected fire(message: TerminalMessage) {
this.messageEvent.fire(message);
}

Expand Down
43 changes: 15 additions & 28 deletions src/extension/tasks/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type * as vscode from 'vscode';

import {type Project} from '../projects/index.js';

import {AnsiModifier, type TaskOutputFile, type TerminalMessage, TerminalMessageEmitter} from './messaging.js';
import {ToolProvider} from './toolprovider.js';
import {AnsiModifier, type TaskOutputFile, TerminalMessageEmitter} from './messaging.js';
import {type ToolProvider} from './toolprovider.js';

export interface TaskDefinition extends vscode.TaskDefinition {
project: string;
Expand Down Expand Up @@ -48,7 +48,10 @@ export abstract class TerminalTask<WorkerOptions extends _WorkerOptions> extends

this.toolProvider = toolProvider;
// proxy all provider messages to terminal
this.toolProvider.onMessage(this.onProviderMessage.bind(this));
this.toolProvider.onMessage((msg) => {
if (this.isDisabled) return;
return this.fire(msg);
});

this.isDisabled = false;
}
Expand All @@ -69,25 +72,6 @@ export abstract class TerminalTask<WorkerOptions extends _WorkerOptions> extends

abstract handleEnd(project: Project, outputFiles: TaskOutputFile[]): Promise<void>;

private onProviderMessage(message: TerminalMessage) {
if (this.isDisabled) return;

switch (message.type) {
case 'println': {
this.println(message.line, message.stream);
break;
}
case 'done': {
this.done(message.outputFiles);
break;
}
case 'error': {
this.error(message.error);
break;
}
}
}

async execute(project: Project, targetId: string) {
const workerOptions = this.getWorkerOptions(project, targetId);

Expand Down Expand Up @@ -115,18 +99,21 @@ export abstract class TerminalTask<WorkerOptions extends _WorkerOptions> extends
}
this.println();

// Print the tool provider and command to execute
this.println(`Tool command (${this.toolProvider.getName()}):`, undefined, AnsiModifier.BOLD);
this.println(` ${command} ${args.join(' ')}`);
this.println();

await this.toolProvider.run({
this.toolProvider.setRunContext({
project,
command,
args,
inputFiles,
outputFiles
});

// Print the tool provider and command to execute
const toolName = await this.toolProvider.getName();
this.println(`Tool command (${toolName}):`, undefined, AnsiModifier.BOLD);
this.println(` ${command} ${args.join(' ')}`);
this.println();

await this.toolProvider.execute();
}

cleanup() {
Expand Down
70 changes: 61 additions & 9 deletions src/extension/tasks/toolprovider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,34 @@ interface Context {
outputFiles: TaskIOFile[];
}

type ToolConfigOption = 'native-managed' | 'native-host' | 'web';
type ToolConfigOption = 'auto' | 'native-managed' | 'native-host' | 'web';

export abstract class ToolProvider extends TerminalMessageEmitter {
protected readonly extensionContext: vscode.ExtensionContext;
protected ctx: Context | null = null;

constructor(extensionContext: vscode.ExtensionContext) {
super();

this.extensionContext = extensionContext;
}

abstract getName(): string;
abstract getName(): Promise<string>;

abstract run(ctx: Context): Promise<void>;
protected abstract run(ctx: Context): Promise<void>;

setRunContext(ctx: Context) {
this.ctx = ctx;
}

async execute(): Promise<void> {
if (!this.ctx) throw new Error('No run context available!');
return await this.run(this.ctx);
}
}

export class WebAssemblyToolProvider extends ToolProvider {
getName() {
async getName(): Promise<string> {
return 'WebAssembly';
}

Expand Down Expand Up @@ -224,7 +234,7 @@ abstract class NativeToolProvider extends ToolProvider {
}

export class ManagedToolProvider extends NativeToolProvider {
getName(): string {
async getName(): Promise<string> {
return 'Native - Managed';
}

Expand All @@ -244,20 +254,62 @@ export class ManagedToolProvider extends NativeToolProvider {
}

export class HostToolProvider extends NativeToolProvider {
getName(): string {
async getName(): Promise<string> {
return 'Native - Host';
}

async getExecutionOptions(command: string): Promise<NativeToolExecutionOptions | null> {
// Host tools should be installed to PATH. Just return the command here - the OS should resolve it.
return {entrypoint: command};
const entrypoint = await node.which()(command, {nothrow: true});
if (!entrypoint) return null;

return {entrypoint};
}
}

export class AutomaticToolProvider extends ToolProvider {
private toolProvider: ToolProvider | null = null;

async getName(): Promise<string> {
if (!this.ctx) return 'Automatic';

const provider = await this.getToolProvider(this.ctx.command);
return `Automatic [${await provider.getName()}]`;
}

private async getToolProvider(command: string): Promise<ToolProvider> {
if (this.toolProvider) return this.toolProvider;

// Always use Web provider in non-node environments
const webProvider = new WebAssemblyToolProvider(this.extensionContext);
if (!node.isAvailable()) return webProvider;

// Use host provider if tool is installed
const hostProvider = new HostToolProvider(this.extensionContext);
if (await hostProvider.getExecutionOptions(command)) return hostProvider;

// Use managed provider, unless installation somehow fails
const managedProvider = new ManagedToolProvider(this.extensionContext);
if (await managedProvider.getExecutionOptions(command)) return managedProvider;

// Fall back to web provider
return webProvider;
}

async run(ctx: Context): Promise<void> {
const provider = await this.getToolProvider(ctx.command);
provider.onMessage(this.fire.bind(this));

provider.setRunContext(ctx);
return await provider.execute();
}
}

export const getConfiguredProvider = (context: vscode.ExtensionContext): ToolProvider => {
const provider = vscode.workspace.getConfiguration('edacation').get('toolProvider') as ToolConfigOption;

if (provider === 'native-managed') {
if (provider === 'auto') {
return new AutomaticToolProvider(context);
} else if (provider === 'native-managed') {
return new ManagedToolProvider(context);
} else if (provider === 'native-host') {
return new HostToolProvider(context);
Expand Down