facebook / hack-codegen
Hack Codegen is a library for programmatically generating Hack code
Installs: 537 012
Dependents: 7
Suggesters: 0
Security: 0
Stars: 341
Watchers: 67
Forks: 93
Open Issues: 7
Language:Hack
Requires
- hhvm: ^4.128
Requires (Dev)
- facebook/difflib: ^1.0
- facebook/fbexpect: ^2.6.1
- hhvm/hacktest: ^2.0
- hhvm/hhast: ^4.80
- hhvm/hhvm-autoload: ^2.0|^3.0
- hhvm/hsl-experimental: ^4.53
- hhvm/type-assert: ^3.1|^4.0
- dev-main
- v4.5.1
- v4.5.0
- v4.4.1
- v4.4.0
- v4.3.12
- v4.3.11
- v4.3.10
- v4.3.9
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- 4.3.2
- v4.3.1
- v4.3.0
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2
- v4.1.1
- v4.1.0
- v4.0.x-dev
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.x-dev
- v3.3.1
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.1
- v3.1.0
- v3.0.8
- v3.0.7
- v3.0.5
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.x-dev
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.0
- v1.x-dev
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1
- v1.0.1
- v1.0
- v0.9-beta
- dev-dependabot/bundler/docs/nokogiri-1.14.3
This package is auto-updated.
Last update: 2024-11-30 01:39:59 UTC
README
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.
Examples
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.
Requirements
Hack Codegen requires:
- HHVM
- Composer
Installing Hack Codegen
This package can be installed via composer:
composer require facebook/hack-codegen
Usage
Include the autoload file generated by composer and you are ready to start using it. For example:
<?hh require 'vendor/autoload.php'; use Facebook\HackCodegen\HackCodegenFactory; $cg = new HackCodegenFactory(new HackCodegenConfig()); echo $cg->codegenFile('HelloWorld.php') ->addClass( $cg->codegenClass('HelloWorld') ->addMethod( $cg->codegenMethod('sayHi') ->setReturnType('void') ->setBody( $cg->codegenHackBuilder() ->addAssignment( '$some_vector', Vector { 1, 2, 3 }, HackBuilderValues::vector( HackBuilderValues::export(), ), ) ->addAssignment( '$debug_info', Map { 'file' => '__FILE__', 'line' => '__LINE__' }, HackBuilderValues::map( HackBuilderKeys::export(), HackBuilderValues::literal(), ), ) ->addAssignment( '$some_vector_of_vectors', Vector { Vector { 1, 2, 3 }, Vector { 4, 5, 6 } }, HackBuilderValues::vector( HackBuilderValues::vector( HackBuilderValues::export(), ), ), ) ->addLine('echo "hello world\n";') ->getCode(); ); ) )->save();
Configuration
You can configure some options such as the maximum line width, spacing and
headers by implementing IHackCodegenConfig
and passing an instance to
HackCodegenFactory
's constructor.
License
Hack Codegen is MIT-licensed.