|
| 1 | +defmodule Forge.Namespace.FileSync do |
| 2 | + |
| 3 | + defmodule Classification do |
| 4 | + defstruct changed: [], |
| 5 | + new: [], |
| 6 | + deleted: [] |
| 7 | + end |
| 8 | + |
| 9 | + alias __MODULE__.Classification |
| 10 | + |
| 11 | + @doc """ |
| 12 | + Classifies files into changed, new, and deleted categories. |
| 13 | +
|
| 14 | + It looks at `**/ebin/*` files in both the base directory and output directory, |
| 15 | + applying namespacing to the file names in the output directory. |
| 16 | + Files can be `.beam` or `.app` files. |
| 17 | +
|
| 18 | + Then compares the mtimes of the files to determine their classification. |
| 19 | +
|
| 20 | + If files in output_directory are not present in base_directory, they are classified as deleted. |
| 21 | + """ |
| 22 | + def classify_files(same, same), do: %Classification{ |
| 23 | + changed: [], |
| 24 | + new: [], |
| 25 | + deleted: [] |
| 26 | + } |
| 27 | + |
| 28 | + def classify_files(base_directory, output_directory) do |
| 29 | + # Files in base directory are not namespaced, eg: |
| 30 | + # lib/foo/ebin/Elixir.Foo.Bar.beam |
| 31 | + # lib/foo/ebin/foo.app |
| 32 | + # |
| 33 | + # Files in output directory are namespaced, eg: |
| 34 | + # lib/xp_foo/ebin/Elixir.XPFoo.Bar.beam |
| 35 | + # lib/xp_foo/ebin/xp_foo.app |
| 36 | + # |
| 37 | + # We need to compare the files by applying namespacing to the base directory paths, |
| 38 | + # then comparing mtimes. |
| 39 | + # |
| 40 | + # For any files in output_directory that don't have a corresponding file in base_directory, |
| 41 | + # we classify them as deleted. |
| 42 | + |
| 43 | + base_files = find_files(Path.join(base_directory, "lib")) |
| 44 | + output_files = find_files(Path.join(output_directory, "lib")) |
| 45 | + |
| 46 | + base_map = |
| 47 | + Enum.reduce(base_files, %{}, fn base_file, acc -> |
| 48 | + relative_path = Path.relative_to(base_file, base_directory) |
| 49 | + namespaced_relative_path = |
| 50 | + relative_path |
| 51 | + |> Forge.Namespace.Path.apply() |
| 52 | + |> maybe_namespace_filename() |
| 53 | + |
| 54 | + dest_path = Path.join(output_directory, namespaced_relative_path) |
| 55 | + Map.put(acc, base_file, dest_path) |
| 56 | + end) |
| 57 | + expected_dest_files = Map.values(base_map) |> MapSet.new() |
| 58 | + output_set = MapSet.new(output_files) |
| 59 | + |
| 60 | + classification = |
| 61 | + Enum.reduce(base_map, %Classification{}, fn {base_file, dest_path}, acc -> |
| 62 | + if File.exists?(dest_path) do |
| 63 | + base_mtime = File.stat!(base_file).mtime |
| 64 | + output_mtime = File.stat!(dest_path).mtime |
| 65 | + |
| 66 | + if base_mtime > output_mtime do |
| 67 | + %{acc | changed: [{base_file, dest_path} | acc.changed]} |
| 68 | + else |
| 69 | + acc |
| 70 | + end |
| 71 | + else |
| 72 | + %{acc | new: [{base_file, dest_path} | acc.new]} |
| 73 | + end |
| 74 | + end) |
| 75 | + |
| 76 | + deleted_files = |
| 77 | + output_set |
| 78 | + |> MapSet.difference(MapSet.new(expected_dest_files)) |
| 79 | + |> MapSet.to_list() |
| 80 | + |
| 81 | + %{classification | deleted: deleted_files} |
| 82 | + end |
| 83 | + |
| 84 | + defp find_files(directory) do |
| 85 | + [directory, "**", "*"] |
| 86 | + |> Path.join() |
| 87 | + |> Path.wildcard() |
| 88 | + |> Enum.filter(&File.regular?/1) |
| 89 | + end |
| 90 | + |
| 91 | + defp maybe_namespace_filename(file_path) do |
| 92 | + # namespace filename for .beam and .app files |
| 93 | + extname = Path.extname(file_path) |
| 94 | + |
| 95 | + if extname in [".beam", ".app"] do |
| 96 | + dirname = Path.dirname(file_path) |
| 97 | + basename = Path.basename(file_path, extname) |
| 98 | + namespaced_basename = Forge.Namespace.Module.apply(String.to_atom(basename)) |> Atom.to_string() |
| 99 | + Path.join(dirname, namespaced_basename <> extname) |
| 100 | + else |
| 101 | + file_path |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments