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.

73 lines
1.8KB

  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. 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. end
  13. def load_pleroma do
  14. Application.load(:pleroma)
  15. end
  16. def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
  17. Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
  18. end
  19. def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
  20. prompt_message = "#{prompt} [#{defname || defval}] "
  21. input =
  22. if mix_shell?(),
  23. do: Mix.shell().prompt(prompt_message),
  24. else: :io.get_line(prompt_message)
  25. case input do
  26. "\n" ->
  27. case defval do
  28. nil ->
  29. shell_prompt(prompt, defval, defname)
  30. defval ->
  31. defval
  32. end
  33. input ->
  34. String.trim(input)
  35. end
  36. end
  37. def shell_yes?(message) do
  38. if mix_shell?(),
  39. do: Mix.shell().yes?("Continue?"),
  40. else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
  41. end
  42. def shell_info(message) do
  43. if mix_shell?(),
  44. do: Mix.shell().info(message),
  45. else: IO.puts(message)
  46. end
  47. def shell_error(message) do
  48. if mix_shell?(),
  49. do: Mix.shell().error(message),
  50. else: IO.puts(:stderr, message)
  51. end
  52. @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
  53. def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
  54. def escape_sh_path(path) do
  55. ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  56. end
  57. end