hatch icon indicating copy to clipboard operation
hatch copied to clipboard

[Feature Request] Better project template configuration options

Open bavalpey opened this issue 1 year ago • 6 comments

I really like hatch. In fact, I've been using it a lot. However, the one area where hatch is weakest is in project templates. We have almost no ability to modify how project templates are created (or at least, none documented). The documentation for the configuration options is lacking, and the blogposts don't seem to be paying any attention to it, either.

First, licenses should be always put in the LICENSES/ directory regardless if there is more than one license. This is in accordance with the REUSE spec. If there is one license, it is fine to have it in the project root as well, but the creation of the LICENSES directory should not be conditional.

Second, there is currently no way to adjust the copyright text in headers. For instance, it always uses the author for the copyright text. This is insufficient for a good percentage of projects, where the author is different from the copyright holder (e.g., where work done is copyrighted by the company and not the author).

Third, is there support for defining arbitrary project template structures? What if I want to add, say, a .editorconfig to the project root. Or maybe I want a default .gitignore, or to change the placeholder text in README.md. If there are ways of doing this already, they are not communicated to the user.

bavalpey avatar Aug 30 '24 16:08 bavalpey

Although not documented, it is possible to create plugins that augment, or completely replace the built in templating. You'll need make a class derived from TemplateInterface and set up the hooks / entry points as described in the plugins section of the docs. Reading the source for the default template will give you a good idea of how to do it.

Once you've created your plugin, you'll need to update your hatch config and add your plugin and its configuration (you can find your configuration with hatch config find).

[template.plugins.YOUR-PLUGIN]  # <<<< add your plugin here
my-opt1 = true

[template.plugins.default]  # <<<< remove this entire section if you want to completely replace the default template
tests = true
ci = false
src-layout = true

djcopley avatar Aug 30 '24 18:08 djcopley

Once you've created your plugin, you'll need to update your hatch config and add your plugin and its configuration (you can find your configuration with hatch config find).

Ok, where exactly does the code for my plugin have to be in order for hatch to locate it? Is there a plugin directory somewhere? Do I have to publish it and it gets installed?

The plugin section of the docs answers none of these questions.

bavalpey avatar Aug 30 '24 19:08 bavalpey

These are python-isms, not necessarily just hatch things.

The plugin has to be accessible to the build system, to do that, declare the plugin as a build time dependency in your pyproject.toml. Typically, these are pip installable, however, you can also specify python distributions that exist locally.

[build-system]
requires = ["hatchling", "hatch-your-plugin @ <PATH_TO_YOUR_PLUGIN>"]
build-backend = "hatchling.build"

djcopley avatar Aug 30 '24 20:08 djcopley

The plugin has to be accessible to the build system, to do that, declare the plugin as a build time dependency in your pyproject.toml. Typically, these are pip installable, however, you can also specify python distributions that exist locally.

Okay this is what doesn't make any sense to me. This is like a chicken-and-egg problem. The point is that the plugin augments the behavior of hatch new which is what creates the pyproject.toml in the first place. How can I specify that hatch should use my plugin if pyproject.toml doesn't exist?

I assume I need to tell hatchling about my plugin in hatch's global configuration file. Is this not the case?

bavalpey avatar Aug 30 '24 21:08 bavalpey

Sorry yes, you're correct. You'll need to install your template plugin into whatever environment hatch is running from. If you installed hatch with pipx, you can use pipx inject. If you used something else, you'll need to figure out where hatch's python environment lives, activate it, and pip install your plugin into it. I've always used pipx so I don't have any specifics for the other installation methods.

djcopley avatar Aug 30 '24 21:08 djcopley

If you used something else, you'll need to figure out where hatch's python environment lives, activate it, and pip install your plugin into it. I've always used pipx so I don't have any specifics for the other installation methods.

Hmm. I'm using the binary distribution from the .tar.gz No idea how to inject it there...

Thanks for all the help!

bavalpey avatar Aug 30 '24 23:08 bavalpey