From 3a64f00d9df4a056d6c748b83b201d0f0e83490b Mon Sep 17 00:00:00 2001 From: Axel Rindle Date: Mon, 29 Apr 2024 13:51:10 +0200 Subject: [PATCH] feat: read hashbang --- package.json | 5 +++++ src/provider/task-provider-shell.ts | 18 ++++++++++++++++-- src/services/config.ts | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 03e8813..5052c96 100644 --- a/package.json +++ b/package.json @@ -116,6 +116,11 @@ "type": "boolean", "default": true, "description": "Whether to scan for additional shell files. Reload window for the changes to take effect." + }, + "task-explorer.defaultShell": { + "type": "string", + "default": "/bin/bash", + "description": "The default shell to use for shell tasks." } } }, diff --git a/src/provider/task-provider-shell.ts b/src/provider/task-provider-shell.ts index 7bc13df..4c7c648 100644 --- a/src/provider/task-provider-shell.ts +++ b/src/provider/task-provider-shell.ts @@ -50,7 +50,21 @@ export default class TaskProviderShell implements TaskProvider { const name = basename(file.path) const folder = workspace.getWorkspaceFolder(file) - // TODO: Get hashbang + let executable = this.config.getOr('defaultShell', '/bin/bash') + let shellArgs: string[] = [] + + const contents = await workspace.fs.readFile(file) + const lines = contents.toString().split('\n') + if (lines.length > 0) { + const firstLine = lines[0].replace('\r', '') + + if (firstLine.startsWith('#!')) { + const parts = firstLine.split(' ') + + executable = parts[0].replace('#!', '') + shellArgs = parts.slice(1) + } + } const task = new ShellTask( new ShellTaskDefinition(), @@ -61,7 +75,7 @@ export default class TaskProviderShell implements TaskProvider { file.fsPath, { cwd: folder?.uri.fsPath, - executable: '/bin/bash', + executable, shellArgs } ) ) diff --git a/src/services/config.ts b/src/services/config.ts index e2e39e9..95386f8 100644 --- a/src/services/config.ts +++ b/src/services/config.ts @@ -8,6 +8,8 @@ export interface ConfigSchema { exclude: string[] scanShell: boolean + + defaultShell: string } type ConfigKey = keyof ConfigSchema