@@ -26,14 +26,17 @@ defmodule Diff.Hex do
2626 end
2727
2828 def get_tarball ( package , version ) do
29- with { :ok , { 200 , _ , tarball } } <- :hex_repo . get_tarball ( @ config , package , version ) do
30- { :ok , tarball }
31- else
32- { :ok , { 403 , _ , _ } } ->
29+ path = Diff.TmpDir . tmp_file ( "tarball" )
30+
31+ case :hex_repo . get_tarball_to_file ( @ config , package , version , to_charlist ( path ) ) do
32+ { :ok , { 200 , _headers } } ->
33+ { :ok , path }
34+
35+ { :ok , { 403 , _ } } ->
3336 { :error , :not_found }
3437
35- { :ok , { status , _ , _ } } ->
36- Logger . error ( "Failed to get package versions . Status: #{ status } ." )
38+ { :ok , { status , _ } } ->
39+ Logger . error ( "Failed to get tarball for package: #{ package } . Status: #{ status } ." )
3740 { :error , :not_found }
3841
3942 { :error , reason } ->
@@ -42,10 +45,9 @@ defmodule Diff.Hex do
4245 end
4346 end
4447
45- def unpack_tarball ( tarball , path ) when is_binary ( path ) do
46- path = to_charlist ( path )
47-
48- with { :ok , _ } <- :hex_tarball . unpack ( tarball , path ) do
48+ def unpack_tarball ( tarball_path , output_path ) do
49+ with { :ok , _ } <-
50+ :hex_tarball . unpack ( { :file , to_charlist ( tarball_path ) } , to_charlist ( output_path ) ) do
4951 :ok
5052 end
5153 end
@@ -73,12 +75,11 @@ defmodule Diff.Hex do
7375 end
7476
7577 def diff ( package , from , to ) do
76- path_from = tmp_path ( "package-#{ package } -#{ from } -" )
77- path_to = tmp_path ( "package-#{ package } -#{ to } -" )
78-
7978 with { :ok , tarball_from } <- get_tarball ( package , from ) ,
79+ path_from = Diff.TmpDir . tmp_dir ( "package-#{ package } -#{ from } " ) ,
8080 :ok <- unpack_tarball ( tarball_from , path_from ) ,
8181 { :ok , tarball_to } <- get_tarball ( package , to ) ,
82+ path_to = Diff.TmpDir . tmp_dir ( "package-#{ package } -#{ to } " ) ,
8283 :ok <- unpack_tarball ( tarball_to , path_to ) do
8384 from_files = tree_files ( path_from )
8485 to_files = tree_files ( path_to )
@@ -92,8 +93,7 @@ defmodule Diff.Hex do
9293 all_files = ( from_files ++ to_files ) |> Enum . uniq ( ) |> Enum . sort ( )
9394
9495 stream =
95- all_files
96- |> Stream . flat_map ( fn file ->
96+ Stream . flat_map ( all_files , fn file ->
9797 { path_old , path_new } =
9898 cond do
9999 file in new_files -> { "/dev/null" , Path . join ( path_to , file ) }
@@ -120,14 +120,6 @@ defmodule Diff.Hex do
120120 [ { :error , { error , reason } } ]
121121 end
122122 end )
123- |> Stream . transform (
124- fn -> :ok end ,
125- fn elem , :ok -> { [ elem ] , :ok } end ,
126- fn :ok ->
127- File . rm_rf ( path_from )
128- File . rm_rf ( path_to )
129- end
130- )
131123
132124 { :ok , stream }
133125 else
@@ -166,9 +158,4 @@ defmodule Diff.Hex do
166158 |> Enum . filter ( & File . regular? ( & 1 , raw: true ) )
167159 |> Enum . map ( & Path . relative_to ( & 1 , directory ) )
168160 end
169-
170- defp tmp_path ( prefix ) do
171- random_string = Base . encode16 ( :crypto . strong_rand_bytes ( 4 ) )
172- Path . join ( [ System . tmp_dir! ( ) , "diff" , prefix <> random_string ] )
173- end
174161end
0 commit comments