Provides a mix task mix compile.proto
for easier integration of elixir-protobuf/protobuf
.
The task will:
- Fetch options, gather
.proto
sources - Check the
protoc
binary exists and is executable - Check the
protoc-gen-elixir
plugin is available and executable - Ensure the target directory exists
- Check if any of the sources are "stale"
- Compile each file replacing the basename
.proto
with.pb.ex
If available in Hex, the package can be installed
by adding protobuf_compiler
to your list of dependencies in mix.exs
:
def deps do
[
{:protobuf, "~> 0.9.0"},
{:protobuf_compiler, "~> 0.2.0"}
]
end
Protoc options can be set in the project:
defmodule MyProject.MixProject do
use Mix.Project
def project do
[
app: :my_project,
...
protoc_opts: [
paths: ["lib"],
dest: "lib/protobuf/",
gen_descriptors: true
]
]
end
defmodule MyProject.MixProject do
use Mix.Project
def project do
[
app: :my_project,
...
protoc_opts: [
...
package_prefix: "Custom"
]
]
end
Your modules will now be generated with a namespace:
defmodule Custom.Example do
@moduledoc false
use Protobuf, syntax: :proto2
@type t :: %__MODULE__{}
defstruct []
end
There is a strict dependency between protobuf
library version and plugin used
for code generation.
This compiler tries to install the best suited plugin version:
- if
protobuf
is a dep in your application, plugin is built from sources; else - if
protoc-gen-elixir
is found in yourPATH
, it will be used (mostly intended to developers); else - compiler uses
mix escript.install hex
to install prebuilt version of the plugin.
When not built from source, you can force a specific version of the plugin with the config:
config :protobuf_compiler, plugin_version: "0.8.0"
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/protobuf_compiler.