Permalink
Browse files

Add Expm.Package.deps/2 API and expm deps CLI command

  • Loading branch information...
1 parent a39d90a commit da90fab9b5e13a96c6b560d4dbe4423b0b950c89 @yrashk committed Oct 15, 2012
Showing with 73 additions and 0 deletions.
  1. +25 −0 lib/cli.ex
  2. +48 −0 lib/package.ex
View
@@ -153,6 +153,31 @@ defrecord Expm.CLI, repository: Expm.Repository.HTTP.new.url, username: nil, pas
end
end
+ @shortdoc "Package dependencies"
+ @doc """
+ $ expm deps package [VERSION]
+
+ Prints out a list of packages with their most matching versions, space delimited
+ """
+ command(["deps", package], rec) do
+ repo = repo(rec)
+ pkg = Expm.Package.fetch repo, package
+ lc {dep, version} inlist Expm.Package.deps(repo, pkg) do
+ IO.puts "#{dep} #{version}"
+ end
+ rescue Expm.Package.VersionNotFound[]=e ->
+ IO.puts e.message
+ end
+ def run(["deps", package, version], rec) do
+ repo = repo(rec)
+ pkg = Expm.Package.fetch repo, package, version
+ lc {dep, version} inlist Expm.Package.deps(repo, pkg) do
+ IO.puts "#{dep} #{version}"
+ end
+ rescue Expm.Package.VersionNotFound[]=e ->
+ IO.puts e.message
+ end
+
@shortdoc "Publish a package"
@doc """
$ expm publish
View
@@ -1,3 +1,8 @@
+defexception Expm.Package.VersionNotFound, version: nil do
+ def message(e) do
+ "Version #{inspect e.version} not found"
+ end
+end
defrecord Expm.Package,
metadata: [],
# required
@@ -134,4 +139,47 @@ defrecord Expm.Package,
defdelegate [valid?(package), validate(package)], to: Expm.Package.Validator
+ def deps(repo, rec) do
+ lc dep inlist dependencies(rec), do: resolve_dep(repo, dep)
+ end
+
+ defp resolve_dep(repo, name) when is_binary(name) do
+ [version|_] = Enum.reverse(versions(repo, name))
+ {name, version}
+ end
+
+ defp resolve_dep(_repo, {name, version}) when is_binary(name) and
+ (is_binary(version) or is_atom(version)) do
+ {name, version}
+ end
+
+ defp resolve_dep(repo, {name, [>: version] = v}) when is_binary(name) and
+ (is_binary(version) or is_atom(version)) do
+ resolve_dep_impl(repo, name, v, fn(v) -> v > version end)
+ end
+
+ defp resolve_dep(repo, {name, [>=: version] = v}) when is_binary(name) and
+ (is_binary(version) or is_atom(version)) do
+ resolve_dep_impl(repo, name, v, fn(v) -> v >= version end)
+ end
+
+ defp resolve_dep(repo, {name, [<: version] = v}) when is_binary(name) and
+ (is_binary(version) or is_atom(version)) do
+ resolve_dep_impl(repo, name, v, fn(v) -> v < version end)
+ end
+
+ defp resolve_dep(repo, {name, [<=: version] = v}) when is_binary(name) and
+ (is_binary(version) or is_atom(version)) do
+ resolve_dep_impl(repo, name, v, fn(v) -> v <= version end)
+ end
+
+ defp resolve_dep_impl(repo, name, version, fun) do
+ versions = Enum.reverse(versions(repo, name))
+ found = Enum.find(versions, fun)
+ unless nil?(found) do
+ {name, found}
+ else
+ raise Expm.Package.VersionNotFound, version: version
+ end
+ end
end

0 comments on commit da90fab

Please sign in to comment.