JuliaLang / Pkg.jl

Pkg - Package manager for the Julia programming language
https://pkgdocs.julialang.org
Other
609 stars 251 forks source link

Add example of manually checking specifications #3847

Open MilesCranmer opened 3 months ago

MilesCranmer commented 3 months ago

The Compatibility guide is very helpful, but I still find the exact syntax easy to forget, and find myself repeatedly checking this guide whenever I'm creating a complex version specification.

I recently found the Pkg.Versions.semver_spec as a way to manually parse the version string. I have found this useful for my own work and I thought it would be helpful to give an example of how to use this to explicitly check versions, and make sure you aren't too constrained or too flexible in a given version spec.

I add the following subsection to the docs:


Checking specifications

Pkg.jl parses a given version specification using Pkg.Versions.semver_spec. You can check if a particular version of a package is contained in a particular range by using this command. For example:

julia> v"0.1.0" in Pkg.Versions.semver_spec("=0.1")
true

julia> v"0.1.0" in Pkg.Versions.semver_spec("=0.1.1")
false

julia> v"0.1.0" in Pkg.Versions.semver_spec("0.1 - 0.2")
true

julia> v"0.3.0" in Pkg.Versions.semver_spec("~0.3.2")
false

julia> v"0.3.3" in Pkg.Versions.semver_spec("0.1 - 0.2, ~0.3.2")
true

julia> # Grid of versions:
       versions = [[VersionNumber(major, minor, patch) for patch=0:8, minor=0:3, major=0:1]...];

julia> filter(v -> v in Pkg.Versions.semver_spec("0.1.8 - 0.2.2, ^0.3.6"), versions)
7-element Vector{VersionNumber}:
 v"0.1.8"
 v"0.2.0"
 v"0.2.1"
 v"0.2.2"
 v"0.3.6"
 v"0.3.7"
 v"0.3.8"
KristofferC commented 3 months ago

I think this is a good idea and we should imo also:

MilesCranmer commented 3 months ago

That all sounds good to me.

For naming, maybe just something like parse_compat?

DilumAluthge commented 3 months ago

Does this mean that we have to make Pkg.Versions.VersionSpec part of the public API? Because semver_spec returns a VersionSpec?