Facebook is open-sourcing a piece of software that is designed to write software. Hack Codegen is a library that allows users to generate Hack code, the company’s programming language developed for HipHop Virtual Machine. (HHVM is a virtual machine designed to execute programs in Hack and PHP.)
“Being able to generate code through automated code generation allows programmers to increase the level of abstraction by making frameworks that are declarative and that are translated into high-quality Hack code,” wrote Alejandro Marcu, software engineer at Facebook, in a blog post. “We’ve been using Hack Codegen at Facebook for a while. After seeing so much internal success, we open-sourced this library so that more people could take advantage of it.”
(Related: Facebook releases Hack)
Before Hack Codegen, Facebook generated code through concatenating strings, but found it was unable to scale.
“We realized early on that we would need a good library to generate code,” wrote Marcu. “At the time, we didn’t do that much code generation at [Facebook], mostly dumping values into arrays, so we didn’t have any good tools except for signing files. This is the need that motivated us to write this library.”
The library includes hack_builder to deal with the concatenation, new lines, indentation, braces, hack keywords, and collections, as well as signed files to regenerate code automatically when a schema is changed.
The open-source version of Hack Codegen is available on GitHub, with a DORM as an example.