Skip to content

Commit 490b493

Browse files
authored
Merge pull request #34 from andrewvy/feature/add-automated-fetch
Add `mix fetch_cdp_protocol` for fetching & updating new protocols.
2 parents 8c01f5b + 73c70aa commit 490b493

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defmodule Mix.Tasks.FetchCdpProtocol do
2+
@moduledoc """
3+
Fetches up-to-date versions of all the Chrome Debugger Protocol files.
4+
5+
These protocol files are stored in the private storage of this library.
6+
"""
7+
8+
use Mix.Task
9+
10+
@shortdoc "Fetches up-to-date versions of all the Chrome Debugger Protocol files."
11+
12+
@protocol_sources %{
13+
"1-2" => %{
14+
url:
15+
"https://raw.githubusercontent.com/ChromeDevTools/debugger-protocol-viewer/master/_data/1-2/protocol.json",
16+
output: "priv/1-2/protocol.json"
17+
},
18+
"1-3" => %{
19+
url:
20+
"https://github.com/ChromeDevTools/debugger-protocol-viewer/blob/master/_data/1-3/protocol.json",
21+
output: "priv/1-3/protocol.json"
22+
},
23+
"tot" => %{
24+
url:
25+
"https://raw.githubusercontent.com/ChromeDevTools/debugger-protocol-viewer/master/_data/tot/protocol.json",
26+
output: "priv/tot/protocol.json"
27+
}
28+
}
29+
30+
@temporary_file "protocol.json"
31+
32+
@impl true
33+
def run(_) do
34+
Map.keys(@protocol_sources)
35+
|> Enum.each(&fetch_protocol/1)
36+
end
37+
38+
def fetch_protocol(version) do
39+
protocol_source = Map.fetch!(@protocol_sources, version)
40+
41+
cmd!("wget #{protocol_source.url}")
42+
File.rename(@temporary_file, protocol_source.output)
43+
end
44+
45+
defp cmd!(cmd) do
46+
Mix.shell().info([:magenta, "Running: #{cmd}"])
47+
48+
exit_status = Mix.shell().cmd(cmd)
49+
50+
if exit_status != 0 do
51+
Mix.raise("Non-zero result (#{exit_status}) from command: #{cmd}")
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)