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