Skip to content

Commit

Permalink
Refactor internals of @__VERSION__
Browse files Browse the repository at this point in the history
Based upon suggestions here:
JuliaLang/Pkg.jl#1501
  • Loading branch information
Curtis Vogt committed Nov 14, 2019
1 parent 0cd8595 commit 1b8493e
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/version.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# TODO: Temporary until this is turned into a package
using Pkg
using Pkg: Pkg

# https://github.com/JuliaLang/julia/pull/33128
if VERSION < v"1.4.0-DEV.397"
function pkgdir(m::Module)
rootmodule = Base.moduleroot(m)
path = pathof(rootmodule)
path === nothing && return nothing
return dirname(dirname(path))
end
end

"""
@__VERSION__ -> Union{VersionNumber, Nothing}
Expand All @@ -8,31 +18,12 @@ Get the `VersionNumber` of the package which expands this macro. If executed out
package `nothing` will be returned.
"""
macro __VERSION__()
ctxt = Pkg.Types.Context()
pkg_id = Base.PkgId(__module__)
pkg_id.uuid === nothing && return nothing

project = ctxt.env.project
project_name = @static if v"1.0-" <= VERSION < v"1.1-"
project["name"]
else
project.name
end
pkg_dir = pkgdir(__module__)

pkg_info = if project_name == pkg_id.name
project
if pkg_dir !== nothing
project_data = Pkg.TOML.parsefile(Pkg.Types.projectfile_path(pkg_dir))
return VersionNumber(project_data["version"])
else
_ctxt = @static VERSION < v"1.4-" ? ctxt.env : ctxt
Pkg.Types.manifest_info(_ctxt, pkg_id.uuid)
return nothing
end

version = @static if v"1.0-" <= VERSION < v"1.1-"
VersionNumber(pkg_info["version"])
elseif v"1.1-" <= VERSION < v"1.2-"
VersionNumber(pkg_info.version)
else
pkg_info.version
end

return version
end

0 comments on commit 1b8493e

Please sign in to comment.