defmodule Mix.Tasks.Compile.ToweropsNif do
  @moduledoc """
  Compiles the towerops_nif C NIF.
  """

  use Mix.Task.Compiler

  @impl true
  def run(_args) do
    {result, _exit_code} = System.cmd("make", [], cd: "c_src", stderr_to_stdout: true, into: IO.stream())

    case result do
      "" ->
        {:ok, []}

      _ ->
        # Output was produced, so the build ran
        {:ok, []}
    end
  rescue
    e ->
      Mix.shell().error("Failed to compile C NIF: #{inspect(e)}")
      {:error, []}
  end

  @impl true
  def clean do
    {_result, _exit_code} = System.cmd("make", ["clean"], cd: "c_src", stderr_to_stdout: true)
    :ok
  rescue
    _ -> :ok
  end
end
