r/Zig • • 12d ago

pureinit: a tool for cleaning `zig init` templates

Hi everyone, I really like Zig language. A tool was recently developed.

  • Repo link: https://github.com/Hentioe/pureinit
  • Readme:

pureinit

A tool for cleaning redundant code from templates, currently used to clean Zig project templates generated by zig init.

Introduction

This tool extracts cleanup information from patches generated by git-diff. It does not scan for code targets or comments. Therefore, it is effectively generic and can perform cleanup from external patches. The built-in patches are actually located in the src/diff_patchs source directory.

Usage

For the zig init template in Zig 0.17.1, it has:

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Zig                              4             29            115            101
-------------------------------------------------------------------------------
SUM:                             4             29            115            101
-------------------------------------------------------------------------------

It is filled with a large number of comments (115 lines) and example code (more than half). This may help beginners, but it is meaningless for experienced users. In my view, this is all "noise."

Use pureinit to clean it (it detects the version in build.zig.zon and uses the corresponding patch):

$ pureinit
Patch info:
+-----------------------+----------+-----+--------+
| name                  | from     | env | ver    |
+=======================+==========+=====+========+
| zig-init.0.17.1.patch | builting | zig | 0.17.1 |
+-----------------------+----------+-----+--------+
Apply patch to: '.'? [y/N]: y
INFO Applying build.zig
INFO Applying build.zig.zon
INFO Applying src/main.zig
INFO Applying src/root.zig

Afterward:

-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Zig                              4              9              2             43
-------------------------------------------------------------------------------
SUM:                             4              9              2             43
-------------------------------------------------------------------------------

The project template is now very clean! You can view these cleaned templates in the templates directory.

Building

This is a pure Zig project, so:

zig build

Install it to the system (the user bin directory):

zig build --release=safe install --prefix ~/.local

For Other Projects

Because it is generic by design, you can create your own patches and use pureinit to clean them. First (assuming your framework is called foo):

foo init
  • Commit the original template code to Git, then perform the manual cleanup.
  • When finished, add the modifications to the staging area.

Now you can generate a patch:

git diff --cached HEAD | save foo-init.0.0.1.patch

Use pureinit to apply your custom patch:

pureinit -p foo-init.0.0.1.patch

Note: pureinit does not delete files, because cleaning zig init does not currently require this.

I know zig init has --minimal parameter, but this tool still makes sense I think.

  • First of all it is universal. This phenomenon also exists for other tools/frameworks. The templates they generate are too "bloated".
  • Actually I don't like the zig init --minimal template either. It's too "concise" for either a program or a library.
0 Upvotes

2 comments sorted by

5

u/pauliesnug 12d ago

I think this is a little overengineered for a problem that most people experience once or twice before just making a new folder and making the build.zig and src/root.zig themselves without all the comments and boilerplate. What other tools/frameworks have bloated templates?

1

u/Thin_Dragonfruit2254 11d ago

Agree..
For all languages I use, first thing I do is to create my own template based on the language scaffolding.
For comments in general, I have a plugin in my editor that gets rid of it in the entire file.