2017 © Pedro Peláez
 

library codegen

Library code generator for Hack

image

hhpack/codegen

Library code generator for Hack

  • Monday, June 25, 2018
  • by holyshared
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2 Installations
  • Hack
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 18 Versions
  • 0 % Grown

The README.md

Codegen

CircleCI, (*1)

This package provides cli program to generate source code.
You can map generators to namespaces in the configuration file., (*2)

Basic usage

Create a generator configuration file

Create a configuration file that specifies the generator class.
In the configuration file, you define the namespace and generator linkage., (*3)

namespace MyPackage\Generators;

use HHPack\Codegen\Contract\{NamedGenerator, GeneratorProvider};
use HHPack\Codegen\HackTest\{TestClassGenerator};
use HHPack\Codegen\Project\{PackageClassGenerator};
use function HHPack\Codegen\Cli\{define_generator, namespace_of};

final class Generators implements GeneratorProvider {
  const LIB = 'HHPack\\Codegen\\Example';
  const LIB_TEST = 'HHPack\\Codegen\\Example\\Test';

  public function generators(): Iterator<NamedGenerator> {
    /**
     * vendor/bin/codegen lib:class LibClass
     */
    yield define_generator("lib:class", "generate library class file.")
      ->mapTo(
        namespace_of(static::LIB, 'example/src')
          ->map(PackageClassGenerator::class),
      );

    /**
     * vendor/bin/codegen lib:testclass LibClassTest
     */
    yield define_generator("lib:testclass", "generate test class file.")
      ->mapTo(
        namespace_of(static::LIB_TEST, 'example/test')
          ->map(TestClassGenerator::class),
      );
  }
}

Append autoload settings to hh_autoload.json

Set the path to read the generator in devRoots.
Please refer to hhvm-autoload for hh_autoload.json., (*4)

{
  "roots": "src",
  "devRoots": "/path/to/"
}

Generate class file for package

The name of the generator is specified as the first argument, and the class name is specified as the second argument., (*5)

vendor/bin/codegen [GEN_NANE] LibClass

Custom Generator

If you want to use your own generator, implement ClassFileGeneratable., (*6)

use HHPack\Codegen\{GenerateClassFile};
use HHPack\Codegen\Contract\{ClassFileGeneratable};
use Facebook\HackCodegen\{ICodegenFactory, CodegenFile, CodegenClass};

final class CustomClassGenerator implements ClassFileGeneratable {

  public function __construct(private ICodegenFactory $cg) {}

  public static function from(ICodegenFactory $factory): this {
    return new self($factory);
  }

  public function generate(GenerateClassFile $target): CodegenFile {
    return
      $this->cg
        ->codegenFile($target->fileName())
        ->setNamespace($target->belongsNamespace())
        ->addClass($this->classOf($target->name()));
  }

  private function classOf(string $className): CodegenClass {
    return $this->cg->codegenClass($className)->setIsFinal(true);
  }

}

You only need to specify it in the generator definition.
In the example below, you can generate code with the command vendor/bin/codegen myproject:loader MyLoader., (*7)

yield define_generator("myproject:loader", "generate loader class file.")
  ->mapTo(
    namespace_of('MyProject\Loader', 'MyProject/Loader')
      ->map(CustomClassGenerator::class) // Map generator to namespace
    );

Run the test

The test can be executed with the following command., (*8)

composer update
composer test

The Versions

25/06 2018

dev-master

9999999-dev

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

25/06 2018

0.2.3

0.2.3.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

25/06 2018

dev-feature/fix_dir

dev-feature/fix_dir

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

13/05 2018

0.2.2

0.2.2.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/04 2018

0.2.1

0.2.1.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/02 2018

0.2.0

0.2.0.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/02 2018

dev-develop

dev-develop

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/02 2018

dev-feature/console_output

dev-feature/console_output

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/02 2018

dev-feature/change_provider_interface

dev-feature/change_provider_interface

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

11/02 2018

dev-feature/help_message

dev-feature/help_message

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

10/02 2018

dev-feature/type_assert

dev-feature/type_assert

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

10/02 2018

dev-feature/change_namespace

dev-feature/change_namespace

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

10/02 2018

dev-feature/contract/namespace

dev-feature/contract/namespace

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

10/02 2018

dev-feature/remove_old_codes

dev-feature/remove_old_codes

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

10/02 2018

dev-feature/custom_gen

dev-feature/custom_gen

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

03/11 2017

0.1.2

0.1.2.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

01/10 2017

0.1.1

0.1.1.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package

01/10 2017

0.1.0

0.1.0.0

Library code generator for Hack

  Sources   Download

MIT

The Requires

 

The Development Requires

by Noritaka Horio

generator hack hhvm package