Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: premix task #12

Merged
merged 5 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,10 @@ $ 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
grzuy marked this conversation as resolved.
Show resolved Hide resolved
```

##### 2. Modify your `mix.exs`.
Expand Down
7 changes: 7 additions & 0 deletions lib/blend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ defmodule Blend do
@blend_dir "blend"
@blendfile_path "blend.exs"
@blendfile_template File.read!(Path.join(__DIR__, "blend/templates/blend.exs"))
@premix_file_path Path.join(@blend_dir, "premix.exs")
@premix_file_template File.read!(Path.join(__DIR__, "blend/templates/premix.exs"))

def init do
case File.read(@blendfile_path) do
Expand All @@ -18,6 +20,11 @@ defmodule Blend do
end
end

def premix do
File.write!(@premix_file_path, @premix_file_template)
IO.puts("Written #{@premix_file_path} file")
end

def within(blend_id, fun) do
host_project_config = Mix.Project.config()

Expand Down
17 changes: 17 additions & 0 deletions lib/blend/templates/premix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
10 changes: 10 additions & 0 deletions lib/mix/tasks/blend/premix.ex
Original file line number Diff line number Diff line change
@@ -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