Skip to content

Templates

Denis Zdorovtsov edited this page May 28, 2017 · 12 revisions

Template is just a folder with control script and special formed prhttps://github.com/notificationsoject files. Template files have access to project configuration file and can read them to use during project generation. Configuration parameters are substituded in right places and finally you get a final project file. It's two kinds of templates. Project templates and FlappyTools templates.

FlappyTools templates are used to generate base for you next work. For example, when you create Unity3d project, you can choose 2d or 3d project will be created, depending of selection different configuration will be applied. Similar way you select type of project in any other IDE project wizards. Project templates are used for generation of desired IDE project.

Template structure

In the root of template you can find two files:

  • generator.js - Entry point of project generation. Generator can prepare context and functions for using in template files. Also it can generate project files without templates, search and use other templates if needed.
  • default.json - Default configuration of the project. All parameters of default configuration can be overridden in project configuration files.

Usually, in here also placed template files itself in separate directory. Template files include js injections which can modify them in same way as PHP injections modify php files in order to generate html.

Template file

Template file is just a usual text file with js injections. For example:

cmake_minimum_required(VERSION 3.0)
project ([= config.name =])
...

As you can see, inline code injections are surrounded by "[=" and "=]" markers. It's same as "= ?>" in PHP. Another example:

target_include_directories ([= config.name =] 
[% for (let i in config.cxx.header_dirs) { %]
    "[= normalize(config.cxx.header_dirs[i]) =]"
[% } %]
)

Here is example of generation list of include directories for CMake. It's combination of inline and control injections. Control injections are surrounded by "[%" and "%]". You can write something from control injections using method "print()". Also you can add your own methods in generator.js script.

Finally, about context of template. As I mentioned before, it can be modified by generator.js, but, it's also some "default" context presented.

From template file available next methods and constants:

  • projectRoot - Root of the project. It's place where placed flappy_conf folder with configuration.
  • config - Merged config of a project. It's merged from default.json + general.json + custom overrides + cli generation parameters.
  • compileDir(context, templateDir, outDir) - Method to generate folder from template folder. Usually used only in generator.js to generate main projects and submodules.
  • templatePath - Root of the template folder, absolute path.
  • modules - Compiled list of dependencies of all submodules and project.
  • outDir - Path to final project folder.
  • createSubContext - Usually used in generator.js to prepare context for submodules.
  • normalize(path) - Returns normalized absolute path to file relative to project root.
  • print(value) - Output value to output file.
  • sourceList(projectRoot, sourceDirs, excludes) - Returns list of sources without excluded files. Both arguments are lists of directories or files.
  • findAllModules(context) - Returns list of all used in project modules.

Clone this wiki locally