Skip to content

How to create a generator

macourtney edited this page Sep 13, 2010 · 5 revisions

Added in version: 0.6
Updated for version: 0.7

A generator is simply a clojure source file in the script/generators directory. The file must have a certain name and include a specific function or the generate.clj script will not be able to find it.

As an example, let’s make a hello-world generator which will simply print out “Hello World!” when called. Our hello-world generator file must be called “hello_world_generator.clj” and be placed in the conjure/script/generators directory.

Since hello_world_generator.clj is in the conjure/script/generators directory, it’s namespace must be:

(ns conjure.script.generators.hello-world-generator)

All generators must contain a function called “generate” which takes a single parameter usually called params. Params is the list of all command line options passed to the generate command after the name of your generator.

Our hello world generate function will simply print hello world to the screen. The complete hello world generator looks like:

(ns conjure.script.generators.hello-world-generator)

(defn 
#^{ :doc "Simply prints \"Hello World!\" to the console." }
  generate [params]
  (println "Hello World!"))

To run your generator, use:

lein conjure generate hello-world

You should see something like the following printed to your console:

.\lib\*;.\vendor;.\app;.\config;.\script;.\db;.\test
Initializing environment...
INFO  [conjure.server.server]: Initializing server...
DEBUG [flavors.h2]: Executing query: ["SELECT * FROM sessions LIMIT 1"]
DEBUG [flavors.h2]: Create table: :sessions with specs: (("id" "INT" "NOT NULL"
"AUTO_INCREMENT" "PRIMARY KEY") ["created_at" "TIMESTAMP"] ("session_id" "VARCHA
R(255)") ["data" "TEXT"])
INFO  [conjure.server.server]: Server Initialized.
INFO  [conjure.server.server]: Initializing plugins...
INFO  [conjure.server.server]: Plugins initialized.
Hello World!
Clone this wiki locally