JuliaCI / PkgTemplates.jl

Create new Julia packages, the easy way
https://juliaci.github.io/PkgTemplates.jl
MIT License
640 stars 101 forks source link

Plugin types must be used at most once #361

Open tpgillam opened 2 years ago

tpgillam commented 2 years ago

There is design decision in PkgTemplates that a "plugin" means "an instance of Plugin of a particular concrete type", and that a plugin can be used at most once.

I only managed to figure this out from looking at the source code though — it wasn't obvious from the documentation.

In my particular case, I was wanting to (mis)use the GitHubActions plugin to copy some other files into the .github/workflows subdirectory. So I'd written something like:

Template(;
    ...,
    plugins=[
        ...,
        GitHubActions(),  # To set up default CI
        GitHubActions(file=src_path, destination="my_workflow.yml"),  # To copy another workflow
    ]
)

and was surprised that it didn't work. And that the order in which I put them makes it not work in a different way. Ultimately it's due to this:

https://github.com/invenia/PkgTemplates.jl/blob/47aa85f0247dc5d2a695f97efcc245c37e414325/src/template.jl#L100


My first thought is a question — I assume the fact that each type of plugin can be used at most once is a feature? And expresses the fact that a plugin type shouldn't be a lower level operation like "copy this file from here to here", but rather represent the highest-level thing you could want to do.

If that's true, then I'd propose that we:

  1. Document this design feature explicitly (both in the user guide, and in the developer guide)

  2. Don't fail silently, but rather throw a helpful exception if the unique call above would remove any user-specified plugins.

nickrobinson251 commented 2 years ago

i think we could change this behaviour https://github.com/invenia/PkgTemplates.jl/issues/266 https://github.com/invenia/PkgTemplates.jl/pull/269

jw3126 commented 1 year ago

In my particular case, I was wanting to (mis)use the GitHubActions plugin to copy some other files into the .github/workflows subdirectory. So I'd written something like:

@tpgillam did you find a workaround? What is currently the recommended way to create multiple GitHub actions?

tpgillam commented 1 year ago

@jw3126 in the end I just wrote my own template, inheriting from PkgTemplates.Plugin.

In this case it was for copying over some formatting stuff.. in case it helps, this is what I wrote (note that I haven't used this in anger in some time, so it might be out of date by now - apologies if so!) This is certainly in no way an official recommendation though, will let Nick comment on that...

# For plugin development, need access to internal functions.
using PkgTemplates: @plugin, @with_kw_noshow, Plugin
using PkgTemplates: combined_view, gen_file, render_file, tags

# FIXME This function is a bit of a hack
function default_file(path...)
    return expanduser(joinpath("~", "dotfiles", "julia_template", path...))
end

# TODO This would be nicer if templated on the CI plugin type, but then I can't use
# @plugin, and I'm lazy.
@plugin struct JuliaFormatterGitHubActions <: Plugin
    style::String="blue"
    julia_formatter_toml::String=default_file("JuliaFormatter.toml")
    julia_formatter_yml::String=default_file("julia_formatter.yml")
end

function PkgTemplates.view(p::JuliaFormatterGitHubActions, t::Template, pkg::AbstractString)
    return Dict("FORMAT_STYLE" => p.style)
end

function PkgTemplates.hook(p::JuliaFormatterGitHubActions, t::Template, pkg_dir::AbstractString)
    # Copy over the workflow file.
    outdir = joinpath(pkg_dir, ".github", "workflows")
    mkpath(outdir)
    cp(p.julia_formatter_yml, joinpath(outdir, "julia_formatter.yml"))

    # Generate files.
    pkg = basename(pkg_dir)
    formatter_toml = render_file(p.julia_formatter_toml, combined_view(p, t, pkg), tags(p))
    gen_file(joinpath(pkg_dir, ".JuliaFormatter.toml"), formatter_toml)
end

Then I just added JuliaFormatterGitHubActions() to the list of plugins.

I ended up putting this along with a couple of placeholder files in the same directory as this template

jw3126 commented 1 year ago

Ok thanks!

hannahilea commented 1 year ago

xref https://github.com/JuliaCI/PkgTemplates.jl/issues/266#issuecomment-1656757335