2019-01-03 11:34:31 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/mix/tasks/pleroma/common.ex
|
|
|
|
|
|
|
|
defmodule Mix.Tasks.Mobilizon.Common do
|
2019-09-22 16:26:23 +02:00
|
|
|
@moduledoc """
|
|
|
|
Common functions to be reused in mix tasks
|
|
|
|
"""
|
2021-05-23 18:49:12 +02:00
|
|
|
require Logger
|
2019-01-03 11:34:31 +01:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
def start_mobilizon do
|
2021-03-11 19:50:54 +01:00
|
|
|
if mix_task?(), do: Mix.Task.run("app.config")
|
2021-05-23 18:49:12 +02:00
|
|
|
|
2021-06-04 16:32:26 +02:00
|
|
|
unless System.get_env("DEBUG") || Application.fetch_env!(:mobilizon, :env) == :test do
|
2021-05-23 18:49:12 +02:00
|
|
|
Logger.configure(level: :error)
|
|
|
|
end
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
|
|
|
|
|
|
|
|
{:ok, _} = Application.ensure_all_started(:mobilizon)
|
|
|
|
end
|
|
|
|
|
2019-01-03 11:34:31 +01:00
|
|
|
def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
|
2020-10-30 15:16:01 +01:00
|
|
|
Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
|
|
|
|
end
|
|
|
|
|
|
|
|
def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
|
|
|
|
prompt_message = "#{prompt} [#{defname || defval}] "
|
2019-01-03 11:34:31 +01:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
input =
|
|
|
|
if mix_shell?(),
|
|
|
|
do: Mix.shell().prompt(prompt_message),
|
|
|
|
else: :io.get_line(prompt_message)
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
case input do
|
|
|
|
"\n" ->
|
|
|
|
case defval do
|
|
|
|
nil ->
|
|
|
|
shell_prompt(prompt, defval, defname)
|
2019-01-03 11:34:31 +01:00
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
defval ->
|
|
|
|
defval
|
|
|
|
end
|
|
|
|
|
|
|
|
input ->
|
|
|
|
String.trim(input)
|
|
|
|
end
|
2019-01-03 11:34:31 +01:00
|
|
|
end
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
def shell_yes?(message) do
|
|
|
|
if mix_shell?(),
|
|
|
|
do: Mix.shell().yes?("Continue?"),
|
|
|
|
else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
|
|
|
|
end
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
@spec shell_info(String.t()) :: :ok
|
2020-10-30 15:16:01 +01:00
|
|
|
def shell_info(message) do
|
|
|
|
if mix_shell?(),
|
|
|
|
do: Mix.shell().info(message),
|
|
|
|
else: IO.puts(message)
|
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
@spec shell_error(String.t()) :: :ok
|
2021-07-20 18:22:18 +02:00
|
|
|
def shell_error(message, options \\ []) do
|
|
|
|
if mix_shell?() do
|
|
|
|
Mix.shell().error(message)
|
|
|
|
else
|
|
|
|
IO.puts(:stderr, message)
|
|
|
|
end
|
|
|
|
|
|
|
|
if Application.fetch_env!(:mobilizon, :env) != :test do
|
|
|
|
exit({:shutdown, Keyword.get(options, :error_code, 1)})
|
|
|
|
end
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
|
|
|
|
2020-10-30 15:16:01 +01:00
|
|
|
@doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
|
|
|
|
def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
|
|
|
|
|
2021-03-11 19:50:54 +01:00
|
|
|
def mix_task?, do: :erlang.function_exported(Mix.Task, :run, 1)
|
|
|
|
|
2019-01-03 11:34:31 +01:00
|
|
|
def escape_sh_path(path) do
|
|
|
|
~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
|
|
|
|
end
|
2020-10-30 15:16:01 +01:00
|
|
|
|
|
|
|
@type task_module :: atom
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets the shortdoc for the given task `module`.
|
|
|
|
Returns the shortdoc or `nil`.
|
|
|
|
"""
|
|
|
|
@spec shortdoc(task_module) :: String.t() | nil
|
|
|
|
def shortdoc(module) when is_atom(module) do
|
|
|
|
case List.keyfind(module.__info__(:attributes), :shortdoc, 0) do
|
|
|
|
{:shortdoc, [shortdoc]} -> shortdoc
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_subtasks_for_module(module_name) do
|
|
|
|
tasks = list_subtasks_for_module(module_name)
|
|
|
|
|
|
|
|
max = Enum.reduce(tasks, 0, fn {name, _doc}, acc -> max(byte_size(name), acc) end)
|
|
|
|
|
|
|
|
Enum.each(tasks, fn {name, doc} ->
|
|
|
|
shell_info("#{String.pad_trailing(name, max + 2)} # #{doc}")
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec list_subtasks_for_module(atom()) :: list({String.t(), String.t()})
|
|
|
|
def list_subtasks_for_module(module_name) do
|
|
|
|
Application.load(:mobilizon)
|
|
|
|
{:ok, modules} = :application.get_key(:mobilizon, :modules)
|
|
|
|
module_name = to_string(module_name)
|
|
|
|
|
|
|
|
modules
|
|
|
|
|> Enum.filter(fn module ->
|
|
|
|
String.starts_with?(to_string(module), to_string(module_name)) &&
|
|
|
|
to_string(module) != to_string(module_name)
|
|
|
|
end)
|
|
|
|
|> Enum.map(&format_module/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp format_module(module) do
|
|
|
|
{format_name(to_string(module)), shortdoc(module)}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp format_name("Elixir.Mix.Tasks.Mobilizon." <> task_name) do
|
|
|
|
String.downcase(task_name)
|
|
|
|
end
|
2019-01-03 11:34:31 +01:00
|
|
|
end
|