mix compile.flatbuf (flatbuf v0.1.0)

Copy Markdown View Source

Mix compiler that regenerates Elixir source from .fbs schemas before the Elixir compiler runs. Wire it into your project by adding :flatbuf to the :compilers list in mix.exs and configuring schema paths in application config:

def project do
  [
    # ...
    compilers: [:flatbuf | Mix.compilers()]
  ]
end

# config/config.exs
config :my_app, :flatbuf,
  schemas: ["priv/fbs/monster.fbs"],
  out: "lib/my_app/schema",
  namespace: "MyApp.Schema",
  wire_module: "MyApp.Schema.Wire",
  include: ["priv/fbs"]

How it works

On each mix compile, the configured schemas are parsed, resolved, and code-generated. An output file is only (re)written when it is missing or its on-disk content differs from the freshly generated source — unchanged artifacts keep their mtime, so downstream compilers don't see spurious changes. A manifest under _build/<env>/lib/<app>/.mix/compile.flatbuf records schema digests, option digests, and the list of files last emitted. Outputs whose schema was removed from :schemas are deleted on the next compile, so a stale .ex from an old schema doesn't linger.

Configuration keys (config :my_app, :flatbuf)

  • :schemas (required) — list of paths to .fbs files. Each is processed independently; cross-schema references go through include statements (or the --include search path).
  • :out — output directory; defaults to "lib".
  • :namespace — root namespace override; see mix flatbuf.gen.
  • :wire_module — name of the emitted wire helper module; defaults to Flatbuf.Generated.Wire.
  • :include — list of include search paths.
  • :niceties — list of atoms enabling generated-table niceties: :behaviour (implements Flatbuf.Table), :jason (derives Jason.Encoder). Unknown atoms abort the compile.

Nicety dependency caveats

Niceties add references the consuming project must satisfy:

  • :jason — the generated @derive Jason.Encoder only compiles if the project depends on :jason.
  • :behaviour — the generated @behaviour Flatbuf.Table needs :flatbuf available when the generated code compiles. With a only: [:dev, :test] dependency on :flatbuf, prod compiles warn that the Flatbuf.Table behaviour is undefined (a hard failure under --warnings-as-errors). Note that using this compiler at all requires :flatbuf in the compile path, so pair :behaviour with a regular dependency.

Summary

Functions

Internal compile entry point with the manifest path threaded explicitly. Used by run/1 and by tests that want to drive the compiler outside Mix's project stack.

Functions

do_compile(schemas, plan_opts, manifest_path)

@spec do_compile([Path.t()], keyword(), Path.t()) ::
  {:ok | :noop, []} | {:error, [term()]}

Internal compile entry point with the manifest path threaded explicitly. Used by run/1 and by tests that want to drive the compiler outside Mix's project stack.