Skip to content

Commit dd77ba7

Browse files
committed
Add config, factory, and hack-builder docs
1 parent c5e5e72 commit dd77ba7

5 files changed

Lines changed: 129 additions & 5 deletions

File tree

docs/_data/nav_docs.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
- title: Documentation
1+
- title: Overview
22
items:
3-
- id: quick-start
3+
- id: overview-quick-start
4+
- id: overview-config-and-factory
5+
- id: overview-hack-builder
46

57
# n title:, 1 items: per title:, n id: per items:
68
# - title: A
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
docid: overview-config-and-factory
3+
title: Config and Factory
4+
layout: docs
5+
permalink: /docs/overview/config-and-factory/
6+
---
7+
8+
[`IHackCodegenConfig`](https://github.com/hhvm/hack-codegen/blob/master/src/IHackCodegenConfig.php)
9+
defines several methods for project-specification, such as indentation preferences, and where
10+
any paths generated should be relative to.
11+
12+
`HackCodegenConfig` is a concrete implementation, which takes the project root directory as a
13+
parameter; [`HackCodegenFactory`](https://github.com/hhvm/hack-codegen/blob/master/src/HackCodegenFactory.php)
14+
is a convenience class to instantiate the codegen builders without having to explicitly pass
15+
configuration into each of them; for example:
16+
17+
``` php
18+
<?hh
19+
20+
use Facebook\HackCodegen\{
21+
CodegenClass
22+
HackCodegenConfig,
23+
HackCodegenFactory
24+
};
25+
26+
$config = new HackCodegenConfig(realpath(__DIR__.'/../'));
27+
28+
$class_a = new CodegenClass($config, 'ClassA');
29+
$class_b = new CodegenClass($config, 'ClassB');
30+
31+
$cg = new HackCodegenFactory($config);
32+
33+
$class_c = $cg->codegenClass('ClassC');
34+
$class_d = $cg->codegenClass('ClassD');
35+
```
36+
37+
`HackCodegenFactory` is the recommended way to use Hack Codegen.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
---
2+
docid: overview-hack-builder
3+
title: Hack Builder
4+
layout: docs
5+
permalink: /docs/overview/hack-builder/
6+
---
7+
8+
[`BaseCodeBuilder`](https://github.com/hhvm/hack-codegen/blob/master/src/BaseCodeBuilder.php)
9+
provides basic features useful for generating code in most C-style programming langauges, such as
10+
indentation, if blocks, and so on.
11+
[`HackBuilder`](https://github.com/hhvm/hack-codegen/blob/master/src/HackBuilder.php) extends
12+
`BaseCodeBuilder` with features of the Hack language.
13+
14+
When using Hack Codegen, `BaseCodeBuilder` is not usually used directly: `HackBuilder` is used to
15+
generate function/method bodies and pseudo-main code, and higher-level APIs are used to create
16+
classes, functions, etc.
17+
18+
The recommended way to get an instance of a `HackBuilder` is to first get an instance of a
19+
`HackCodegenFactory`, then call `->codegenHackBuilder()` on it.
20+
21+
Fundamentals
22+
------------
23+
24+
`$builder->add('some code');` is the lowest-level function: it appends the string you provide to
25+
the generated code. On top of this, there is:
26+
27+
- `->addLine('some code')`: also adds a newline
28+
- `->addLines($vector_of_lines)`: adds multiple lines
29+
30+
Many methods have a `sprintf`-style shortcut - eg:
31+
32+
- `->addf('foo %s bar', $var)`
33+
- `->addLinef('foo %s bar', $var)`
34+
35+
As readable code is a goal of Hack Codegen, several whitespace helpers are provided:
36+
37+
- `->ensureNewLine()`: add a newline if we're not currently at the start of a line
38+
- `->ensureEmptyLine()`: adds newlines as needed to have an empty line before any more code
39+
- `->indent()`: increase the number of spaces added to the start of any new non-empty lines
40+
(by default, this increases by 2 spaces - see `IHackCodegenConfig` to change this)
41+
- `->unindent()`: do the opposite
42+
- `->addLineWithSuggestedLineBreaks()`: any `\t` in the input string is replaced with a
43+
space if it will still fit in the desired maximum line length, otherwise a newline is
44+
added (and indented if needed)
45+
- `->addMultilineCall($call, $args)`: either renders the call all on one line, or with
46+
one argument on one line, depending on if it can fit within the maximum line length
47+
48+
Values
49+
------
50+
51+
While the fundamentals can be combined with `var_export()` directly to generate code
52+
for values, Hack Codegen provides an extensible system to simplify this; the simplest
53+
interface to this is
54+
`HackBuilder::addValue<T>(T $value, IHackBuilderValueRenderer<T> $formatter)`.
55+
56+
The two simplest renderers are:
57+
58+
- `HackBuilderValues::export()` implements `IHackBuilderValueRenderer<mixed>`, so is able
59+
to render any value; it uses `var_export()`, but fixes up builtins to be instantiable,
60+
eg replacing `HH\Vector { }` with `Vector { }`
61+
- `HackBuilderValues::literal()` implements `IHackBuilderValueRenderer<string>`, and
62+
takes the value as literal code
63+
64+
More complicated renderers are available, including nested definitions for collections.
65+
Additionally, shortcuts are provided for common uses:
66+
67+
- `->addAssigment('$someVar', $value, $renderer)`: assigns `$value` to `$somevar`
68+
- `->addReturn('$somevar', $value, $renderer)`: return $value from the current function
69+
70+
`->addAssignmentf()` and `->addReturnf()` are also available, however they always treat the
71+
final string as literal code.
72+
73+
Blocks
74+
------
75+
76+
Helpers are provided for common block constructs - eg:
77+
78+
``` php
79+
$builder
80+
->startIf('$condition')
81+
->addLine('doStuff();')
82+
->endIf()
83+
```
84+
85+
See the `HackBuilder` source for a complete list.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
2-
docid: quick-start
2+
docid: overview-quick-start
33
title: Quick Start
44
layout: docs
5-
permalink: /docs/quick-start/
5+
permalink: /docs/overview/quick-start/
66
---
77

88
## Installation

docs/docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
id: docs
33
title: Docs
44
layout: redirect
5-
destination: quick-start
5+
destination: overview/quick-start/
66
---

0 commit comments

Comments
 (0)