 
   defp nerves_br_version_to_metadata(nerves_br_version) do
     [
-      Task.async(API, :fetch_buildroot_version!, [nerves_br_version]),
-      Task.async(API, :fetch_otp_version!, [nerves_br_version])
+      Task.async(NervesSystemCompatibility.API, :fetch_buildroot_version!, [nerves_br_version]),
+      Task.async(NervesSystemCompatibility.API, :fetch_otp_version!, [nerves_br_version])
     ]
     |> Task.await_many(:timer.seconds(10))
     |> Enum.reduce(%{"nerves_br_version" => nerves_br_version}, fn %{} = data, acc ->


  @spec fetch_nerves_system_versions! :: keyword([binary])
  def fetch_nerves_system_versions! do
    NervesSystemCompatibility.target_systems()
    |> Task.async_stream(&{&1, fetch_nerves_system_versions!(&1)}, timeout: 10_000)
    |> Enum.reduce([], fn {:ok, kv}, acc -> [kv | acc] end)
  end

#!/usr/bin/env elixir

#
# Write the Nerves System compatibility information to local `tmp` directory.
# The information is gathered from Nerves Project's Github repos.
# By default, the Github API only allows us 60 requests per hour.
# With an API token, we could get our rate limit bumped to 5000 requests an hour.
# See https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api#authentication
#
# ## Usage
#
#   scripts/nerves_system_compatibility.exs
#
#   GITHUB_API_TOKEN=xxxxxx nerves_system_compatibility.exs
#

Mix.install([:req, :earmark])

defmodule NervesSystemCompatibility do
  @targets [:bbb, :rpi, :rpi0, :rpi2, :rpi3, :rpi3a, :rpi4, :osd32mp1, :x86_64, :grisp2]
  def targets, do: @targets

  def run do
    compatibility_data = NervesSystemCompatibility.Data.get()
    data_by_target = NervesSystemCompatibility.Data.group_data_by_target(compatibility_data)

    html =
      for target <- @targets do
        table_html =
          NervesSystemCompatibility.Table.build(
            target,
            Access.fetch!(data_by_target, target),
            NervesSystemCompatibility.Data.list_target_system_versions(compatibility_data, target)
          )
          |> to_string()
          |> Earmark.as_html!()

        "<details><summary>#{target}</summary>#{table_html}</details>"
        |> String.replace(~r/\s+/, " ")
        |> String.replace(~r/> </, "><")
        |> String.replace(~r/ style="text-align: left;"/, "")
      end
      |> Enum.join("\n")

    File.mkdir("tmp")
    file = "tmp/nerves_system_compatibility_#{DateTime.to_unix(DateTime.utc_now())}.html"
    IO.puts("writing the Nerves System compatibility information to #{file}")
    File.write!(file, html)
  end
end

