Packages
protox
2.0.0
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/protox/generate.ex
defmodule Protox.Generate do
@moduledoc false
defmodule FileContent do
@moduledoc false
defstruct([:name, :content])
end
def generate_module_code(files, output_path, multiple_files, include_paths, opts \\ [])
when is_list(files) and is_binary(output_path) and is_boolean(multiple_files) do
paths =
case include_paths do
[] -> nil
_ -> Enum.map(include_paths, &Path.expand/1)
end
case launch_protoc(files, paths) do
{:ok, file_descriptor_set} ->
{:ok, definition} = Protox.Parse.parse(file_descriptor_set, opts)
code = Protox.Define.define(definition, opts)
{:ok, generate_files(output_path, code, multiple_files)}
{:error, msg} ->
{:error, msg}
end
end
# -- Private
defp launch_protoc(files, paths) do
files
|> Enum.map(&Path.expand/1)
|> Protox.Protoc.run(paths)
end
defp generate_files(output_path, code, false = _muliple_files) do
[
%FileContent{
name: output_path,
content: generate_file_content(code)
}
]
end
defp generate_files(output_path, code, true = _muliple_files) do
# Protox.Define.define outputs a block, we thus have to extract the content
# on which we can iterate.
{:__block__, _, block} = code
Enum.map(block, fn {:defmodule, _, [module_name | _]} = module_code ->
snake_module_name =
module_name
|> Module.split()
|> Enum.map_join("_", &Macro.underscore/1)
%FileContent{
name: Path.join(output_path, snake_module_name <> ".ex"),
content: generate_file_content(module_code)
}
end)
end
defp generate_file_content(code) do
[
"# Code generated by protox. Don't edit.\n",
"# credo:disable-for-this-file\n",
code |> Macro.to_string() |> Code.format_string!(),
"\n"
]
end
end