Skip to content

Commit

Permalink
Add dedup option
Browse files Browse the repository at this point in the history
  • Loading branch information
peng1999 committed Dec 3, 2022
1 parent 804966d commit 57d4d93
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Option to show the repo link ([#11])
- Option to not cut duplicate branches in tree ([#10])

### Fixed

Expand Down Expand Up @@ -55,5 +56,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#5]: https://github.com/peng1999/PkgDependency.jl/issues/5
[#6]: https://github.com/peng1999/PkgDependency.jl/issues/6
[#7]: https://github.com/peng1999/PkgDependency.jl/issues/7
[#10]: https://github.com/peng1999/PkgDependency.jl/issues/10
[#11]: https://github.com/peng1999/PkgDependency.jl/issues/11

11 changes: 6 additions & 5 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ julia> PkgDependency.tree("Tables")

Unless otherwise specified, all methods of `tree` function support following kwargs:

| kwarg | description |
| --- | --- |
| `reverse=true` | get a reverse dependency tree |
| `compat=true` | show compat info in tree |
| `show_link=true` | show packages' repo link in tree |
| kwarg | default | description |
| --- | --- | --- |
| `reverse` | `false` | get a reverse dependency tree |
| `compat` | `false` | show compat info in tree |
| `show_link` | `false` | show packages' repo link in tree |
| `dedup` | `true` | hide duplicate dependencies in tree |

## API

Expand Down
17 changes: 10 additions & 7 deletions src/PkgDependency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function __init__()
end

"""
tree(...; reverse=false, compat=false, show_link=false)
tree(...; reverse=false, compat=false, show_link=false, dedup=true)
"""
function tree end
Expand All @@ -20,7 +20,7 @@ function tree end
Print dependency tree of current project. `reverse` kwarg is not supported in this method.
"""
function tree(; compat=false, show_link=false)
function tree(; compat=false, show_link=false, dedup=true)
project = Pkg.project()
if project.ispackage
name = something(project.name, "Unnamed Project")
Expand All @@ -31,15 +31,15 @@ function tree(; compat=false, show_link=false)
end

registries = check_and_get_registries(; show_link)
Tree(builddict(project.uuid, project; compat, registries), title="$name $version")
Tree(builddict(project.uuid, project; compat, registries, dedup), title="$name $version")
end

"""
tree(uuid::UUID; kwargs...)
Print dependency tree of a package identified by UUID
"""
function tree(uuid::UUID; reverse=false, compat=false, show_link=false)
function tree(uuid::UUID; reverse=false, compat=false, show_link=false, dedup=true)
graph = Pkg.dependencies()
if reverse
revgraph = Pkg.dependencies()
Expand All @@ -59,7 +59,7 @@ function tree(uuid::UUID; reverse=false, compat=false, show_link=false)

# registries is used to find url
registries = check_and_get_registries(; show_link)
Tree(builddict(uuid, project; graph, compat, registries), title="$name v$version")
Tree(builddict(uuid, project; graph, compat, registries, dedup), title="$name v$version")
end

"""
Expand Down Expand Up @@ -110,7 +110,7 @@ compatstr(c::String) = c
compatstr(c::Any) = c.str

# returns dependencies of info as OrderedDict, or nothing when no dependencies
function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), listed=Set{UUID}(), compat=false, registries=nothing)
function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), listed=Set{UUID}(), dedup=true, compat=false, registries=nothing)
deps = info.dependencies
compats = compat && !isnothing(uuid) ? compatinfo(uuid) : Dict()
children = OrderedDict()
Expand Down Expand Up @@ -143,7 +143,10 @@ function builddict(uuid::Union{Nothing,UUID}, info; graph=Pkg.dependencies(), li
child = nothing
if uuid listed
push!(listed, uuid)
child = builddict(uuid, subpkg; graph, listed, compat, registries)
child = builddict(uuid, subpkg; graph, listed, compat, registries, dedup)
if !dedup
pop!(listed, uuid)
end
end
push!(children, name => child)
end
Expand Down
12 changes: 7 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ using PkgDependency
import Term: Tree

@testset "Make sure function runs without error" begin
for dedup in [true; false]
for compat in [true; false]
@test PkgDependency.tree(; compat) isa Tree
@test PkgDependency.tree("Term"; compat) isa Tree
@test PkgDependency.tree("Term"; reverse=true, compat) isa Tree
@test PkgDependency.tree(; compat, dedup) isa Tree
@test PkgDependency.tree("Term"; compat, dedup) isa Tree
@test PkgDependency.tree("Term"; reverse=true, compat, dedup) isa Tree
if VERSION < v"1.7"
@test_throws ErrorException PkgDependency.tree("Term"; compat, show_link=true) isa Tree
@test_throws ErrorException PkgDependency.tree("Term"; compat, show_link=true, dedup) isa Tree
else
@test PkgDependency.tree("Term"; compat, show_link=true) isa Tree
@test PkgDependency.tree("Term"; compat, show_link=true, dedup) isa Tree
end
end
end
end

0 comments on commit 57d4d93

Please sign in to comment.