improvement: add copy_template/4

This commit is contained in:
Zach Daniel 2024-08-20 14:32:02 -04:00
parent 9133d9d05b
commit f39ca441b5
3 changed files with 33 additions and 1 deletions

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,3 @@
.<%= @class %> {
background: black
}