Add new negated-type syntax for disabling default plugins (#167)

This commit is contained in:
Chris de Graaf 2020-05-24 22:02:30 -05:00 committed by GitHub
parent 6409473dbf
commit 35002583b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 16 deletions

View File

@ -16,7 +16,7 @@ The recurring theme is "everything is a plugin now".
| :------------------: | :-------------------------------: |
| `license="ISC"` | `plugins=[License(; name="ISC")]` |
| `develop=true` * | `plugins=[Develop()]` |
| `git=false` | `disable_defaults=[Git]` |
| `git=false` | `plugins=[!Git]` |
| `julia_version=v"1"` | `julia=v"1"` |
| `ssh=true` | `plugins=[Git(; ssh=true)]` |
| `manifest=true` | `plugins=[Git(; manifest=true)]` |

View File

@ -31,7 +31,7 @@ There are a number of plugins available to automate common boilerplate tasks.
### Default Plugins
These plugins are included by default.
They can be overridden by supplying another value via the `plugins` keyword, or disabled by supplying the type via the `disable_defaults` keyword.
They can be overridden by supplying another value, or disabled by negating the type (`!Type`), both as elements of the `plugins` keyword.
```@docs
ProjectFile

View File

@ -5,6 +5,9 @@ function Base.:(==)(a::T, b::T) where T <: Plugin
return all(n -> getfield(a, n) == getfield(b, n), fieldnames(T))
end
struct Disabled{P<:Plugin} end
Base.:(!)(P::Type{<:Plugin}) = Disabled{P}()
"""
Secret(name::AbstractString)

View File

@ -41,10 +41,10 @@ A configuration used to generate packages.
### Template Plugins
- `plugins::Vector{<:Plugin}=Plugin[]`: A list of [`Plugin`](@ref)s used by the template.
- `disable_defaults::Vector{DataType}=DataType[]`: Default plugins to disable.
The default plugins are [`ProjectFile`](@ref), [`SrcDir`](@ref), [`Tests`](@ref),
[`Readme`](@ref), [`License`](@ref), and [`Git`](@ref).
To override a default plugin instead of disabling it altogether, supply it via `plugins`.
To disable a default plugin, pass in the negated type: `!PluginType`.
To override a default plugin instead of disabling it, pass in your own instance.
---
@ -80,11 +80,11 @@ function Template(::Val{false}; kwargs...)
authors isa Vector || (authors = map(strip, split(authors, ",")))
# User-supplied plugins come first, so that deduping the list will remove the defaults.
plugins = Plugin[]
append!(plugins, getkw!(kwargs, :plugins))
disabled = getkw!(kwargs, :disable_defaults)
plugins = Vector{Any}(collect(getkw!(kwargs, :plugins)))
disabled = map(d -> first(typeof(d).parameters), filter(p -> p isa Disabled, plugins))
filter!(p -> p isa Plugin, plugins)
append!(plugins, filter(p -> !(typeof(p) in disabled), default_plugins()))
plugins = sort(unique(typeof, plugins); by=string)
plugins = Vector{Plugin}(sort(unique(typeof, plugins); by=string))
if isempty(user)
foreach(plugins) do p
@ -157,7 +157,6 @@ getkw!(kwargs, k) = pop!(kwargs, k, defaultkw(Template, k))
defaultkw(::Type{T}, s::Symbol) where T = defaultkw(T, Val(s))
defaultkw(::Type{Template}, ::Val{:authors}) = default_authors()
defaultkw(::Type{Template}, ::Val{:dir}) = Pkg.devdir()
defaultkw(::Type{Template}, ::Val{:disable_defaults}) = DataType[]
defaultkw(::Type{Template}, ::Val{:host}) = "github.com"
defaultkw(::Type{Template}, ::Val{:julia}) = default_version()
defaultkw(::Type{Template}, ::Val{:plugins}) = Plugin[]

View File

@ -1,6 +1,6 @@
@testset "Git repositories" begin
@testset "Does not create Git repo" begin
t = tpl(; disable_defaults=[Git])
t = tpl(; plugins=[!Git])
with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)
@test !isdir(joinpath(pkg_dir, ".git"))
@ -47,7 +47,7 @@
@testset "Adds version to commit message" begin
# We're careful to avoid a Pkg.update as it triggers Cassette#130.
t = tpl(; disable_defaults=[Tests], plugins=[Git()])
t = tpl(; plugins=[Git(), !Tests])
mock(PT.version_of => _p -> v"1.2.3") do _i
with_pkg(t) do pkg
pkg_dir = joinpath(t.dir, pkg)

View File

@ -3,7 +3,7 @@
@testset "user" begin
mock(PT.default_user => () -> "") do _du
@test_throws ArgumentError Template()
@test isempty(Template(; disable_defaults=[Git]).user)
@test isempty(Template(; plugins=[!Git]).user)
end
mock(PT.default_user => () -> "username") do _du
@test Template().user == "username"
@ -27,9 +27,9 @@
@test tpl(; dir="~/foo").dir == abspath(expanduser("~/foo"))
end
@testset "plugins / disabled_defaults" begin
function test_plugins(plugins, expected, disabled=DataType[])
t = tpl(; plugins=plugins, disable_defaults=disabled)
@testset "plugins" begin
function test_plugins(plugins, expected)
t = tpl(; plugins=plugins)
@test all(map(==, sort(t.plugins; by=string), sort(expected; by=string)))
end
@ -41,7 +41,7 @@
g = Git(; ssh=true)
test_plugins([g], union(setdiff(defaults, [default_g]), [g]))
# Disabling a default plugin.
test_plugins([], setdiff(defaults, [default_g]), [Git])
test_plugins([!Git], setdiff(defaults, [default_g]))
end
@testset "Unsupported keywords warning" begin