|
| 1 | +defmodule Mix.Tasks.Compile.PhoenixIconify do |
| 2 | + @moduledoc """ |
| 3 | + Compiles icon assets by discovering icons used in templates. |
| 4 | +
|
| 5 | + This compiler: |
| 6 | + 1. Scans compiled modules for icon component calls |
| 7 | + 2. Extracts literal icon names from the `name` attribute |
| 8 | + 3. Fetches missing icons from Iconify |
| 9 | + 4. Updates the manifest in priv/iconify/ |
| 10 | +
|
| 11 | + ## Usage |
| 12 | +
|
| 13 | + Add to your mix.exs: |
| 14 | +
|
| 15 | + def project do |
| 16 | + [ |
| 17 | + compilers: Mix.compilers() ++ [:phoenix_iconify], |
| 18 | + # ... |
| 19 | + ] |
| 20 | + end |
| 21 | +
|
| 22 | + """ |
| 23 | + |
| 24 | + use Mix.Task.Compiler |
| 25 | + |
| 26 | + alias PhoenixIconify.{Cache, Manifest, Scanner} |
| 27 | + |
| 28 | + @recursive true |
| 29 | + |
| 30 | + @impl true |
| 31 | + def run(_args) do |
| 32 | + # Ensure Finch is started for HTTP requests |
| 33 | + {:ok, _} = Application.ensure_all_started(:req) |
| 34 | + |
| 35 | + # Scan source files for icon usage |
| 36 | + scanned_icons = Scanner.scan() |
| 37 | + |
| 38 | + # Add extra icons from config (for dynamic usage) |
| 39 | + extra_icons = |
| 40 | + Application.get_env(:phoenix_iconify, :extra_icons, []) |
| 41 | + |> Enum.map(&PhoenixIconify.normalize_name/1) |
| 42 | + |> Enum.reject(&is_nil/1) |
| 43 | + |
| 44 | + icons = Enum.uniq(scanned_icons ++ extra_icons) |> Enum.sort() |
| 45 | + |
| 46 | + if icons == [] do |
| 47 | + {:ok, []} |
| 48 | + else |
| 49 | + process_icons(icons) |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + defp process_icons(icon_names) do |
| 54 | + # Validate icon names |
| 55 | + {valid, invalid} = |
| 56 | + Enum.split_with(icon_names, fn name -> |
| 57 | + case Iconify.parse_name(name) do |
| 58 | + {:ok, _, _} -> true |
| 59 | + :error -> false |
| 60 | + end |
| 61 | + end) |
| 62 | + |
| 63 | + # Warn about invalid icon names |
| 64 | + for name <- invalid do |
| 65 | + Mix.shell().error( |
| 66 | + "PhoenixIconify: Invalid icon name format: #{inspect(name)}. " <> |
| 67 | + "Expected format: \"prefix:icon-name\" (e.g., \"heroicons:user\")" |
| 68 | + ) |
| 69 | + end |
| 70 | + |
| 71 | + # Read existing manifest |
| 72 | + manifest = Manifest.read() |
| 73 | + |
| 74 | + # Find icons we don't have yet |
| 75 | + missing = |
| 76 | + valid |
| 77 | + |> Enum.reject(&Map.has_key?(manifest, &1)) |
| 78 | + |
| 79 | + if missing == [] do |
| 80 | + # All icons already cached |
| 81 | + {:ok, []} |
| 82 | + else |
| 83 | + # Fetch missing icons |
| 84 | + Mix.shell().info("PhoenixIconify: Fetching #{length(missing)} icon(s)...") |
| 85 | + |
| 86 | + fetched = fetch_icons(missing) |
| 87 | + |
| 88 | + # Update manifest |
| 89 | + updated = Map.merge(manifest, fetched) |
| 90 | + Manifest.write(updated) |
| 91 | + |
| 92 | + # Clear the persistent_term cache so it reloads |
| 93 | + Manifest.clear_cache() |
| 94 | + |
| 95 | + fetched_count = map_size(fetched) |
| 96 | + failed_count = length(missing) - fetched_count |
| 97 | + |
| 98 | + if failed_count > 0 do |
| 99 | + Mix.shell().info("PhoenixIconify: Fetched #{fetched_count}, failed #{failed_count}") |
| 100 | + else |
| 101 | + Mix.shell().info("PhoenixIconify: Fetched #{fetched_count} icon(s)") |
| 102 | + end |
| 103 | + |
| 104 | + {:ok, []} |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + defp fetch_icons(icon_names) do |
| 109 | + # Group by prefix for efficient fetching |
| 110 | + results = |
| 111 | + icon_names |
| 112 | + |> Enum.group_by(fn name -> |
| 113 | + case Iconify.parse_name(name) do |
| 114 | + {:ok, prefix, _icon_name} -> prefix |
| 115 | + :error -> nil |
| 116 | + end |
| 117 | + end) |
| 118 | + |> Enum.reject(fn {prefix, _} -> is_nil(prefix) end) |
| 119 | + |> Enum.flat_map(fn {prefix, names} -> |
| 120 | + fetch_prefix_icons(prefix, names) |
| 121 | + end) |
| 122 | + |
| 123 | + # Warn about icons that couldn't be fetched |
| 124 | + fetched_names = Enum.map(results, fn {name, _} -> name end) |
| 125 | + not_found = icon_names -- fetched_names |
| 126 | + |
| 127 | + for name <- not_found do |
| 128 | + Mix.shell().error("PhoenixIconify: Icon not found: #{name}") |
| 129 | + end |
| 130 | + |
| 131 | + Map.new(results) |
| 132 | + end |
| 133 | + |
| 134 | + defp fetch_prefix_icons(prefix, full_names) do |
| 135 | + # Extract just the icon names (without prefix) |
| 136 | + icon_names = |
| 137 | + full_names |
| 138 | + |> Enum.map(fn name -> |
| 139 | + {:ok, _prefix, icon_name} = Iconify.parse_name(name) |
| 140 | + icon_name |
| 141 | + end) |
| 142 | + |
| 143 | + # Try to get icons from cache first |
| 144 | + case Cache.fetch_set(prefix) do |
| 145 | + {:ok, set} -> |
| 146 | + # Get icons from cached set |
| 147 | + Enum.flat_map(icon_names, fn icon_name -> |
| 148 | + case Iconify.Set.get(set, icon_name) do |
| 149 | + {:ok, icon} -> |
| 150 | + full_name = "#{prefix}:#{icon_name}" |
| 151 | + data = %{body: icon.body, viewbox: Iconify.Icon.viewbox(icon)} |
| 152 | + [{full_name, data}] |
| 153 | + |
| 154 | + :error -> |
| 155 | + [] |
| 156 | + end |
| 157 | + end) |
| 158 | + |
| 159 | + {:error, _} -> |
| 160 | + # Fall back to API for individual icons |
| 161 | + fetch_icons_from_api(prefix, icon_names) |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + defp fetch_icons_from_api(prefix, icon_names) do |
| 166 | + case Iconify.Fetcher.fetch_icons(prefix, icon_names) do |
| 167 | + {:ok, icons} -> |
| 168 | + Enum.map(icons, fn {name, icon} -> |
| 169 | + full_name = "#{prefix}:#{name}" |
| 170 | + data = %{body: icon.body, viewbox: Iconify.Icon.viewbox(icon)} |
| 171 | + {full_name, data} |
| 172 | + end) |
| 173 | + |
| 174 | + {:error, reason} -> |
| 175 | + Mix.shell().error("PhoenixIconify: Failed to fetch #{prefix} icons: #{inspect(reason)}") |
| 176 | + [] |
| 177 | + end |
| 178 | + end |
| 179 | +end |
0 commit comments