511 words
3 minutes
Replace your Makefile with Taskfile

Introduction#

From long time, I am using Makefile to run repetitive tasks. Makefile provides organization and structure to your scripts.

It has some disadvantages like it is not cross-platform (only available for Linux and Mac) and hard to maintain and understand specially for new developers.

Today, I come across a new tool i.e. Taskfile which is amazing tool. It is a cross-platform alternative to Make and Rake. It is a simple and elegant solution to run repetitive tasks. In this article, we will learn how to install and use Taskfile.

Installation#

You can install Taskfile using the following command:

sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b ~/.local/bin

Note: On macOS and Windows, ~/.local/bin not added to $PATH by default. You may need to add them manually.

How to use Taskfile#

Create a file named Taskfile.yml in your project root directory. This file will contain all the tasks you want to run. Let’s take an example of a project which has frontend and backend. We will create a task to run frontend and backend.

---
version: '3'
includes:
  tools: ./.taskfiles/tools.yaml
  frontend: ./.taskfiles/frontend.yaml
  backend: ./.taskfiles/backend.yaml
tasks:
  default:
    cmds:
      - task -l
    silent: true

In the above file, we have created a task named default which will list all the available tasks. We have also included other task files using includes key.

Let’s create a task file for frontend .taskfile/frontend.yaml.

---
version: '3'
vars:
  SRC_ROOT:
    sh: 'pwd'
tasks:
  install:
    dir: '{{.SRC_ROOT}}/frontend'
    desc: Install npm dependencies
    cmds:
      - npm install
  dev:
    dir: '{{.SRC_ROOT}}/frontend'
    desc: Start development server
    cmds:
      - npm run start

Let’s create a task file for backend .taskfile/backend.yaml.

---
version: '3'
vars:
  SRC_ROOT:
    sh: 'pwd'
tasks:
  test:
    dir: '{{.SRC_ROOT}}/backend'
    desc: Build and run tests
    cmds:
      - ./gradlew clean test
  dev:
    dir: '{{.SRC_ROOT}}/backend'
    desc: Start backend development server
    cmds:
      - ./gradlew bootRun

Finally, let’s create a task file for tools .taskfile/tools.yaml.

---
version: '3'
vars: {}
tasks:
  all:
    desc: Install Tools
    cmds:
      - echo "Installing necessary tools"
    deps:
      - pre-commit
      - node
      - gradle
      - java
  pre-commit:
    desc: Install Pre-commit
    cmds:
      - echo "Installing pre-commit"
      - 'python3 -m pip install --user pre-commit'
    status:
      - type pre-commit
    silent: true
  node:
    desc: Install Node
    cmds:
      - echo "Installing nodejs"
      - nvm install lts
    status:
      - type node
    silent: true
  gradle:
    desc: Install Gradle
    cmds:
      - echo "Installing gradle"
      - sdk install gradle
    status:
      - type gradle
    silent: true
  java:
    desc: Install Java
    cmds:
      - echo "Installing gradle"
      - sdk install java
    status:
      - type java
    silent: true

List all the available tasks using the following command:

$ task -l
task: Available tasks for this project:
* backend:dev:            Start backend development server
* backend:test:           Build and run tests
* frontend:dev:           Start development server
* frontend:install:       Install npm dependencies
* tools:all:              Install Tools
* tools:gradle:           Install Gradle
* tools:java:             Install Java
* tools:node:             Install Node
* tools:pre-commit:       Install Pre-commit

Now, you can run the tasks using the following command:

task <task-name>
task frontend:install
task frontend:dev

That’s it. You can use Taskfile to run repetitive tasks.

Conclusion#

In this article, we learned how to install and use Taskfile. We also learned how to create task files and run tasks.

Github Code#

This article is based on working code that you can find on GitHub.

Replace your Makefile with Taskfile
https://semusings.dev/posts/2023/02/22/replace-makefile-by-taskfiles/
Author
Bhuwan Prasad Upadhyay
Published at
2023-02-22