Provides a mix task mix compile.proto for easier integration of elixir-protobuf/protobuf.
The task will:
- Fetch options, gather
.protosources - Check the
protocbinary exists and is executable - Check the
protoc-gen-elixirplugin is available and executable - Ensure the target directory exists
- Check if any of the sources are "stale"
- Compile each file replacing the basename
.protowith.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"}
]
endProtoc 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 []
endThere 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
protobufis a dep in your application, plugin is built from sources; else - if
protoc-gen-elixiris found in yourPATH, it will be used (mostly intended to developers); else - compiler uses
mix escript.install hexto 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.