Skip to content

Commit

Permalink
provide a way to run REPL commands outside the REPL (#173)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7a8d376e02fe8073153e148269cc74be337d3619)
  • Loading branch information
KristofferC committed Mar 6, 2018
1 parent 3047cf0 commit 98d4332
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 26 deletions.
3 changes: 3 additions & 0 deletions stdlib/Pkg3/src/Pkg3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ include("REPLMode.jl")

import .API: add, rm, up, test, gc, init, build, installed, pin, free, checkout, develop
const update = up
import .REPLMode: @pkg_str
export @pkg_str


function __init__()
if isdefined(Base, :active_repl)
Expand Down
22 changes: 22 additions & 0 deletions stdlib/Pkg3/src/REPLMode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,28 @@ end
######################
# REPL mode creation #
######################

# Provide a string macro pkg"cmd" that can be used in the same way
# as the REPLMode `pkg> cmd`. Useful for testing and in environments
# where we do not have a REPL, e.g. IJulia.
struct MiniREPL <: REPL.AbstractREPL
display::TextDisplay
t::REPL.Terminals.TTYTerminal
end
function MiniREPL()
MiniREPL(TextDisplay(stdout), REPL.Terminals.TTYTerminal(get(ENV, "TERM", Sys.iswindows() ? "" : "dumb"), stdin, stdout, stderr))
end
REPL.REPLDisplay(repl::MiniREPL) = repl.display

__init__() = minirepl[] = MiniREPL()

const minirepl = Ref{MiniREPL}()

macro pkg_str(str::String)
:($(do_cmd)(minirepl[], $str))
end

# Set up the repl Pkg REPLMode
function create_mode(repl, main)
pkg_mode = LineEdit.Prompt("pkg> ";
prompt_prefix = Base.text_colors[:blue],
Expand Down
27 changes: 1 addition & 26 deletions stdlib/Pkg3/test/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,9 @@ using Pkg3.Types
import Random: randstring
import LibGit2

function temp_pkg_dir(fn::Function)
local env_dir
local old_load_path
local old_depot_path
try
old_load_path = copy(LOAD_PATH)
old_depot_path = copy(DEPOT_PATH)
empty!(LOAD_PATH)
empty!(DEPOT_PATH)
mktempdir() do env_dir
mktempdir() do depot_dir
pushfirst!(LOAD_PATH, env_dir)
pushfirst!(DEPOT_PATH, depot_dir)
# Add the standard library paths back
vers = "v$(VERSION.major).$(VERSION.minor)"
push!(LOAD_PATH, abspath(Sys.BINDIR, "..", "local", "share", "julia", "site", vers))
push!(LOAD_PATH, abspath(Sys.BINDIR, "..", "share", "julia", "site", vers))
fn(env_dir)
end
end
finally
append!(LOAD_PATH, old_load_path)
append!(DEPOT_PATH, old_depot_path)
end
end
include("utils.jl")

const TEST_PKG = (name = "Example", uuid = UUID("7876af07-990d-54b4-ab0e-23690620f79a"))
isinstalled(pkg) = Base.locate_package(Base.PkgId(pkg.uuid, pkg.name)) !== nothing

temp_pkg_dir() do project_path

Expand Down
20 changes: 20 additions & 0 deletions stdlib/Pkg3/test/repl.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module REPLTests

using Pkg3
using UUIDs
using Test

include("utils.jl")

const TEST_PKG = (name = "Example", uuid = UUID("7876af07-990d-54b4-ab0e-23690620f79a"))

temp_pkg_dir() do project_path
cd(project_path) do
@show LOAD_PATH
pkg"init"
pkg"add Example"
@test isinstalled(TEST_PKG)
end
end

end
27 changes: 27 additions & 0 deletions stdlib/Pkg3/test/utils.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function temp_pkg_dir(fn::Function)
local env_dir
local old_load_path
local old_depot_path
try
old_load_path = copy(LOAD_PATH)
old_depot_path = copy(DEPOT_PATH)
empty!(LOAD_PATH)
empty!(DEPOT_PATH)
mktempdir() do env_dir
mktempdir() do depot_dir
pushfirst!(LOAD_PATH, env_dir)
pushfirst!(DEPOT_PATH, depot_dir)
# Add the standard library paths back
vers = "v$(VERSION.major).$(VERSION.minor)"
push!(LOAD_PATH, abspath(Sys.BINDIR, "..", "local", "share", "julia", "site", vers))
push!(LOAD_PATH, abspath(Sys.BINDIR, "..", "share", "julia", "site", vers))
fn(env_dir)
end
end
finally
append!(LOAD_PATH, old_load_path)
append!(DEPOT_PATH, old_depot_path)
end
end

isinstalled(pkg) = Base.locate_package(Base.PkgId(pkg.uuid, pkg.name)) !== nothing

0 comments on commit 98d4332

Please sign in to comment.