diff --git a/Project.toml b/Project.toml index 3b19f00..1eab65d 100644 --- a/Project.toml +++ b/Project.toml @@ -23,10 +23,12 @@ KittyTerminalImages = "b7fa5abe-5c7d-46c6-a1ae-1026d0d509b9" Netpbm = "f09324ee-3d7c-5217-9330-fc30815ba969" OhMyREPL = "5fb14364-9ced-5910-84b2-373655c76a03" PNGFiles = "f57f5aa1-a3ce-4bc8-8ab9-96f992907883" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" QOI = "4b34888f-f399-49d4-9bb3-47ed5cae4e65" TiffImages = "731e570b-9d59-4bfa-96dc-6df516fadf69" [extensions] +PkgExt = "Pkg" cairomakie = "CairoMakie" gadfly = "Gadfly" hdf5 = "HDF5" diff --git a/src/pkgstack.jl b/ext/PkgExt.jl similarity index 98% rename from src/pkgstack.jl rename to ext/PkgExt.jl index 8728812..4fc8bd6 100644 --- a/src/pkgstack.jl +++ b/ext/PkgExt.jl @@ -1,8 +1,10 @@ -module PkgStack +module PkgExt -import Pkg +import Setup: lazypkg import Markdown: @md_str +const Pkg = lazypkg() + function stack(envs) if isempty(envs) printstyled(" The current stack:\n", bold=true) diff --git a/src/Setup.jl b/src/Setup.jl index 82f508c..9beecac 100644 --- a/src/Setup.jl +++ b/src/Setup.jl @@ -1,7 +1,6 @@ module Setup import REPL -import Pkg export about, @about @@ -18,7 +17,6 @@ include("install.jl") include("cmdpuns.jl") include("termsetup.jl") include("about.jl") -include("pkgstack.jl") include("autoloads.jl") using .Autoloads @@ -27,12 +25,35 @@ include("autoload_data.jl") include("sessions.jl") using .Sessions -using .PkgStack +@static if VERSION <= v"1.10" + const Pkg = let pkg_id = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg") + @something(get(Base.loaded_modules, pkg_id, nothing), + Base.require(pkg_id)) + end + lazypkg() = Pkg -function ensurepkg(pkg::String) + include("../ext/PkgExt.jl") + using .PkgExt +else + function lazypkg() + pkg_id = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg") + @something(get(Base.loaded_modules, pkg_id, nothing), + Base.require(pkg_id)) + end +end + +function ensureglobalpkg(pkg::String) if isnothing(Base.find_package(pkg)) + Pkg = lazypkg() + oldproj = Base.current_project() + Pkg.activate() @info "Installing $pkg" - Pkg.add(pkg) + lazypkg().add(pkg) + if isnothing(oldproj) + Pkg.activate() + else + Pkg.activate(oldproj) + end end end @@ -44,16 +65,6 @@ function __init__() TERM[] = termcode() Main.Threads.@spawn if TERM[] == "xterm-kitty" - if isnothing(Base.find_package("KittyTerminalImages")) - @info "Installing KittyTerminalImages" - currentproj = Pkg.project().path - try - Pkg.activate() - # Pkg.add("KittyTerminalImages") - finally - Pkg.activate(currentproj) - end - end termtitle(pwd()) end diff --git a/src/install.jl b/src/install.jl index 1c00c56..2940b44 100644 --- a/src/install.jl +++ b/src/install.jl @@ -6,17 +6,22 @@ const STARTUP_MANAGED_MESSAGE = const STARTUP_CONTENT = """ $STARTUP_MANAGED_MESSAGE if VERSION < v"1.7" - @warn "Setup disabled as Julia \$VERSION < 1.7 is unsupported" + @warn "Setup disabled as Julia $VERSION < 1.7 is unsupported" else - let pkg_id = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg") - Pkg = Base.loaded_modules[pkg_id] - isnothing(Base.find_package("Setup")) && - Pkg.develop(path=joinpath(@__DIR__, "Setup")) + try + using Setup + catch e + if e isa ArgumentError && isnothing(Base.find_package("Setup")) # Package not found in current path + let pkg_id = Base.PkgId(Base.UUID("44cfe95a-1eb2-52ea-b672-e2afdf69b78f"), "Pkg") + Base.require(pkg_id).develop(path=joinpath(@__DIR__, "Setup")) + end + using Setup + else + rethrow() + end end - using Setup - - Setup.ensurepkg("Revise") + Setup.ensureglobalpkg("Revise") @async @eval using Revise end """ diff --git a/src/termsetup.jl b/src/termsetup.jl index 5510685..a810b14 100644 --- a/src/termsetup.jl +++ b/src/termsetup.jl @@ -137,7 +137,7 @@ end const ATTEMPTED_TERMIMAGE_LOAD = Ref(false) -function load_termimage_pkg() +function load_termimage_pkg(autoinstall::Bool=true) ATTEMPTED_TERMIMAGE_LOAD[] && return pkg, pkgsetup = get(TERM_IMAGE_PACKAGES, Setup.TERM[], (nothing, nothing)) if !isnothing(pkg) && !haskey(Base.loaded_modules, pkg) @@ -146,6 +146,16 @@ function load_termimage_pkg() # Don't to this async because it may affect a `display` call # that's just about to occur. Core.eval(Main, :(import $(Symbol(pkg.name)); $pkgsetup)) + elseif autoinstall + Pkg = lazypkg() + currentproj = Pkg.project().path + try + Pkg.activate() + Pkg.add(pkg.name) + load_termimage_pkg(false) + finally + Pkg.activate(currentproj) + end else @info "Consider installing $(pkg.name) for better in-terminal image previews" end