mirror of
https://github.com/ash-project/igniter.git
synced 2024-09-19 13:02:51 +12:00
improvement: add copy_template/4
This commit is contained in:
parent
9133d9d05b
commit
f39ca441b5
3 changed files with 33 additions and 1 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
3
test/templates/template.css.eex
Normal file
3
test/templates/template.css.eex
Normal file
|
@ -0,0 +1,3 @@
|
|||
.<%= @class %> {
|
||||
background: black
|
||||
}
|
Loading…
Reference in a new issue