From 5e920eec8408018134e14b3ec654697da5436b85 Mon Sep 17 00:00:00 2001 From: Gonzalo <456459+grzuy@users.noreply.github.com> Date: Tue, 6 Feb 2024 12:31:02 -0300 Subject: [PATCH] refactor: rename juggler => blend (#5) --- .gitignore | 2 +- README.md | 8 +-- lib/blend.ex | 80 +++++++++++++++++++++++ lib/juggler.ex | 76 --------------------- lib/mix/tasks/blend/get.ex | 18 +++++ lib/mix/tasks/blend/init.ex | 10 +++ lib/mix/tasks/juggler/get.ex | 18 ----- lib/mix/tasks/juggler/init.ex | 10 --- lib/mix/tasks/juggler/update.ex | 18 ----- mix.exs | 4 +- test/{juggler_test.exs => blend_test.exs} | 20 +++--- 11 files changed, 125 insertions(+), 139 deletions(-) create mode 100644 lib/blend.ex delete mode 100644 lib/juggler.ex create mode 100644 lib/mix/tasks/blend/get.ex create mode 100644 lib/mix/tasks/blend/init.ex delete mode 100644 lib/mix/tasks/juggler/get.ex delete mode 100644 lib/mix/tasks/juggler/init.ex delete mode 100644 lib/mix/tasks/juggler/update.ex rename test/{juggler_test.exs => blend_test.exs} (56%) diff --git a/.gitignore b/.gitignore index 0631934..ce4c195 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ erl_crash.dump *.ez # Ignore package tarball (built via "mix hex.build"). -juggler-*.tar +blend-*.tar # Temporary files, for example, from tests. /tmp/ diff --git a/README.md b/README.md index 10db1c6..9d69e4d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Juggler +# Blend Generates and maintains multiple lockfiles so that you can test your elixir app against different variations of your dependencies @@ -6,17 +6,17 @@ against different variations of your dependencies ## Installation If [available in Hex](https://hex.pm/docs/publish), the package can be installed -by adding `juggler` to your list of dependencies in `mix.exs`: +by adding `blend` to your list of dependencies in `mix.exs`: ```elixir def deps do [ - {:juggler, "~> 0.1.0"} + {:blend, "~> 0.1.0"} ] end ``` Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc) and published on [HexDocs](https://hexdocs.pm). Once published, the docs can -be found at . +be found at . diff --git a/lib/blend.ex b/lib/blend.ex new file mode 100644 index 0000000..7465767 --- /dev/null +++ b/lib/blend.ex @@ -0,0 +1,80 @@ +defmodule Blend do + require Logger + + @moduledoc """ + Documentation for `Blend`. + """ + + @blend_dir "blend" + @blendfile_path "blend.exs" + @blendfile_template """ + # Example for testing against 1.x and 2.x of some dependency + # %{ + # "dep-name-1-0": [{:dep_name, "~> 1.0"}], + # "dep-name-2-0": [{:dep_name, "~> 2.0"}], + # } + """ + + def init do + case File.read(@blendfile_path) do + {:ok, _} -> + Logger.info("#{@blendfile_path} file already exists, doing nothing") + + {:error, :enoent} -> + File.write(@blendfile_path, @blendfile_template) + Logger.info("Successfully created #{@blendfile_path} file") + end + end + + def within(blend_id, fun) do + with_project(blend_id, blend_deps(blend_id), fun) + end + + def blends do + case File.read(@blendfile_path) do + {:ok, contents} -> + case Code.eval_string(contents) do + {%{} = map, _} -> + map + + _ -> + raise "Couldn't find a map defining your blends in #{@blendfile_path} file" + end + + {:error, :enoent} -> + raise "Couldn't find a #{@blendfile_path} file" + end + end + + defp blend_deps(blend_id) do + blends() |> Map.fetch!(blend_id) + end + + defp with_project(blend_id, deps, fun) do + :ok = + Mix.ProjectStack.push( + Blend.TmpProject, + Mix.Project.config() + |> Keyword.merge( + app: blend_id, + deps: + deps + |> Enum.reduce( + Mix.Project.config()[:deps], + fn dep, acc -> + acc + |> List.keystore(elem(dep, 0), 0, dep) + end + ), + lockfile: "#{@blend_dir}/#{blend_id}.mix.lock", + build_path: "#{@blend_dir}/_build/#{blend_id}", + deps_path: "#{@blend_dir}/deps/#{blend_id}" + ), + "nofile" + ) + + fun.() + after + Mix.ProjectStack.pop() + end +end diff --git a/lib/juggler.ex b/lib/juggler.ex deleted file mode 100644 index 6cc0033..0000000 --- a/lib/juggler.ex +++ /dev/null @@ -1,76 +0,0 @@ -defmodule Juggler do - require Logger - - @juggler_dir "juggler" - @juggles_file_path "juggles.exs" - @juggles_template """ - # Example for testing against 1.x and 2.x of some dependency - # %{ - # "dep-name-1-0": [{:dep_name, "~> 1.0"}], - # "dep-name-2-0": [{:dep_name, "~> 2.0"}], - # } - """ - - def init do - case File.read(@juggles_file_path) do - {:ok, _} -> - Logger.info("#{@juggles_file_path} file already exists, doing nothing") - - {:error, :enoent} -> - File.write(@juggles_file_path, @juggles_template) - Logger.info("Successfully created #{@juggles_file_path} file") - end - end - - def juggles do - case File.read(@juggles_file_path) do - {:ok, contents} -> - case Code.eval_string(contents) do - {%{} = map, _} -> - map - - _ -> - raise "Couldn't find a map defining your juggles in #{@juggles_file_path} file" - end - - {:error, :enoent} -> - raise "Couldn't find a #{@juggles_file_path} file" - end - end - - def within(juggle_id, fun) do - with_project(juggle_id, juggle_deps(juggle_id), fun) - end - - defp juggle_deps(juggle_id) do - juggles() |> Map.fetch!(juggle_id) - end - - defp with_project(name, deps, fun) do - :ok = - Mix.ProjectStack.push( - Juggler.TmpProject, - Mix.Project.config() - |> Keyword.merge( - app: name, - deps: - deps - |> Enum.reduce( - Mix.Project.config()[:deps], - fn dep, acc -> - acc - |> List.keystore(elem(dep, 0), 0, dep) - end - ), - lockfile: "#{@juggler_dir}/#{name}.mix.lock", - build_path: "#{@juggler_dir}/_build/#{name}", - deps_path: "#{@juggler_dir}/deps/#{name}" - ), - "nofile" - ) - - fun.() - after - Mix.ProjectStack.pop() - end -end diff --git a/lib/mix/tasks/blend/get.ex b/lib/mix/tasks/blend/get.ex new file mode 100644 index 0000000..20daa26 --- /dev/null +++ b/lib/mix/tasks/blend/get.ex @@ -0,0 +1,18 @@ +defmodule Mix.Tasks.Blend.Get do + use Mix.Task + + @shortdoc "Generates lockfiles from blend.exs" + + @impl true + def run(args) do + Blend.blends() + |> Enum.map(fn {blend_id, _deps} -> + Blend.within( + blend_id, + fn -> + Mix.Task.rerun("deps.get", args) + end + ) + end) + end +end diff --git a/lib/mix/tasks/blend/init.ex b/lib/mix/tasks/blend/init.ex new file mode 100644 index 0000000..bf31048 --- /dev/null +++ b/lib/mix/tasks/blend/init.ex @@ -0,0 +1,10 @@ +defmodule Mix.Tasks.Blend.Init do + use Mix.Task + + @shortdoc "Initializes an empty blend.exs file" + + @impl true + def run(_args) do + Blend.init() + end +end diff --git a/lib/mix/tasks/juggler/get.ex b/lib/mix/tasks/juggler/get.ex deleted file mode 100644 index 0815f0b..0000000 --- a/lib/mix/tasks/juggler/get.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Mix.Tasks.Juggler.Get do - use Mix.Task - - @shortdoc "Generates lockfiles from juggles.exs" - - @impl true - def run(args) do - Juggler.juggles() - |> Enum.map(fn {juggle_id, _deps} -> - Juggler.within( - juggle_id, - fn -> - Mix.Task.rerun("deps.get", args) - end - ) - end) - end -end diff --git a/lib/mix/tasks/juggler/init.ex b/lib/mix/tasks/juggler/init.ex deleted file mode 100644 index 1727a6d..0000000 --- a/lib/mix/tasks/juggler/init.ex +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Mix.Tasks.Juggler.Init do - use Mix.Task - - @shortdoc "Initializes an empty juggles.exs file" - - @impl true - def run(_args) do - Juggler.init() - end -end diff --git a/lib/mix/tasks/juggler/update.ex b/lib/mix/tasks/juggler/update.ex deleted file mode 100644 index 88186ce..0000000 --- a/lib/mix/tasks/juggler/update.ex +++ /dev/null @@ -1,18 +0,0 @@ -defmodule Mix.Tasks.Juggler.Update do - use Mix.Task - - @shortdoc "Updates lockfiles from juggles.exs" - - @impl true - def run(args) do - Juggler.juggles() - |> Enum.map(fn {juggle_id, _deps} -> - Juggler.within( - juggle_id, - fn -> - Mix.Task.rerun("deps.update", args) - end - ) - end) - end -end diff --git a/mix.exs b/mix.exs index 578e5bf..3cee675 100644 --- a/mix.exs +++ b/mix.exs @@ -1,9 +1,9 @@ -defmodule Juggler.MixProject do +defmodule Blend.MixProject do use Mix.Project def project do [ - app: :juggler, + app: :blend, version: "0.1.0", elixir: "~> 1.15", start_permanent: Mix.env() == :prod, diff --git a/test/juggler_test.exs b/test/blend_test.exs similarity index 56% rename from test/juggler_test.exs rename to test/blend_test.exs index 0dd291c..d9d013f 100644 --- a/test/juggler_test.exs +++ b/test/blend_test.exs @@ -1,6 +1,6 @@ -defmodule JugglerTest do +defmodule BlendTest do use ExUnit.Case - doctest Juggler + doctest Blend @tmp_path Path.expand("../tmp", __DIR__) @@ -10,23 +10,23 @@ defmodule JugglerTest do :ok end - test "init/0 generates juggles file" do + test "init/0 generates blend file" do File.cd!( @tmp_path, fn -> - refute File.exists?("juggles.exs") - assert Juggler.init() == :ok - assert File.exists?("juggles.exs") + refute File.exists?("blend.exs") + Mix.Task.run("blend.init") + assert File.exists?("blend.exs") end ) end - test "juggler.get task" do + test "blend.get task" do File.cd!( @tmp_path, fn -> File.write( - "juggles.exs", + "blend.exs", """ %{ "jason-1-0": [{:jason, "~> 1.0"}] @@ -34,8 +34,8 @@ defmodule JugglerTest do """ ) - Mix.Task.run("juggler.get") - File.exists?("juggler/jason-1-0.mix.lock") + Mix.Task.run("blend.get") + File.exists?("blend/jason-1-0.mix.lock") end )