From f39ca441b5babda5e9246c7e660347b4093d9155 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Tue, 20 Aug 2024 14:32:02 -0400 Subject: [PATCH] improvement: add `copy_template/4` --- lib/igniter.ex | 13 ++++++++++++- test/igniter_test.exs | 18 ++++++++++++++++++ test/templates/template.css.eex | 3 +++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/templates/template.css.eex diff --git a/lib/igniter.ex b/lib/igniter.ex index a5468de..fa8fbd4 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -412,7 +412,18 @@ defmodule Igniter do ) end - @doc "Creates a new (non-elixir) file in the project with the provided string contents. Adds an error if it already exists." + @spec copy_template( + igniter :: Igniter.t(), + target :: Path.t(), + source :: Path.t(), + assigns :: Keyword.t() + ) :: Igniter.t() + def copy_template(igniter, source, target, assigns) do + contents = EEx.eval_file(source, assigns: assigns) + create_new_file(igniter, target, contents) + end + + @doc "Creates a new file in the project with the provided string contents. Adds an error if it already exists." @spec create_new_file(t(), Path.t(), String.t()) :: Igniter.t() def create_new_file(igniter, path, contents \\ "", opts \\ []) do source_handler = source_handler(path, opts) diff --git a/test/igniter_test.exs b/test/igniter_test.exs index ddfb1c9..c6d9d28 100644 --- a/test/igniter_test.exs +++ b/test/igniter_test.exs @@ -1,4 +1,22 @@ defmodule IgniterTest do use ExUnit.Case doctest Igniter + + describe "Igniter.copy_template/4" do + test "it evaluates and writes the template" do + %{rewrite: rewrite} = + Igniter.new() + |> Igniter.copy_template("test/templates/template.css.eex", "lib/foobar.css", + class: "hello" + ) + + config_file = Rewrite.source!(rewrite, "lib/foobar.css") + + assert Rewrite.Source.get(config_file, :content) == """ + .hello { + background: black + } + """ + end + end end diff --git a/test/templates/template.css.eex b/test/templates/template.css.eex new file mode 100644 index 0000000..9648a00 --- /dev/null +++ b/test/templates/template.css.eex @@ -0,0 +1,3 @@ +.<%= @class %> { + background: black +}