absinthe.plug.graphiql.assets.download.ex (890B)
1 defmodule Mix.Tasks.Absinthe.Plug.Graphiql.Assets.Download do 2 use Mix.Task 3 4 @shortdoc "Download GraphiQL assets" 5 6 def run(args) do 7 Mix.Absinthe.Plug.GraphiQL.AssetsTask.run(args) 8 9 {:ok, _} = Application.ensure_all_started(:inets) 10 {:ok, _} = Application.ensure_all_started(:ssl) 11 12 Absinthe.Plug.GraphiQL.Assets.get_remote_asset_mappings() 13 |> Enum.map(&download_file/1) 14 end 15 16 defp download_file({destination, asset_url}) do 17 {:ok, response} = http_get(asset_url) 18 19 case response do 20 {{_, http_code, _}, _, body} when http_code in [200] -> 21 Mix.Generator.create_file(destination, body, force: true) 22 23 _ -> 24 Mix.raise(""" 25 Something went wrong downloading #{asset_url} Please try again. 26 """) 27 end 28 end 29 30 defp http_get(url), 31 do: :httpc.request(:get, {String.to_charlist(url), []}, [], body_format: :binary) 32 end