553c74c25caab3c39ec9e90b110aaee2e62b7c81
[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 @apps [:restarter, :ecto, :ecto_sql, :postgrex, :db_connection, :cachex]
7 @cachex_childs ["object", "user"]
8 @doc "Common functions to be reused in mix tasks"
9 def start_pleroma do
10 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
11
12 if Pleroma.Config.get(:env) != :test do
13 Application.put_env(:logger, :console, level: :debug)
14 end
15
16 apps =
17 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
18 [:gun | @apps]
19 else
20 [:hackney | @apps]
21 end
22
23 Enum.each(apps, &Application.ensure_all_started/1)
24
25 childs = [Pleroma.Repo, Pleroma.Config.TransferTask, Pleroma.Web.Endpoint]
26
27 cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, []))
28
29 Supervisor.start_link(childs ++ cachex_childs,
30 strategy: :one_for_one,
31 name: Pleroma.Supervisor
32 )
33
34 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
35 pleroma_rebooted?()
36 end
37 end
38
39 defp pleroma_rebooted? do
40 if Restarter.Pleroma.rebooted?() do
41 :ok
42 else
43 Process.sleep(10)
44 pleroma_rebooted?()
45 end
46 end
47
48 def load_pleroma do
49 Application.load(:pleroma)
50 end
51
52 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
53 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
54 end
55
56 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
57 prompt_message = "#{prompt} [#{defname || defval}] "
58
59 input =
60 if mix_shell?(),
61 do: Mix.shell().prompt(prompt_message),
62 else: :io.get_line(prompt_message)
63
64 case input do
65 "\n" ->
66 case defval do
67 nil ->
68 shell_prompt(prompt, defval, defname)
69
70 defval ->
71 defval
72 end
73
74 input ->
75 String.trim(input)
76 end
77 end
78
79 def shell_yes?(message) do
80 if mix_shell?(),
81 do: Mix.shell().yes?("Continue?"),
82 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
83 end
84
85 def shell_info(message) do
86 if mix_shell?(),
87 do: Mix.shell().info(message),
88 else: IO.puts(message)
89 end
90
91 def shell_error(message) do
92 if mix_shell?(),
93 do: Mix.shell().error(message),
94 else: IO.puts(:stderr, message)
95 end
96
97 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
98 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
99
100 def escape_sh_path(path) do
101 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
102 end
103 end