1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.Common do
6 @doc "Common functions to be reused in mix tasks"
8 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
9 {:ok, _} = Application.ensure_all_started(:pleroma)
12 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
13 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
16 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
17 prompt_message = "#{prompt} [#{defname || defval}]"
21 do: Mix.shell().prompt(prompt_message),
22 else: :io.get_line(prompt_message)
28 shell_prompt(prompt, defval, defname)
39 def shell_yes?(message) do
41 do: Mix.shell().yes?("Continue?"),
42 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
45 def shell_info(message) do
47 do: Mix.shell().info(message),
48 else: IO.puts(message)
51 def shell_error(message) do
53 do: Mix.shell().error(message),
54 else: IO.puts(:stderr, message)
57 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
58 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
60 def escape_sh_path(path) do
61 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')