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