Skip to content

Commit

Permalink
get rid of the mode field in PackageSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Oct 17, 2020
1 parent 1e480a5 commit 67ce5b2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
13 changes: 5 additions & 8 deletions src/API.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ end
function rm(ctx::Context, pkgs::Vector{PackageSpec}; mode=PKGMODE_PROJECT, kwargs...)
require_not_empty(pkgs, :rm)
pkgs = deepcopy(pkgs) # deepcopy for avoid mutating PackageSpec members
foreach(pkg -> pkg.mode = mode, pkgs)

for pkg in pkgs
if pkg.name === nothing && pkg.uuid === nothing
Expand All @@ -213,19 +212,18 @@ function rm(ctx::Context, pkgs::Vector{PackageSpec}; mode=PKGMODE_PROJECT, kwarg

Context!(ctx; kwargs...)

project_deps_resolve!(ctx, pkgs)
manifest_resolve!(ctx, pkgs)
mode == PKGMODE_PROJECT && project_deps_resolve!(ctx, pkgs)
mode == PKGMODE_MANIFEST && manifest_resolve!(ctx, pkgs)
ensure_resolved(ctx, pkgs)

Operations.rm(ctx, pkgs)
Operations.rm(ctx, pkgs; mode)
return
end

function up(ctx::Context, pkgs::Vector{PackageSpec};
level::UpgradeLevel=UPLEVEL_MAJOR, mode::PackageMode=PKGMODE_PROJECT,
update_registry::Bool=true, kwargs...)
pkgs = deepcopy(pkgs) # deepcopy for avoid mutating PackageSpec members
foreach(pkg -> pkg.mode = mode, pkgs)

Context!(ctx; kwargs...)
if update_registry
Expand All @@ -244,6 +242,8 @@ function up(ctx::Context, pkgs::Vector{PackageSpec};
end
end
else
mode == PKGMODE_PROJECT && project_deps_resolve!(ctx, pkgs)
mode == PKGMODE_MANIFEST && manifest_resolve!(ctx, pkgs)
project_deps_resolve!(ctx, pkgs)
manifest_resolve!(ctx, pkgs)
ensure_resolved(ctx, pkgs)
Expand Down Expand Up @@ -280,7 +280,6 @@ function pin(ctx::Context, pkgs::Vector{PackageSpec}; kwargs...)
end
end

foreach(pkg -> pkg.mode = PKGMODE_PROJECT, pkgs)
project_deps_resolve!(ctx, pkgs)
ensure_resolved(ctx, pkgs)
Operations.pin(ctx, pkgs)
Expand All @@ -303,7 +302,6 @@ function free(ctx::Context, pkgs::Vector{PackageSpec}; kwargs...)
end
end

foreach(pkg -> pkg.mode = PKGMODE_MANIFEST, pkgs)
manifest_resolve!(ctx, pkgs)
ensure_resolved(ctx, pkgs)

Expand Down Expand Up @@ -892,7 +890,6 @@ function build(ctx::Context, pkgs::Vector{PackageSpec}; verbose=false, kwargs...
end
end
project_resolve!(ctx, pkgs)
foreach(pkg -> pkg.mode = PKGMODE_MANIFEST, pkgs)
manifest_resolve!(ctx, pkgs)
ensure_resolved(ctx, pkgs)
Operations.build(ctx, pkgs, verbose)
Expand Down
50 changes: 26 additions & 24 deletions src/Operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1070,17 +1070,18 @@ end
##############
# Operations #
##############
function rm(ctx::Context, pkgs::Vector{PackageSpec})
function rm(ctx::Context, pkgs::Vector{PackageSpec}; mode::PackageMode)
drop = UUID[]
# find manifest-mode drops
for pkg in pkgs
pkg.mode == PKGMODE_MANIFEST || continue
info = manifest_info(ctx, pkg.uuid)
if info !== nothing
pkg.uuid in drop || push!(drop, pkg.uuid)
else
str = has_name(pkg) ? pkg.name : string(pkg.uuid)
@warn("`$str` not in manifest, ignoring")
if mode == PKGMODE_MANIFEST
for pkg in pkgs
info = manifest_info(ctx, pkg.uuid)
if info !== nothing
pkg.uuid in drop || push!(drop, pkg.uuid)
else
str = has_name(pkg) ? pkg.name : string(pkg.uuid)
@warn("`$str` not in manifest, ignoring")
end
end
end
# drop reverse dependencies
Expand All @@ -1096,22 +1097,23 @@ function rm(ctx::Context, pkgs::Vector{PackageSpec})
clean && break
end
# find project-mode drops
for pkg in pkgs
pkg.mode == PKGMODE_PROJECT || continue
found = false
for (name::String, uuid::UUID) in ctx.env.project.deps
pkg.name == name || pkg.uuid == uuid || continue
pkg.name == name ||
error("project file name mismatch for `$uuid`: $(pkg.name)$name")
pkg.uuid == uuid ||
error("project file UUID mismatch for `$name`: $(pkg.uuid)$uuid")
uuid in drop || push!(drop, uuid)
found = true
break
if mode == PKGMODE_PROJECT
for pkg in pkgs
found = false
for (name::String, uuid::UUID) in ctx.env.project.deps
pkg.name == name || pkg.uuid == uuid || continue
pkg.name == name ||
error("project file name mismatch for `$uuid`: $(pkg.name)$name")
pkg.uuid == uuid ||
error("project file UUID mismatch for `$name`: $(pkg.uuid)$uuid")
uuid in drop || push!(drop, uuid)
found = true
break
end
found && continue
str = has_name(pkg) ? pkg.name : string(pkg.uuid)
@warn("`$str` not in project, ignoring")
end
found && continue
str = has_name(pkg) ? pkg.name : string(pkg.uuid)
@warn("`$str` not in project, ignoring")
end
# delete drops from project
n = length(ctx.env.project.deps)
Expand Down
11 changes: 4 additions & 7 deletions src/Pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ An enum with the instances
* `PKGMODE_PROJECT`
Determines if operations should be made on a project or manifest level.
Used as an argument to [`PackageSpec`](@ref) or as an argument to [`Pkg.rm`](@ref).
Used as an argument to [`Pkg.rm`](@ref), [`Pkg.update`](@ref) and [`Pkg.status`](@ref).
"""
const PackageMode = Types.PackageMode

Expand Down Expand Up @@ -148,10 +148,10 @@ Pkg.precompile()
const precompile = API.precompile

"""
Pkg.rm(pkg::Union{String, Vector{String}})
Pkg.rm(pkg::Union{PackageSpec, Vector{PackageSpec}})
Pkg.rm(pkg::Union{String, Vector{String}}; mode::PackageMode = PKGMODE_PROJECT)
Pkg.rm(pkg::Union{PackageSpec, Vector{PackageSpec}}; mode::PackageMode = PKGMODE_PROJECT)
Remove a package from the current project. If the `mode` of `pkg` is
Remove a package from the current project. If `mode` is equal to
`PKGMODE_MANIFEST` also remove it from the manifest including all
recursive dependencies of `pkg`.
Expand Down Expand Up @@ -443,8 +443,6 @@ This includes:
* A `url` and an optional git `rev`ision. `rev` can be a branch name or a git commit SHA1.
* A local `path`. This is equivalent to using the `url` argument but can be more descriptive.
* A `subdir` which can be used when adding a package that is not in the root of a repository.
* A `mode`, which is an instance of the enum [`PackageMode`](@ref), with possible values `PKGMODE_PROJECT`
(the default) or `PKGMODE_MANIFEST`. Used in e.g. [`Pkg.rm`](@ref).
Most functions in Pkg take a `Vector` of `PackageSpec` and do the operation on all the packages
in the vector.
Expand All @@ -468,7 +466,6 @@ Below is a comparison between the REPL version and the API version:
| `Package#master` | `PackageSpec(name="Package", rev="master")` |
| `local/path#feature` | `PackageSpec(path="local/path"; rev="feature")` |
| `www.mypkg.com` | `PackageSpec(url="www.mypkg.com")` |
| `--manifest Package` | `PackageSpec(name="Package", mode=PKGSPEC_MANIFEST)` |
| `--major Package` | `PackageSpec(name="Package", version=PKGLEVEL_MAJOR)` |
"""
Expand Down
9 changes: 3 additions & 6 deletions src/Types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ Base.@kwdef mutable struct PackageSpec
repo::GitRepo = GitRepo()
path::Union{Nothing,String} = nothing
pinned::Bool = false
mode::PackageMode = PKGMODE_PROJECT
end
PackageSpec(name::AbstractString) = PackageSpec(;name=name)
PackageSpec(name::AbstractString, uuid::UUID) = PackageSpec(;name=name, uuid=uuid)
Expand All @@ -105,7 +104,7 @@ PackageSpec(n::AbstractString, u::UUID, v::VersionTypes) = PackageSpec(;name=n,
function Base.:(==)(a::PackageSpec, b::PackageSpec)
return a.name == b.name && a.uuid == b.uuid && a.version == b.version &&
a.tree_hash == b.tree_hash && a.repo == b.repo && a.path == b.path &&
a.pinned == b.pinned && a.mode == b.mode
a.pinned == b.pinned
end

function err_rep(pkg::PackageSpec)
Expand Down Expand Up @@ -562,7 +561,7 @@ function handle_repo_add!(ctx::Context, pkg::PackageSpec)
@assert pkg.repo.rev !== nothing
# First, we try resolving against the manifest and current registry to avoid updating registries if at all possible.
# This also handles the case where we _only_ wish to switch the tracking branch for a package.
manifest_resolve!(ctx, [pkg]; force=true)
manifest_resolve!(ctx, [pkg])
if isresolved(pkg)
entry = manifest_info(ctx, pkg.uuid)
if entry !== nothing && entry.repo.source !== nothing # reuse source in manifest
Expand Down Expand Up @@ -728,7 +727,6 @@ function project_deps_resolve!(ctx::Context, pkgs::AbstractVector{PackageSpec})
uuids = ctx.env.project.deps
names = Dict(uuid => name for (name, uuid) in uuids)
for pkg in pkgs
pkg.mode == PKGMODE_PROJECT || continue
if has_name(pkg) && !has_uuid(pkg) && pkg.name in keys(uuids)
pkg.uuid = uuids[pkg.name]
end
Expand All @@ -739,15 +737,14 @@ function project_deps_resolve!(ctx::Context, pkgs::AbstractVector{PackageSpec})
end

# Disambiguate name/uuid package specifications using manifest info.
function manifest_resolve!(ctx::Context, pkgs::AbstractVector{PackageSpec}; force=false)
function manifest_resolve!(ctx::Context, pkgs::AbstractVector{PackageSpec})
uuids = Dict{String,Vector{UUID}}()
names = Dict{UUID,String}()
for (uuid, entry) in ctx.env.manifest
push!(get!(uuids, entry.name, UUID[]), uuid)
names[uuid] = entry.name # can be duplicate but doesn't matter
end
for pkg in pkgs
force || pkg.mode == PKGMODE_MANIFEST || continue
if has_name(pkg) && !has_uuid(pkg) && pkg.name in keys(uuids)
length(uuids[pkg.name]) == 1 && (pkg.uuid = uuids[pkg.name][1])
end
Expand Down

0 comments on commit 67ce5b2

Please sign in to comment.