Current section

Files

Jump to
nerves_bootstrap lib mix tasks nerves.bootstrap.ex
Raw

lib/mix/tasks/nerves.bootstrap.ex

# SPDX-FileCopyrightText: 2022 Jon Carstens
# SPDX-FileCopyrightText: 2025 Frank Hunleth
#
# SPDX-License-Identifier: Apache-2.0
#
defmodule Mix.Tasks.Nerves.Bootstrap do
# Bootstrap Nerves tooling into the current Mix tooling
#
# The purpose of this task is to ensure the Nerves integration is compiled
# first and available to be injected into the Mix tooling since NervesBootstrap
# is available as an archive. (i.e. this is like having `:nerves` as a
# dependency of this archive even though archives do not allow traditional
# dependencies)
#
# It is not intended to be run manually
@moduledoc false
use Mix.Task
alias NervesBootstrap.UpdateChecker
@impl Mix.Task
def run(_args) do
UpdateChecker.check()
nerves_ver = nerves_version()
if is_nil(nerves_ver) do
Mix.raise("""
Your project is using Nerves bootstrap but doesn't depend on Nerves.
Please check the dependency section of your mix.exs.
""")
end
# Check that the Nerves version is new enough to support this
# version of Nerves Bootstrap.
#
# This version of Nerves Bootstrap fails to install when the
# Elixir version is less than 1.15, so we're guaranteed that
# version. That forces a constraint on what Nerves versions
# can be used. Here's a truncated list for convenience.
#
# | Nerves version | Min Elixir version | Max Elixir |
# |----------------|-----------------------------|------------|
# | 1.11.3 | 1.13 | 1.18 |
# | 1.12.0 | 1.15.1 | 1.19. |
# | 1.14.3 | 1.15.1 (inherited) | 1.20. |
if Version.match?(nerves_ver, "< 1.11.3") do
Mix.raise("""
You are using :nerves #{nerves_ver} which is incompatible with this version
of nerves_bootstrap and will result in compilation failures.
Please update to :nerves >= 1.11.3.
""")
end
if Mix.target() != :host and not Code.ensure_loaded?(Nerves.Env) do
# The tooling mix tasks are maintained in :nerves so we need to
# ensure it is compiled here first so the tasks are available.
# If the target is host, then this will be handled by the regular
# compilation process
_ = Mix.Tasks.Deps.Loadpaths.run(["--no-compile"])
Mix.Tasks.Deps.Compile.run(["nerves", "--include-children"])
end
# Do not call Nerves Bootstrap code from other modules here. The modules
# aren't available in Elixir 1.18 and earlier if `:nerves` doesn't pull
# them in.
end
defp nerves_version() do
if path = Mix.Project.deps_paths()[:nerves] do
Mix.Project.in_project(:nerves, path, fn _ -> Mix.Project.config()[:version] end)
end
catch
_, _ -> nil
end
end