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