Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it easier to add or remove multiple packages w/ Vector{String} #135

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ These functions are intended to be used interactively when the Pkg REPL is not a
(e.g. if you are in a notebook):

- `status()` shows the Conda dependencies of the current project.
- `add(pkg; version="", channel="")` adds/replaces a dependency.
- `rm(pkg)` removes a dependency.
- `add(pkg; version="", channel="")` adds/replaces a dependency or a vector of dependencies.
- `rm(pkg)` removes a dependency or a vector of dependencies.
- `add_channel(channel)` adds a channel.
- `rm_channel(channel)` removes a channel.
- `add_pip(pkg; version="")` adds/replaces a pip dependency.
Expand Down
14 changes: 11 additions & 3 deletions src/deps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,16 @@ function status(; io::IO=stderr)
end
end

function add(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
# Do nothing for existing specs
_to_spec(s::Union{PkgSpec,PipPkgSpec,ChannelSpec}; channel="") = s
# Convert strings to PkgSpec
_to_spec(s::AbstractString; channel="") = PkgSpec(s; channel)

function add(pkgs::AbstractVector; channel="", resolve=true, file=cur_deps_file(), kw...)
toml = read_deps(; file)

for pkg in pkgs
add!(toml, pkg)
add!(toml, _to_spec(pkg; channel))
end
write_deps(toml; file)
STATE.resolved = false
Expand Down Expand Up @@ -271,7 +277,7 @@ end
function rm(pkgs::AbstractVector; resolve=true, file=cur_deps_file(), kw...)
toml = read_deps(; file)
for pkg in pkgs
rm!(toml, pkg)
rm!(toml, _to_spec(pkg))
end
write_deps(toml; file)
STATE.resolved = false
Expand Down Expand Up @@ -309,13 +315,15 @@ end

"""
add(pkg; version="", channel="", build="", resolve=true)
add([pkg1, pkg2, ...]; channel="", resolve=true)

Adds a dependency to the current environment.
"""
add(pkg::AbstractString; version="", channel="", build="", kw...) = add(PkgSpec(pkg, version=version, channel=channel, build=build); kw...)

"""
rm(pkg; resolve=true)
rm([pkg1, pkg2, ...]; resolve=true)

Removes a dependency from the current environment.
"""
Expand Down
26 changes: 26 additions & 0 deletions test/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@ end
end
end

@testitem "install/remove multiple python packages" begin
include("setup.jl")
CondaPkg.add("python", version="==3.10.2")
# verify package isn't already installed
@test !occursin("jsonlines ", status())
@test !occursin("cowpy", status())
CondaPkg.withenv() do
isnull || @test_throws Exception run(`python -c "import jsonlines"`)
isnull || @test_throws Exception run(`python -c "import cowpy"`)
end

# install multiple packages
CondaPkg.add(["jsonlines", "cowpy"])
@test occursin("jsonlines", status())
@test occursin("cowpy", status())

# remove multiple packages
CondaPkg.rm(["jsonlines", "cowpy"])
@test !occursin("jsonlines ", status())
@test !occursin("cowpy", status())
CondaPkg.withenv() do
isnull || @test_throws Exception run(`python -c "import jsonlines"`))
isnull || @test_throws Exception run(`python -c "import cowpy"`))
mkitti marked this conversation as resolved.
Show resolved Hide resolved
end
end

@testitem "pip install/remove python package" begin
include("setup.jl")
CondaPkg.add("python", version="==3.10.2")
Expand Down
Loading