Skip to content
Library to programatically generate Hack code and write it to signed files
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin .hack-ify, fix, and test hh-codegen-verify-signatures Apr 17, 2019
docs Update gh-pages dependencies Feb 20, 2019
examples/dorm lint Apr 17, 2019
src
tests Support for rendering enums in CodegenFile Feb 28, 2019
.gitattributes add linguist language override (#114) Apr 18, 2019
.gitignore
.hhconfig be friendly with 3.30 Dec 14, 2018
.travis.sh Make travis.sh use /bin/bash instead of /bin/sh Apr 17, 2019
.travis.yml travis notifications on slack Feb 21, 2019
CODE_OF_CONDUCT.md add code of conduct Feb 20, 2019
CONTRIBUTING.md License and readme Aug 6, 2015
LICENSE
README.md Relicense to MIT Apr 12, 2018
composer.json .hack-ify, fix, and test hh-codegen-verify-signatures Apr 17, 2019
composer.lock update dependencies Apr 12, 2019
hh_autoload.json
hhast-lint.json

README.md

Hack Codegen Build Status

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.

You can’t perform that action at this time.