|
| 1 | +# BrightScript Tasks |
| 2 | + |
| 3 | +The BrightScript language extension provides a task system that integrates with VS Code's task runner, allowing you to automate common development workflows like building, testing, and linting. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Custom shell commands with variable substitution |
| 8 | +- Interactive folder selection with glob patterns |
| 9 | +- Environment variables and custom working directories |
| 10 | +- Problem matcher integration for error detection |
| 11 | +- Background task support for watchers |
| 12 | + |
| 13 | +## Basic Task Configuration |
| 14 | + |
| 15 | +Tasks are defined in `.vscode/tasks.json` in your workspace. Here's a simple example: |
| 16 | + |
| 17 | +```json |
| 18 | +{ |
| 19 | + "version": "2.0.0", |
| 20 | + "tasks": [ |
| 21 | + { |
| 22 | + "type": "brightscript", |
| 23 | + "label": "Build Project", |
| 24 | + "command": "npx bsc" |
| 25 | + } |
| 26 | + ] |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +### Required Properties |
| 31 | + |
| 32 | +- **type**: Must be `"brightscript"` to use the BrightScript task provider |
| 33 | +- **label**: Display name shown in the task picker |
| 34 | +- **command**: The shell command to execute |
| 35 | + |
| 36 | +## Variable Substitution |
| 37 | + |
| 38 | +BrightScript tasks support variable substitution in the `command` field. Variables are resolved before the command is executed. |
| 39 | + |
| 40 | +### Using `${folderForFile: <glob>}` |
| 41 | + |
| 42 | +The `${folderForFile: <glob>}` variable finds files matching a glob pattern and resolves to the directory containing those files. This is useful in monorepos or multi-project workspaces. |
| 43 | + |
| 44 | +**Example: Build a specific project** |
| 45 | + |
| 46 | +```json |
| 47 | +{ |
| 48 | + "type": "brightscript", |
| 49 | + "label": "Build Selected Project", |
| 50 | + "command": "cd ${folderForFile: **/bsconfig.json} && npx bsc" |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +When you run this task: |
| 55 | +- If one `bsconfig.json` is found, it uses that directory automatically |
| 56 | +- If multiple are found, you get a quick pick menu to select which project |
| 57 | + |
| 58 | +**Common glob patterns:** |
| 59 | + |
| 60 | +| Pattern | Matches | |
| 61 | +|---------|---------| |
| 62 | +| `**/bsconfig.json` | Any `bsconfig.json` file at any depth | |
| 63 | +| `apps/**/package.json` | `package.json` files under the `apps` directory | |
| 64 | +| `*.config.js` | Config files in the root directory only | |
| 65 | + |
| 66 | +**Note**: Files in `node_modules` are automatically excluded. |
| 67 | + |
| 68 | +## Advanced Configuration |
| 69 | + |
| 70 | +### Environment Variables |
| 71 | + |
| 72 | +Add environment variables available to your command: |
| 73 | + |
| 74 | +```json |
| 75 | +{ |
| 76 | + "type": "brightscript", |
| 77 | + "label": "Build with Custom Env", |
| 78 | + "command": "npx bsc", |
| 79 | + "options": { |
| 80 | + "env": { |
| 81 | + "NODE_ENV": "production" |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Custom Working Directory |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "type": "brightscript", |
| 92 | + "label": "Build from Subdirectory", |
| 93 | + "command": "npx bsc", |
| 94 | + "options": { |
| 95 | + "cwd": "${workspaceFolder}/apps/my-roku-app" |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Problem Matchers |
| 101 | + |
| 102 | +Problem matchers parse command output to detect errors and warnings. Here's a basic example for BrighterScript compiler output: |
| 103 | + |
| 104 | +```json |
| 105 | +{ |
| 106 | + "type": "brightscript", |
| 107 | + "label": "Compile", |
| 108 | + "command": "npx bsc", |
| 109 | + "problemMatcher": { |
| 110 | + "owner": "brightscript", |
| 111 | + "fileLocation": "relative", |
| 112 | + "pattern": { |
| 113 | + "regexp": "^(.*)\\((\\d+),(\\d+)\\):\\s+(error|warning)\\s+(.*)$", |
| 114 | + "file": 1, |
| 115 | + "line": 2, |
| 116 | + "column": 3, |
| 117 | + "severity": 4, |
| 118 | + "message": 5 |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Background Tasks |
| 125 | + |
| 126 | +Background tasks are useful for file watchers or development servers: |
| 127 | + |
| 128 | +```json |
| 129 | +{ |
| 130 | + "type": "brightscript", |
| 131 | + "label": "Watch", |
| 132 | + "command": "npx bsc --watch", |
| 133 | + "isBackground": true |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Pre-Launch Tasks |
| 138 | + |
| 139 | +Use tasks as pre-launch actions to build your project before debugging: |
| 140 | + |
| 141 | +```json |
| 142 | +{ |
| 143 | + "type": "brightscript", |
| 144 | + "name": "Launch on Roku", |
| 145 | + "request": "launch", |
| 146 | + "host": "${promptForHost}", |
| 147 | + "password": "${promptForPassword}", |
| 148 | + "preLaunchTask": "Build Project" |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +## Complete Example |
| 153 | + |
| 154 | +Here's a basic `tasks.json` for a monorepo workflow: |
| 155 | + |
| 156 | +```json |
| 157 | +{ |
| 158 | + "version": "2.0.0", |
| 159 | + "tasks": [ |
| 160 | + { |
| 161 | + "type": "brightscript", |
| 162 | + "label": "Build Selected Project", |
| 163 | + "command": "cd ${folderForFile: **/bsconfig.json} && npx bsc", |
| 164 | + "group": { |
| 165 | + "kind": "build", |
| 166 | + "isDefault": true |
| 167 | + } |
| 168 | + }, |
| 169 | + { |
| 170 | + "type": "brightscript", |
| 171 | + "label": "Watch Selected Project", |
| 172 | + "command": "cd ${folderForFile: **/bsconfig.json} && npx bsc --watch", |
| 173 | + "isBackground": true |
| 174 | + } |
| 175 | + ] |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +## Troubleshooting |
| 180 | + |
| 181 | +**Task not found:** |
| 182 | +- Ensure `type` is set to `"brightscript"` |
| 183 | +- Verify `tasks.json` has valid JSON syntax |
| 184 | + |
| 185 | +**Variable not resolving:** |
| 186 | +- Check that files matching the glob pattern exist |
| 187 | +- Remember that `node_modules` is excluded |
| 188 | + |
| 189 | +**Command not executing:** |
| 190 | +- Check the terminal output for errors |
| 191 | +- Verify the command works in a regular terminal |
| 192 | +- Ensure required programs are in your PATH |
| 193 | + |
| 194 | +## Best Practices |
| 195 | + |
| 196 | +- Use descriptive task labels |
| 197 | +- Add problem matchers for instant error feedback |
| 198 | +- Set default build and test tasks for keyboard shortcuts |
| 199 | +- Use the `${folderForFile}` variable for monorepo flexibility |
| 200 | + |
| 201 | +## See Also |
| 202 | + |
| 203 | +- [VS Code Tasks Documentation](https://code.visualstudio.com/docs/editor/tasks) |
| 204 | +- [Variable Substitutions](./variable-substitutions.md) |
| 205 | + |
| 206 | +## Supported Variables |
| 207 | + |
| 208 | +| Variable | Description | Example Value | |
| 209 | +|----------|-------------|---------------| |
| 210 | +| **Workspace Variables** | | | |
| 211 | +| `${workspaceFolder}` | Path of the workspace folder | `/Users/user/project` | |
| 212 | +| `${workspaceFolderBasename}` | Name of the workspace folder | `project` | |
| 213 | +| `${fileWorkspaceFolderBasename}` | Name of workspace folder containing active file | `my-app` | |
| 214 | +| **File Variables** (require an active file) | | | |
| 215 | +| `${file}` | Full path of the currently opened file | `/Users/user/project/src/main.brs` | |
| 216 | +| `${fileWorkspaceFolder}` | Workspace folder of the currently opened file | `/Users/user/project` | |
| 217 | +| `${relativeFile}` | Current file relative to workspace folder | `src/main.brs` | |
| 218 | +| `${relativeFileDirname}` | Current file's directory relative to workspace | `src` | |
| 219 | +| `${fileBasename}` | Current file's basename | `main.brs` | |
| 220 | +| `${fileBasenameNoExtension}` | Current file's basename without extension | `main` | |
| 221 | +| `${fileExtname}` | Current file's extension | `.brs` | |
| 222 | +| `${fileDirname}` | Current file's directory path | `/Users/user/project/src` | |
| 223 | +| `${fileDirnameBasename}` | Current file's directory name | `src` | |
| 224 | +| **Editor Variables** (require an active editor) | | | |
| 225 | +| `${lineNumber}` | Current line number in active file (1-based) | `42` | |
| 226 | +| `${columnNumber}` | Current column number in active file (1-based) | `15` | |
| 227 | +| `${selectedText}` | Currently selected text in active file | `function main()` | |
| 228 | +| **System Variables** | | | |
| 229 | +| `${userHome}` | User's home directory | `/Users/user` | |
| 230 | +| `${cwd}` | Current working directory of VS Code | `/Users/user/project` | |
| 231 | +| `${execPath}` | Path to VS Code executable | `/Applications/VSCode.app` | |
| 232 | +| `${pathSeparator}` | OS-specific path separator | `/` (macOS/Linux) or `\` (Windows) | |
| 233 | +| `${/}` | Shorthand for `${pathSeparator}` | `/` or `\` | |
| 234 | +| **Custom Variables** | | | |
| 235 | +| `${folderForFile: <glob>}` | Directory containing file(s) matching glob pattern | `/Users/user/project/apps/app1` | |
0 commit comments