Julia - getting the version number of my module

Viewed 251

I am writing a package for Julia and within the package I would like to be able to print the my package current version number. I can of course do that manually but I was looking for a way to read it directly from the module. the version number is written in the Project.toml file, but I have not been able to find a way to read it from the code. Any ideas?

4 Answers

You could use something like

julia> using Pkg

julia> loadedversion(m::Module) =  VersionNumber(Pkg.TOML.parsefile(joinpath(string(first(methods(m.eval)).file), "..", "..", "Project.toml"))["version"])
loadedversion (generic function with 1 method)

julia> loadedversion(Atom)
v"0.12.8"

Some error handling would be prudent, of course, but that will give you the version of a package that's actually loaded (instead of the one specified in the current environment).

Possibly -

using Pkg
Pkg.TOML.parse(read("Project.toml", String))["version"]

-> "0.0.1"

using Pkg
function pkginfo(pkgname::AbstractString, key = "version")
    # Check if Package Project
    proj = Pkg.TOML.parsefile(Base.current_project()) 
    proj["name"] == pkgname && return proj[key]
    
    # If not in Project, pkgname must be in deps. Look in Manifest.toml
    # An error will be thrown if Manifest.toml is missing
    man = Pkg.TOML.parsefile(joinpath(Base.current_project() |> dirname, "Manifest.toml"));
    for (pkg, info) in man
        pkginfo = info |> first # Play with 'man' to see why
        pkg == pkgname && return pkginfo[key]
    end
end

This will give you the version (as string) of a package (or other data) in the current enviroment. But a package could be in a deeper enviroment in the stack, so...

Could it be that this is an answer?

using Pkg
Pkg.project().version

v"0.1.2"
Related