Adfaft

How to Series - Taskfile sebagai cross-platform Task Runner

Table of Content

Reference

Summary

Taskfile adalah task runner yang tujuannya simple to use da cross platform, dibangun lewat GO hanya single binary tanpa dependency.

Taskfile dapat digunakan jika ingin mengelola sejumlah script yang cross-platform. Seperti menggunakan Makefile di linux, atau NPM di package.json. Memiliki nama, deskripsi, menerima argument, untuk menyimpan daftar script / template agar tidak mudah lupa.

Personal Opinion

Akan tetapi, sayangnya Taskfile tidak memiliki cross-platform file management seperti cd, ls, mv dan lain-lain. Sehingga, untuk menjalankan script untuk OS tertentu, tetap dibutuhkan shell terpisah seperti sh di linux atau batch file di windows.

Saat ini saya gunakan untuk ready to use script yang terkumpul dan mudah dikelola. Tapi, nggak jauh beda dengan simple git repository yang menyimpan berbagai script shell untuk linux / bat untuk windows. Still useful, but not that recommended.*

Jika bisa menghandle cross-platform file management, bakal patut dipertimbangkan.

Prerequisites

How to

Instalation

Filenaming

# Taskfile.yml untuk parent taskfile
version: '3'

includes:
  build: ./Taskfile_{{OS}}.yml
# Taskfile_windows.yml
version: '3'

greet:
    cmds:
      - echo $GREETING
    env:
      GREETING: Hey, there! in Windows

Example

version: '3'

# global variable
vars:
  GREETING: Hey, there!

tasks:

  # menggunakan global variable
  # NOTE: variable dapat ditimpa via command line, `task [taskname] [variable]=[value]`, ex: `task greet GREETING="from ci"`
  greet:
    desc: my task description
    summary: |
      This is multiline summary
      Release your project to github

      It will build your project before starting the release.
      Please make sure that you have set GITHUB_TOKEN before starting.
    cmds: Hello, {{.GREETING}}

  # menggunakan local variable
  greet-local:
    aliases: [local-greet]
    cmds: Hello, {{.GREETING}}
    vars: 
      - GREETING: local Hey, there!
    

  # dijalankan di lokasi file Taskfile.yml berada
  from-home:
    cmds:
      - pwd

  # dijalankan di folder saat ini, tidak bergantung dari lokasi file Taskfile.yml
  from-working-directory:
    dir: '{{.USER_WORKING_DIR}}'
    cmds:
      - pwd
      # call another task
      - task: from-home 

  # include another Taskfile agar mudah dikelola
  includes:
    other-task: ./Taskfile-other.yml
    os-only-task: ./Taskfile-for-{{OS}}.yml # OS Specific task
    task-with-vars:
      taskfile: ./Taskfile-with-vars.yml
      vars:
        MY_INCLUDE_VAR=value
      aliases: [task-include-vars]

  # Fingerprinting, agar tidak dijalankan berulang tergantung dari checksum file apakah berubah (seperti Makefile)
  build-fingerprinting:
    cmds:
      - esbuild --bundle --minify css/index.css > public/bundle.css
    sources:
      - mysources/**/*.css
      - exclude: mysources/ignoreme.css
    generates:
      - public/bundle.css

  # Forward CI Argument, `task yarn -- install`
  yarn:
    cmds:
      - yarn {{.CLI_ARGS}}


  # Warnign promp
  dangerous:
    prompt: This is a dangerous command... Do you want to continue?
    cmds:
      - echo 'dangerous command'

CLI

Other Reading

Footnote