diff --git a/lib/deps_nix.ex b/lib/deps_nix.ex index f1025a0..2193994 100644 --- a/lib/deps_nix.ex +++ b/lib/deps_nix.ex @@ -68,7 +68,7 @@ defmodule DepsNix do :ok <- File.write("#{dir}/deps-nix-tarball", body), {_, 0} <- System.cmd("tar", ["-xf", "#{dir}/deps-nix-tarball"], cd: dir), {output, 0} <- System.cmd("nix", ["hash", "path", path]) do - String.trim_trailing(output) + {String.trim_trailing(output), find_builder_from_path(path)} end end @@ -124,6 +124,14 @@ defmodule DepsNix do end end + defp find_builder_from_path(path) do + cond do + File.exists?(Path.join(path, "mix.exs")) -> "buildMix" + File.exists?(Path.join(path, "rebar.config")) -> "buildRebar3" + true -> :unknown + end + end + @spec to_opts({list(), any(), any()}) :: Options.t() defp to_opts({[], _, _}) do %Options{ diff --git a/lib/deps_nix/derivation.ex b/lib/deps_nix/derivation.ex index 8edcd7c..9169536 100644 --- a/lib/deps_nix/derivation.ex +++ b/lib/deps_nix/derivation.ex @@ -48,21 +48,28 @@ defmodule DepsNix.Derivation do def from(%Mix.Dep{scm: Mix.SCM.Git} = dep, options) do {:git, url, rev, _} = dep.opts[:lock] - prefetcher = options.github_prefetcher - case parse_git_url(url) do [owner: owner, repo: repo] -> + prefetcher = options.github_prefetcher + + {hash, builder} = + if prefetcher do + prefetcher.(owner, repo, rev) + else + {"", "buildMix"} + end + fetcher = %FetchFromGitHub{ owner: owner, repo: repo, rev: rev, - hash: if(prefetcher, do: prefetcher.(owner, repo, rev), else: "") + hash: hash } new(dep, version: rev, src: fetcher, - builder: "buildMix", + builder: builder, app_config_path: app_config_path(options) ) diff --git a/test/deps_nix/derivation_test.exs b/test/deps_nix/derivation_test.exs index 44aef34..7a5b992 100644 --- a/test/deps_nix/derivation_test.exs +++ b/test/deps_nix/derivation_test.exs @@ -74,7 +74,7 @@ defmodule DepsNix.DerivationTest do Derivation.from(dep, %DepsNix.Options{ github_prefetcher: fn "code-supply", "mudbrick", ^rev -> - generated_hash + {generated_hash, "buildMix"} end }) end diff --git a/test/deps_nix_test.exs b/test/deps_nix_test.exs index 2170f2c..522b8f0 100644 --- a/test/deps_nix_test.exs +++ b/test/deps_nix_test.exs @@ -10,7 +10,7 @@ defmodule DepsNixTest do "code-supply", "deps_nix", "8a6c3537c958fe3fd1810d56bdee6c13fb35d089" - ) == "sha256-zJOkGOSBBA0Y9HPRmwPmBpqaqsoRa0oR7VjMMyukvX4=" + ) == {"sha256-zJOkGOSBBA0Y9HPRmwPmBpqaqsoRa0oR7VjMMyukvX4=", "buildMix"} end test "works for repos with ../ relative symlinks" do @@ -18,7 +18,23 @@ defmodule DepsNixTest do "Strech", "avrora", "a2df4d8f177dacc7be24aa3e6bc76b52c3f114a9" - ) == "sha256-msktKtQGBhe2UrZr9uiKiRFCiXCkFa0+zbOy8KQIhc4=" + ) == {"sha256-msktKtQGBhe2UrZr9uiKiRFCiXCkFa0+zbOy8KQIhc4=", "buildMix"} + end + + test "detects rebar3-only repositories" do + assert DepsNix.github_prefetcher( + "klarna", + "mnesia_eleveldb", + "af6d0556a78aec2918b3471f0c85121402a1f5b1" + ) == {"sha256-+ZZ5Uyoe/HK0wL0ev1vn9Tuiaps4X88izETtuRszKYE=", "buildRebar3"} + end + + test "prefers buildMix over buildRebar3 for repos that have both" do + assert DepsNix.github_prefetcher( + "erlef", + "oidcc", + "fdf45b06d79813c7110171c5c6d334394c2f1190" + ) == {"sha256-QUwRH9GWMTrG2HsFqiPNNKm+K5+cwPigdtlx9OUNK58=", "buildMix"} end test "fails for nonsense repos" do