Skip to content

Commit

Permalink
remove Options
Browse files Browse the repository at this point in the history
no 'struct per func' needed (i.e. close #61).
I tried that but not good. Cause: the docstring of the options was intertwined with the function. So annoying to separate.

solved keyword distr problem with a `select(kw, f)`, using `Base.kwarg_decl(only(methods(f)))`

So like this is good.
enduser func docstrs should now refer to the eventual funcs docstrs, and not Settings header. i.e. delete that header (it's duplicate non DRY info)
  • Loading branch information
tfiers committed Jan 13, 2023
1 parent 0f0938a commit 6f4a731
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 144 deletions.
3 changes: 2 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ DocMeta.setdocmeta!(PkgGraph, :DocTestSetup, :(using PkgGraph); recursive=true,
println("<makedocs>") # ..including Documenter.HTML(…) construction call
makedocs(
source = srcmod,
# modules = [PkgGraph],
modules = [PkgGraph],
# ↪ To get a warning if there are any docstrings not mentioned in the markdown.
# Might also be necessary to run doctests!
sitename = "PkgGraph.jl",
# ↪ Displayed in page title and navbar.
doctest = true,
Expand Down
32 changes: 0 additions & 32 deletions docs/src/ref/end-user.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,3 @@ PkgGraph.open
```@docs
PkgGraph.create
```

## Settings

These are keyword arguments that can be used with [`PkgGraph.open`](@ref) and
[`PkgGraph.create`](@ref).\
(They are also fields to [`PkgGraph.Options`](@ref)).

#### `jll`
Whether to include binary 'JLL' dependencies in the graph
(default: `true`)

#### `stdlib`
Whether to include packages from the standard library in the graph
(default: `true`)

#### `mode`
Either `:light` (default) or `:dark`.\
Whether to use black lines and black text on a white background, or vice versa.\
Note that locally-generated SVGs get both colour-schemes simultaneously (through [`SVG.add_darkmode`](@ref)), so this option is irrelevant for them.

#### `bg`
Background colour for the image.\
Default is `"transparent"`.\
`"white"` (in combination with `mode = :light`) might be a sensible value when you are
creating a PNG but do not know on what background it will be seen. (A light-mode PNG with transparent background looks bad on a dark background).

#### `style`
Custom Graphviz styling. See [`default_style`](@ref).

#### `base_url`
See [`url`](@ref).
By default, the first entry in [`webapps`](@ref).
6 changes: 0 additions & 6 deletions docs/src/ref/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ url
webapps
```

## Settings object

```@docs
Options
```

## Graphs.jl conversion

```@docs
Expand Down
1 change: 0 additions & 1 deletion src/PkgGraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ using URIs: escapeuri
using Base: @kwdef
include("includes/dotcommand.jl")
include("includes/webapps.jl")
include("includes/options.jl")
include("includes/enduser.jl")

# No package exports (no namespace pollution)
Expand Down
37 changes: 31 additions & 6 deletions src/includes/enduser.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

"""
open(pkgname; kw...)
open(pkgname, base_url = first(webapps); kw...)
Open the browser to an image of `pkgname`'s dependency graph.
See [Settings](@ref) for possible keyword arguments.
See [`url`](@ref) for more on `base_url`, and for possible keyword
arguments see [`depgraph`](@ref) and [`to_dot_str`](@ref) .
"""
function open(pkgname; dryrun = false, kw...)
link = url(pkgname, Options(; kw...))
function open(pkgname, base_url = first(webapps); dryrun = false, kw...)
dotstr = depgraph_as_dotstr(pkgname; kw...)
link = url(dotstr, base_url)
if !dryrun
DefaultApplication.open(link)
# ↪ Passing a URL (and not a file) opens the browser on all
Expand All @@ -32,13 +34,14 @@ light and dark-mode CSS.
To only create the image, without automatically opening it, pass
`open = false`.
See [Settings](@ref) for more keyword arguments.
See [`depgraph`](@ref) and [`to_dot_str`](@ref) for more keyword
arguments.
"""
function create(pkgname, dir=tempdir(); fmt=:png, bg=bg(fmt), open=true, dryrun=false, kw...)
if !is_dot_available() && !dryrun
error("`dot` program not found on `PATH`. Get it at https://graphviz.org/download/")
end
dotstr = to_dot_str(pkgname, Options(; bg, kw...))
dotstr = depgraph_as_dotstr(pkgname; bg, kw...)
imgpath = output_path(pkgname, dir, fmt)
if !dryrun
create_dot_image(dotstr, fmt, imgpath)
Expand All @@ -51,3 +54,25 @@ function create(pkgname, dir=tempdir(); fmt=:png, bg=bg(fmt), open=true, dryrun=
end

bg(fmt) = (fmt == :png ? :white : :transparent)

depgraph_as_dotstr(pkgname; kw...) = begin
edges = depgraph(pkgname; select(kw, depgraph)...)
dotstr = to_dot_str(
edges;
select(kw, to_dot_str)...,
emptymsg="($pkgname has no dependencies)",
)
end

"""
Extract the keyword arguments from `kw` that are applicable to the
single-method function `f`.
"""
select(kw, f) = begin
m = only(methods(f))
kwargnames_f = Base.kwarg_decl(m)
selected_kw = [
name => val for (name, val) in kw
if name in kwargnames_f
]
end
33 changes: 0 additions & 33 deletions src/includes/options.jl

This file was deleted.

1 change: 1 addition & 0 deletions src/modules/DepGraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export STDLIB,
STDLIB_NAMES

include("DepGraph/project.jl")
export packages_in_active_manifest

include("DepGraph/registry.jl")

Expand Down
60 changes: 1 addition & 59 deletions src/modules/DotString.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,7 @@ module DotString
export to_dot_str,
default_style

"""
to_dot_str(
edges;
mode = :light,
bg = "transparent",
style = default_style(),
indent = 4,
emptymsg = nothing,
)
Build a string that represents the given directed graph in the
[Graphviz DOT format ↗](https://graphviz.org/doc/info/lang.html).
`mode` and `bg` specify the colour scheme and background colour for the
graph (see [Settings](@ref)).
`style` is a list of strings, inserted as lines in the output (after the
lines generated for `mode` and `bg`, and just before the graph edge
lines). To use Graphviz's default style, pass `style = []`. For the
default see [`default_style`](@ref). For more on how dot-graphs can be
styled, see [Styling Graphviz output](@ref).
`indent` is the number of spaces to indent each line in the "`digraph`"
block with.
If there are no `edges`, a single node with `emptymsg` is created. If
`emptymsg` is `nothing` (default), no nodes are created, and the image
rendered from the DOT-string will be empty.
## Example:
```jldoctest
julia> edges = [:A => :B, "yes" => "no"];
julia> style = ["node [color=\\"red\\"]"];
julia> using PkgGraph
julia> PkgGraph.to_dot_str(edges; style, bg=:blue, indent=2) |> println
digraph {
bgcolor = "blue"
node [fillcolor="white", fontcolor="black", color="black"]
edge [color="black"]
node [color="red"]
A -> B
yes -> no
}
julia> emptymsg="(empty graph)";
julia> PkgGraph.to_dot_str([]; emptymsg, mode=:dark, style=[]) |> println
digraph {
bgcolor = "transparent"
node [fillcolor="black", fontcolor="white", color="white"]
edge [color="white"]
onlynode [label=\" (empty graph) \", shape=\"plaintext\"]
}
```
"""
@doc readchomp(joinpath(@__DIR__, "to_dot_string.md"))
function to_dot_str(
edges;
mode = :light,
Expand Down
75 changes: 75 additions & 0 deletions src/modules/to_dot_string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
```julia
to_dot_str(
edges;
mode = :light,
bg = "transparent",
style = default_style(),
indent = 4,
emptymsg = nothing,
)
```

Build a string that represents the given directed graph in the
[Graphviz DOT format ↗](https://graphviz.org/doc/info/lang.html).

## Keyword arguments

### `mode`
Either `:light` (default) or `:dark`.\
Whether to use black lines and black text on a white background, or vice
versa.\
Note that locally-generated SVGs get both colour-schemes simultaneously,
so this option is irrelevant for them.

### `bg`
Background colour for the image.\
Default is `"transparent"`.\
`"white"` (in combination with `mode = :light`) might be a sensible
value when you are creating a PNG but do not know on what background it
will be seen. (A light-mode PNG with transparent background looks bad on
a dark background).

### `style`
A list of strings, inserted as lines in the output (after the lines
generated for `mode` and `bg`, and just before the graph edge lines). To
use Graphviz's default style, pass `style = []`. For the default see
[`default_style`](@ref). For more on how dot-graphs can be styled, see
[Styling Graphviz output](@ref).

### `indent`
The number of spaces to indent each line in the "`digraph`" block with.

### `emtpymsg`
If there are no `edges`, a single node with `emptymsg` is created. If
`emptymsg` is `nothing` (default), no nodes are created, and the image
rendered from the DOT-string will be empty.

## Example:

```jldoctest
julia> edges = [:A => :B, "yes" => "no"];
julia> style = ["node [color=\"red\"]"];
julia> using PkgGraph
julia> PkgGraph.to_dot_str(edges; style, bg=:blue, indent=2) |> println
digraph {
bgcolor = "blue"
node [fillcolor="white", fontcolor="black", color="black"]
edge [color="black"]
node [color="red"]
A -> B
yes -> no
}
julia> emptymsg="(empty graph)";
julia> PkgGraph.to_dot_str([]; emptymsg, mode=:dark, style=[]) |> println
digraph {
bgcolor = "transparent"
node [fillcolor="black", fontcolor="white", color="white"]
edge [color="white"]
onlynode [label=" (empty graph) ", shape="plaintext"]
}
```
2 changes: 1 addition & 1 deletion test/integration/all.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using PkgGraph
using Test

@testset "integration" begin
@testset "integration" verbose=true begin
include("deps-as-dot.jl")
include("graphsjl_example.jl")
end
8 changes: 3 additions & 5 deletions test/integration/deps-as-dot.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

using PkgGraph: Options

@testset "deps-as-dot" begin

@test PkgGraph.to_dot_str(:TOML, Options()) ==
@test PkgGraph.depgraph_as_dotstr(:TOML) ==
"""
digraph {
bgcolor = "transparent"
Expand All @@ -17,7 +15,7 @@ using PkgGraph: Options
}
"""

@test PkgGraph.to_dot_str(:TOML, Options(style=[], mode=:dark, bg=:white)) ==
@test PkgGraph.depgraph_as_dotstr(:TOML, style=[], mode=:dark, bg=:white) ==
"""
digraph {
bgcolor = "white"
Expand All @@ -29,7 +27,7 @@ using PkgGraph: Options
}
"""

@test PkgGraph.to_dot_str(:URIs, Options(style=[])) ==
@test PkgGraph.depgraph_as_dotstr(:URIs, style=[]) ==
"""
digraph {
bgcolor = "transparent"
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ using Test
include("unit/all.jl")
include("integration/all.jl")
end

nothing # To not print big "Test.DefaultTestSet(…)", when running manually
1 change: 1 addition & 0 deletions test/unit/DepGraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ using Test
# The latter does not have Graphs in its manifest; so it comes from registry, and there
# deps have different order (also on julia 1.6 there's no registry querying).
# Heeence, why `../standalone` exists.
@assert dirname(Base.active_project()) == dirname(@__DIR__) "Please use [activate_standalone.jl]"
@test PkgGraph.depgraph("Graphs"; jll = false, stdlib = false) == [
"Graphs" => "ArnoldiMethod"
"ArnoldiMethod" => "StaticArrays"
Expand Down

0 comments on commit 6f4a731

Please sign in to comment.