Refine pkg setup, make work with 1.10 and 1.11

This commit is contained in:
TEC 2024-01-27 02:59:48 +08:00
parent 6a72ab6d45
commit 08f4b58c1a
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
5 changed files with 56 additions and 26 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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
"""

View File

@ -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