https://github.com/hhpack/codegen
Library code generator for Hack
https://github.com/hhpack/codegen
code-generator hacklang hhvm
Last synced: about 2 months ago
JSON representation
Library code generator for Hack
- Host: GitHub
- URL: https://github.com/hhpack/codegen
- Owner: hhpack
- License: mit
- Created: 2017-09-24T03:54:18.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-04-30T06:49:45.000Z (almost 7 years ago)
- Last Synced: 2024-04-24T18:55:04.969Z (almost 2 years ago)
- Topics: code-generator, hacklang, hhvm
- Language: Shell
- Homepage:
- Size: 97.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Codegen
[](https://circleci.com/gh/hhpack/codegen/tree/master)
This package provides cli program to generate source code.
You can map generators to **namespaces** in the configuration file.
## 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.
```hack
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 {
/**
* 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](https://github.com/hhvm/hhvm-autoload/blob/master/composer.json) for hh_autoload.json.
```json
{
"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.
```shell
vendor/bin/codegen [GEN_NANE] LibClass
```
## Custom Generator
If you want to use your own generator, implement **ClassFileGeneratable**.
```hack
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**.
```
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.
```shell
composer update
composer test
```