Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement fixup function #288

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/PkgTemplates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export
SrcDir,
TagBot,
Tests,
TravisCI
TravisCI,
fixup

"""
Plugins are PkgTemplates' source of customization and extensibility.
Expand All @@ -48,10 +49,19 @@ When implementing a new plugin, subtype this type to have full control over its
"""
abstract type Plugin end

"""
isfixable(::Plugin, pkg_dir) -> Bool

Determines whether or not the plugin can be updated on an existing project via
[`fixup`](@ref).
"""
isfixable(::Plugin, pkg_dir) = false

include("template.jl")
include("plugin.jl")
include("show.jl")
include("interactive.jl")
include("fixup.jl")
include("deprecated.jl")

# Run some function with a project activated at the given path.
Expand Down
15 changes: 15 additions & 0 deletions src/fixup.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function fixup(tpl::Template, pkg_dir)
pkg_dir = realpath(pkg_dir)
ispath(pkg_dir) || throw(ArgumentError("Not a directory."))
isdir(joinpath(pkg_dir, "src")) || throw(ArgumentError("No `src/` directory."))

fixable = filter(p -> isfixable(p, pkg_dir), tpl.plugins)
foreach((prehook, hook, posthook)) do h
@info "Running $(nameof(h))s"
foreach(sort(fixable; by=p -> priority(p, h), rev=true)) do p
h(p, tpl, pkg_dir)
end
end
@info "Fixed up package at $pkg_dir"
# TODO: some magic to add badges to an existing Readme?!
end
11 changes: 11 additions & 0 deletions src/plugin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ This function **must** be implemented.
"""
function destination end

"""
isfixable(p::FilePlugin) -> Bool

Determines whether or not [`fixup`](@ref) should update the files created by `p`.

By default, returns `true` if the [`destination(p)`](@ref) file does not exist.
Subtype of [`FilePlugin`](@ref) should implement their own method if they require
different behaviour.
"""
isfixable(p::FilePlugin, pkg_dir) = !isfile(joinpath(pkg_dir, destination(p)))

"""
Badge(hover::AbstractString, image::AbstractString, link::AbstractString)

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/documenter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ function validate(p::Documenter{T}, t::Template) where T <: YesDeploy
end
end

# Do not edit existing docs.
isfixable(::Documenter, pkg_dir) = !isdir(joinpath(pkg_dir, "docs"))

function hook(p::Documenter, t::Template, pkg_dir::AbstractString)
pkg = basename(pkg_dir)
docs_dir = joinpath(pkg_dir, "docs")
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ function validate(p::Git, t::Template)
end
end

# fixup only if pkg_dir not a git repo
function isfixable(::Git, pkg_dir)
try
r = GitRepo(pkg_dir)
return !isa(r, GitRepo)
catch
return true
end
end

# Set up the Git repository.
function prehook(p::Git, t::Template, pkg_dir::AbstractString)
LibGit2.with(LibGit2.init(pkg_dir)) do repo
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/license.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ view(::License, t::Template, ::AbstractString) = Dict(
"YEAR" => year(today()),
)

function isfixable(::License, pkg_dir)
return !any(isfile, joinpath.(pkg_dir, ("LICENSE", "LICENSE.md")))
end

function prompt(::Type{License}, ::Type, ::Val{:name})
options = readdir(default_file("licenses"))
# Move MIT to the top.
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/project_file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ priority(::ProjectFile, ::typeof(hook)) = typemax(Int) - 5

function hook(p::ProjectFile, t::Template, pkg_dir::AbstractString)
toml = Dict(
"name" => basename(pkg_dir),
"name" => let pkg = basename(pkg_dir)
endswith(pkg, ".jl") ? pkg[1:end-3] : pkg
end,
"uuid" => string(uuid4()),
"authors" => t.authors,
"version" => string(p.version),
Expand All @@ -38,3 +40,7 @@ function compat_version(v::VersionNumber)
"$(v.major).$(v.minor).$(v.patch)"
end
end

function isfixable(::ProjectFile, pkg_dir)
return !any(isfile, joinpath.(pkg_dir, ("Project.toml", "JuliaProject.toml")))
end
3 changes: 3 additions & 0 deletions src/plugins/src_dir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ view(::SrcDir, ::Template, pkg::AbstractString) = Dict("PKG" => pkg)
function prehook(p::SrcDir, t::Template, pkg_dir::AbstractString)
p.destination = joinpath("src", basename(pkg_dir) * ".jl")
end

# TODO: should this return `true` if `src/` exists but `src/pkg_name.jl` doesn't?
isfixable(p::SrcDir, pkg_dir) = false