Skip to content

Commit

Permalink
Add pkgversion (#807)
Browse files Browse the repository at this point in the history
* pkgversion

* back to original order

* add test for pkg without version

* update README

* Apply suggestions from code review

Co-authored-by: Martin Holters <martin.holters@hsu-hh.de>

* Apply suggestions from code review

Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>

* Update README.md

---------

Co-authored-by: Martin Holters <martin.holters@hsu-hh.de>
Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>
  • Loading branch information
3 people authored Jan 10, 2024
1 parent 0a507df commit 8e59d92
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.10.1"
version = "4.11.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[compat]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ changes in `julia`.

* `@something` and `@coalesce` as short-circuiting versions of `something` and `coalesce` ([#40729]) (since Compat 3.29)

* `pkgversion(m::Module)` returns the version of the package that loaded a given module ([#45607]) (since Compat 4.11)

## Developer tips

One of the most important rules for `Compat.jl` is to avoid breaking user code
Expand Down Expand Up @@ -170,5 +172,7 @@ Note that you should specify the correct minimum version for `Compat` in the
[#43334]: https://github.com/JuliaLang/julia/issues/43334
[#43354]: https://github.com/JuliaLang/julia/issues/43354
[#43852]: https://github.com/JuliaLang/julia/issues/43852
[#45607]: https://github.com/JuliaLang/julia/issues/45607
[#46104]: https://github.com/JuliaLang/julia/issues/46104
[#48038]: https://github.com/JuliaLang/julia/issues/48038
[#50105]: https://github.com/JuliaLang/julia/issues/50105
65 changes: 65 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,71 @@ end
end
end

# this function is available as of Julia 1.9
# https://github.com/JuliaLang/julia/pull/45607
# https://github.com/JuliaLang/julia/pull/45695
# https://github.com/JuliaLang/julia/pull/45861
# https://github.com/JuliaLang/julia/pull/46738
@static if !isdefined(Base, :pkgversion)
using TOML: parsefile
export pkgversion

const require_lock = isdefined(Base, :require_lock) ? Base.require_lock : Base.ReentrantLock()
const project_names = ("JuliaProject.toml", "Project.toml")

function locate_project_file(env::String)
for proj in project_names
project_file = joinpath(env, proj)
if Base.isfile_casesensitive(project_file)
return project_file
end
end
return nothing
end

function get_pkgversion_from_path(path)
project_file = locate_project_file(path)
if project_file isa String
d = parsefile(project_file)
v = get(d, "version", nothing)
if v !== nothing
return VersionNumber(v::String)
end
end
return nothing
end

"""
pkgversion(m::Module)
Return the version of the package that imported module `m`,
or `nothing` if `m` was not imported from a package, or imported
from a package without a version field set.
The version is read from the package's Project.toml during package
load.
To get the version of the package that imported the current module
the form `pkgversion(@__MODULE__)` can be used.
"""
function pkgversion(m::Module)
path = pkgdir(m)
path === nothing && return nothing
Base.@lock require_lock begin
v = get_pkgversion_from_path(path)
# https://github.com/JuliaLang/julia/pull/44318
@static if hasfield(Base.PkgOrigin, :version)
pkgorigin = get(Base.pkgorigins, Base.PkgId(Base.moduleroot(m)), nothing)
# Cache the version
if pkgorigin !== nothing && pkgorigin.version === nothing
pkgorigin.version = v
end
end
return v
end
end
end

# https://github.com/JuliaLang/julia/pull/43334
if VERSION < v"1.9.0-DEV.1163"
import Base: IteratorSize, HasLength, HasShape, OneTo
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Compat
using Dates
using TOML
using Test

@test isempty(detect_ambiguities(Base, Core, Compat))
Expand Down Expand Up @@ -457,6 +458,12 @@ end
@test isempty(ea)
end

@testset "pkgversion" begin
toml = joinpath(pkgdir(Compat), "Project.toml")
@test pkgversion(Compat) == VersionNumber(TOML.parsefile(toml)["version"])
@test pkgversion(Base) === nothing
end

# https://github.com/JuliaLang/julia/pull/43334
@testset "stack" begin
# Basics
Expand Down

2 comments on commit 8e59d92

@omus
Copy link
Member

@omus omus commented on 8e59d92 Jan 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/98662

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.11.0 -m "<description of version>" 8e59d922748116df792ac47fac550382a851492f
git push origin v4.11.0

Please sign in to comment.