From cd40cb4b7edcb875c6d6a3eeb28d5032b390c18d Mon Sep 17 00:00:00 2001 From: Chris de Graaf Date: Wed, 25 Sep 2019 09:45:08 +0700 Subject: [PATCH] Allow empty username when it's not used --- docs/src/developer.md | 7 ++++--- src/plugin.jl | 8 ++++++++ src/plugins/ci.jl | 2 ++ src/plugins/citation.jl | 2 ++ src/plugins/coverage.jl | 2 ++ src/plugins/documenter.jl | 2 ++ src/plugins/git.jl | 2 ++ src/template.jl | 20 ++++++++++++++------ test/template.jl | 8 +++++--- 9 files changed, 41 insertions(+), 12 deletions(-) diff --git a/docs/src/developer.md b/docs/src/developer.md index 54bab83..96cdf5f 100644 --- a/docs/src/developer.md +++ b/docs/src/developer.md @@ -297,12 +297,13 @@ For more examples, see the plugins in the [Continuous Integration (CI)](@ref) an For an overview of writing template files for Mustache.jl, see [Custom Template Files](@ref) in the user guide. -### Traits +### Predicates -There are a few traits for plugin types that are occassionally used to answer questions like "does this `Template` have any code coverage plugins?". -If you're implementing a plugin that fits into one of the following categories, it would be wise to implement the corresponding trait function to return `true` for your type. +There are a few predicate functions for plugins that are occasionally used to answer questions like "does this `Template` have any code coverage plugins?". +If you're implementing a plugin that fits into one of the following categories, it would be wise to implement the corresponding predicate function to return `true` for instances of your type. ```@docs +needs_username is_ci is_coverage ``` diff --git a/src/plugin.jl b/src/plugin.jl index f202a9d..88c76fb 100644 --- a/src/plugin.jl +++ b/src/plugin.jl @@ -218,6 +218,14 @@ function render_text(text::AbstractString, view::Dict{<:AbstractString}, tags=no return tags === nothing ? render(text, view) : render(text, view; tags=tags) end +""" + needs_username(::Plugin) -> Bool + +Determine whether or not a plugin needs a Git hosting service username to function correctly. +If you are implementing a plugin that uses the `user` field of a [`Template`](@ref), you should implement this function and return `true`. +""" +needs_username(::Plugin) = false + include(joinpath("plugins", "project_file.jl")) include(joinpath("plugins", "src_dir.jl")) include(joinpath("plugins", "tests.jl")) diff --git a/src/plugins/ci.jl b/src/plugins/ci.jl index 7a74387..d582e15 100644 --- a/src/plugins/ci.jl +++ b/src/plugins/ci.jl @@ -261,3 +261,5 @@ If you are adding a CI plugin, you should implement this function and return `tr """ is_ci(::Plugin) = false is_ci(::Union{AppVeyor, TravisCI, CirrusCI, GitLabCI}) = true + +needs_username(::Union{AppVeyor, TravisCI, CirrusCI, GitLabCI}) = true diff --git a/src/plugins/citation.jl b/src/plugins/citation.jl index abd8ba9..805d446 100644 --- a/src/plugins/citation.jl +++ b/src/plugins/citation.jl @@ -24,3 +24,5 @@ view(::Citation, t::Template, pkg::AbstractString) = Dict( "URL" => "https://$(t.host)/$(t.user)/$pkg.jl", "YEAR" => year(today()), ) + +needs_username(::Citation) = true diff --git a/src/plugins/coverage.jl b/src/plugins/coverage.jl index 358ab24..448cc36 100644 --- a/src/plugins/coverage.jl +++ b/src/plugins/coverage.jl @@ -52,3 +52,5 @@ If you are adding a coverage plugin, you should implement this function and retu """ is_coverage(::Plugin) = false is_coverage(::Union{Codecov, Coveralls}) = true + +needs_username(::Union{Codecov, Coveralls}) = true diff --git a/src/plugins/documenter.jl b/src/plugins/documenter.jl index 1623878..0963bfc 100644 --- a/src/plugins/documenter.jl +++ b/src/plugins/documenter.jl @@ -110,3 +110,5 @@ gitlab_pages_url(t::Template, pkg::AbstractString) = "https://$(t.user).gitlab.i make_canonical(::Type{TravisCI}) = github_pages_url make_canonical(::Type{GitLabCI}) = gitlab_pages_url make_canonical(::Type{Nothing}) = nothing + +needs_username(::Documenter) = true diff --git a/src/plugins/git.jl b/src/plugins/git.jl index a94c855..5b413ab 100644 --- a/src/plugins/git.jl +++ b/src/plugins/git.jl @@ -88,3 +88,5 @@ function commit(p::Git, repo::GitRepo, pkg_dir::AbstractString, msg::AbstractStr LibGit2.commit(repo, msg) end end + +needs_username(::Git) = true diff --git a/src/template.jl b/src/template.jl index 8956947..93d39d6 100644 --- a/src/template.jl +++ b/src/template.jl @@ -19,7 +19,7 @@ A configuration used to generate packages. ### User Options - `user::AbstractString="$(default_user())"`: GitHub (or other code hosting service) username. The default value comes from the global Git config (`github.user`). - If no value is obtained, an `ArgumentError` is thrown. + If no value is obtained, many plugins that use this value will not work. - `authors::Union{AbstractString, Vector{<:AbstractString}}="$(default_authors())"`: Package authors. Like `user`, it takes its default value from the global Git config (`user.name` and `user.email`). @@ -61,15 +61,13 @@ Template(; interactive::Bool=false, kwargs...) = Template(Val(interactive); kwar # Non-interactive constructor. function Template(::Val{false}; kwargs...) user = getkw(kwargs, :user) - isempty(user) && throw(ArgumentError("No user set, please pass user=username")) - - authors = getkw(kwargs, :authors) - authors isa Vector || (authors = map(strip, split(authors, ","))) - dir = abspath(expanduser(getkw(kwargs, :dir))) host = replace(getkw(kwargs, :host), r".*://" => "") julia_version = getkw(kwargs, :julia_version) + authors = getkw(kwargs, :authors) + 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)) @@ -77,6 +75,16 @@ function Template(::Val{false}; kwargs...) append!(plugins, filter(p -> !(typeof(p) in disabled), default_plugins())) plugins = sort(unique(typeof, plugins); by=string) + if isempty(user) + foreach(plugins) do p + if needs_username(p) + T = nameof(typeof(p)) + s = "$T: Git hosting service username is required, supply user=username" + throw(ArgumentError(s)) + end + end + end + return Template(authors, dir, host, julia_version, plugins, user) end diff --git a/test/template.jl b/test/template.jl index a28b76f..b4b583c 100644 --- a/test/template.jl +++ b/test/template.jl @@ -1,11 +1,13 @@ @testset "Template" begin @testset "Template constructor" begin @testset "user" begin - if isempty(PT.default_user()) + mock(PT.default_user => () -> "") do _du @test_throws ArgumentError Template() - haskey(ENV, "CI") && run(`git config --global github.user $USER`) + @test isempty(Template(; disable_defaults=[Git]).user) + end + mock(PT.default_user => () -> "username") do _du + @test Template().user == PT.default_user() end - @test Template().user == PT.default_user() end @testset "authors" begin