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.

86 lines
2.1KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 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. if Pleroma.Config.get(:env) != :test do
  9. Application.put_env(:logger, :console, level: :debug)
  10. end
  11. {:ok, _} = Application.ensure_all_started(:pleroma)
  12. if Pleroma.Config.get(:env) not in [:test, :benchmark] do
  13. pleroma_rebooted?()
  14. end
  15. end
  16. defp pleroma_rebooted? do
  17. if Restarter.Pleroma.rebooted?() do
  18. :ok
  19. else
  20. Process.sleep(10)
  21. pleroma_rebooted?()
  22. end
  23. end
  24. def load_pleroma do
  25. Application.load(:pleroma)
  26. end
  27. def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
  28. Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
  29. end
  30. def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
  31. prompt_message = "#{prompt} [#{defname || defval}] "
  32. input =
  33. if mix_shell?(),
  34. do: Mix.shell().prompt(prompt_message),
  35. else: :io.get_line(prompt_message)
  36. case input do
  37. "\n" ->
  38. case defval do
  39. nil ->
  40. shell_prompt(prompt, defval, defname)
  41. defval ->
  42. defval
  43. end
  44. input ->
  45. String.trim(input)
  46. end
  47. end
  48. def shell_yes?(message) do
  49. if mix_shell?(),
  50. do: Mix.shell().yes?("Continue?"),
  51. else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
  52. end
  53. def shell_info(message) do
  54. if mix_shell?(),
  55. do: Mix.shell().info(message),
  56. else: IO.puts(message)
  57. end
  58. def shell_error(message) do
  59. if mix_shell?(),
  60. do: Mix.shell().error(message),
  61. else: IO.puts(:stderr, message)
  62. end
  63. @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
  64. def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
  65. def escape_sh_path(path) do
  66. ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  67. end
  68. end