Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.0"
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ changes in `julia`.

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

* `pkgversion` Return the version of the package that imported 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 @@ -172,6 +174,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
50 changes: 50 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,56 @@ end
end
end

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

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)
isnothing(path) && return nothing
return get_pkgversion_from_path(path)
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