Skip to content

Commit

Permalink
format_elixir: true option for file generation (#13644)
Browse files Browse the repository at this point in the history
  • Loading branch information
am-kantox authored and josevalim committed Jun 6, 2024
1 parent 278cf65 commit 615bb55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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` (since v1.18.0) - if `true`, 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

0 comments on commit 615bb55

Please sign in to comment.