PkgTemplates.jl
repository·master·Indexed 20 days ago
https://github.com/juliaci/pkgtemplates.jlA tool for creating new Julia packages in an easy, repeatable, and customizable way. It provides a structured framework for generating package boilerplate and is extensible via a plugin system (Plugin, FilePlugin, and BadgePlugin) to automate tasks such as Git setup, GitHub Actions, Codecov, and documentation generation.
What's inside PkgTemplates.jl
- PkgTemplates.jl is a tool designed to create new Julia packages in an easy, repeatable, and customizable way. It provides a structured framework for generating package boilerplate, ensuring consistency across projects.
How the Template and Package Creation Pipeline works
masterThe package generation process follows a structured pipeline involving validation and multi-stage execution of plugins.
1. Template Construction
When a
Templateis constructed, the system:- Extracts values from keyword arguments.
- Creates the
Templateobject. - Runs
validatefor each plugin to catch configuration errors before generation begins.
2. Package Generation
Once validated, the package is generated by executing plugin hooks in a specific order based on their
priority:prehook: Runs before the main generation stage (e.g., initializing a Git repository).hook: The main stage where files are typically generated or modified.posthook: Runs after the main stage (e.g., committing generated files to Git).
Plugins can use
priorityto ensure they run at the correct time relative to others within the same stage.Extend PkgTemplates with Plugins
masterPkgTemplates is extensible via three types of plugins. You can implement a custom plugin by subtyping one of these:
Plugin: The most powerful type. Provides full control over theprehook,hook, andposthookstages. Use this for complex logic like Git initialization or documentation setup.FilePlugin: A specialized plugin designed for the common task of generating a single templated file. It automates thehookstage by usingsource,destination, andviewmethods.BadgePlugin: Specifically used for managing README badges.
To implement a full
Plugin, you can use the@pluginmacro to simplify struct definition and method implementation.How plugins work in PkgTemplates
masterPlugins are used to add functionality to
Templates, automating boilerplate tasks like CI configuration, documentation setup, or license generation.Plugins can be:
- Included by default: Many common plugins are included automatically.
- Overridden: Provide a different value for the same plugin type.
- Disabled: Use the negation operator
!on the type (e.g.,!Git) within thepluginskeyword to disable a default plugin.
Quickstart: Generate a new package with PkgTemplates
masterTo use PkgTemplates, create a
Templateobject and call it with the desired package name as a string. This will generate the package structure in your current directory.using PkgTemplates t = Template() t("MyPkg")Use custom template files with Mustache syntax
masterMany plugins allow you to specify a custom template file via a
fileargument. PkgTemplates uses Mustache.jl for text templating.Syntax Rules:
- Variables: Use
{{{name}}}for variables. - Escaping: Use triple curlies
{{{key}}}to disable HTML escaping (recommended for most files). Use double curlies{{key}}if you want escaping. - Truthiness: Values like
nothing,false, or empty collections are considered 'not existing'. Use{{#key}}...{{/key}}to render content only if the key is truthy. - Iteration:
{{#list}}...{{/list}}iterates over collections. Use{{{.}}}to refer to the current item in the list. - NamedTuples: When using
NamedTuples in a list, you must use the syntax{{{:name}}}instead of{{{name}}}. - Delimiters: If you use custom delimiters like
<<foo>>, use<<&foo>>to disable escaping.
- Variables: Use
Migrate Custom Plugins to the new Extension API
masterCustom plugins written for versions prior to 0.7+ are incompatible with the current version. PkgTemplates 0.7+ uses a new extension API. If you maintain custom plugins, you must refer to the Developer Guide to implement the new interface.Run reference tests locally using Julia 1.7.2
masterThe reference tests in PkgTemplates are designed to run on a specific Julia version (defined by
REFERENCE_JULIA_VERSIONintest/runtests.jl). To ensure correctness and avoid skipping these tests, you should test against Julia 1.7.2.You can use
juliaupto install and switch to this version:# Install Julia 1.7.2 juliaup add 1.7.2 # Start Julia using version 1.7.2 julia +1.7.2Install PkgTemplates
masterInstall
PkgTemplatesusing the Julia package managerPkg. You can do this either from thePkgREPL mode or via the standard Julia REPL.pkg> add PkgTemplatesor
using Pkg; Pkg.add("PkgTemplates")Save and reuse templates
masterTo ensure consistency across multiple packages, you can save your
Templateconfigurations using several methods:1. Function in
startup.jl(Recommended)Define a function that returns your template in your
startup.jlfile. This allows you to call it from anywhere without startup overhead.2. String Representation
Write the string representation of the template to a
.jlfile andincludeit later.3. Serialization
Use the
Serializationstandard library to save the template as a binary file (.bin) anddeserializeit when needed. Note that this format may not be stable across Julia versions.# Method 1: Function approach function template() @eval begin using PkgTemplates Template(; #= ... =#) end end # Method 2: String representation const t = Template(; #= ... =#) open("template.jl", "w") do io println(io, "using PkgTemplates") print(io, t) end # Usage: const t = include("template.jl") # Method 3: Serialization using Serialization const t = Template(; #= ... =#) open(io -> serialize(io, t), "template.bin", "w") # Usage: const t = open(deserialize, "template.bin")Create a package using a Template
masterTo create a package, first instantiate a
Templateobject and then call it with the name of the package you wish to create.Note on Git Configuration: The default
Template()constructor assumes you have the following Git configurations already set up on your system viagit config --global:user.name: Your real name.user.email: Your email address.github.user: Your GitHub username.
using PkgTemplates tpl = Template() tpl("MyPkg")Migrate Documentation plugins in PkgTemplates 0.7+
masterThe documentation plugins have been reworked to use a more explicit
Documenterconstructor with CI provider types. If you were using the old specialized page plugins, migrate to the newDocumentersyntax:- GitHub Pages: Use
Documenter{TravisCI}instead ofGitHubPages. - GitLab Pages: Use
Documenter{GitLabCI}instead ofGitLabPages.
# New way to configure documentation plugins Template( plugins = [ Documenter{TravisCI}() ] )- GitHub Pages: Use