Skip to content

Commit

Permalink
resolving doesnt create new environment from scratch, commands auto-r…
Browse files Browse the repository at this point in the history
…esolve, verbosity decreased
  • Loading branch information
Christopher Doris committed Mar 30, 2022
1 parent 6528f0a commit 2ba2d04
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 103 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ julia> using CondaPkg
julia> # now press ] to enter the Pkg REPL
pkg> conda status # see what we have installed
pkg> conda add python perl # adds conda packages
pkg> conda add --pip build # adds pip packages
pkg> conda pip_add build # adds pip packages
pkg> conda rm perl # removes conda packages
pkg> conda run python --version # runs the given command in the conda environment
```

For more information do `?` or `?conda` from the Pkg REPL.

**Note:** Adding and removing dependencies only edits the `CondaPkg.toml` file, it does
not immediately modify the Conda environment. The dependencies are installed when required,
such as by the `conda run` command above. In the above example, `perl` was never installed.
You can do `conda resolve` to resolve dependencies.

**Note:** We recommend against adding Pip packages unless necessary - if there is a
corresponding Conda package then use that. Pip does not handle version conflicts
gracefully, so it is possible to get incompatible versions.
Expand Down Expand Up @@ -158,3 +153,11 @@ The default backend is an implementation detail, but is currently `MicroMamba`.

If you set `JULIA_CONDAPKG_EXE` but not `JULIA_CONDAPKG_BACKEND` then the `System` backend
is used.

### Verbosity

You can control the verbosity of any `conda` or `pip` commands executed by setting the
environment variable `JULIA_CONDAPKG_VERBOSITY` to a number:
- `-1` is quiet mode (the default).
- `0` is normal mode.
- `1`, `2`, etc. are verbose modes, useful for debugging.
62 changes: 13 additions & 49 deletions src/PkgREPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ end

### options

const resolve_opt = Pkg.REPLMode.OptionDeclaration([
:name => "resolve",
:short_name => "r",
:api => :resolve => true,
])

const force_opt = Pkg.REPLMode.OptionDeclaration([
:name => "force",
:short_name => "f",
Expand Down Expand Up @@ -96,21 +90,17 @@ const resolve_spec = Pkg.REPLMode.CommandSpec(

### add

function add(args; resolve=false)
function add(args)
CondaPkg.add(parse_pkg.(args))
resolve && CondaPkg.resolve()
end

const add_help = Markdown.parse("""
```
conda add [-r|--resolve] pkg ...
conda add pkg ...
```
Add packages to the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -127,26 +117,21 @@ const add_spec = Pkg.REPLMode.CommandSpec(
help = add_help,
description = "add Conda packages",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### channel_add

function channel_add(args; resolve=false)
function channel_add(args)
CondaPkg.add(parse_channel.(args))
resolve && CondaPkg.resolve()
end

const channel_add_help = Markdown.parse("""
```
conda channel_add [-r|--resolve] channel ...
conda channel_add channel ...
```
Add channels to the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -161,26 +146,21 @@ const channel_add_spec = Pkg.REPLMode.CommandSpec(
help = channel_add_help,
description = "add Conda channels",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### pip_add

function pip_add(args; resolve=false)
function pip_add(args)
CondaPkg.add(parse_pip_pkg.(args))
resolve && CondaPkg.resolve()
end

const pip_add_help = Markdown.parse("""
```
conda pip_add [-r|--resolve] pkg ...
conda pip_add pkg ...
```
Add Pip packages to the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -195,26 +175,21 @@ const pip_add_spec = Pkg.REPLMode.CommandSpec(
help = pip_add_help,
description = "add Pip packages",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### rm

function rm(args; resolve=false)
function rm(args)
CondaPkg.rm(parse_pkg.(args))
resolve && CondaPkg.resolve()
end

const rm_help = Markdown.parse("""
```
conda rm|remove [-r|--resolve] pkg ...
conda rm|remove pkg ...
```
Remove packages from the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -230,26 +205,21 @@ const rm_spec = Pkg.REPLMode.CommandSpec(
help = rm_help,
description = "remove Conda packages",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### channel_rm

function channel_rm(args; resolve=false)
function channel_rm(args)
CondaPkg.rm(parse_channel.(args))
resolve && CondaPkg.resolve()
end

const channel_rm_help = Markdown.parse("""
```
conda channel_rm|channel_remove [-r|--resolve] channel ...
conda channel_rm|channel_remove channel ...
```
Remove channels from the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -265,26 +235,21 @@ const channel_rm_spec = Pkg.REPLMode.CommandSpec(
help = channel_rm_help,
description = "remove Conda channels",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### pip_rm

function pip_rm(args; resolve=false)
function pip_rm(args)
CondaPkg.rm(parse_pip_pkg.(args))
resolve && CondaPkg.resolve()
end

const pip_rm_help = Markdown.parse("""
```
conda pip_rm|pip_remove [-r|--resolve] pkg ...
conda pip_rm|pip_remove pkg ...
```
Remove Pip packages from the environment.
The Conda environment is not immediately resolved. Use the `--resolve` or `-r`
flag to force resolve.
**Examples**
```
Expand All @@ -300,7 +265,6 @@ const pip_rm_spec = Pkg.REPLMode.CommandSpec(
help = pip_rm_help,
description = "remove Pip packages",
arg_count = 0 => Inf,
option_spec = [resolve_opt],
)

### gc
Expand Down Expand Up @@ -361,7 +325,7 @@ const SPECS = Dict(
"channel_add" => channel_add_spec,
"channel_remove" => channel_rm_spec,
"channel_rm" => channel_rm_spec,
"pip add" => pip_add_spec,
"pip_add" => pip_add_spec,
"pip_remove" => pip_rm_spec,
"pip_rm" => pip_rm_spec,
"gc" => gc_spec,
Expand Down
34 changes: 18 additions & 16 deletions src/deps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,18 @@ function status(; io::IO=stderr)
end
end

function add(pkgs::AbstractVector)
function add(pkgs::AbstractVector; resolve=true)
toml = read_deps()
for pkg in pkgs
add!(toml, pkg)
end
write_deps(toml)
STATE.resolved = false
resolve && CondaPkg.resolve()
return
end

add(pkg::Union{PkgSpec,PipPkgSpec,ChannelSpec}) = add([pkg])
add(pkg::Union{PkgSpec,PipPkgSpec,ChannelSpec}; resolve=true) = add([pkg], resolve=resolve)

function add!(toml, pkg::PkgSpec)
deps = get!(Dict{String,Any}, toml, "deps")
Expand Down Expand Up @@ -219,17 +220,18 @@ function add!(toml, pkg::PipPkgSpec)
deps[pkg.name] = pkg.version
end

function rm(pkgs::AbstractVector)
function rm(pkgs::AbstractVector; resolve=true)
toml = read_deps()
for pkg in pkgs
rm!(toml, pkg)
end
write_deps(toml)
STATE.resolved = false
resolve && CondaPkg.resolve()
return
end

rm(pkg::Union{PkgSpec,PipPkgSpec,ChannelSpec}) = rm([pkg])
rm(pkg::Union{PkgSpec,PipPkgSpec,ChannelSpec}; resolve=true) = rm([pkg], resolve=resolve)

function rm!(toml, pkg::PkgSpec)
deps = get!(Dict{String,Any}, toml, "deps")
Expand Down Expand Up @@ -258,35 +260,35 @@ function rm!(toml, pkg::PipPkgSpec)
end

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

"""
rm(pkg)
rm(pkg; resolve=true)
Removes a dependency from the current environment.
"""
rm(pkg::AbstractString) = rm(PkgSpec(pkg))
rm(pkg::AbstractString; resolve=true) = rm(PkgSpec(pkg); resolve=resolve)

"""
add_channel(channel)
add_channel(channel; resolve=true)
Adds a channel to the current environment.
"""
add_channel(channel::AbstractString) = add(ChannelSpec(channel))
add_channel(channel::AbstractString; resolve=true) = add(ChannelSpec(channel), resolve=resolve)

"""
rm_channel(channel)
rm_channel(channel; resolve=true)
Removes a channel from the current environment.
"""
rm_channel(channel::AbstractString) = rm(ChannelSpec(channel))
rm_channel(channel::AbstractString; resolve=true) = rm(ChannelSpec(channel), resolve=resolve)

"""
add_pip(pkg; version="")
add_pip(pkg; version="", resolve=true)
Adds a pip dependency to the current environment.
Expand All @@ -295,11 +297,11 @@ Adds a pip dependency to the current environment.
Use conda dependencies instead if at all possible. Pip does not handle version
conflicts gracefully, so it is possible to get incompatible versions.
"""
add_pip(pkg::AbstractString; version="") = add(PipPkgSpec(pkg, version=version))
add_pip(pkg::AbstractString; version="", resolve=true) = add(PipPkgSpec(pkg, version=version), resolve=resolve)

"""
rm_pip(pkg)
rm_pip(pkg; resolve=true)
Removes a pip dependency from the current environment.
"""
rm_pip(pkg::AbstractString) = rm(PipPkgSpec(pkg))
rm_pip(pkg::AbstractString; resolve=true) = rm(PipPkgSpec(pkg), resolve=resolve)
Loading

0 comments on commit 2ba2d04

Please sign in to comment.