Skip to content

Commit

Permalink
Add optional dependency load
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer committed Aug 12, 2018
1 parent 1c7a2f5 commit 886900d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
53 changes: 49 additions & 4 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule Pow.MixProject do
app: :pow,
version: @version,
elixir: "~> 1.6",
elixirc_paths: elixirc_paths(Mix.env()),
elixirc_paths: elixirc_paths(Mix.env(), optional_deps()),
start_permanent: Mix.env() == :prod,
compilers: [:phoenix] ++ Mix.compilers(),
compilers: compilers(optional_deps()),
deps: deps(),

# Hex
Expand Down Expand Up @@ -49,8 +49,53 @@ defmodule Pow.MixProject do
]
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
def elixirc_paths(:test, _optional_deps), do: ["lib", "test/support"]
def elixirc_paths(_, optional_deps) do
case optional_deps_missing?(optional_deps) do
true -> paths_without_missing_optional_deps(optional_deps)
false -> ["lib"]
end
end

def compilers(optional_deps) do
case phoenix_missing?(optional_deps) do
true -> [:phoenix] ++ Mix.compilers
_ -> Mix.compilers()
end
end

defp phoenix_missing?(optional_deps) do
Keyword.get(optional_deps, :phoenix)
end

defp optional_deps_missing?(optional_deps) do
not Enum.empty?(optional_deps_missing(optional_deps))
end

defp optional_deps_missing(optional_deps) do
Enum.reject(optional_deps, &elem(&1, 1))
end

defp optional_deps do
for dep <- [:phoenix, :ecto, :plug] do
case Mix.ProjectStack.peek() do
%{config: config} -> {dep, Keyword.has_key?(config[:deps], :phoenix)}
_ -> {dep, true}
end
end
end

defp paths_without_missing_optional_deps(optional_deps) do
deps = optional_deps_missing(optional_deps)

"lib/**/*.ex"
|> Path.wildcard()
|> Enum.reject(&reject_deps_path?(deps, &1))
end

defp reject_deps_path?(deps, path) do
Enum.any?(deps, &String.contains?(path, "/#{elem(&1, 0)}"))
end

defp package do
[
Expand Down
34 changes: 34 additions & 0 deletions test/mix_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule Pow.MixTest do
use ExUnit.Case

alias Pow.MixProject

test "elixirc_paths/1" do
assert MixProject.elixirc_paths(:test, []) == ["lib", "test/support"]
assert MixProject.elixirc_paths(:test, [phoenix: false]) == ["lib", "test/support"]

assert MixProject.elixirc_paths(:dev, []) == ["lib"]

assert paths = MixProject.elixirc_paths(:dev, [phoenix: false])
refute some_paths?(paths, "/phoenix")
assert some_paths?(paths, "/ecto")
assert some_paths?(paths, "/plug")

assert paths = MixProject.elixirc_paths(:dev, [ecto: false, plug: false])
assert some_paths?(paths, "/phoenix")
refute some_paths?(paths, "/ecto")
refute some_paths?(paths, "/plug")
end

test "compilers/1" do
compilers = [:yecc, :leex, :erlang, :elixir, :xref, :app]

assert MixProject.compilers([]) == compilers
assert MixProject.compilers([{:phoenix, false}]) == compilers
assert MixProject.compilers([{:phoenix, true}]) == [:phoenix] ++ compilers
end

defp some_paths?(paths, path) do
Enum.any?(paths, &(String.contains?(&1, path)))
end
end

0 comments on commit 886900d

Please sign in to comment.