MBU: Mix Build Utilities

Hex.pm:hex.pm/packages/mbu

Hexdocs:hexdocs.pm/mbu

MBU is a collection of utility functions and scripts to turn Mix into a build tool like Make. Sort of. With it, you can write tasks that build parts of your system, be it front end or back end, without having to leave the safety of Elixir.

Shortly put, MBU allows you to write Mix tasks that depend on other tasks and contains helper functions and macros to make writing them easier. See the basic usage section for code examples.

Basic Usage

A typical MBU task looks like this:

defmodule Mix.Task.Build.Css do
  use MBU.BuildTask
  import MBU.TaskUtils

  @deps [
    "build.scss",
    "build.assets"
  ]

  task _args do
    exec("css-processor", ["--output", "dist/css"]) |> listen()
  end
end

There are a few parts to note here:

MBU also has watch support both for watches builtin to commands and custom watches:

defmodule Mix.Task.Watch.Css do
  use MBU.BuildTask
  import MBU.TaskUtils

  @deps [
    "build.css"
  ]

  task _args do
    [
      # Builtin watch
      exec("css-processor", ["--output", "dist-css", "-w"]),

      # Custom watch
      watch("CopyAssets", "/path/to/assets", fn _events -> File.cp_r!("from", "to") end)
    ]
    |> listen(watch: true)
  end
end

As you can see, there are two types of watches here. The css-processor command has its own watch, activated with a command line flag -w. The second watch is a custom watch, useful for when CLI tools don't have watch support or when you want to run custom Elixir code. The MBU.TaskUtils.watch/3 function takes in the watch name (for logging), directory to watch and a callback function that is called for change events.

Here, the listening function is given an argument watch: true. The arguments makes it listen to the user's keyboard input and if the user presses the enter key, the watches and programs are stopped. Otherwise you would have to kill the task to stop them.

Installation

If available in Hex, the package can be installed by adding mbu to your list of dependencies in mix.exs:

def deps do
  [{:mbu, "~> 1.0.1"}]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/mbu.