Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.7KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Mix.Pleroma do
  5. @doc "Common functions to be reused in mix tasks"
  6. def start_pleroma do
  7. Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
  8. {:ok, _} = Application.ensure_all_started(:pleroma)
  9. end
  10. def load_pleroma do
  11. Application.load(:pleroma)
  12. end
  13. def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
  14. Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
  15. end
  16. def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
  17. prompt_message = "#{prompt} [#{defname || defval}] "
  18. input =
  19. if mix_shell?(),
  20. do: Mix.shell().prompt(prompt_message),
  21. else: :io.get_line(prompt_message)
  22. case input do
  23. "\n" ->
  24. case defval do
  25. nil ->
  26. shell_prompt(prompt, defval, defname)
  27. defval ->
  28. defval
  29. end
  30. input ->
  31. String.trim(input)
  32. end
  33. end
  34. def shell_yes?(message) do
  35. if mix_shell?(),
  36. do: Mix.shell().yes?("Continue?"),
  37. else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
  38. end
  39. def shell_info(message) do
  40. if mix_shell?(),
  41. do: Mix.shell().info(message),
  42. else: IO.puts(message)
  43. end
  44. def shell_error(message) do
  45. if mix_shell?(),
  46. do: Mix.shell().error(message),
  47. else: IO.puts(:stderr, message)
  48. end
  49. @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
  50. def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
  51. def escape_sh_path(path) do
  52. ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  53. end
  54. end