@@ -359,22 +359,32 @@ defmodule Mix.Utils do
359
359
Used by tasks like `archive.install` and `local.rebar` that support
360
360
installation either from a URL or a local file.
361
361
362
- Raises if the given path is not a url, nor a file or if the
363
- file or url are invalid.
362
+ Raises if the given path is not a URL, nor a file or if the
363
+ file or URL are invalid.
364
+
365
+ ## Options
366
+
367
+ * `:shell` - Forces the use of `wget` or `curl` to fetch the file if the
368
+ given path is a URL.
364
369
"""
365
- def read_path! ( path ) do
370
+ def read_path! ( path , opts \\ [ ] ) do
366
371
cond do
367
- url? ( path ) -> read_url ( path )
368
- file? ( path ) -> read_file ( path )
369
- :else -> Mix . raise "Expected #{ path } to be a url or a local file path"
372
+ url? ( path ) && opts [ :shell ] ->
373
+ read_shell ( path )
374
+ url? ( path ) ->
375
+ read_httpc ( path )
376
+ file? ( path ) ->
377
+ read_file ( path )
378
+ true ->
379
+ Mix . raise "Expected #{ path } to be a url or a local file path"
370
380
end
371
381
end
372
382
373
383
defp read_file ( path ) do
374
384
File . read! ( path )
375
385
end
376
386
377
- defp read_url ( path ) do
387
+ defp read_httpc ( path ) do
378
388
{ :ok , _ } = Application . ensure_all_started ( :ssl )
379
389
{ :ok , _ } = Application . ensure_all_started ( :inets )
380
390
@@ -420,6 +430,33 @@ defmodule Mix.Utils do
420
430
end
421
431
end
422
432
433
+ defp read_shell ( path ) do
434
+ filename = URI . parse ( path ) . path |> Path . basename
435
+ out_path = Path . join ( System . tmp_dir! , filename )
436
+ File . rm ( out_path )
437
+
438
+ cond do
439
+ System . find_executable ( "wget" ) ->
440
+ Mix . shell . cmd ( ~s( wget -O "#{ out_path } " "#{ path } ") )
441
+ System . find_executable ( "curl" ) ->
442
+ Mix . shell . cmd ( ~s( curl -L -o "#{ out_path } " "#{ path } ") )
443
+ windows? && System . find_executable ( "powershell" ) ->
444
+ command = ~s[ $client = new-object System.Net.WebClient; ] <>
445
+ ~s[ $client.DownloadFile(\\ "#{ path } \\ ", \\ "#{ out_path } \\ ")]
446
+ Mix . shell . cmd ( ~s[ powershell -Command "& {#{ command } }"] )
447
+ true ->
448
+ Mix . raise "wget or curl not installed, download manually: #{ path } "
449
+ end
450
+
451
+ data = File . read! ( out_path )
452
+ File . rm! ( out_path )
453
+ data
454
+ end
455
+
456
+ def windows? do
457
+ match? ( { :win32 , _ } , :os . type )
458
+ end
459
+
423
460
defp file? ( path ) do
424
461
File . regular? ( path )
425
462
end
0 commit comments