diff --git a/README.md b/README.md index 3fef2cd..5cba1f2 100644 --- a/README.md +++ b/README.md @@ -95,23 +95,14 @@ $ mix test A more permanent configuration for running mix tasks in the context of a blend lockfile with a simple env var can be acomplished by customizing your `mix.exs` a bit, with the following steps. -##### 1. Create a new file `blend/premix.exs` with the following contents: +##### 1. Create a new file `blend/premix.exs` with the following command: -```elixir -# blend/premix.exs - -maybe_put_env = fn varname, value -> - System.put_env(varname, System.get_env(varname, value)) -end - -blend = System.get_env("BLEND") - -if blend && String.length(blend) > 0 do - maybe_put_env.("MIX_LOCKFILE", "blend/#{blend}.mix.lock") - maybe_put_env.("MIX_DEPS_PATH", "blend/deps/#{blend}") - maybe_put_env.("MIX_BUILD_ROOT", "blend/_build/#{blend}") -end ``` +$ mix blend.premix +``` + +This will generate a `blend/premix.exs` file that needs to be compiled at the top of your `mix.exs` file +so that some mix env vars are properly set based on the `BLEND` env var before running any mix task. ##### 2. Modify your `mix.exs`. @@ -155,6 +146,7 @@ $ mix blend.init # Generate blend.exs $ mix blend.get # Generate blend lockfiles $ mix blend.update --all # Update blend lockfiles to latest possible versions $ mix blend.list # List blends +$ mix blend.premix # Generate premix.exs file ``` ## License diff --git a/lib/blend.ex b/lib/blend.ex index b6453ff..ba2fcff 100644 --- a/lib/blend.ex +++ b/lib/blend.ex @@ -4,20 +4,31 @@ defmodule Blend do """ @blend_dir "blend" - @blendfile_path "blend.exs" + @blendfile_name "blend.exs" @blendfile_template File.read!(Path.join(__DIR__, "blend/templates/blend.exs")) + @premix_file_name "premix.exs" + @premix_file_template File.read!(Path.join(__DIR__, "blend/templates/premix.exs")) def init do - case File.read(@blendfile_path) do + case File.read(@blendfile_name) do {:ok, _} -> - IO.puts("#{@blendfile_path} file already exists, doing nothing") + IO.puts("#{@blendfile_name} file already exists, doing nothing") {:error, :enoent} -> - File.write!(@blendfile_path, @blendfile_template) - IO.puts("Successfully created #{@blendfile_path} file") + File.write!(@blendfile_name, @blendfile_template) + IO.puts("Successfully created #{@blendfile_name} file") end end + def premix do + path = Path.join(@blend_dir, @premix_file_name) + + File.mkdir_p!(@blend_dir) + File.write!(path, @premix_file_template) + + IO.puts("Written #{path} file") + end + def within(blend_id, fun) do host_project_config = Mix.Project.config() @@ -49,18 +60,18 @@ defmodule Blend do end def blends do - case File.read(@blendfile_path) do + case File.read(@blendfile_name) do {:ok, contents} -> case Code.eval_string(contents) do {%{} = map, _} -> map _ -> - raise "Couldn't find a map defining your blends in #{@blendfile_path} file" + raise "Couldn't find a map defining your blends in #{@blendfile_name} file" end {:error, :enoent} -> - raise "Couldn't find a #{@blendfile_path} file" + raise "Couldn't find a #{@blendfile_name} file" end end end diff --git a/lib/blend/templates/premix.exs b/lib/blend/templates/premix.exs new file mode 100644 index 0000000..3101f68 --- /dev/null +++ b/lib/blend/templates/premix.exs @@ -0,0 +1,22 @@ +# This file is autogenerated by blend package. +# +# Run `mix blend.premix` to update it's contents after +# each blend package version update. + +maybe_put_env = fn varname, value -> + System.put_env(varname, System.get_env(varname, value)) +end + +existing_blend = fn name -> + Code.eval_file("blend.exs") + |> elem(0) + |> Map.fetch!(String.to_atom(name)) +end + +blend = System.get_env("BLEND") + +if blend && String.length(blend) > 0 && existing_blend.(blend) do + maybe_put_env.("MIX_LOCKFILE", "blend/#{blend}.mix.lock") + maybe_put_env.("MIX_DEPS_PATH", "blend/deps/#{blend}") + maybe_put_env.("MIX_BUILD_ROOT", "blend/_build/#{blend}") +end diff --git a/lib/mix/tasks/blend/premix.ex b/lib/mix/tasks/blend/premix.ex new file mode 100644 index 0000000..3a980cb --- /dev/null +++ b/lib/mix/tasks/blend/premix.ex @@ -0,0 +1,10 @@ +defmodule Mix.Tasks.Blend.Premix do + use Mix.Task + + @shortdoc "Generates premix.exs file" + + @impl true + def run(_args) do + Blend.premix() + end +end