Skip to content
Matthew Beatty edited this page Apr 26, 2015 · 14 revisions

Usage:

codgen.rb config.json

codgen takes a config that points at a config that provides a json map, a set of templates, and a json data file or object. It takes the json data adds any style automation and does any value mapping if a json map file is provided and injects that data into the templates and outputs the results to the specified paths.

Json Config File

The config json file should be a valid json file. In order for it to be called using the directory it should be named config.json. The root object has three properties:

  • map: A a file path string pointing to a map json file or a json object containing the map data
  • templates: An array containing the template object
  • data: A path or url string pointing to a data json file either on the user's filesystem or on the internet or a json object containing the data

The template objects have the following properties:

  • in: The template file to be processed
  • out: The location to output the file to. This is prcessed as a template and can have values inserted from the data json
  • source (optional): If you want the template to pull from an array of data rather than the root node specify an array property on the root object to pull from. It will then run the template on each member of the array.

Json Data File

The json data file should be a valid json file. The data in the json root node will correspond to the root template of the template file unless otherwise specified in the config.

Valid json key characters are: digits, letters, and underscores. In addition space, '#', '$', and '@' are special characters that you can use in your identifier to give specific meanings.

To have a key translated to a variety of casings/naming standards and having pluralized forms add simply uses spaces instead of camel casing or underscoring. To have this done to the value start the key with an '@' and leave spaces as mentioned previously in the actual value. To indicate that the value is plural and should be converted to singular instead add '#plural' onto the end. For single word keys where you wish to have the key translated but not the value you can start the key with a '$'.

Data should be structured such that each template should correspond to a named array of objects except template root which can correspond to the root object or a specified array. Each object in the array should contain the properties it will inject into the template. Each template inherits data properties from it's parent unless they get overridden at a deeper level

Json Map File

The optional file map should be structured as follows:

{
  "name_of_property_in_json_data_file": {
    "name_of_property_in_template": {
      "value_in_json_data_file": "value_to_be_printed_into_template"
    }
  }
}

Template File

Template files can be mustache or handlebars templates, or you can just have a file copied verbatim. To access naming/casing standards translation and pluralizations in mustache and handlebars templates use the following syntax if it is a two word key

{{MyVar}}
{{myVar}}
{{my_var}}
{{MY_VAR}}
{{MyVars}}
{{myVars}}
{{my_vars}}
{{MY_VARS}}

Or if it is a single word key to be specific:

{{MyVar?CapCamel}}
{{myVar?camelCase}}
{{my_var?underscored}}
{{MY_VAR?CAPS_UNDERSCORE}}
{{MyVars?CapCamel}}
{{myVars?camelCase}}
{{my_vars?underscored}}
{{MY_VARS?CAPS_UNDERSCORE}}

If there is ambiguity between plural and singular key:

{{MyVar?CapCamel?singular}}
{{myVar?camelCase?singular}}
{{my_var?underscored?singular}}
{{MY_VAR?CAPS_UNDERSCORE?singular}}
{{MyVars?CapCamel?plural}}
{{myVars?camelCase?plural}}
{{my_vars?underscored?plural}}
{{MY_VARS?CAPS_UNDERSCORE?plural}}

Clone this wiki locally