MobNew.VersionCheck (mob_new v0.4.11)

Copy Markdown View Source

Best-effort "is a newer mob.new available?" check, in the spirit of phx.new's installer-version notice.

mix mob.new calls print_notice/0 once at the end of a run. It fetches the latest published version from the Hex API and, if the installed archive is behind, prints a one-line hint to update via mix archive.install hex mob_new.

Constraints this module respects:

  • Archive-safe. mob_new ships as a Mix .ez that bundles only its own beams, so this code touches OTP/Elixir stdlib only (:httpc, :ssl, Version, Regex) — never a hex dep like Jason. The JSON body is parsed with a narrow regex rather than a JSON library for the same reason.
  • Never fatal, never slow. Every failure mode (offline, DNS, timeout, malformed body, unparseable version) collapses to "say nothing." The network call is capped by a short timeout so a fresh mix mob.new never hangs on it.

Summary

Functions

The version of this installed mob_new archive.

Fetches the latest published version string from the Hex API.

The user-facing notice given the current version and a fetch_latest/1 result. Returns the message string when the installed archive is strictly behind the latest, otherwise nil (up to date, ahead, fetch failed, or either version is unparseable). Pure — public for testing.

Extracts the latest stable version from a Hex package API JSON body without a JSON dependency. Prefers latest_stable_version, falls back to latest_version. Returns {:ok, version} or :error. Pure — public for testing.

Fetches the latest version and prints an update hint if one is warranted. Always returns :ok; never raises and never blocks beyond the fetch timeout.

Functions

current_version()

@spec current_version() :: String.t()

The version of this installed mob_new archive.

fetch_latest(timeout \\ 2000)

@spec fetch_latest(non_neg_integer()) :: {:ok, String.t()} | :error

Fetches the latest published version string from the Hex API.

Returns {:ok, version} or :error (any failure). Best-effort, capped by timeout ms. Public for testing the orchestration boundary.

notice(current, arg2)

@spec notice(String.t(), {:ok, String.t()} | :error) :: String.t() | nil

The user-facing notice given the current version and a fetch_latest/1 result. Returns the message string when the installed archive is strictly behind the latest, otherwise nil (up to date, ahead, fetch failed, or either version is unparseable). Pure — public for testing.

parse_latest(body)

@spec parse_latest(binary()) :: {:ok, String.t()} | :error

Extracts the latest stable version from a Hex package API JSON body without a JSON dependency. Prefers latest_stable_version, falls back to latest_version. Returns {:ok, version} or :error. Pure — public for testing.