Hack Codegen is a library for easily generating Hack code and writing it into signed files that prevent undesired modifications. The idea behind writing code that writes code is to raise the level of abstraction and reduce coupling. You can use your own way of describing a problem and generate the corresponding code. E.g. see examples/dorm. In this example, we use a schema to describe the structure of the data, and we use Hack Codegen to write the matching code.
The DORM example shows how to use the different aspects of the code generation in a simplified real-life example. The included tests also exemplify the usage of the different components.
Hack Codegen requires:
- HHVM
- Composer
To install this package via composer, just add the package to require and start using it.
{
"require": {
"facebook/hack-codegen": "*"
}
}
Include the autoload file generated by composer and you are ready to start using it. For example:
<?hh
require 'vendor/autoload.php';
echo codegen_file('HelloWorld.php')
->addClass(
codegen_class('HelloWorld')
->addMethod(
codegen_method('sayHi')
->setBody('echo "hello world\n";')
)
)->save();
You can configure some options such as the maximum line width, spacing and headers by editing the file src/HackCodegenConfig.php
Hack Codegen is BSD-licensed. We also provide an additional patent grant.