Robowr Code Generator Library 3.0
Robowr is a code generator, which can generate any code that can be written in text format.
New: Code difference added to code writer
Current version adds useDiff
option, which is enabled by default. Each directory will have .diff
subdirectory which
is generated for the purpose of diffing the originally written file to the current version.
- If the files are not changed, the code generator will overrride changes
- If the files are changed the code generator will follow the changes made in the changed files
R.CreateContext({})
.file("./", "diff_test.ts")
.write([`if(true) {`, [[`console.log("OK");`]], `}`])
.save("./test/difftest", { useDiff: true, usePrettier: true });
Installing
npm i -g robowr
Create a simple generator from command line
You can start creating generators from scratch but sometimes it is easier to create them from source code automatically.
Basicly what you need is just a source file to transform to a generator. The generated generator will include the source code for re-creating the given file. You can then use the generated generator to generate more similar files.
From stdin
cat hello.ts | robowr
cat hello.ts | robowr -o gen/HelloWorld.ts
Import File as generator template
robowr -f docker-compose.yml
or if you want to specify the output file
robowr -f docker-compose.yml -o createDocker.ts
To create it you can use for example ts-node
ts-node createDocker.ts
Using programmatically
Example of creating some static HTML content. Notice that using the context variables is absolutely optional.
In the example they are used to define title
and imported scripts for the HTML files. It is absolutely
fine to use anything else to manage internal state of the generator.
; ;;;R.CreateContext .file"./", "index.html" .write .file"./", "home.html" .write .save"./static/";
Examples
Contexts are generic, so you can use any support data as parameter to the writer.
First initialize the context
; // The data used by the robowr;;
Then you can create code using the context and return string, arrays, nested arrays (blocks) or functions which will be evaluated lazily.
;
To get the code call newCtx.writer.getCode()
// The generated code from aboveswitch value { case "value1": console.log"Found value value1"; break; case "value2": console.log"Found value value2"; break; case "value3": console.log"Found value value3"; break;}
Since context is passed to callback functions you can use them to fine grained control of the generator or you can use then just to make code easier to write
// simple function returning a comment block;
R.Walkctx, ;
Modifying context by the generators
Sometimes you want to modify the context before the output is written, for example you can run multipled passes to the context before actually outputting any code.
This is very simple, any callback function can simply modify the data variable in the context
;;
Immutable context
Context can be immutable, you can use any immutable library with the context data or you can use the built-in Immer -support like this
expect R.WalkR.CreateContext, .data.cnt.to.equal3;