A Small Utility to Help With Extracting Code Snippets

It’s been a while since I’ve written anything here. Part of the reason has been due to the writing I’ve done over on the blog at The Last Pickle. In the lsat few years, I’ve written about our tlp-stress tool, tips for new Cassandra clusters, and a variety of performance posts related to Compaction, Compression, and GC Tuning.

The other reason is the eight blog posts I’ve got in the draft folder. One of the reasons why there are so many is the way I write. If the post is programming related, I usually start with the post, then start coding, pull snippets out, learn more, rework the post, then rework snippets. It’s an annoying, manual process. The posts sitting in my draft folder have incomplete code, and reworking the code is a tedious process that I get annoyed with, leading to abandoned posts.

To address this, I wrote a small tool. It’s not particularly complex, but I think it’ll be useful. It works by extracting blocks of code between single line comments that are labeled with a :marker. For example, let’s suppose a source file contains this block:

// :example
fun something() {
    doStuff()
}
// :example

The tool will generate a file called example.snippet containing the code between the two blocks. code2snippets takes two parameters, a source and destination directory. The names of the files will correspond to the marker name + the .snippet extension.

After I’ve gotten a few posts out using the tool, I’ll post Linux and Mac packages as well as a Docker container. I’m sure there’s going to be a few bugs that pop up or usability issues I’ll want to improve before I make it generally available.

For now, the source is on GitHub. You’ll need to compile it if you want to use it. You can build the tarball by doing the following:

git clone https://github.com/rustyrazorblade/code2snippets.git
cd code2snippets
./gradlew distTar

There will be a tar archive in build/distributions. Extract and copy wherever you want to install it.

You’ll need Java 8 to run it.

If you use it, please drop me a note on Twitter BlueSky, I’m @rustyrazorblade. Bug reports and PRs welcome on Github.

If you found this post helpful, please consider sharing to your network. I'm also available to help you be successful with your distributed systems! Please reach out if you're interested in working with me, and I'll be happy to schedule a free one-hour consultation.