From 481d8430e6f4ba4c065cf3edbffd178c4685584a Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Mon, 2 Sep 2019 21:20:33 +0700 Subject: [PATCH] Add show methods --- docs/src/user.md | 16 +++++++++++++++ src/PkgTemplates.jl | 3 ++- src/interactive.jl | 32 +++++++++++++++++++++++++++++ src/plugins/ci.jl | 8 ++++---- src/plugins/citation.jl | 2 +- src/plugins/coverage.jl | 4 ++-- src/plugins/defaults.jl | 6 +++--- src/plugins/documenter.jl | 2 +- test/runtests.jl | 3 ++- test/show.jl | 42 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 105 insertions(+), 13 deletions(-) create mode 100644 src/interactive.jl create mode 100644 test/show.jl diff --git a/docs/src/user.md b/docs/src/user.md index 3d7c85f..0d00db2 100644 --- a/docs/src/user.md +++ b/docs/src/user.md @@ -65,3 +65,19 @@ Documenter ```@docs Citation ``` + +## Saving Templates + +One of the main reasons for PkgTemplates' existence is for new packages to be consistent. +This means using the same template more than once, so we want a way to save a template to be used later. + +Here's my recommendation for loading a template whenever it's needed: + +```julia +function template() + @eval using PkgTemplates + Template(; #= ... =#) +end +``` + +Add this to your `startup.jl`, and you can create your template from anywhere, without incurring any startup cost. diff --git a/src/PkgTemplates.jl b/src/PkgTemplates.jl index 5fd5e06..81a4a38 100644 --- a/src/PkgTemplates.jl +++ b/src/PkgTemplates.jl @@ -10,7 +10,7 @@ using Pkg: Pkg, TOML, PackageSpec using REPL.TerminalMenus: MultiSelectMenu, RadioMenu, request using Mustache: entityMap, render -using Parameters: @with_kw +using Parameters: @with_kw_noshow export Template, @@ -35,5 +35,6 @@ abstract type Plugin end include("template.jl") include("generate.jl") include("plugin.jl") +include("interactive.jl") end diff --git a/src/interactive.jl b/src/interactive.jl new file mode 100644 index 0000000..8b80e01 --- /dev/null +++ b/src/interactive.jl @@ -0,0 +1,32 @@ +# const PLUGIN_TYPES = let +# leaves(T::Type) = isconcretetype(T) ? [T] : vcat(map(leaves, subtypes(T))...) +# leaves(Plugin) +# end + +function Base.show(io::IO, p::T) where T <: Plugin + indent = get(io, :indent, 0) + print(io, repeat(' ', indent), T, ":") + foreach(fieldnames(T)) do n + println(io) + print(io, repeat(' ', indent + 2), n, ": ", show_field(getfield(p, n))) + end +end + +show_field(x) = repr(x) +show_field(x::AbstractString) = repr(contractuser(x)) + +function Base.show(io::IO, t::Template) + println(io, "Template:") + foreach(fieldnames(Template)) do n + n === :plugins || println(io, repeat(' ', 2), n, ": ", show_field(getfield(t, n))) + end + if isempty(t.plugins) + print(io, " plugins: None") + else + print(io, repeat(' ', 2), "plugins:") + foreach(values(t.plugins)) do p + println(io) + show(IOContext(io, :indent => 4), p) + end + end +end diff --git a/src/plugins/ci.jl b/src/plugins/ci.jl index d71a5fb..9a43bc5 100644 --- a/src/plugins/ci.jl +++ b/src/plugins/ci.jl @@ -40,7 +40,7 @@ Integrates your packages with [Travis CI](https://travis-ci.com). - `coverage::Bool`: Whether or not to publish code coverage (another code coverage plugin such as [`Codecov`](@ref) must also be included). $EXTRA_VERSIONS_DOC """ -@with_kw struct TravisCI <: BasicPlugin +@with_kw_noshow struct TravisCI <: BasicPlugin file::String = default_file("travis.yml") linux::Bool = true osx::Bool = true @@ -109,7 +109,7 @@ Integrates your packages with [AppVeyor](https://appveyor.com) via [AppVeyor.jl] - `coverage::Bool`: Whether or not to publish code coverage ([`Codecov`](@ref) must also be included). $EXTRA_VERSIONS_DOC """ -@with_kw struct AppVeyor <: BasicPlugin +@with_kw_noshow struct AppVeyor <: BasicPlugin file::String = default_file("appveyor.yml") x86::Bool = false coverage::Bool = true @@ -162,7 +162,7 @@ $EXTRA_VERSIONS_DOC !!! note Code coverage submission from Cirrus CI is not yet supported by [Coverage.jl](https://github.com/JuliaCI/Coverage.jl). """ -@with_kw struct CirrusCI <: BasicPlugin +@with_kw_noshow struct CirrusCI <: BasicPlugin file::String = default_file("cirrus.yml") image::String = "freebsd-12-0-release-amd64" coverage::Bool = true @@ -211,7 +211,7 @@ See [`Documenter`](@ref) for more information. !!! note Nightly Julia is not supported. """ -@with_kw struct GitLabCI <: BasicPlugin +@with_kw_noshow struct GitLabCI <: BasicPlugin file::String = default_file("gitlab-ci.yml") coverage::Bool = true # Nightly has no Docker image. diff --git a/src/plugins/citation.jl b/src/plugins/citation.jl index eb1f6a1..a52469c 100644 --- a/src/plugins/citation.jl +++ b/src/plugins/citation.jl @@ -10,7 +10,7 @@ Creates a `CITATION.bib` file for citing package repositories. - `file::AbstractString`: Template file for `CITATION.bib`. - `readme::Bool`: Whether or not to include a section about citing in the README. """ -@with_kw struct Citation <: BasicPlugin +@with_kw_noshow struct Citation <: BasicPlugin file::String = default_file("CITATION.bib") readme::Bool = false end diff --git a/src/plugins/coverage.jl b/src/plugins/coverage.jl index 064b829..8d030ef 100644 --- a/src/plugins/coverage.jl +++ b/src/plugins/coverage.jl @@ -8,7 +8,7 @@ Sets up code coverage submission from CI to [Codecov](https://codecov.io). ## Keyword Arguments - `file::Union{AbstractString, Nothing}`: Template file for `.codecov.yml`, or `nothing` to create no file. """ -@with_kw struct Codecov <: BasicPlugin +@with_kw_noshow struct Codecov <: BasicPlugin file::Union{String, Nothing} = nothing end @@ -29,7 +29,7 @@ Sets up code coverage submission from CI to [Coveralls](https://coveralls.io). ## Keyword Arguments - `file::Union{AbstractString, Nothing}`: Template file for `.coveralls.yml`, or `nothing` to create no file. """ -@with_kw struct Coveralls <: BasicPlugin +@with_kw_noshow struct Coveralls <: BasicPlugin file::Union{String, Nothing} = nothing end diff --git a/src/plugins/defaults.jl b/src/plugins/defaults.jl index 794479e..53ea086 100644 --- a/src/plugins/defaults.jl +++ b/src/plugins/defaults.jl @@ -30,7 +30,7 @@ By default, it includes badges for other included plugins For example, values of `"README"` or `"README.rst"` might be desired. - `inline_badges::Bool`: Whether or not to put the badges on the same line as the package name. """ -@with_kw struct Readme <: BasicPlugin +@with_kw_noshow struct Readme <: BasicPlugin file::String = default_file("README.md") destination::String = "README.md" inline_badges::Bool = false @@ -114,7 +114,7 @@ Creates a `.gitignore` file. - `ds_store::Bool`: Whether or not to ignore MacOS's `.DS_Store` files. - `dev::Bool`: Whether or not to ignore the directory of locally-developed packages. """ -@with_kw struct Gitignore <: Plugin +@with_kw_noshow struct Gitignore <: Plugin ds_store::Bool = true dev::Bool = true end @@ -142,7 +142,7 @@ Sets up testing for packages. ## Keyword Arguments - `file::AbstractString`: Template file for the `runtests.jl`. """ -@with_kw struct Tests <: BasicPlugin +@with_kw_noshow struct Tests <: BasicPlugin file::String = default_file("runtests.jl") end diff --git a/src/plugins/documenter.jl b/src/plugins/documenter.jl index 8a4950e..cf002a9 100644 --- a/src/plugins/documenter.jl +++ b/src/plugins/documenter.jl @@ -31,7 +31,7 @@ struct Documenter{T<:Union{TravisCI, GitLabCI, Nothing}} <: Plugin make_jl::String index_md::String - # Can't use @with_kw due to some weird precompilation issues. + # Can't use @with_kw_noshow due to some weird precompilation issues. function Documenter{T}(; assets::Vector{<:AbstractString}=String[], makedocs_kwargs::Dict{Symbol}=Dict{Symbol, Any}(), diff --git a/test/runtests.jl b/test/runtests.jl index 94312f9..0f3dfec 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -using Base.Filesystem: path_separator +using Base.Filesystem: contractuser, path_separator using LibGit2: LibGit2, GitCommit, GitRemote, GitRepo using Pkg: Pkg @@ -43,6 +43,7 @@ mktempdir() do dir include("template.jl") include("plugin.jl") include("git.jl") + include("show.jl") # Quite a bit of output depends on the Julia version, # and the test fixtures are made with Julia 1.2. diff --git a/test/show.jl b/test/show.jl new file mode 100644 index 0000000..54b9cac --- /dev/null +++ b/test/show.jl @@ -0,0 +1,42 @@ +const DEFAULTS_DIR = contractuser(PT.DEFAULTS_DIR) +const LICENSE_DIR = contractuser(PT.LICENSE_DIR) + +@testset "Show methods" begin + @testset "Plugins" begin + expected = """ + Readme: + file: "$DEFAULTS_DIR/README.md" + destination: "README.md" + inline_badges: false + """ + @test sprint(show, Readme()) == rstrip(expected) + end + + @testset "Template" begin + expected = """ + Template: + authors: ["$USER"] + develop: true + dir: "$(Pkg.devdir())" + git: true + host: "github.com" + julia_version: v"1.0.0" + manifest: false + ssh: false + user: "$USER" + plugins: + Readme: + file: "$DEFAULTS_DIR/README.md" + destination: "README.md" + inline_badges: false + Tests: + file: "$DEFAULTS_DIR/runtests.jl" + Gitignore: + ds_store: true + dev: true + License: + path: "$LICENSE_DIR/MIT" + destination: "LICENSE" + """ + end +end