|
| 1 | +# Module responsible for fetching (getting/updating) |
| 2 | +# dependencies from their sources. |
| 3 | +# |
| 4 | +# The new_lock and old_lock mechanism exists to signal |
| 5 | +# externally which dependencies need to be updated and |
| 6 | +# which ones do not. |
| 7 | +defmodule Mix.Deps.Fetcher do |
| 8 | + @moduledoc false |
| 9 | + |
| 10 | + import Mix.Deps, only: [format_dep: 1, check_lock: 2, out_of_date?: 1, available?: 1, ok?: 1] |
| 11 | + |
| 12 | + @doc """ |
| 13 | + Fetches all dependencies. |
| 14 | + """ |
| 15 | + def all(old_lock, new_lock, opts) do |
| 16 | + do_finalize Mix.Deps.unloaded({ [], new_lock }, &do_fetch/2), old_lock, opts |
| 17 | + end |
| 18 | + |
| 19 | + @doc """ |
| 20 | + Fetches the dependencies with the given names and their children recursively. |
| 21 | + """ |
| 22 | + def by_name(names, old_lock, new_lock, opts) do |
| 23 | + do_finalize Mix.Deps.unloaded_by_name(names, { [], new_lock }, &do_fetch/2), old_lock, opts |
| 24 | + end |
| 25 | + |
| 26 | + defp do_fetch(dep, { acc, lock }) do |
| 27 | + Mix.Dep[app: app, scm: scm, opts: opts] = dep = check_lock(dep, lock) |
| 28 | + |
| 29 | + cond do |
| 30 | + # Dependencies that cannot be fetched are always compiled afterwards |
| 31 | + not scm.fetchable? -> |
| 32 | + { dep, { [app|acc], lock } } |
| 33 | + |
| 34 | + # If the dependency is not available or we have a lock mismatch |
| 35 | + out_of_date?(dep) -> |
| 36 | + new = |
| 37 | + if scm.checked_out?(opts) do |
| 38 | + Mix.shell.info "* Updating #{format_dep(dep)}" |
| 39 | + scm.update(opts) |
| 40 | + else |
| 41 | + Mix.shell.info "* Getting #{format_dep(dep)}" |
| 42 | + scm.checkout(opts) |
| 43 | + end |
| 44 | + |
| 45 | + if new do |
| 46 | + { dep, { [app|acc], Keyword.put(lock, app, new) } } |
| 47 | + else |
| 48 | + { dep, { acc, lock } } |
| 49 | + end |
| 50 | + |
| 51 | + # The dependency is ok or has some other error |
| 52 | + true -> |
| 53 | + { dep, { acc, lock } } |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + defp do_finalize({ all_deps, { apps, new_lock } }, old_lock, opts) do |
| 58 | + # Let's get the loaded versions of deps |
| 59 | + deps = Mix.Deps.loaded_by_name(apps, all_deps) |
| 60 | + |
| 61 | + # Do not attempt to compile dependencies that are not available. |
| 62 | + # mix deps.check at the end will emit proper status in case they failed. |
| 63 | + deps = Enum.filter(deps, &available?/1) |
| 64 | + |
| 65 | + # Note we only retrieve the parent dependencies of the updated |
| 66 | + # deps if all dependencies are available. This is because if a |
| 67 | + # dependency is missing, it could be a children of the parent |
| 68 | + # (aka a sibling) which would make parent compilation fail. |
| 69 | + # |
| 70 | + # If there is any other dependency that is not ok, we include |
| 71 | + # it for compilation too, this is our best to try to solve the |
| 72 | + # maximum we can at each deps.get and deps.update. |
| 73 | + if Enum.all?(all_deps, &available?/1) do |
| 74 | + deps = with_depending(deps, all_deps) ++ |
| 75 | + Enum.filter(all_deps, fn dep -> not ok?(dep) end) |
| 76 | + end |
| 77 | + |
| 78 | + # Merge the new lock on top of the old to guarantee we don't |
| 79 | + # leave out things that could not be fetched and save it. |
| 80 | + lock = Dict.merge(old_lock, new_lock) |
| 81 | + Mix.Deps.Lock.write(lock) |
| 82 | + |
| 83 | + do_compile(deps, opts) |
| 84 | + apps |
| 85 | + end |
| 86 | + |
| 87 | + defp do_compile(deps, opts) do |
| 88 | + apps = Enum.map(deps, &(&1.app)) |> Enum.uniq |
| 89 | + |
| 90 | + unless opts[:no_compile] do |
| 91 | + if apps != [] do |
| 92 | + args = if opts[:quiet], do: ["--quiet"|apps], else: apps |
| 93 | + Mix.Task.run("deps.compile", args) |
| 94 | + end |
| 95 | + |
| 96 | + unless opts[:no_deps_check] do |
| 97 | + Mix.Task.run("deps.check", []) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + defp with_depending(deps, all_deps) do |
| 103 | + (deps ++ do_with_depending(deps, all_deps)) |> Enum.uniq(&(&1.app)) |
| 104 | + end |
| 105 | + |
| 106 | + defp do_with_depending([], _all_deps) do |
| 107 | + [] |
| 108 | + end |
| 109 | + |
| 110 | + defp do_with_depending(deps, all_deps) do |
| 111 | + dep_names = Enum.map(deps, fn dep -> dep.app end) |
| 112 | + |
| 113 | + parents = Enum.filter all_deps, fn dep -> |
| 114 | + Enum.any?(dep.deps, &(&1 in dep_names)) |
| 115 | + end |
| 116 | + |
| 117 | + do_with_depending(parents, all_deps) ++ parents |
| 118 | + end |
| 119 | +end |
0 commit comments