Skip to content

Commit

Permalink
refactor: add possibility to choose which features to generate with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ahamez committed Nov 28, 2024
1 parent 02f04d7 commit 3fef285
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- BREAKING CHANGE: The JSON library is now configurable via the `:json_library` option at compile time
- BREAKING CHANGE: Configuration of features to generate via the CLI mix task is done with the `--generate` argument
- Drop support for Elixir < 1.15
- Use `Jason.encode_to_iodata!` rather than `Jason.encode!` for default JSON encoding

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ The generated file will be usable in any project as long as `protox` is declared
* `--namespace`
Prepends a [namespace](#prepend-namespaces) to all generated modules.

* `--keep-unknown-fields=[true|false]`
Toggles support of [unknown fields](#unknown-fields). Default to `true`.
* `--generate=none|all|unknown_fields`
Toggles support of features to generate. Currently, only `unknown_fields` is supported.

## Conformance

Expand Down
29 changes: 26 additions & 3 deletions lib/mix/tasks/protox/generate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,31 @@ defmodule Mix.Tasks.Protox.Generate do
include_path: :keep,
namespace: :string,
multiple_files: :boolean,
keep_unknown_fields: :boolean
generate: :string
]

@default_generate_opt_all [
keep_unknown_fields: true
]

@default_generate_opt_none [
keep_unknown_fields: false
]

@map_generate_opts %{
"unknown_fields" => {:keep_unknown_fields, true}
}

@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(files, output_path, multiple_files, include_paths, opts) do
{generate_opts, opts} = Keyword.pop(opts, :generate, "all"),
opts <- transform_generate_opts(generate_opts, opts),
{:ok, files_content} <- generate(files, output_path, multiple_files, include_paths, opts) do
Enum.each(files_content, &generate_file/1)
else
err ->
Expand All @@ -47,6 +60,16 @@ defmodule Mix.Tasks.Protox.Generate do

# -- Private

defp transform_generate_opts("all", opts), do: opts ++ @default_generate_opt_all
defp transform_generate_opts("none", opts), do: opts ++ @default_generate_opt_none

defp transform_generate_opts(generate_opts, opts) when is_binary(generate_opts) do
generate_opts
|> String.split(",")
|> Enum.map(&Map.fetch!(@map_generate_opts, &1))
|> Enum.concat(opts)
end

defp generate_file(%Protox.Generate.FileContent{name: file_name, content: content}) do
File.write!(file_name, content)
end
Expand Down
4 changes: 2 additions & 2 deletions test/code_generation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Protox.CodeGenerationTest do
code_generation_path: path,
protox_path: protox_path
} do
launch(path, protox_path, "single_without_unknown_fields.ex", ["--keep-unknown-fields=false"])
launch(path, protox_path, "single_without_unknown_fields.ex", ["--generate=none"])
end

test "Generate multiple files, with unknown fields", %{
Expand All @@ -45,7 +45,7 @@ defmodule Protox.CodeGenerationTest do
code_generation_path: path,
protox_path: protox_path
} do
launch(path, protox_path, ".", ["--multiple-files", "--keep-unknown-fields=false"])
launch(path, protox_path, ".", ["--multiple-files", "--generate=none"])
end

test "Generate single file, with namespace", %{
Expand Down

0 comments on commit 3fef285

Please sign in to comment.