2017-04-03 20:24:52 +00:00
|
|
|
defmodule MBU.BuildTask do
|
2017-03-21 05:44:44 +00:00
|
|
|
@moduledoc """
|
|
|
|
BuildTask contains the macros that are used for making the tasks look nicer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Sets up the necessary things for a build task.
|
|
|
|
|
2017-04-03 20:24:52 +00:00
|
|
|
Each build task should have `use MBU.BuildTask` at its beginning. BuildTask automatically
|
2018-03-18 15:35:39 +00:00
|
|
|
imports Mix.Task and Logger so they don't need to be added into the task itself.
|
|
|
|
|
|
|
|
The following options may be passed to the use clause:
|
|
|
|
|
|
|
|
- `:auto_path`: Set to `true` to autogenerate an `out_path/0` function for this task. Default:
|
|
|
|
`false`. Overrides the `:auto_paths` config.
|
|
|
|
- `:create_out_path`: Set to `true` to create the output path specified by `out_path/0` when the
|
|
|
|
task is started. Default: `false`. Overrides the `:create_out_paths` config.
|
2017-03-21 05:44:44 +00:00
|
|
|
"""
|
2018-03-18 15:35:39 +00:00
|
|
|
defmacro __using__(opts \\ []) do
|
2017-03-21 05:44:44 +00:00
|
|
|
quote do
|
|
|
|
use Mix.Task
|
|
|
|
require Logger
|
|
|
|
|
2017-04-03 20:24:52 +00:00
|
|
|
import MBU.BuildTask
|
2017-03-21 05:44:44 +00:00
|
|
|
|
|
|
|
# Dependencies of the task that will be automatically run before it
|
|
|
|
@deps []
|
2018-03-18 15:35:39 +00:00
|
|
|
|
|
|
|
@create_out_path Keyword.get(unquote(opts), :create_out_path, nil)
|
|
|
|
|
|
|
|
auto_path = Keyword.get(unquote(opts), :auto_path, nil)
|
|
|
|
|
|
|
|
if auto_path || (Application.get_env(:mbu, :auto_paths, false) and auto_path == nil) do
|
|
|
|
def out_path() do
|
|
|
|
Path.join([tmp_path(), Mix.Task.task_name(__MODULE__)])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp tmp_path() do
|
|
|
|
# Get temporary directory or raise error
|
|
|
|
case Application.get_env(:mbu, :tmp_path) do
|
|
|
|
nil ->
|
|
|
|
raise "MBU must be configured with a directory to put temporary files in if automatic "
|
|
|
|
"directory handling is used! Use the `:tmp_path` key to set a desired path."
|
|
|
|
|
|
|
|
path ->
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-03-21 05:44:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Replacement for Mix.Task's run/1, used similarly. Code inside will be run when the task
|
|
|
|
is called, with @deps being run first unless `deps: false` is given in the arguments.
|
|
|
|
"""
|
|
|
|
defmacro task(args, do: block) do
|
|
|
|
quote do
|
2017-04-03 20:24:52 +00:00
|
|
|
def run(unquote(args) = mbu_buildtask_args) do
|
2017-03-21 05:44:44 +00:00
|
|
|
task = Mix.Task.task_name(__MODULE__)
|
2018-03-18 15:35:39 +00:00
|
|
|
Logger.info("[#{task}] Started")
|
|
|
|
|
|
|
|
if @create_out_path ||
|
|
|
|
(Application.get_env(:mbu, :create_out_paths, false) and @create_out_path == nil) do
|
|
|
|
op = apply(__MODULE__, :out_path, [])
|
|
|
|
Logger.info("[#{task}] Creating output path #{op}...")
|
|
|
|
:ok = File.mkdir_p!(op)
|
|
|
|
end
|
2017-03-21 05:44:44 +00:00
|
|
|
|
2017-04-03 20:24:52 +00:00
|
|
|
if Keyword.get(mbu_buildtask_args, :deps, true) and not Enum.empty?(@deps) do
|
|
|
|
MBU.TaskUtils.run_tasks(@deps)
|
2017-03-21 05:44:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
unquote(block)
|
|
|
|
|
2018-03-18 15:35:39 +00:00
|
|
|
Logger.info("[#{task}] Finished")
|
2017-03-21 05:44:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|