0fbb6f1cdde0842eefbb892deb308e6fef641656
[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 = [
26 Pleroma.Repo,
27 {Pleroma.Config.TransferTask, false},
28 Pleroma.Web.Endpoint
29 ]
30
31 cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, []))
32
33 Supervisor.start_link(childs ++ cachex_childs,
34 strategy: :one_for_one,
35 name: Pleroma.Supervisor
36 )
37
38 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
39 pleroma_rebooted?()
40 end
41 end
42
43 defp pleroma_rebooted? do
44 if Restarter.Pleroma.rebooted?() do
45 :ok
46 else
47 Process.sleep(10)
48 pleroma_rebooted?()
49 end
50 end
51
52 def load_pleroma do
53 Application.load(:pleroma)
54 end
55
56 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
57 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
58 end
59
60 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
61 prompt_message = "#{prompt} [#{defname || defval}] "
62
63 input =
64 if mix_shell?(),
65 do: Mix.shell().prompt(prompt_message),
66 else: :io.get_line(prompt_message)
67
68 case input do
69 "\n" ->
70 case defval do
71 nil ->
72 shell_prompt(prompt, defval, defname)
73
74 defval ->
75 defval
76 end
77
78 input ->
79 String.trim(input)
80 end
81 end
82
83 def shell_yes?(message) do
84 if mix_shell?(),
85 do: Mix.shell().yes?("Continue?"),
86 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
87 end
88
89 def shell_info(message) do
90 if mix_shell?(),
91 do: Mix.shell().info(message),
92 else: IO.puts(message)
93 end
94
95 def shell_error(message) do
96 if mix_shell?(),
97 do: Mix.shell().error(message),
98 else: IO.puts(:stderr, message)
99 end
100
101 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
102 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
103
104 def escape_sh_path(path) do
105 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
106 end
107 end