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