Rake task auto detection in VSCode
There are a tons of tools in the world to automate tasks like linting, building, packaging, etc. These tools are mostly run from the command line but if you are using VSCode, you can run the tasks without having to leave the editor. Moreover, vanilla VSCode can auto-detect tasks for Gulp, Grunt, Jake and npm.
You may ask how about other build tools? For example, Rubyists usually use Rake to automate their workflow but can they have the same experience as those Node tools? Definitely yes! VSCode provides API for extension authors to implement tasks auto detection with ease.
We already added Rake tasks auto detection in Ruby extension for VSCode. Let’s see how easy it is to make it happen.
API
/**
* Register a task provider.
*/
export function registerTaskProvider(type: string, provider: TaskProvider): Disposable;
First of all, we need to register a task provider for the target language, in our case, type
is ruby
. The next thing is creating a task provider and let’s look at its definition first:
export interface TaskProvider { /**
* Provides tasks.
* @param token A cancellation token.
* @return an array of tasks
*/
provideTasks(token?: CancellationToken): ProviderResult<Task[]>; /**
* Resolves a task the has no execution set.
*/ resolveTask(task: Task, token?: CancellationToken): ProviderResult<Task>;
}
With the class/interface definitions in mind, the workflow of task auto detection is very straightforward:
- Users open Command Palette and type
Run Task
- VSCode checks all task providers registered in current workspace and ask them to
provideTasks
- Task Provider does the magic part and return a list of tasks, which VSCode understands how to display (
name
) and how to execute (process/shellCmd
).’
Task Detection
Now we come to the “magic” part 🙈, it’s just one simple shell command
~/code/rails-task: rake -AT
rake about # List versions of all Rails frameworks and the environment
rake app:template # Applies the template supplied by LOCATION=(/path/to/template) or URL
...
...
rake db:_dump #
rake db:abort_if_pending_migrations #
rake db:charset #
rake db:check_protected_environments #
rake db:collation #
rake db:create # Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Wi...
rake db:create:all #
...
...
We only need to split the whole result content by Line Break, do a regex match, extract name
, description
and the shell command line, and lastly create the corresponding Task
object
{
"name": "about",
"execution": {
"commandLine": "rake about"
}
}
That’s pretty much it.
It took me no more than 1 hour to implement the task provider, testing and release. Hope my tiny blog inspires you somehow , please let me know if you create something interesting 😊.
Links:
- VSCode Task documentation https://code.visualstudio.com/docs/editor/task
- Auto Task Detection https://code.visualstudio.com/docs/editor/tasks#_task-autodetection
- Auto Task Detection sample https://github.com/Microsoft/vscode-extension-samples/tree/master/task-provider-sample