Skip to content
Shiva Wu edited this page Nov 14, 2013 · 16 revisions

This page covers the advanced topics for Greed, like template definition, hidden config keys, custom renderers.

A word for the configuration format

Greed uses a 3rd-party config library typesafe-config. It's a very powerful config library with a flexible DSL for the users. For advanced config file writing, refer to its documentation and the default.conf in Greed.

Available info for template rendering

Both the config file and the template file have many data bound when rendering. I'm going to describe how to refer to them in config and templates.

Before that, please note that, although the config library and the template engine uses the same syntax for resolving data, ${key}, they're of different implementations and have different semantics. In the config file, ${a.b.c} means the value of a.b.c following the path from the root of the config file. However, in the template file, ${a.b.c} simply binds to the value inside a map m, which is m.get("a").get("b").get("c"). This is the core difference.

Sometimes (often) Greed needs to specify some data in the template file, like ${Problem.Name} in the config file, for example, to specify the output file name. And we must prevent the config library to resolve this reference. We achieve this by putting the value in quotation, that's why you'll see a lot of "" in the default.conf.

Here's a table for all the keys and their values with their available scopes.

Template definition

Greed gives the users power to define their own templates, according to the following schema. Let's continue with the templateDef config key and start with an example.

greed.language.java.templateDef {
  unittest {
    override = false
    templateFile = "builtin unittest/junit.java.tmpl"
    outputKey = UnitTestCode
    outputFile = "${Contest.Name}/${Problem.Name}-WrongName"
    transformers = [ empty-block, cont-blank-line ]

    afterFileGen {
      execute = mv
      arguments = [ "${GeneratedFileName}", "${Contest.Name}/${Problem.Name}Test.java" ]
    }
  }
}

This example may look somewhat stupid, because it gives the output a wrong name and then rename it. But it covers all the possible keys of a template def.

  • override, if false, greed will skip the file if already exists.
    However, if user click Regenerate code in the UI, it will be force override. Don't worry, the old files will be backuped.
  • templateFile specify the path to the template file use by the template engine to render to the output.
    The prefix builtin shows that this template is located in the distributed jar of Greed, in greed.jar:/templates.
  • If outputKey is set, the rendered output will be bound to a key, in this case UnitTestCode, and available for the later templates (in the templates sequence) to use as a key in their template files. Note this does not conflict with outputFile.
  • outputFile and [ outputFileName, outputFileExtension ] conflict with each other and must not be set at the same time! Or Greed will raise an error of config exception. The former set the whole name while the latter specify the two parts of the file name separately.
  • transformers specify a list of actions to transform the code after the code is generated. Right now there're only two available.
    • empty-block removes empty cutting block (surrounded by cutBegin and cutEnd) to cleanup the output code.
    • cont-blank-line merges continuous blank lines to single one blank line.
  • afterFileGen is an external command executed after the file is generated (no output file, no action!). You can specify any scripts or programs to run.

Template file writing

This part is basically the details of the template engine, and is covered separately in another page Play with templates.

If you're interested in customizing your own template, go ahead!

Clone this wiki locally