Skip to content

Proposal: adding Igniter.copyΒ #215

Description

@mayel

Describe the solution you'd like
Igniter.copy to copy file(s) or directories recursively.

Describe alternatives you've considered

I currently have this in a helper module used by my tasks, but thought it may make sense for it to be supported upstream:

  def igniter_copy(igniter, source, target, opts \\ [])
  def igniter_copy(igniter, sources, target, opts) when is_list(sources) do
    IO.puts("Batch copying #{inspect sources} to #{target}")
    Enum.reduce(sources, igniter, fn source, igniter ->
      igniter_copy(igniter, source, prepare_target_path(source, target), opts)
    end)
  end
  def igniter_copy(igniter, source, target, opts) do
    IO.puts("Copying #{source} to #{target}")

    if File.dir?(source) do

      sources = File.ls!(source)
      |> Enum.map(fn file -> Path.join(source, file) end)

      igniter_copy(igniter, sources, target, opts)

    else
      contents_to_copy = File.read!(source)

      Igniter.create_or_update_file(igniter, target, contents_to_copy, fn source ->
          Rewrite.Source.update(source, :content, contents_to_copy)
      end)
    end
  end

  defp prepare_target_path(source, target) do
    # Remove the repeated part of source from target
    if String.contains?(target, source) do
      String.replace(target, source, "")
      |> String.trim_leading("/")
      |> (&Path.join(target, &1)).()
    else
      Path.join(target, Path.basename(source))
    end
  end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions