Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format_elixir: true option for file generation #13644

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/mix/lib/mix/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ defmodule Mix.Generator do

if opts[:force] || overwrite?(path, contents) do
File.mkdir_p!(Path.dirname(path))

contents =
case opts[:format_elixir] do
true -> [Code.format_string!(contents), ?\n]
_ -> contents
end

File.write!(path, contents)
true
else
Expand Down Expand Up @@ -94,6 +101,7 @@ defmodule Mix.Generator do

* `:force` - forces copying without a shell prompt
* `:quiet` - does not log command output
* `:format_elixir` - apply formatter to the generated file

## Examples

Expand Down
7 changes: 7 additions & 0 deletions lib/mix/test/mix/generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ defmodule Mix.GeneratorTest do
assert_received {:mix_shell, :yes?, ["foo already exists, overwrite?"]}
end)
end

test "with `format_elixir: true`" do
in_tmp("create_file", fn ->
create_file("foo", "%{ foo: :bar }", format_elixir: true)
assert File.read!("foo") == "%{foo: :bar}\n"
end)
end
end

describe "copy_file/3" do
Expand Down