Skip to content
Library to programatically generate Hack code and write it to signed files
Branch: master
Clone or download
fredemmott Stop commiting compser.lock
With weekly releases, it creates more work than it saves
Latest commit 4195c17 Apr 26, 2019
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
examples/dorm lint Apr 17, 2019
src
tests
.gitattributes add linguist language override (#114) Apr 18, 2019
.gitignore Stop commiting compser.lock Apr 26, 2019
.hhconfig be friendly with 3.30 Dec 14, 2018
.travis.sh Stop commiting compser.lock Apr 26, 2019
.travis.yml Stop commiting compser.lock Apr 26, 2019
CODE_OF_CONDUCT.md add code of conduct Feb 20, 2019
CONTRIBUTING.md License and readme Aug 6, 2015
LICENSE Relicense to MIT Apr 12, 2018
README.md Relicense to MIT Apr 12, 2018
composer.json .hack-ify, fix, and test hh-codegen-verify-signatures Apr 17, 2019
hh_autoload.json test => tests for consitency with other proejcts Oct 5, 2018
hhast-lint.json DOnt require strict mode for dorm demo Feb 12, 2019

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.