File: process.ex

package info (click to toggle)
erlang-hex 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,204 kB
  • sloc: erlang: 2,950; sh: 203; makefile: 10
file content (76 lines) | stat: -rw-r--r-- 1,652 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
defmodule Hex.Shell.Process do
  @moduledoc false

  @behaviour Mix.Shell

  def flush(callback \\ fn x -> x end) do
    receive do
      {:mix_shell, _, _} = message ->
        callback.(message)
        flush(callback)

      {:mix_shell_input, _, _} = message ->
        callback.(message)
        flush(callback)
    after
      0 -> :done
    end
  end

  def print_app do
    if name = Mix.Shell.printable_app_name() do
      send(process(), {:mix_shell, :info, ["==> #{name}"]})
    end
  end

  def info(message) do
    print_app()
    send(process(), {:mix_shell, :info, [format(message)]})
    :ok
  end

  def error(message) do
    print_app()
    send(process(), {:mix_shell, :error, [format(message)]})
    :ok
  end

  defp format(message) do
    message |> IO.ANSI.format(false) |> IO.iodata_to_binary()
  end

  def prompt(message) do
    print_app()
    send(process(), {:mix_shell, :prompt, [message]})

    receive do
      {:mix_shell_input, :prompt, response} -> response
    after
      0 -> raise "no shell process input given for prompt/1"
    end
  end

  def yes?(message, _options \\ []) do
    print_app()
    send(process(), {:mix_shell, :yes?, [message]})

    receive do
      {:mix_shell_input, :yes?, response} -> response
    after
      0 -> raise "no shell process input given for yes?/1"
    end
  end

  def cmd(command, opts \\ []) do
    print_app? = Keyword.get(opts, :print_app, true)

    Hex.Shell.cmd(command, opts, fn data ->
      if print_app?, do: print_app()
      send(process(), {:mix_shell, :run, [data]})
    end)
  end

  defp process() do
    Hex.State.fetch!(:shell_process) || self()
  end
end