Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.
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
3 changes: 1 addition & 2 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
!LICENSE
!README.md
!icon.png
!node_modules/**
!out/src/**.js
!dist/extension.js
!package-lock.json
!package.json
37 changes: 37 additions & 0 deletions launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/dist/extension.js"
],
"preLaunchTask": "npm"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--extensionTestsPath=${workspaceRoot}/out/test"
],
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/out/test/**/*.js"
],
"preLaunchTask": "npm: test-compile"
}
]
}
6,392 changes: 4,369 additions & 2,023 deletions package-lock.json

Large diffs are not rendered by default.

25 changes: 16 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.3",
"publisher": "owenfarrell",
"engines": {
"vscode": "^1.18.1"
"vscode": "^1.29.1"
},
"license": "MIT",
"displayName": "µTask",
Expand All @@ -27,12 +27,13 @@
"url": "https://github.com/owenfarrell/vscode-microtask.git"
},
"galleryBanner": {},
"main": "./out/src/extension",
"main": "./dist/extension",
"contributes": {
"commands": [
{
"command": "microtask.runFromHere",
"title": "Tasks: Run From Here"
"title": "Run From Here",
"category": "Tasks"
}
],
"menus": {
Expand All @@ -57,15 +58,21 @@
"markdown": "github",
"dependencies": {},
"devDependencies": {
"ajv": "^6.9.1",
"ts-loader": "^6.2.2",
"typescript": "^2.6.2",
"vscode": "^1.1.5",
"mocha": "^3.5.3",
"@types/node": "^8.0.30",
"@types/mocha": "^2.2.44"
"vscode": "^1.1.36",
"webpack": "^4.42.1",
"webpack-cli": "^3.3.11",
"@types/mocha": "^2.2.44",
"@types/node": "^8.10.60"
},
"scripts": {
"vscode:prepublish": "tsc -p ./",
"compile": "tsc -watch -p ./",
"package": "vsce package",
"vscode:prepublish": "webpack --mode production",
"test-compile": "tsc -p ./",
"compile": "webpack --mode development --watch",
"webpack": "webpack --mode development",
"postinstall": "node ./node_modules/vscode/bin/install"
},
"icon": "icon.png"
Expand Down
105 changes: 41 additions & 64 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,99 +1,76 @@
"use strict";

import * as cp from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as Path from "path";
import * as vscode from "vscode";

const outputChannel: vscode.OutputChannel = vscode.window.createOutputChannel("Sub Launcher");
const outputChannel = vscode.window.createOutputChannel("Sub Launcher");
let taskItems: TaskQuickPickItem[] = [];
let taskMap: { [key: string]: string; } = {};
let cwdMap = new Map<string, string>();

export function activate(context: vscode.ExtensionContext) {
// If a workspace has been opened
if (vscode.workspace.rootPath) {
// When the configuration changes, reload the task list
vscode.workspace.onDidChangeConfiguration(loadTaskList);
// Load the task list for the first time
loadTaskList();
// Subscribe to "microtask.runFromHere" events
context.subscriptions.push(vscode.commands.registerCommand("microtask.runFromHere", runFromHere));

context.subscriptions.push(
// Subscribe to "change configuration" events
vscode.workspace.onDidChangeConfiguration(loadTaskList),
// Register the command
vscode.commands.registerCommand("microtask.runFromHere", runFromHere),
// Subscribe to "task process end" events
vscode.tasks.onDidEndTaskProcess(resetTaskCwd),
outputChannel
);
}
}

export function deactivate() {
outputChannel.dispose();
}

interface TaskQuickPickItem extends vscode.QuickPickItem {
// TODO Strongly type this field to an interface provided by the vscode namespace
task: any;
task: vscode.Task;
}

function loadTaskList() {
let refreshedItems: TaskQuickPickItem[] = [];
// Get the task configuration for the workspace
let tasksConfiguration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("tasks");
// If there are multiple tasks defined
if (tasksConfiguration.tasks) {
// Get the subtasks
let taskArray: any[] = tasksConfiguration.get("tasks");
// If multiple tasks are defined
if (taskArray.length > 0) {
// Fetch the list of tasks
vscode.tasks.fetchTasks()
.then((taskList) => {
// Create an array of QuickPickItems
let refreshedList: TaskQuickPickItem[] = [];
// Create a Map of
let refreshedMap = new Map<string, string>();
// For each task
taskArray.forEach((element: any) => {
// Add a quick pick item to the array
refreshedItems.push({
description: "",
label: element.label || element.taskName,
task: element
});
taskList.forEach((element) => {
// Wrap the task in a QuickPickItem and push it on to the array
refreshedList.push({ description: "", label: element.name, task: element });
// Cache the default CWD in the map
refreshedMap[element.name] = element.execution.options.cwd
});
}
}
taskItems = refreshedItems;
// Replace the cached list of QuickPickItems
taskItems = refreshedList;
// Replace the cached map of Task CWDs
cwdMap = refreshedMap;
});
}

function runFromHere(uri: vscode.Uri) {
// Show the quick pick menu
vscode.window.showQuickPick<TaskQuickPickItem>(taskItems)
// Spawn a process based on the selected task
.then((selectedItem: TaskQuickPickItem) => {
.then((selectedItem) => {
if (selectedItem) {
let processArguments = selectedItem.task.args || [];
let spawnOptions: cp.SpawnOptions = {
// Get the folder of the specified uri (or workspace root if not sepecified)
cwd: uri ? fs.lstatSync(uri.fsPath).isDirectory() ? uri.fsPath : Path.dirname(uri.fsPath) : vscode.workspace.rootPath,
env: selectedItem.task.env,
// FIXME Update this conditional to support optional values
shell: selectedItem.task.type === "shell"
};
// Get the presentation configuration of the task (or create a default version if none exists)
let presentationConfig = selectedItem.task.presentation || {
echo: true,
panel: "shared",
reveal: "always"
};

// If the output should be sent to a new presentation panel, create a new panel
// FIXME Create a new output channel on demand
if (presentationConfig.panel === "new") outputChannel.clear();
// If the output should be sent to a dedicated presentation panel, use the existing panel
if (presentationConfig.panel === "dedicated") outputChannel.clear();

// If the command should be printed as part of the presentation, echo the command and arguments
if (presentationConfig.echo === true) outputChannel.appendLine(`> Executing task: ${selectedItem.task.command} ${processArguments.join(" ")} <${os.EOL}`);

// If the presentation should always be revealed, show the panel
if (presentationConfig.reveal === "always") outputChannel.show();

// Spawn a child process
let childProcess: cp.ChildProcess = cp.spawn(selectedItem.task.command, processArguments, spawnOptions);
// When data is written STDOUT of the child process, write the stringified data to the output channel
childProcess.stdout.on("data", (stdoutData: any) => outputChannel.append(stdoutData.toString()));
// If the presentation should be revealed on error, show the panel
if (presentationConfig.reveal === "silent") childProcess.stdout.on("error", () => outputChannel.show());
// Override the CWD for the task
selectedItem.task.execution.options.cwd = uri ? fs.lstatSync(uri.fsPath).isDirectory() ? uri.fsPath : Path.dirname(uri.fsPath) : vscode.workspace.rootPath;
// Execute the task
vscode.tasks.executeTask(selectedItem.task);
}
});
}

function resetTaskCwd(event: vscode.TaskProcessEndEvent) {
// TODO Possible race condition where configuration is reloaded after task execution started
// Replace the CWD with the original value
event.execution.task.execution.options.cwd = cwdMap.get(event.execution.task.name);
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"rootDir": "."
},
"exclude": [
"dist",
"node_modules",
".vscode-test"
]
Expand Down
41 changes: 41 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//@ts-check

'use strict';

const path = require('path');

/**@type {import('webpack').Configuration}*/
const config = {
target: 'node', // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/

entry: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'),
filename: 'extension.js',
libraryTarget: 'commonjs2',
devtoolModuleFilenameTemplate: '../[resource-path]'
},
devtool: 'source-map',
externals: {
vscode: 'commonjs vscode' // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
},
resolve: {
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader'
}
]
}
]
}
};
module.exports = config;