Packages
protox
2.0.4
2.0.9
2.0.8
2.0.7
2.0.6
2.0.5
2.0.4
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-dev
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
retired
1.7.3
1.7.2
1.7.1
1.7.0
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.1
1.5.0
1.4.0
1.3.2
1.3.1
1.3.0
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.1
1.1.0
1.0.0
0.25.0
0.24.0
0.23.1
0.23.0
0.22.0
0.21.0
0.20.0
0.19.1
0.19.0
0.18.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.2
0.15.1
0.15.0
0.14.0
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.0
0.9.1
0.9.0
0.8.0
0.7.1
0.7.0
A fast, easy to use and 100% conformant Elixir library for Google Protocol Buffers (aka protobuf)
Current section
Files
Jump to
Current section
Files
lib/mix/tasks/protox/generate.ex
defmodule Mix.Tasks.Protox.Generate do
@shortdoc "Generate Elixir code from Protobuf definitions"
@moduledoc """
Generate Elixir code from `.proto` files.
Example:
`mix protox.generate --output-path=lib/message.ex --include-path=. message.proto`
The generated file will be usable in any project as long as protox is declared
in the dependencies (the generated code still needs functions from the protox runtime).
You can use the `--namespace` option to prepend a namespace to all generated modules.
If you have large protobuf files, you can use the `--multiple-files` option to generate
one file per module (it will leverage parallel compilation).
"""
use Mix.Task
@options [
output_path: :string,
include_path: :keep,
namespace: :string,
multiple_files: :boolean
]
@impl Mix.Task
@spec run(any) :: any
def run(args) do
with {opts, files, []} <- OptionParser.parse(args, strict: @options),
{:ok, output_path} <- Keyword.fetch(opts, :output_path),
{include_paths, opts} = Keyword.pop_values(opts, :include_path),
{multiple_files, opts} = Keyword.pop(opts, :multiple_files, false),
{:ok, files_content} <-
generate_code(files, output_path, multiple_files, include_paths, opts) do
Enum.each(files_content, &generate_file/1)
else
err ->
IO.puts(:stderr, "Failed to generate code: #{inspect(err)}")
exit({:shutdown, 1})
end
end
# -- Private
defp generate_file(%Protox.Generate.FileContent{name: file_name, content: content}) do
File.write!(file_name, content)
end
defp generate_code(files, output_path, multiple_files, include_paths, opts) do
Protox.Generate.generate_module_code(
files,
Path.expand(output_path),
multiple_files,
include_paths,
opts
)
end
end