Setup.jl/src/install.jl

80 lines
2.5 KiB
Julia

const SETUP_GIT_URL = "https://git.tecosaur.net/tec/Startup.jl"
const STARTUP_MANAGED_MESSAGE =
"# !! Managed by the Setup package !!"
const STARTUP_CONTENT = """
$STARTUP_MANAGED_MESSAGE
if VERSION < v"1.7"
@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"))
end
using Setup
Setup.ensurepkg("Revise")
@async @eval using Revise
end
"""
function _install_paths()
config = joinpath(first(Base.DEPOT_PATH), "config")
startup = joinpath(config, "startup.jl")
setup = joinpath(config, "Setup")
(; config, startup, setup)
end
function install()
(; config, startup, setup) = _install_paths()
isdir(config) || mkpath(config)
if isfile(startup)
if readline(startup) == STARTUP_MANAGED_MESSAGE
isdir(setup) && update()
else
@info "Moving original startup file to $(startup).old"
mv(startup, startup * ".old")
end
end
write(startup, STARTUP_CONTENT)
if isdir(setup) && !isdir(joinpath(setup, ".git"))
@warn "Setup folder exists, but it is not a git repository. Re-creating..."
rm(setup, recursive=true)
end
if !isdir(setup)
success(`git clone $SETUP_GIT_URL $setup`) ||
@error "Failed to clone $SETUP_GIT_URL"
end
end
function uninstall()
(; startup, setup) = _install_paths()
isfile(startup) && readline(startup) != STARTUP_MANAGED_MESSAGE &&
rm(startup)
isfile(startup * ".old") && !isfile(startup) && mv(startup * ".old", startup)
if isdir(joinpath(setup, ".git")) && !isempty(read(Cmd(`git status --porcelain=v1`, dir=setup)))
@warn "Unstaged changes in $(setup)! Manually fix this and try again."
elseif isdir(setup)
rm(setup, recursive=true, force=true)
end
end
function update()
(; startup, setup) = _install_paths()
if readline(startup) != STARTUP_MANAGED_MESSAGE
@warn "Startup.jl is not managed by Setup, re-installing"
return install()
else
write(startup, STARTUP_CONTENT)
end
if !ispath(setup) || !isdir(joinpath(setup, ".git"))
ispath(setup) || @warn "Setup missing, re-installing"
return install()
end
success(Cmd(`git pull`, dir=setup)) ||
@error "Failed to update Setup"
end