Skip to content

Commit

Permalink
lazily load Pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jul 3, 2024
1 parent a03e19f commit b636be7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Expand All @@ -35,6 +34,7 @@ Showoff = "992d4aef-0814-514b-bc4d-f2e9a6c4116f"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
UnicodeFun = "1cfade01-22cf-5700-b092-accc4b62d6e1"
UnitfulLatexify = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
Expand Down
2 changes: 1 addition & 1 deletion src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
@eval Base.Experimental.@max_methods 1
end

using Pkg, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random
using Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random, TOML
using PrecompileTools, Reexport, RelocatableFolders
using Base.Meta
@reexport using RecipesBase
Expand Down
34 changes: 18 additions & 16 deletions src/backends.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
struct NoBackend <: AbstractBackend end

const _plots_project = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml"))
const _current_plots_version = _plots_project.version
const _plots_compats = _plots_project.compat
lazyloadPkg() = Base.require(@__MODULE__, :Pkg)

const _current_plots_version = pkgversion(Plots)
const _plots_compats = TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))["compat"]

const _backendSymbol = Dict{DataType,Symbol}(NoBackend => :none)
const _backendType = Dict{Symbol,DataType}(:none => NoBackend)
const _backend_packages = Dict{Symbol,Symbol}()
const _initialized_backends = Set{Symbol}()
const _backends = Symbol[]

const _plots_deps = let toml = Pkg.TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))
const _plots_deps = let toml = TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))
merge(toml["deps"], toml["extras"])
end

Expand Down Expand Up @@ -38,22 +39,20 @@ function _check_installed(backend::Union{Module,AbstractString,Symbol}; warn = t
version = if pkg_id === nothing
nothing
else
get(Pkg.dependencies(), pkg_id.uuid, (; version = nothing)).version
pkg = lazyloadPkg()
get(@invokelatest(pkg.dependencies()), pkg_id.uuid, (; version = nothing)).version
end
version === nothing && @warn "backend `$str` is not installed."
version
end

function _check_compat(m::Module; warn = true)
(be_v = _check_installed(m; warn)) === nothing && return
if (be_c = _plots_compats[string(m)]) isa String # julia 1.6
if be_v Pkg.Types.semver_spec(be_c)
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c)."
end
else
if intersect(be_v, be_c.val) |> isempty
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c.str)."
end
be_c = _plots_compats[string(m)]
pkg = lazyloadPkg()
semver = @invokelatest pkg.Types.semver_spec(be_c)
if @invokelatest(be_v semver)
@warn "`$m` $be_v is not compatible with this version of `Plots`. The declared compatibility is $(be_c)."
end
nothing
end
Expand Down Expand Up @@ -214,9 +213,10 @@ function diagnostics(io::IO = stdout)
else
be_name = string(backend_package_name(be))
@info "selected `Plots` backend: $be_name, from $origin"
Pkg.status(
pkg = lazyloadPkg()
@invokelatest pkg.status(
["Plots", "RecipesBase", "RecipesPipeline", be_name];
mode = Pkg.PKGMODE_MANIFEST,
mode = pkg.PKGMODE_MANIFEST,
io,
)
end
Expand Down Expand Up @@ -379,7 +379,9 @@ function _initialize_backend(pkg::AbstractBackend)
@eval name === :GR ? Plots : Main begin
import $name
export $name
$(_check_compat)($name)
if $(QuoteNode(name)) !== :GR
$(_check_compat)($name)
end
end
_post_imports(pkg)
_runtime_init(pkg)
Expand Down

0 comments on commit b636be7

Please sign in to comment.