-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlightning_css.ex
110 lines (90 loc) · 2.83 KB
/
lightning_css.ex
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
defmodule LightningCSS do
@moduledoc "README.md" |> File.read!() |> String.split("<!-- MDOC !-->") |> Enum.fetch!(1)
use Application
require Logger
def start(_, _) do
if !LightningCSS.Versions.configured() do
Logger.warning("""
lightning_css version is not configured. Please set it in your config files:
config :lightning_css, :version, "#{LightningCSS.Versions.latest()}"
""")
end
configured_version = LightningCSS.Versions.to_use()
case LightningCSS.Versions.bin() do
{:ok, ^configured_version} ->
:ok
{:ok, version} ->
Logger.warning("""
Outdated lightning_css version. Expected #{configured_version}, got #{version}. \
Please run `mix lightning_css.install` or update the version in your config files.\
""")
:error ->
:ok
end
Supervisor.start_link([], strategy: :one_for_one, name: __MODULE__.Supervisor)
end
@doc """
Runs the given command with `args`.
The given args will be appended to the configured args.
The task output will be streamed directly to stdio. It
returns the status of the underlying call.
"""
@spec run(atom(), list(), Keyword.t()) :: :ok | {:error, {:exited, integer()}}
def run(profile, extra_args, opts) when is_atom(profile) and is_list(extra_args) do
watch = Keyword.get(opts, :watch, false)
id =
([profile] ++ extra_args ++ [watch]) |> Enum.map_join("_", &to_string/1) |> String.to_atom()
ref =
__MODULE__.Supervisor
|> Supervisor.start_child(
Supervisor.child_spec(
{LightningCSS.Runner,
%{
profile: profile,
extra_args: extra_args,
watch: watch
}},
id: id,
restart: :transient
)
)
|> case do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
|> Process.monitor()
receive do
{:DOWN, ^ref, _, _, {:error_and_no_watch, code}} ->
{:error, {:exited, code}}
_ ->
:ok
end
end
@doc """
Installs, if not available, and then runs `lightning_css`.
Returns the same as `run/2`.
"""
@spec install_and_run(atom(), list(), Keyword.t()) :: integer()
def install_and_run(profile, args, opts \\ []) do
File.exists?(LightningCSS.Paths.bin()) || start_unique_install_worker()
run(profile, args, opts)
end
defp start_unique_install_worker do
ref =
__MODULE__.Supervisor
|> Supervisor.start_child(
Supervisor.child_spec({Task, &LightningCSS.Installer.install/0},
restart: :transient,
id: __MODULE__.Installer
)
)
|> case do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
|> Process.monitor()
receive do
{:DOWN, ^ref, _, _, _} -> :ok
end
end
end