diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aca28a..b757fb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,17 @@ # Change Log -All notable changes to the "python-task-provider" extension will be documented in this file. +## 0.2.0 -Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +- Additional args support +- Task resolver support for tasks.json +- Support for non-root files (setup.py or manage.py) +- Added flit and poetry discovery via pyproject.toml metadata sections -## [Unreleased] +## 0.1.0 + +- Setuptools command support +- Django command support + +## 0.0.1 - Initial release \ No newline at end of file diff --git a/README.md b/README.md index dae4e35..e38415c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ An extension for VS Code that provides tasks to the VS Code Task system for Python applications. + - Discovers setuptools (`setup.py`) sub-commands + - Discovers Django `manage.py` and makes all Django commands available as tasks + - Looks at `pyproject.toml` and detects both flit and poetry then makes those tasks available + ## Setuptools Task Provider Automatically discovers available commands for setuptools (`setup.py`) and exposes them to the **Run Task** command: @@ -15,6 +19,26 @@ Automatically discovers available subcommands for Django (`manage.py`) and expos ![](doc/static/django-task-discovery.png) +## Poetry Task Provider + +The poetry discovery script will look for the existence of `pyproject.toml` and the `[tool.poetry]` section within it. + +It will then make available the following commands as tasks: + +"new", "init", "install", "update", "add", "remove", "show", "build", "publish", "config", "run", "shell", "check", "search", "lock", "version", "export", "env", "cache" + +![](doc/static/poetry-task-discovery.png) + +## Flit Task Provider + +The flit discovery script will look for the existence of `pyproject.toml` and the `[tool.flit.*]` section within it. + +It will then make available the following commands as tasks: + +"build", "publish", "install", "init" + +![](doc/static/flit-task-discovery.png) + ## Calling from tasks.json If you want to save the task and recall it, add it to `tasks.json`: @@ -39,6 +63,25 @@ If you want to save the task and recall it, add it to `tasks.json`: The type is either `django` or `setuptools`, and the `task` is the subcommand. You can optionally provide a property `file` to be specific about which `setup.py` or `manage.py` to use (if you have more than 1). +## Providing additional arguments to tasks + +Any tasks which require command-line arguments can be given arguments using the `args` array: + +```javascript +{ + "version": "2.0.0", + "tasks": [ + { + "type": "setuptools", + "task": "build", + "args": ["--generator", "ninja"] + } + ] +} +``` + +## Task Explorer UI Support + If you have the Task Explorer UI extension, it will make these available as UI shortcuts: ![](doc/static/task-explorer-shortcut.png) \ No newline at end of file diff --git a/doc/static/flit-task-discovery.png b/doc/static/flit-task-discovery.png new file mode 100644 index 0000000..65ed223 Binary files /dev/null and b/doc/static/flit-task-discovery.png differ diff --git a/doc/static/poetry-task-discovery.png b/doc/static/poetry-task-discovery.png new file mode 100644 index 0000000..91d2daf Binary files /dev/null and b/doc/static/poetry-task-discovery.png differ diff --git a/package.json b/package.json index a778ce6..40f1b0a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "python-task-provider", "displayName": "Python task provider", "description": "Automatically provide tasks for setuptools and django to the Run Task/tasks.json system.", - "version": "0.1.1", + "version": "0.2.0", "engines": { "vscode": "^1.52.0" }, @@ -60,6 +60,38 @@ "description": "Optional list of arguments to the task" } } + }, + { + "type": "flit", + "required": [ + "task" + ], + "properties": { + "task": { + "type": "string", + "description": "The flit command" + }, + "args": { + "type": "array", + "description": "Optional list of arguments to the task" + } + } + }, + { + "type": "poetry", + "required": [ + "task" + ], + "properties": { + "task": { + "type": "string", + "description": "The poetry command" + }, + "args": { + "type": "array", + "description": "Optional list of arguments to the task" + } + } } ] }, diff --git a/src/djangoTaskProvider.ts b/src/djangoTaskProvider.ts index 932e65c..b097577 100644 --- a/src/djangoTaskProvider.ts +++ b/src/djangoTaskProvider.ts @@ -36,7 +36,9 @@ export class DjangoTaskProvider implements vscode.TaskProvider { if (task) { // resolveTask requires that the same definition object be used. const definition: DjangoTaskDefinition = _task.definition; - return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.task, 'django', new vscode.ShellExecution(`${pythonPath} ${definition.task}`)); + const pythonFile = definition.file ? definition.file : 'manage.py'; + const args = definition.args ? definition.args.join(' ') : ''; + return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.task, 'django', new vscode.ShellExecution(`${pythonPath} ${pythonFile} ${definition.task} ${args}`)); } return undefined; } @@ -79,6 +81,11 @@ interface DjangoTaskDefinition extends vscode.TaskDefinition { * The python file containing the task */ file?: string; + + /** + * Optional arguments to the task + */ + args?: string[]; } function isBuildTask(name: string): boolean { diff --git a/src/extension.ts b/src/extension.ts index 7edfa8e..4e60cf4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,9 +1,12 @@ import * as vscode from 'vscode'; import { SetupToolsTaskProvider } from './setupToolsTaskProvider'; import { DjangoTaskProvider } from './djangoTaskProvider'; +import { PyProjectTaskProvider } from './pyprojectTaskProvider'; let setupToolsProvider: vscode.Disposable | undefined; let djangoProvider: vscode.Disposable | undefined; +let flitProvider: vscode.Disposable | undefined; +let poetryProvider: vscode.Disposable | undefined; export function activate(_context: vscode.ExtensionContext): void { const workspaceRoots = vscode.workspace.workspaceFolders; @@ -13,6 +16,8 @@ export function activate(_context: vscode.ExtensionContext): void { setupToolsProvider = vscode.tasks.registerTaskProvider(SetupToolsTaskProvider.SetupToolsType, new SetupToolsTaskProvider()); djangoProvider = vscode.tasks.registerTaskProvider(DjangoTaskProvider.DjangoType, new DjangoTaskProvider()); + flitProvider = vscode.tasks.registerTaskProvider(PyProjectTaskProvider.PoetryType, new PyProjectTaskProvider(PyProjectTaskProvider.PoetryType)); + poetryProvider = vscode.tasks.registerTaskProvider(PyProjectTaskProvider.FlitType, new PyProjectTaskProvider(PyProjectTaskProvider.FlitType)); } export function deactivate(): void { @@ -22,4 +27,10 @@ export function deactivate(): void { if (djangoProvider) { djangoProvider.dispose(); } + if (flitProvider) { + flitProvider.dispose(); + } + if (poetryProvider) { + poetryProvider.dispose(); + } } \ No newline at end of file diff --git a/src/pyprojectTaskProvider.ts b/src/pyprojectTaskProvider.ts new file mode 100644 index 0000000..0596594 --- /dev/null +++ b/src/pyprojectTaskProvider.ts @@ -0,0 +1,150 @@ +import * as path from 'path'; +import * as fs from 'fs'; +import * as cp from 'child_process'; +import * as vscode from 'vscode'; + +export class PyProjectTaskProvider implements vscode.TaskProvider { + static PoetryType = 'poetry'; + static FlitType = 'flit'; + private pyprojectPromise: Thenable | undefined = undefined; + private buildType: string = 'undefined'; + + constructor(type: string) { + if (!vscode.workspace.workspaceFolders) { + return; + } + this.buildType = type; + vscode.workspace.workspaceFolders.forEach(workspaceRoot => { + const pattern = path.join(workspaceRoot.uri.fsPath, 'pyproject.toml'); + const fileWatcher = vscode.workspace.createFileSystemWatcher(pattern); + fileWatcher.onDidChange(() => this.pyprojectPromise = undefined); + fileWatcher.onDidCreate(() => this.pyprojectPromise = undefined); + fileWatcher.onDidDelete(() => this.pyprojectPromise = undefined); + }); + } + + public provideTasks(): Thenable | undefined { + if (!this.pyprojectPromise) { + this.pyprojectPromise = getTasks(this.buildType); + } + return this.pyprojectPromise; + } + + public resolveTask(_task: vscode.Task): vscode.Task | undefined { + const task = _task.definition.task; + var pythonPath = vscode.workspace.getConfiguration('python').get('pythonPath'); + if (!pythonPath){ + pythonPath = 'python'; + } + if (task) { + // resolveTask requires that the same definition object be used. + const definition: PyProjectTaskDefinition = _task.definition; + const args = definition.args ? definition.args.join(' ') : ''; + return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.task, definition.type, new vscode.ShellExecution(`${pythonPath} -m ${definition.type} ${definition.task} ${args}`)); + } + return undefined; + } +} + +function exists(file: string): Promise { + return new Promise((resolve, _reject) => { + fs.exists(file, (value) => { + resolve(value); + }); + }); +} + +function exec(command: string, options: cp.ExecOptions): Promise<{ stdout: string; stderr: string }> { + return new Promise<{ stdout: string; stderr: string }>((resolve, reject) => { + cp.exec(command, options, (error, stdout, stderr) => { + if (error) { + reject({ error, stdout, stderr }); + } + resolve({ stdout, stderr }); + }); + }); +} + +let _channel: vscode.OutputChannel; +function getOutputChannel(): vscode.OutputChannel { + if (!_channel) { + _channel = vscode.window.createOutputChannel('Pyproject.toml Auto Detection'); + } + return _channel; +} + +interface PyProjectTaskDefinition extends vscode.TaskDefinition { + /** + * The task name + */ + task: string; + + /** + * Optional arguments to the task + */ + args?: string[]; +} + +function getBuiltinTasks(type: string): Array { + if (type === "flit") { + return ["build", "publish", "install", "init"]; + } else if (type === "poetry") { + return ["new", "init", "install", "update", "add", "remove", "show", "build", "publish", "config", "run", + "shell", "check", "search", "lock", "version", "export", "env", "cache"]; + } else { + return []; + } +} + +async function getTasks(buildType: string): Promise { + const workspaceFolders = vscode.workspace.workspaceFolders; + const result: vscode.Task[] = []; + if (!workspaceFolders || workspaceFolders.length === 0) { + return result; + } + for (const workspaceFolder of workspaceFolders) { + const folderString = workspaceFolder.uri.fsPath; + if (!folderString) { + continue; + } + const pyprojectTomlFile = path.join(folderString, 'pyproject.toml'); + if (!await exists(pyprojectTomlFile)) { + continue; + } + var pythonPath = vscode.workspace.getConfiguration('python').get('pythonPath'); + if (!pythonPath){ + pythonPath = 'python'; + } + try { + vscode.workspace.openTextDocument(path.join(folderString, 'pyproject.toml')).then((document) => { + let text = document.getText(); + if (text && text.search(`\\[tool\\.${buildType}`) !== -1) { + getBuiltinTasks(buildType).forEach(taskName => { + const kind: PyProjectTaskDefinition = { + type: buildType, + task: taskName + }; + const task = new vscode.Task(kind, workspaceFolder, taskName, buildType, new vscode.ShellExecution(`${pythonPath} -m ${buildType} ${taskName}`)); + result.push(task); + + if (taskName === "build") { + task.group = vscode.TaskGroup.Build; + } + }); + } + }); + } catch (err) { + const channel = getOutputChannel(); + if (err.stderr) { + channel.appendLine(err.stderr); + } + if (err.stdout) { + channel.appendLine(err.stdout); + } + channel.appendLine('Auto detecting pyproject.toml commands failed.'); + channel.show(true); + } + + } + return result; +} \ No newline at end of file diff --git a/src/setupToolsTaskProvider.ts b/src/setupToolsTaskProvider.ts index 3273962..6c3b152 100644 --- a/src/setupToolsTaskProvider.ts +++ b/src/setupToolsTaskProvider.ts @@ -36,7 +36,9 @@ export class SetupToolsTaskProvider implements vscode.TaskProvider { if (task) { // resolveTask requires that the same definition object be used. const definition: SetupToolsTaskDefinition = _task.definition; - return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.task, 'setuptools', new vscode.ShellExecution(`${pythonPath} ${definition.task}`)); + const pythonFile = definition.file ? definition.file : 'setup.py'; + const args = definition.args ? definition.args.join(' ') : ''; + return new vscode.Task(definition, _task.scope ?? vscode.TaskScope.Workspace, definition.task, 'setuptools', new vscode.ShellExecution(`${pythonPath} ${pythonFile} ${definition.task} ${args}`)); } return undefined; } @@ -79,6 +81,11 @@ interface SetupToolsTaskDefinition extends vscode.TaskDefinition { * The python file containing the task */ file?: string; + + /** + * Optional arguments to the task + */ + args?: string[]; } const buildNames: string[] = ['bdist', 'bdist_wheel', 'sdist', 'build'];