Skip to content

Commit

Permalink
Add Pkg.API.get_uuid() and Pkg.API.get_version()
Browse files Browse the repository at this point in the history
These functions provide a nice interface for determining the
currently-running package's UUID and VersionNumber, identified by loaded
`Module`.  We explicitly do not include a lookup by name, in expectation
of potential future naming clashes due to having multiple packages with
the same name but different UUIDs.
  • Loading branch information
staticfloat committed May 22, 2020
1 parent 5fbcb0a commit 6679131
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1047,4 +1047,31 @@ Package(name::AbstractString) = PackageSpec(name)
Package(name::AbstractString, uuid::UUID) = PackageSpec(name, uuid)
Package(name::AbstractString, uuid::UUID, version::VersionTypes) = PackageSpec(name, uuid, version)


"""
get_uuid(m::Module)
Given a `Module`, find the `UUID` of the parent package to that `Module`. If none can be
found (e.g. `Main`, `Base`, anonymous modules, etc...) returns `nothing`.
"""
function get_uuid(m::Module)
return Base.PkgId(m).uuid
end

"""
get_version(pkg)
Given a `UUID` identifying a package, returns the version of that package within the
current project. If the package cannot be found by the given UUID, throws an
`ArgumentError`.
"""
function get_version(uuid::UUID)
ctx = Pkg.Types.Context()
matching_pkgs = filter(((u, e),) -> u == uuid, ctx.env.manifest)
if isempty(matching_pkgs)
throw(ArgumentError("Unable to find given UUID in environment"))
end
return first(matching_pkgs)[2].version
end

end # module

0 comments on commit 6679131

Please sign in to comment.