Merge branch 'develop' into issue/1276
[akkoma] / lib / mix / pleroma.ex
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
5 defmodule Mix.Pleroma do
6 @doc "Common functions to be reused in mix tasks"
7 def start_pleroma do
8 Mix.Task.run("app.start")
9 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
10
11 if Pleroma.Config.get(:env) != :test do
12 Application.put_env(:logger, :console, level: :debug)
13 end
14
15 {:ok, _} = Application.ensure_all_started(:pleroma)
16
17 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
18 pleroma_rebooted?()
19 end
20 end
21
22 defp pleroma_rebooted? do
23 if Restarter.Pleroma.rebooted?() do
24 :ok
25 else
26 Process.sleep(10)
27 pleroma_rebooted?()
28 end
29 end
30
31 def load_pleroma do
32 Application.load(:pleroma)
33 end
34
35 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
36 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
37 end
38
39 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
40 prompt_message = "#{prompt} [#{defname || defval}] "
41
42 input =
43 if mix_shell?(),
44 do: Mix.shell().prompt(prompt_message),
45 else: :io.get_line(prompt_message)
46
47 case input do
48 "\n" ->
49 case defval do
50 nil ->
51 shell_prompt(prompt, defval, defname)
52
53 defval ->
54 defval
55 end
56
57 input ->
58 String.trim(input)
59 end
60 end
61
62 def shell_yes?(message) do
63 if mix_shell?(),
64 do: Mix.shell().yes?("Continue?"),
65 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
66 end
67
68 def shell_info(message) do
69 if mix_shell?(),
70 do: Mix.shell().info(message),
71 else: IO.puts(message)
72 end
73
74 def shell_error(message) do
75 if mix_shell?(),
76 do: Mix.shell().error(message),
77 else: IO.puts(:stderr, message)
78 end
79
80 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
81 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
82
83 def escape_sh_path(path) do
84 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
85 end
86 end