Looking for a standalone, command line, code generator script
Asked Answered
L

5

23

I'm looking for a library or command line script that will allow me to create custom templates that I can generate from the command line. The ruby on rails scaffolding generator is almost identical to what I am trying to do. I would even prefer that it be written in Ruby (yet it cannot require Rails because I may not be using it on a Ruby application). What sorts of scripts like this are already available?

Lambart answered 20/9, 2011 at 20:25 Comment(0)
S
9

I've also been on the lookout for something like this -- haven't found what I hoped for. The two approaches I've used instead have been acceptable. But I'm still hoping to find the real thing.

  • obvious, but sed for simple use cases

  • for medium-complexity use cases, if you can assume some version of Python is present on the machine, string.Template in the standard library works well. You could write a small Python script that uses this function, and since it is Python, tests / looping etc. that might normally be provided by the template engine could pretty easily be handled in the Python code instead.

  • I've just discovered Mustache (see http://mustache.github.io/). Seems like a solid, purpose-built solution. From its web site, Mustache has implementations in Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, and node.js. If those choices suit your environment, you could script or compile Mustache support to be a command line utility pretty easily.

UPDATE 15-OCT-2013

Now that I've used Mustache for a while -- it's a great tool, simple but powerful.

Snashall answered 3/6, 2013 at 15:15 Comment(1)
I was using the ruby mustache cli, but it seems too simple. At least the docs are.Washy
L
8

Try this one: https://gomplate.hairyhenderson.ca Rich set of functions and just a stanalone binary (written in Go)

Luciolucita answered 4/1, 2019 at 13:47 Comment(1)
This very one is interesting. Kudos!Triple
T
0

For simple use cases you can use envsubst fom the gettext GNU package. Basically, it reads any (text) file from stdin, replaces all occurrences of $VARIABLE or ${VARIABLE} from environment and writes the result to stdout. Little more, nothing less.

Full documentation is here.

Pros:

  • It is a small binary, no python (or other runtime) required
  • It is very fast and has tiny memory footprint

Cons:

  • It does just that. No special interpolations and functions (like loops, conditionals etc.)
  • It doesn't support special bash-like "Parameter Expansion"

For more advanced use cases I recommend j2cli, a "standalone" Jinja2 templating engine you can install from pip as it is in Python. Code can be found here (though it looks like a little bit stale...) and documentation is there too.

Pros:

  • It's Jinja2 with all bells and whistles!

Cons:

  • You need full Python runtime to run it and PIP to install it.
  • Computing resources could not be negligible in constrained environments.
Triple answered 20/12, 2022 at 15:46 Comment(0)
S
0

You can use ESH – a simple templating engine based on shell. It’s written ~290 LoC of POSIX shell and awk.

Example from the project’s readme:

http {
    access_log <%= $logs_dir/access.log %> main;

    resolver <%= $(sed -En 's/^nameserver ([^#]+)/\1/p' /etc/resolv.conf) %>;

    <% if nginx -V 2>&1 | grep -q lua-nginx-module; then -%>
    lua_package_path '<%= $(pkg-config --variable=INSTALL_LMOD lua) %>/?.lua';
    <% fi -%>

    <%+ ./http-common.esh %>

    <%# The rest of the config is omitted %>
}
Supererogate answered 8/1 at 14:51 Comment(0)
L
0

Actually the Ruby on Rails generators are based on a library called Thor. It's very useful for creating custom CLI tools for generating code based on templates.

Lambart answered 9/1 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.