Merge branch 'develop' into feature/admin-api-render-whole-status
[akkoma] / lib / mix / pleroma.ex
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
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 end
16
17 def load_pleroma do
18 Application.load(:pleroma)
19 end
20
21 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
22 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
23 end
24
25 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
26 prompt_message = "#{prompt} [#{defname || defval}] "
27
28 input =
29 if mix_shell?(),
30 do: Mix.shell().prompt(prompt_message),
31 else: :io.get_line(prompt_message)
32
33 case input do
34 "\n" ->
35 case defval do
36 nil ->
37 shell_prompt(prompt, defval, defname)
38
39 defval ->
40 defval
41 end
42
43 input ->
44 String.trim(input)
45 end
46 end
47
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
54 def shell_info(message) do
55 if mix_shell?(),
56 do: Mix.shell().info(message),
57 else: IO.puts(message)
58 end
59
60 def shell_error(message) do
61 if mix_shell?(),
62 do: Mix.shell().error(message),
63 else: IO.puts(:stderr, message)
64 end
65
66 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
67 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
68
69 def escape_sh_path(path) do
70 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
71 end
72 end