Skip to content

Commit

Permalink
add REPL hook to prompt to install missing packages, if available in …
Browse files Browse the repository at this point in the history
…registry
  • Loading branch information
IanButterworth committed Apr 28, 2021
1 parent e5f9e61 commit 13eada7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,43 @@ function download_source(ctx::Context; readonly=true)
return Set{UUID}(entry.pkg.uuid for entry in pkgs_to_install)
end

function try_prompt_pkg_add(mods::Vector{Symbol})
ctx = Context()
available_uuids = [Types.registered_uuids(ctx.registries, String(mod)) for mod in mods] # vector of vectors
available_mods = mods[isempty.(available_uuids) .== false]
isempty(available_mods) && return false
resp = try
plural1 = length(mods) == 1 ? "" : "s"
plural2 = length(available_mods) == 1 ? "is" : "are"
missing_mod_list = length(mods) == 1 ? String(mods[1]) : "[$(join(mods, ", "))]"
available_mod_list = length(available_mods) == 1 ? repr(String(available_mods[1])) : repr(String.(available_mods))
println(ctx.io, " \033[32m\033[1m│\033[22m\033[0m Package$(plural1) $(missing_mod_list) not found,",
" but $(available_mod_list) $(plural2) available from a registry.")
print(ctx.io, " \033[32m\033[1m│\033[22m\033[0m ")
if ctx.env.pkg !== nothing
printpkgstyle(ctx.io, :Project, string(ctx.env.pkg.name, " v", ctx.env.pkg.version, " ", pathrepr(Base.active_project())), true; color=Base.info_color())
else
printpkgstyle(ctx.io, :Project, pathrepr(Base.active_project()), true, color=Base.info_color())
end
Base.prompt(" \033[32m\033[1m└\033[22m\033[0m Add $(available_mod_list) to the active project and load? (y/n)", default = "n")
catch err
if err isa InterruptException
println()
return false
end
rethrow()
end
if lowercase(resp) in ["y", "yes"]
Pkg.add(string.(available_mods))
if length(available_mods) < length(mods)
return false # declare that some mods couldn't be installed
else
return true
end
end
return false
end

################################
# Manifest update and pruning #
################################
Expand Down
2 changes: 2 additions & 0 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ function __init__()
end
end
end
push!(empty!(REPL.install_packages_hooks), Operations.try_prompt_pkg_add)
OFFLINE_MODE[] = get(ENV, "JULIA_PKG_OFFLINE", nothing) == "true"
return nothing
end
Expand Down Expand Up @@ -671,6 +672,7 @@ const precompile_script = """
Pkg.add("TestPkg")
Pkg.develop(Pkg.PackageSpec(path="TestPkg.jl"))
Pkg.add(Pkg.PackageSpec(path="TestPkg.jl/"))
Pkg.try_prompt_pkg_add(Symbol[:notapackage])
] add Te\t\t$CTRL_C
] st
$CTRL_C
Expand Down

0 comments on commit 13eada7

Please sign in to comment.