You've already forked tribes-plugin-new
f1838e4d7d
Create the tribes-plugin-new Igniter generator package with standalone project generation, install/page tasks, upgrade hook scaffolding, and focused tests.
33 lines
853 B
Elixir
33 lines
853 B
Elixir
defmodule TribesPlugin.Writer do
|
|
@moduledoc false
|
|
|
|
def write_project!(root, files, opts \\ []) do
|
|
force? = Keyword.get(opts, :force?, false)
|
|
|
|
File.mkdir_p!(root)
|
|
|
|
if !force? && !empty_dir?(root) do
|
|
raise ArgumentError, "#{root} is not empty; pass --force to write into it"
|
|
end
|
|
|
|
Enum.each(files, fn {relative, content} ->
|
|
path = Path.join(root, relative)
|
|
File.mkdir_p!(Path.dirname(path))
|
|
File.write!(path, content)
|
|
|
|
if relative == "scripts/plugin" do
|
|
File.chmod!(path, 0o755)
|
|
end
|
|
end)
|
|
end
|
|
|
|
defp empty_dir?(path) do
|
|
case File.ls(path) do
|
|
{:ok, []} -> true
|
|
{:ok, entries} -> Enum.all?(entries, &(&1 in [".", ".."]))
|
|
{:error, :enoent} -> true
|
|
{:error, reason} -> raise File.Error, reason: reason, action: "list directory", path: path
|
|
end
|
|
end
|
|
end
|