compile.copy_srcs.ex 988 B

12345678910111213141516171819202122232425262728293031323334353637
  1. defmodule Mix.Tasks.Compile.CopySrcs do
  2. use Mix.Task.Compiler
  3. @recursive true
  4. @impl true
  5. def run(_args) do
  6. Mix.Project.get!()
  7. config = Mix.Project.config()
  8. extra_dirs = config[:extra_dirs]
  9. unless extra_dirs && is_list(extra_dirs) do
  10. Mix.raise("application option :extra_dirs in #{Mix.Project.project_file()} must be a list of directories under the application")
  11. end
  12. app_root = File.cwd!()
  13. app_build_path = Mix.Project.app_path(config)
  14. for extra_dir <- extra_dirs do
  15. src = Path.join([app_root, extra_dir])
  16. dest = Path.join([app_build_path, extra_dir])
  17. File.rm(dest)
  18. case File.ln_s(src, dest) do
  19. :ok ->
  20. :ok
  21. {:error, :eexist} ->
  22. Mix.shell().info(IO.ANSI.format([:yellow, "#{dest} still exists after attempted removal"]))
  23. :ok
  24. {:error, error} ->
  25. Mix.raise("error trying to link #{src} to #{dest}: #{error}")
  26. end
  27. end
  28. {:noop, []}
  29. end
  30. end