diff --git a/README.md b/README.md index 0c42826..2271e23 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,41 @@ When the extension detects a Piral instance or pilet folder opened it automatica ![Scaffold Pilet](./docs/workspace.png) +### Tasks + +You can add provided tasks for your Piral instances or pilets. + +- `piral: debug` - a build (watch) task to debug the Piral instance +- `piral: build` - a build task to build the Piral instance +- `piral: validate` - a test task to validate the Piral instance +- `piral: declaration` - a build task to generate the declaration for the Piral instance +- `pilet: debug` - a build (watch) task to debug the pilet +- `pilet: build` - a build task to build the pilet +- `pilet: validate` - a test task to validate the pilet +- `pilet: pack` - a build task to pack the pilet + +You can either run these tasks manually or use them, e.g., in a *launch.json*: + +```json +{ + "version": "0.2.0", + "configurations": [ + { + "type": "chrome", + "request": "launch", + "name": "Launch Chrome against localhost", + "url": "http://localhost:1234", + "webRoot": "${workspaceFolder}", + "preLaunchTask": "pilet: debug" + } + ] +} +``` + +### Problem Matcher + +If you make your own task using the `piral-cli` you can use the `$piral-cli-debug` problem matcher to monitor the `piral debug` / `pilet debug` commands. + ### Command Palette All commands that are relevant to Piral can be run from the command palette. diff --git a/src/extension/commands.ts b/src/extension/commands.ts index 981ba66..1545b87 100644 --- a/src/extension/commands.ts +++ b/src/extension/commands.ts @@ -9,7 +9,7 @@ function debugPilet() { } function buildPilet() { - runCommand('npx pilet build', RepoType.Pilet); + runCommand('npx pilet build', RepoType.Pilet); } function publishPilet() { diff --git a/src/extension/helpers/utils.ts b/src/extension/helpers/utils.ts index 4845259..2f199bf 100644 --- a/src/extension/helpers/utils.ts +++ b/src/extension/helpers/utils.ts @@ -21,9 +21,7 @@ export function readJson(filePath: string) { } } -export function getRepoType(): RepoType { - const workspaceFolder = getWorkspaceRoot(); - +export function getRepoTypeOf(workspaceFolder: vscode.WorkspaceFolder | undefined) { if (workspaceFolder !== undefined) { const packageJson = readJson(resolve(workspaceFolder.uri.fsPath, 'package.json')); const piralJson = readJson(resolve(workspaceFolder.uri.fsPath, 'piral.json')); @@ -43,6 +41,11 @@ export function getRepoType(): RepoType { return RepoType.Undefined; } +export function getRepoType(): RepoType { + const workspaceFolder = getWorkspaceRoot(); + return getRepoTypeOf(workspaceFolder); +} + export function getWorkspaceRoot() { return vscode.workspace.workspaceFolders?.[0]; } diff --git a/src/extension/providers/items.ts b/src/extension/providers/items.ts index be0c3a8..d7aaf3f 100644 --- a/src/extension/providers/items.ts +++ b/src/extension/providers/items.ts @@ -10,7 +10,8 @@ export class CommandTreeItem extends vscode.TreeItem { label, children === undefined ? vscode.TreeItemCollapsibleState.None : vscode.TreeItemCollapsibleState.Expanded, ); - this.contextValue = target !== undefined ? target != '' ? 'showFullContextPlugin' : 'showWebLinkContextPlugin' : undefined; + this.contextValue = + target !== undefined ? (target != '' ? 'showFullContextPlugin' : 'showWebLinkContextPlugin') : undefined; this.children = children; this.commandName = commandName; this.target = target; diff --git a/src/extension/providers/piletTaskProvider.ts b/src/extension/providers/piletTaskProvider.ts index 5352404..3972b30 100644 --- a/src/extension/providers/piletTaskProvider.ts +++ b/src/extension/providers/piletTaskProvider.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import { RepoType, getRepoTypeOf, getWorkspaceRoot } from '../helpers'; export class PiletTaskProvider implements vscode.TaskProvider { static Type = 'pilet'; @@ -14,18 +15,7 @@ export class PiletTaskProvider implements vscode.TaskProvider { } public resolveTask(_task: vscode.Task): vscode.Task | undefined { - const flags = _task.definition.flags || []; - const command = ['npx', 'pilet', 'debug', ...flags].join(' '); - const definition = _task.definition; - - return new vscode.Task( - definition, - _task.scope ?? vscode.TaskScope.Workspace, - 'debug', - PiletTaskProvider.Type, - new vscode.ShellExecution(command), - ['$piral-cli-debug'], - ); + return undefined; } } @@ -36,29 +26,88 @@ interface PiletTaskDefinition extends vscode.TaskDefinition { flags?: Array; } +function createDebugTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiletTaskDefinition = { + type: PiletTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'pilet', 'debug', ...(kind.flags || [])].join(' '); + const task = new vscode.Task( + kind, + workspaceFolder, + 'debug', + PiletTaskProvider.Type, + new vscode.ShellExecution(command), + ['$piral-cli-debug'], + ); + task.isBackground = true; + task.group = vscode.TaskGroup.Build; + return task; +} + +function createBuildTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiletTaskDefinition = { + type: PiletTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'pilet', 'build', ...(kind.flags || [])].join(' '); + const task = new vscode.Task( + kind, + workspaceFolder, + 'build', + PiletTaskProvider.Type, + new vscode.ShellExecution(command), + ); + task.group = vscode.TaskGroup.Build; + return task; +} + +function createValidateTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiletTaskDefinition = { + type: PiletTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'pilet', 'validate', ...(kind.flags || [])].join(' '); + const task = new vscode.Task( + kind, + workspaceFolder, + 'validate', + PiletTaskProvider.Type, + new vscode.ShellExecution(command), + ); + task.group = vscode.TaskGroup.Test; + return task; +} + +function createPackTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiletTaskDefinition = { + type: PiletTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'pilet', 'pack', ...(kind.flags || [])].join(' '); + const task = new vscode.Task( + kind, + workspaceFolder, + 'pack', + PiletTaskProvider.Type, + new vscode.ShellExecution(command), + ); + task.group = vscode.TaskGroup.Build; + return task; +} + async function getPiletTasks(): Promise { - const workspaceFolders = vscode.workspace.workspaceFolders; + const workspaceFolder = getWorkspaceRoot(); + const repoType = getRepoTypeOf(workspaceFolder); const result: vscode.Task[] = []; - if (workspaceFolders?.length) { - for (const workspaceFolder of workspaceFolders) { - const kind: PiletTaskDefinition = { - type: PiletTaskProvider.Type, - flags: [], - }; - const command = ['npx', 'pilet', 'debug', ...(kind.flags || [])].join(' '); - const task = new vscode.Task( - kind, - workspaceFolder, - 'debug', - PiletTaskProvider.Type, - new vscode.ShellExecution(command), - ['$piral-cli-debug'], - ); - task.isBackground = true; - task.group = vscode.TaskGroup.Build; - result.push(task); - } + if (workspaceFolder && repoType === RepoType.Pilet) { + result.push( + createDebugTask(workspaceFolder), + createBuildTask(workspaceFolder), + createValidateTask(workspaceFolder), + createPackTask(workspaceFolder), + ); } return result; diff --git a/src/extension/providers/piralTaskProvider.ts b/src/extension/providers/piralTaskProvider.ts index f961085..19e19d3 100644 --- a/src/extension/providers/piralTaskProvider.ts +++ b/src/extension/providers/piralTaskProvider.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import { RepoType, getRepoTypeOf, getWorkspaceRoot } from '../helpers'; export class PiralTaskProvider implements vscode.TaskProvider { static Type = 'piral'; @@ -14,18 +15,7 @@ export class PiralTaskProvider implements vscode.TaskProvider { } public resolveTask(_task: vscode.Task): vscode.Task | undefined { - const flags = _task.definition.flags || []; - const command = ['npx', 'piral', 'debug', ...flags].join(' '); - const definition = _task.definition; - - return new vscode.Task( - definition, - _task.scope ?? vscode.TaskScope.Workspace, - 'debug', - PiralTaskProvider.Type, - new vscode.ShellExecution(command), - ['$piral-cli-debug'], - ); + return undefined; } } @@ -36,29 +26,65 @@ interface PiralTaskDefinition extends vscode.TaskDefinition { flags?: Array; } +function createDebugTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiralTaskDefinition = { + type: PiralTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'piral', 'debug', ...(kind.flags || [])].join(' '); + const task = new vscode.Task(kind, workspaceFolder, 'debug', 'piral', new vscode.ShellExecution(command), [ + '$piral-cli-debug', + ]); + task.isBackground = true; + task.group = vscode.TaskGroup.Build; + return task; +} + +function createBuildTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiralTaskDefinition = { + type: PiralTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'piral', 'build', ...(kind.flags || [])].join(' '); + const task = new vscode.Task(kind, workspaceFolder, 'build', 'piral', new vscode.ShellExecution(command)); + task.group = vscode.TaskGroup.Build; + return task; +} + +function createValidateTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiralTaskDefinition = { + type: PiralTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'piral', 'validate', ...(kind.flags || [])].join(' '); + const task = new vscode.Task(kind, workspaceFolder, 'validate', 'piral', new vscode.ShellExecution(command)); + task.group = vscode.TaskGroup.Test; + return task; +} + +function createDeclarationTask(workspaceFolder: vscode.WorkspaceFolder) { + const kind: PiralTaskDefinition = { + type: PiralTaskProvider.Type, + flags: [], + }; + const command = ['npx', 'piral', 'declaration', ...(kind.flags || [])].join(' '); + const task = new vscode.Task(kind, workspaceFolder, 'declaration', 'piral', new vscode.ShellExecution(command)); + task.group = vscode.TaskGroup.Build; + return task; +} + async function getPiralTasks(): Promise { - const workspaceFolders = vscode.workspace.workspaceFolders; + const workspaceFolder = getWorkspaceRoot(); + const repoType = getRepoTypeOf(workspaceFolder); const result: vscode.Task[] = []; - if (workspaceFolders?.length) { - for (const workspaceFolder of workspaceFolders) { - const kind: PiralTaskDefinition = { - type: PiralTaskProvider.Type, - flags: [], - }; - const command = ['npx', 'piral', 'debug', ...(kind.flags || [])].join(' '); - const task = new vscode.Task( - kind, - workspaceFolder, - 'debug', - 'piral', - new vscode.ShellExecution(command), - ['$piral-cli-debug'], - ); - task.isBackground = true; - task.group = vscode.TaskGroup.Build; - result.push(task); - } + if (workspaceFolder && repoType === RepoType.Piral) { + result.push( + createDebugTask(workspaceFolder), + createBuildTask(workspaceFolder), + createValidateTask(workspaceFolder), + createDeclarationTask(workspaceFolder), + ); } return result;