Simplify internal API

This commit is contained in:
Chris de Graaf 2018-09-28 15:30:10 -05:00
parent ab2829a6be
commit 9c6a160bc2
12 changed files with 106 additions and 147 deletions

View File

@ -43,7 +43,7 @@ function generate(pkg_name::AbstractString, t::Template)
gen_readme(pkg_dir, t),
gen_gitignore(pkg_dir, t),
gen_license(pkg_dir, t),
vcat(map(p -> gen_plugin(p, t, t.dir, pkg_name), values(t.plugins))...),
vcat(map(p -> gen_plugin(p, t, pkg_name), values(t.plugins))...),
)
LibGit2.add!(repo, files...)

View File

@ -56,7 +56,7 @@ Generic plugins are plugins that add any number of patterns to the generated pac
end
end
interactive(plugin_type::Type{MyPlugin}) = interactive(plugin_type; file="my-plugin.toml")
interactive(::Type{MyPlugin}) = interactive(MyPlugin; file="my-plugin.toml")
```
The above plugin ignores files ending with `.mgp`, copies `defaults/my-plugin.toml` by
@ -101,12 +101,7 @@ pattern. They can implement [`gen_plugin`](@ref), [`badges`](@ref), and
MyPlugin() = new([], rand() > 0.8)
function gen_plugin(
plugin::MyPlugin,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
function gen_plugin(p::MyPlugin, t::Template, pkg_name::AbstractString)
if plugin.lucky
text = substitute(
"You got lucky with {{PKGNAME}}, {{USER}}!",
@ -137,7 +132,7 @@ pattern. They can implement [`gen_plugin`](@ref), [`badges`](@ref), and
end
end
interactive(plugin_type::Type{MyPlugin}) = MyPlugin()
interactive(t:Type{MyPlugin}) = MyPlugin()
```
This plugin doesn't do much, but it demonstrates how [`gen_plugin`](@ref), [`badges`](@ref)
@ -175,96 +170,73 @@ Return `badge`'s data formatted as a Markdown string.
format(b::Badge) = "[![$(b.hover)]($(b.image))]($(b.link))"
"""
gen_plugin(
plugin::Plugin,
template::Template,
dir::AbstractString,
pkg_name::AbstractString
) -> Vector{String}
gen_plugin(p::Plugin, t::Template, pkg_name::AbstractString) -> Vector{String}
Generate any files associated with a plugin.
# Arguments
* `plugin::Plugin`: Plugin whose files are being generated.
* `template::Template`: Template configuration.
* `dir::AbstractString`: The directory in which the files will be generated. Note that
this will be joined to `pkg_name`.
* `p::Plugin`: Plugin whose files are being generated.
* `t::Template`: Template configuration.
* `pkg_name::AbstractString`: Name of the package.
Returns an array of generated file/directory names.
"""
function gen_plugin(
plugin::Plugin,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
return String[]
end
gen_plugin(::Plugin, ::Template, ::AbstractString) = String[]
function gen_plugin(
plugin::GenericPlugin,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
if plugin.src === nothing
function gen_plugin(p::GenericPlugin, t::Template, pkg_name::AbstractString)
if p.src === nothing
return String[]
end
text = substitute(
read(plugin.src, String),
template;
view=merge(Dict("PKGNAME" => pkg_name), plugin.view),
read(p.src, String),
t;
view=merge(Dict("PKGNAME" => pkg_name), p.view),
)
gen_file(joinpath(dir, pkg_name, plugin.dest), text)
return [plugin.dest]
gen_file(joinpath(t.dir, pkg_name, p.dest), text)
return [p.dest]
end
"""
badges(plugin::Plugin, user::AbstractString, pkg_name::AbstractString) -> Vector{String}
badges(p::Plugin, user::AbstractString, pkg_name::AbstractString) -> Vector{String}
Generate Markdown badges for the plugin.
# Arguments
* `plugin::Plugin`: Plugin whose badges we are generating.
* `p::Plugin`: Plugin whose badges we are generating.
* `user::AbstractString`: Username of the package creator.
* `pkg_name::AbstractString`: Name of the package.
Returns an array of Markdown badges.
"""
badges(plugin::Plugin, user::AbstractString, pkg_name::AbstractString) = String[]
badges(::Plugin, ::AbstractString, ::AbstractString) = String[]
function badges(plugin::GenericPlugin, user::AbstractString, pkg_name::AbstractString)
function badges(p::GenericPlugin, user::AbstractString, pkg_name::AbstractString)
# Give higher priority to replacements defined in the plugin's view.
view = merge(Dict("USER" => user, "PKGNAME" => pkg_name), plugin.view)
return map(b -> substitute(format(b), view), plugin.badges)
view = merge(Dict("USER" => user, "PKGNAME" => pkg_name), p.view)
return map(b -> substitute(format(b), view), p.badges)
end
"""
interactive(
plugin_type::Type{<:Plugin};
file::Union{AbstractString, Nothing}="",
) -> Plugin
interactive(t::Type{<:Plugin}; file::Union{AbstractString, Nothing}="") -> Plugin
Interactively create a plugin of type `plugin_type`, where `file` is the plugin type's
default config template with a non-standard name (for `MyPlugin`, this is anything but
Interactively create a plugin of type `t`, where `file` is the plugin type's default
config template with a non-standard name (for `MyPlugin`, this is anything but
"myplugin.yml").
"""
function interactive(
plugin_type::Type{<:GenericPlugin};
file::Union{AbstractString, Nothing}="",
)
plugin_name = string(nameof(plugin_type))
function interactive(t::Type{<:GenericPlugin}; file::Union{AbstractString, Nothing}="")
name = string(nameof(t))
# By default, we expect the default plugin file template for a plugin called
# "MyPlugin" to be called "myplugin.yml".
fn = file != nothing && isempty(file) ? "$(lowercase(plugin_name)).yml" : file
fn = file != nothing && isempty(file) ? "$(lowercase(name)).yml" : file
default_config_file = fn == nothing ? fn : joinpath(DEFAULTS_DIR, fn)
print("$plugin_name: Enter the config template filename (\"None\" for no file) ")
print("$name: Enter the config template filename (\"None\" for no file) ")
if default_config_file == nothing
print("[None]: ")
else
print("[$(replace(default_config_file, homedir() => "~"))]: ")
end
config_file = readline()
config_file = if uppercase(config_file) == "NONE"
nothing
@ -273,5 +245,6 @@ function interactive(
else
config_file
end
return plugin_type(; config_file=config_file)
return t(; config_file=config_file)
end

View File

@ -4,34 +4,34 @@ generation via [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl).
"""
abstract type Documenter <: CustomPlugin end
function gen_plugin(
plugin::Documenter,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
path = joinpath(dir, pkg_name)
function gen_plugin(p::Documenter, t::Template, pkg_name::AbstractString)
path = joinpath(t.dir, pkg_name)
docs_dir = joinpath(path, "docs", "src")
mkpath(docs_dir)
if !isempty(plugin.assets)
assets_string = if !isempty(p.assets)
mkpath(joinpath(docs_dir, "assets"))
for file in plugin.assets
for file in p.assets
cp(file, joinpath(docs_dir, "assets", basename(file)))
end
# We want something that looks like the following:
# [
# assets/file1,
# assets/file2,
# ]
tab = repeat(" ", 4)
assets_string = "[\n"
for asset in plugin.assets
assets_string *= """$(tab^2)"assets/$(basename(asset))",\n"""
s = "[\n"
for asset in p.assets
s *= """$(tab^2)"assets/$(basename(asset))",\n"""
end
assets_string *= "$tab]"
s *= "$tab]"
s
else
assets_string = "[]"
"[]"
end
text = """
using Documenter, $pkg_name
@ -41,20 +41,21 @@ function gen_plugin(
pages=[
"Home" => "index.md",
],
repo="https://$(template.host)/$(template.user)/$pkg_name.jl/blob/{commit}{path}#L{line}",
repo="https://$(t.host)/$(t.user)/$pkg_name.jl/blob/{commit}{path}#L{line}",
sitename="$pkg_name.jl",
authors="$(template.authors)",
authors="$(t.authors)",
assets=$assets_string,
)
"""
gen_file(joinpath(dirname(docs_dir), "make.jl"), text)
open(joinpath(docs_dir, "index.md"), "w") do fp
write(fp, "# $pkg_name")
end
readme_path = joinpath(dir, pkg_name, "README.md")
# If the README exists, use it as the default docs.
readme_path = joinpath(t.dir, pkg_name, "README.md")
if isfile(readme_path)
cp(readme_path, joinpath(docs_dir, "index.md"), force=true)
cp(readme_path, joinpath(docs_dir, "index.md"))
else
gen_file(joinpath(docs_dir, "index.md"), "# $pkg_name")
end
end
@ -77,8 +78,8 @@ function Base.show(io::IO, p::Documenter)
n > 0 && print(io, ": $(join(map(g -> "\"$g\"", p.gitignore), ", "))")
end
function interactive(plugin_type::Type{<:Documenter})
t = nameof(plugin_type)
print("$t: Enter any Documenter asset files (separated by spaces) []: ")
return plugin_type(; assets=String.(split(readline())))
function interactive(t::Type{<:Documenter})
name = string(nameof(t))
print("$name: Enter any Documenter asset files (separated by spaces) []: ")
return t(; assets=string.(split(readline())))
end

View File

@ -38,25 +38,18 @@ function badges(::GitHubPages, user::AbstractString, pkg_name::AbstractString)
]
end
function gen_plugin(
plugin::GitHubPages,
template::Template,
dir::AbstractString,
pkg_name::AbstractString,
)
invoke(
gen_plugin, Tuple{Documenter, Template, AbstractString, AbstractString},
plugin, template, dir, pkg_name,
)
if haskey(template.plugins, TravisCI)
docs_src = joinpath(dir, pkg_name, "docs", "src")
function gen_plugin(p::GitHubPages, t::Template, pkg_name::AbstractString)
invoke(gen_plugin, Tuple{Documenter, Template, AbstractString}, p, t, pkg_name)
if haskey(t.plugins, TravisCI)
docs_src = joinpath(t.dir, pkg_name, "docs", "src")
open(joinpath(dirname(docs_src), "make.jl"), "a") do file
write(
file,
"""
deploydocs(;
repo="github.com/$(template.user)/$pkg_name.jl",
repo="$(t.host)/$(t.user)/$pkg_name.jl",
target="build",
julia="1.0",
deps=nothing,

View File

@ -55,11 +55,11 @@ generated repositories, and appropriate badge(s) to the README.
end
end
function interactive(plugin_type::Type{GitLabCI})
function interactive(::Type{GitLabCI})
name = "GitLabCI"
kwargs = Dict{Symbol, Any}()
default_config_file = joinpath(DEFAULTS_DIR, "gitlab-ci.yml")
print("$name: Enter the config template filename (\"None\" for no file) ")
print("[$default_config_file]: ")
config_file = readline()

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "AppVeyor" begin
@testset "Plugin creation" begin
@ -31,7 +30,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
# Without a coverage plugin in the template, there should be no post-test step.
p = AppVeyor()
@test gen_plugin(p, t, temp_dir, test_pkg) == [".appveyor.yml"]
@test gen_plugin(p, t, test_pkg) == [".appveyor.yml"]
@test isfile(joinpath(pkg_dir, ".appveyor.yml"))
appveyor = read(joinpath(pkg_dir, ".appveyor.yml"), String)
@test !occursin("on_success", appveyor)
@ -40,7 +39,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
# Generating the plugin with CodeCov in the template should create a post-test step.
t.plugins[CodeCov] = CodeCov()
gen_plugin(p, t, temp_dir, test_pkg)
gen_plugin(p, t, test_pkg)
delete!(t.plugins, CodeCov)
appveyor = read(joinpath(pkg_dir, ".appveyor.yml"), String)
@test occursin("on_success", appveyor)
@ -50,9 +49,9 @@ pkg_dir = joinpath(temp_dir, test_pkg)
# TODO: Add Coveralls tests when AppVeyor.jl supports it.
p = AppVeyor(; config_file=nothing)
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
@test !isfile(joinpath(pkg_dir, ".appveyor.yml"))
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "CodeCov" begin
@testset "Plugin creation" begin
@ -30,12 +29,13 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
p = CodeCov()
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test !isfile(joinpath(pkg_dir, ".codecov.yml"))
p = CodeCov(; config_file=nothing)
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
@test !isfile(joinpath(pkg_dir, ".codecov.yml"))
p = CodeCov(; config_file=test_file)
@test gen_plugin(p, t, test_pkg) == [".codecov.yml"]
@test isfile(joinpath(pkg_dir, ".codecov.yml"))
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "Coveralls" begin
@testset "Plugin creation" begin
@ -30,13 +29,12 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
p = Coveralls()
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
@test !isfile(joinpath(pkg_dir, ".coveralls.yml"))
p = Coveralls(; config_file=test_file)
@test gen_plugin(p, t, temp_dir, test_pkg) == [".coveralls.yml"]
@test gen_plugin(p, t, test_pkg) == [".coveralls.yml"]
@test isfile(joinpath(pkg_dir, ".coveralls.yml"))
rm(joinpath(pkg_dir, ".coveralls.yml"))
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "GitHubPages" begin
@testset "Plugin creation" begin
@ -22,7 +21,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
p = GitHubPages()
@test gen_plugin(p, t, temp_dir, test_pkg) == ["docs/"]
@test gen_plugin(p, t, test_pkg) == ["docs/"]
@test isdir(joinpath(pkg_dir, "docs"))
@test isfile(joinpath(pkg_dir, "docs", "make.jl"))
make = readchomp(joinpath(pkg_dir, "docs", "make.jl"))
@ -34,7 +33,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@test index == "# $test_pkg"
rm(joinpath(pkg_dir, "docs"); recursive=true)
p = GitHubPages(; assets=[test_file])
@test gen_plugin(p, t, temp_dir, test_pkg) == ["docs/"]
@test gen_plugin(p, t, test_pkg) == ["docs/"]
make = readchomp(joinpath(pkg_dir, "docs", "make.jl"))
# Check the formatting of the assets list.
@test occursin(
@ -48,16 +47,17 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@test isfile(joinpath(pkg_dir, "docs", "src", "assets", basename(test_file)))
rm(joinpath(pkg_dir, "docs"); recursive=true)
t.plugins[TravisCI] = TravisCI()
@test gen_plugin(p, t, temp_dir, test_pkg) == ["docs/"]
@test gen_plugin(p, t, test_pkg) == ["docs/"]
make = readchomp(joinpath(pkg_dir, "docs", "make.jl"))
@test occursin("deploydocs", make)
rm(joinpath(pkg_dir, "docs"); recursive=true)
end
@testset "Package generation with GitHubPages plugin" begin
t = Template(; user=me, plugins=[GitHubPages()])
temp_dir = mktempdir()
t = Template(; user=me, dir=temp_dir, plugins=[GitHubPages()])
generate(test_pkg, t)
pkg_dir = joinpath(default_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
# Check that the gh-pages branch exists.
repo = LibGit2.GitRepo(pkg_dir)
@ -68,8 +68,8 @@ pkg_dir = joinpath(temp_dir, test_pkg)
readme = read(joinpath(pkg_dir, "README.md"), String)
index = read(joinpath(pkg_dir, "docs", "src", "index.md"), String)
@test readme == index
rm(pkg_dir; recursive=true)
rm(temp_dir; recursive=true)
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "GitLabCI" begin
@testset "Plugin creation" begin
@ -47,7 +46,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
p = GitLabCI()
@test gen_plugin(p, t, temp_dir, test_pkg) == [".gitlab-ci.yml"]
@test gen_plugin(p, t, test_pkg) == [".gitlab-ci.yml"]
@test isfile(joinpath(pkg_dir, ".gitlab-ci.yml"))
gitlab = read(joinpath(pkg_dir, ".gitlab-ci.yml"), String)
# The default plugin should enable the coverage step.
@ -55,16 +54,16 @@ pkg_dir = joinpath(temp_dir, test_pkg)
rm(joinpath(pkg_dir, ".gitlab-ci.yml"))
p = GitLabCI(; coverage=false)
gen_plugin(p, t, temp_dir, test_pkg)
gen_plugin(p, t, test_pkg)
gitlab = read(joinpath(pkg_dir, ".gitlab-ci.yml"), String)
# If coverage is false, there should be no coverage step.
@test !occursin("using Coverage", gitlab)
rm(joinpath(pkg_dir, ".gitlab-ci.yml"))
p = GitLabCI(; config_file=nothing)
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
@test !isfile(joinpath(pkg_dir, ".gitlab-ci.yml"))
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -1,6 +1,5 @@
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
@testset "TravisCI" begin
@testset "Plugin creation" begin
@ -31,7 +30,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
@testset "File generation" begin
# Without a coverage plugin in the template, there should be no post-test step.
p = TravisCI()
@test gen_plugin(p, t, temp_dir, test_pkg) == [".travis.yml"]
@test gen_plugin(p, t, test_pkg) == [".travis.yml"]
@test isfile(joinpath(pkg_dir, ".travis.yml"))
travis = read(joinpath(pkg_dir, ".travis.yml"), String)
@ -43,7 +42,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
# Generating the plugin with CodeCov in the template should create a post-test step.
t.plugins[CodeCov] = CodeCov()
gen_plugin(p, t, temp_dir, test_pkg)
gen_plugin(p, t, test_pkg)
delete!(t.plugins, CodeCov)
travis = read(joinpath(pkg_dir, ".travis.yml"), String)
@test occursin("after_success", travis)
@ -54,7 +53,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
# Coveralls should do the same.
t.plugins[Coveralls] = Coveralls()
gen_plugin(p, t, temp_dir, test_pkg)
gen_plugin(p, t, test_pkg)
delete!(t.plugins, Coveralls)
travis = read(joinpath(pkg_dir, ".travis.yml"), String)
@test occursin("after_success", travis)
@ -65,7 +64,7 @@ pkg_dir = joinpath(temp_dir, test_pkg)
# With a Documenter plugin, there should be a docs deployment step.
t.plugins[GitHubPages] = GitHubPages()
gen_plugin(p, t, temp_dir, test_pkg)
gen_plugin(p, t, test_pkg)
delete!(t.plugins, GitHubPages)
travis = read(joinpath(pkg_dir, ".travis.yml"), String)
@test occursin("after_success", travis)
@ -75,9 +74,9 @@ pkg_dir = joinpath(temp_dir, test_pkg)
rm(joinpath(pkg_dir, ".travis.yml"))
p = TravisCI(; config_file=nothing)
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
@test !isfile(joinpath(pkg_dir, ".travis.yml"))
end
end
rm(temp_dir; recursive=true)
rm(pkg_dir; recursive=true)

View File

@ -368,8 +368,7 @@ end
@testset "Plugins" begin
t = Template(; user=me)
temp_dir = mktempdir()
pkg_dir = joinpath(temp_dir, test_pkg)
pkg_dir = joinpath(t.dir, test_pkg)
# Check badge constructor and formatting.
badge = Badge("A", "B", "C")
@ -380,11 +379,11 @@ end
p = Bar()
@test isempty(badges(p, me, test_pkg))
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
p = Baz()
@test isempty(badges(p, me, test_pkg))
@test isempty(gen_plugin(p, t, temp_dir, test_pkg))
@test isempty(gen_plugin(p, t, test_pkg))
include(joinpath("plugins", "travisci.jl"))
include(joinpath("plugins", "appveyor.jl"))
@ -392,8 +391,6 @@ end
include(joinpath("plugins", "codecov.jl"))
include(joinpath("plugins", "coveralls.jl"))
include(joinpath("plugins", "githubpages.jl"))
rm(temp_dir; recursive=true)
end
rm(test_file)