npm i rainbow-js-codegeneration --saveDev
The module exposes one method generate that accepts options object as a parameter
import { generator, IOptions } from 'rainbow-js-codegeneration';
//...
const options: IOptions = {
cwd: path,
pattern,
};
const result = await generator(options);
//...This method search for files based on pattern provided, read them and construct templates objects. Once processing is done it complies Handlebars template and generates C# code.
cwd:string(required) - defines a root directory for aglobsearch;pattern:string|string[](required) - defines a pattern for theglobsearch;templatePath:string(default: standard template forGlassMapperembedded) - path to aHandlebarstemplate file that will process templates info.;Using:string[]- a list ofusingthat should be rendered in the header of the file;ToClass:function(name:string)(default: PascalCase class name) - overrides a logic that generates class names;ToInterface:function(name:string)(default: class name prepended with 'I')- overrides a logic that generates interface names;ToNamespace:function(path:string)(default: section of a path excluding/sitecore/templatesand template name) - overrides a logic that generates namespace names;ToProperty:function(name:string)(default: PascalCase property name)- overrides a logic that generates property names from field names;ToPropertyType:function(type:string, id:string)(default: simple mapping to available types in GlassMapper) - extends logic related to mapping of a fields to C# tpe of a property. If returnundefinedwill fall back to default.
The method will compile provided or embedded template and pass two objects there templates and options
Sample of a template file:
guid-b: will convert text to UPPER CASE and wrap in { }. Could be used to emulate output of C# method .ToString("B")
guid-d: will convert text to UPPER CASE. Could be used to emulate output of C# method .ToString("D")
The library provides a Gulp plugin. The plugin will generate code file for each configuration file detected. See example below:
// other imports
var codeGen = require('rainbow-js-codegeneration').generationPlugin;
// code generation task
gulp.task("Generate-Code", function (callback) {
gulp.src('**/codegeneration.config.js', { base: "./" })
.pipe(codeGen())
.pipe(rename(function (path) {
path.basename = "Templates.Generated";
path.extname = ".cs"
}))
.pipe(gulp.dest('./'))
.on("end", function () { // will make sure that you wait for generation to finish
callback();
});
});Configuration of the library could be provided via JS module. See example below:
var path = require("path");
module.exports = {
cwd: path.join(__dirname, '..'),
pattern: '**/serialization/*.Templates/**/*.yml',
Using: [ 'System.CodeDom.Compiler' ],
templatePath: path.join(__dirname, 'codegeneration.tmpl'),
}clean- remove coverage data, Jest cache and transpiled files,build- transpile TypeScript to ES6,watch- interactive watch mode to automatically transpile source files,lint- lint source files and tests,test- run tests,test:watch- interactive watch mode to automatically re-run tests
Licensed under the MIT. See the LICENSE file for details.
