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 all commits
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
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
grzuy marked this conversation as resolved.
Show resolved Hide resolved
```

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`.

Expand Down Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions lib/blend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
22 changes: 22 additions & 0 deletions lib/blend/templates/premix.exs
Original file line number Diff line number Diff line change
@@ -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
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