diff --git a/lib/igniter.ex b/lib/igniter.ex index c1985e1..b014aca 100644 --- a/lib/igniter.ex +++ b/lib/igniter.ex @@ -10,6 +10,7 @@ defmodule Igniter do warnings: [], notices: [], assigns: %{}, + mkdirs: [], moves: %{}, args: %Igniter.Mix.Task.Args{} ] @@ -23,6 +24,7 @@ defmodule Igniter do warnings: [String.t()], notices: [String.t()], assigns: map(), + mkdirs: [String.t()], moves: %{optional(String.t()) => String.t()}, args: Igniter.Mix.Task.Args.t() } @@ -665,6 +667,22 @@ defmodule Igniter do create_new_file(igniter, target, contents, opts) end + @doc """ + Creates a folder in the project. + """ + + @spec mkdir(t(), Path.t()) :: Igniter.t() + def mkdir(igniter, path) do + current_dir = Path.expand(".") + target_path = Path.expand(path) + + if String.starts_with?(target_path, current_dir) do + %{igniter | mkdirs: List.wrap(path) ++ igniter.mkdirs} + else + add_issue(igniter, "Igniter.mkdir invalid path: #{path} is outside the current directory.") + end + end + @doc """ Creates a new file in the project with the provided string contents. Adds an error if it already exists. @@ -955,6 +973,8 @@ defmodule Igniter do display_warnings(igniter, title) + display_mkdirs(igniter) + display_moves(igniter) display_tasks(igniter, result_of_dry_run, opts) @@ -981,6 +1001,13 @@ defmodule Igniter do Mix.shell().cmd("mix deps.get") end + igniter.mkdirs + |> Enum.map(&Path.expand(&1, "")) + |> Enum.uniq() + |> Enum.each(fn path -> + File.mkdir_p!(path) + end) + igniter.moves |> Enum.each(fn {from, to} -> File.mkdir_p!(Path.dirname(to)) @@ -1529,6 +1556,21 @@ defmodule Igniter do |> display_list() end + @doc false + def display_mkdirs(igniter) do + igniter.mkdirs + |> Enum.map(&Path.expand(&1, "")) + |> Enum.uniq() + |> Enum.sort() + |> Enum.map(fn path -> + if not File.exists?(path) do + [:green, path] + end + end) + |> Enum.reject(&is_nil/1) + |> display_list("These folders will be created:") + end + @doc false def display_moves(igniter) do igniter.moves diff --git a/test/igniter/code/file_test.exs b/test/igniter/code/file_test.exs index f20a688..8d6e75a 100644 --- a/test/igniter/code/file_test.exs +++ b/test/igniter/code/file_test.exs @@ -39,4 +39,20 @@ defmodule Igniter.Code.FileTest do |> Rewrite.source!("README.md") |> Rewrite.Source.get(:content) == "Hello Test" end + + test "can create folder" do + assert %{mkdirs: mkdirs} = + Igniter.new() + |> Igniter.mkdir("empty_folder") + + assert mkdirs == ["empty_folder"] + end + + test "can only create folder inside project directory" do + %{mkdirs: mkdirs} = + Igniter.new() + |> Igniter.mkdir("../empty_folder") + + assert mkdirs == [] + end end