Singer

Generates code from templates. Originally intended for Sinatra applications (and models etc.), highly configurable and extensible.

Installation

Run gem install singer.

Usage

singer [OPTION]... [TEMPLATE_NAME] PROJECT_NAME

Run singer --help to see up-to-date list of options. Run singer --list-templates to see available templates' names, both shipped with the gem and found on current system.

For PROJECT_NAME, using snake_case is best, although an attempt will be made to understand CamelCase, too.

Writing your own templates

Many features of Singer will become clearer when seeing actual usage - please feel encouraged to examine the templates shipped with the gem (you can find them in templates_from_gem shown by the singer --show-paths).

Template location

User-provided templates should be placed in the directory named templates_from_user in the singer --show-paths output. Each template resides in a subdirectory - the subdirectory's name becomes the template's name, and is not included in the generated paths (Singer will only replicate the directory structure inside it).

Variables available to templates

Templates written in ERB or other templating mechanisms that execute Ruby code will have access to Singer::CONFIGURATION object and its accessors. For example, the following code in a my_template's file:

class <%= Singer::CONFIGURATION.project_name_camelcase %>

will, when Singer is called with singer my_template foo_bar_baz, result in this written to output file:

class FooBarBaz

To see an up-to-date list of available variables, run singer --list-variables.

Special segments in template paths

Any of the variables above can be also used in paths/filenames of template files, capitalized and surrounded by double underscores. So, if my_template has a file at this path:

docs_for___PROJECT_NAME_SNAKECASE__/__PROJECT_NAME_CAMELCASE__ - humble beginnings.txt

and you run singer my_template alpha_beta, a file named like this will be created:

docs_for_alpha_beta/AlphaBeta - humble beginnings.txt

Tiny caveat for completeness - while template_file_name_actual can be used inside template files, the __TEMPLATE_FILE_NAME_ACTUAL__ will not be substituted in paths. "Stack level too deep" and all that ;)

Development

git clone [email protected]:tanstaafl/singer.git
cd singer
bundle install
bundle exec rake

This will run tests (Minitest) and linter (Rubocop, custom config, see .rubocop.yml).

Note: the repo does not force the bundle to be vendorized, but should work fine if you set BUNDLE_PATH: "vendor" in your ~/.bundle/config. Hint, hint ;)

You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine from sources, run bundle exec rake install.

Contributing

Bug reports and merge requests are welcome on GitLab at https://gitlab.com/tanstaafl/singer.

License

The gem is available as open source under the terms of the MIT License.