Every time I sit down to write a quick piece of code for a blog post, it starts with “lein new.” This is amazing and wonderful: it’s super fast to set up a clean project. Good practice, good play.[1]
But not fast enough! I usually start with a property-based test, so the first thing I do every time is add test.check to the classpath, and import generators and properties and defspec in the test file. And now that I’ve got the hang of declaring input and output types with prismatic.schema, I want that everywhere too.
I can’t bring myself to do this again – it’s time to shave the yak and make my own leiningen template.
The instructions are good, but there are some quirks. Here’s how to make your own personal template, bringing your own favorite libraries in every time.
It’s less confusing if the template project directory is not exactly the template name, so start with:
lein new template your-name –to-dir your-name-template
cd your-name-template
Next, all the files in that directory are boring. Pretty them up if you want, but the meat is down in src/leiningen/new.
In src/leiningen/new/your-name.clj is the code that will create the project when your template is activated. This is where you’ll calculate anything you need to include in your template, and render files into the right location. The template template gives you one that’s pretty useless, so I dug into leiningen’s code to steal and modify the default template’s definition. Here’s mine:
(defn jessitron
[name]
(let [data {:name name
:sanitized (sanitize name)
:year (year)}]
(main/info “Generating fresh project with test.check and schema.“)
(->files data
[“src/{{sanitized}}/core.clj” (render “core.clj” data)]
[“project.clj” (render “project.clj” data)]
[“README.md” (render “README.md” data)]
[“LICENSE” (render “LICENSE” data)]
[“.gitignore” (render “gitignore” data)]
[“test/{{sanitized}}/core_test.clj” (render “test.clj” data)]))
As input, we get the name of the project that someone is creating with our template.
The data map contains information available to the templates: that’s both the destination file names and the initial file contents. Put whatever you like in here.
Then, set the message that will appear when you use the template.
Finally, there’s a vector of destinations, paired with renderings from source templates.
Next, find the template files in src/leiningen/new/your-name/. By default, there’s only one. I stole the ones leiningen uses for the default template, from here. They didn’t work for me immediately, though: they referenced some data, such as {{namespace}}, that wasn’t in the data map. Dunno how that works in real life; I changed them to use {{name}} and other items provided in the data.
When it’s time to test, two choices: go to the root of your template directory, and use it.
lein new your-name shiny-new-project
This feels weird, calling lein new within a project, but it works. Now
cd shiny-new-project
lein test
and check for problems. Delete, change the template, try again.
Once it works, you’ll want to use the template outside the template project. To get this to work, first edit project.clj, and remove -SNAPSHOT from the project version.[3] Then
lein install
Done! From now on I can lein new your-name shiny-new-project all day long.
And now that I have it, maybe I’ll get back to the post I was trying to write when I refused to add test.check manually one last time.
[1] Please please will somebody make this for sbt? Starting a Scala project is a pain in the arse[2] compared to “lein new,” which leans me toward Clojure over Scala for toy projects, and therefore real projects.
[2] and don’t say use IntelliJ, it’s even more painful there to start a new Scala project.
[3] At least for me, this was necessary. lein install didn’t get it into my classpath until I declared it a real (non-snapshot) version.