Skip to content

Commit 0a19236

Browse files
authored
Run merge for mix gettext.extract pot files even if they are unchanged (#385)
Resolves #377
1 parent e508160 commit 0a19236

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

lib/gettext/extractor.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ defmodule Gettext.Extractor do
222222
# %{path => {:merged, :unchanged | %Messages{}}, path => {:unmerged, :unchanged | %Messages{}}, path => {:new, %Messages{}}}
223223
Map.merge(pot_files, po_structs, &merge_existing_and_extracted(&1, &2, &3, gettext_config))
224224
|> Enum.map(&tag_files(&1, gettext_config))
225-
|> Enum.reject(&match?({_, {_, :unchanged}}, &1))
226225
|> Enum.map(&dump_tagged_file/1)
227226
end
228227

@@ -273,7 +272,8 @@ defmodule Gettext.Extractor do
273272
# This function "dumps" merged files and unmerged files without any changes,
274273
# and dumps new POT files adding an informative comment to them. This doesn't
275274
# write anything to disk, it just returns `{path, contents}` tuples.
276-
defp dump_tagged_file({path, {_tag, po}}), do: {path, PO.compose(po)}
275+
defp dump_tagged_file({path, {_tag, :unchanged}}), do: {path, :unchanged}
276+
defp dump_tagged_file({path, {_tag, po}}), do: {path, {:changed, PO.compose(po)}}
277277

278278
defp prune_unmerged(path, gettext_config) do
279279
merge_or_unchanged(path, %Messages{messages: []}, gettext_config)

lib/mix/tasks/gettext.extract.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule Mix.Tasks.Gettext.Extract do
7575
end
7676

7777
defp run_message_extraction(pot_files, opts, args) do
78-
for {path, contents} <- pot_files do
78+
for {path, {:changed, contents}} <- pot_files do
7979
File.mkdir_p!(Path.dirname(path))
8080
File.write!(path, contents)
8181
Mix.shell().info("Extracted #{Path.relative_to_cwd(path)}")
@@ -89,9 +89,9 @@ defmodule Mix.Tasks.Gettext.Extract do
8989
end
9090

9191
defp run_up_to_date_check(pot_files) do
92-
not_extracted_paths = for {path, _contents} <- pot_files, do: path
92+
not_extracted_paths = for {path, {:changed, _contents}} <- pot_files, do: path
9393

94-
if pot_files == [] do
94+
if not_extracted_paths == [] do
9595
:ok
9696
else
9797
Mix.raise("""

test/gettext/extractor_test.exs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ defmodule Gettext.ExtractorTest do
3333
Extractor.merge_pot_files(extracted_po_structs, [paths.tomerge, paths.ignored], [])
3434

3535
# Unchanged files are not returned
36-
assert List.keyfind(structs, paths.ignored, 0) == nil
36+
assert {_path, :unchanged} = List.keyfind(structs, paths.ignored, 0)
3737

38-
{_, contents} = List.keyfind(structs, paths.tomerge, 0)
38+
assert {_, {:changed, contents}} = List.keyfind(structs, paths.tomerge, 0)
3939

4040
assert IO.iodata_to_binary(contents) == """
4141
msgid "foo"
@@ -45,7 +45,7 @@ defmodule Gettext.ExtractorTest do
4545
msgstr ""
4646
"""
4747

48-
{_, contents} = List.keyfind(structs, paths.new, 0)
48+
assert {_, {:changed, contents}} = List.keyfind(structs, paths.new, 0)
4949
contents = IO.iodata_to_binary(contents)
5050

5151
assert contents =~ """
@@ -385,7 +385,11 @@ defmodule Gettext.ExtractorTest do
385385
assert [] = Extractor.pot_files(:unknown, [])
386386

387387
pot_files = Extractor.pot_files(:test_application, [])
388-
dumped = Enum.map(pot_files, fn {k, v} -> {k, IO.iodata_to_binary(v)} end)
388+
389+
dumped =
390+
pot_files
391+
|> Enum.reject(&match?({_path, :unchanged}, &1))
392+
|> Enum.map(fn {k, {:changed, v}} -> {k, IO.iodata_to_binary(v)} end)
389393

390394
# We check that dumped strings end with the `expected` string because
391395
# there's the informative comment at the start of each dumped string.

test/mix/tasks/gettext.extract_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ defmodule Mix.Tasks.Gettext.ExtractTest do
6060
msgid "other"
6161
msgstr ""
6262
"""
63+
64+
capture_io(fn ->
65+
Mix.Project.in_project(test, tmp_dir, fn _module -> run(["--merge"]) end)
66+
end) =~ "Wrote priv/gettext/it/LC_MESSAGES/my_domain.po"
6367
end
6468

6569
test "--check-up-to-date should fail if no POT files have been created",

0 commit comments

Comments
 (0)