# Ship a CLI

Run a pane check in a fresh Mix project using libtmux 0.1.3 from Hex, then
run the same check from a relocated Mix release. The last smoke test
shows what a plain escript built with the published version can run.

| Delivery | Smoke test |
| --- | --- |
| Mix project with a Hex dependency | Reads a pane ID |
| Relocated Mix release | Reads a pane ID |
| Plain escript with libtmux 0.1.3 | Validates a reference. Helper launch fails. |

For a CLI built from this source tree, choose the delivery method that
matches its use of tmux.

- [Deploy a single-file escript](ownership.md#deploy-a-single-file-escript)
  for a command that starts and stops its own server.
- [Deploy an escript with its helper](ownership.md#deploy-an-escript-with-its-helper)
  for a command that attaches to an existing server or keeps a control connection.

## Build a Mix consumer

On Linux, use Elixir and OTP from the
[support requirements](implementation.md#packages-and-support), a C11
compiler, Linux headers and tmux on `PATH`. Start in a directory where
`pane_demo` does not exist.

```console
$ unset TMUX TMUX_PANE LIBTMUX_SOCKET LIBTMUX_SOCKET_PATH && \
    mix new pane_demo && cd pane_demo
```

Replace `mix.exs` with this project. The guide pins Hex libtmux 0.1.3, which
needs the consumer's `application/0` below to include OTP's `:crypto` in
the release. If you use libtmux from the checkout that ships the escript
recipes above, omit that function. The libtmux application includes
`:crypto` in the release itself.

```elixir
defmodule PaneDemo.MixProject do
  use Mix.Project

  def project do
    [
      app: :pane_demo,
      version: "0.1.0",
      deps: [{:libtmux, "0.1.3"}],
      escript: [main_module: PaneDemo, include_priv_for: [:libtmux]]
    ]
  end

  def application, do: [extra_applications: [:crypto]]
end
```

Replace `lib/pane_demo.ex` with the program below. It prints the pane ID
after `Server.with_owned/2` has stopped the private server.

<!-- example: examples/pane_demo.exs -->

```elixir
defmodule PaneDemo do
  @moduledoc "Prints the ID of the sole pane on a private tmux server."

  alias LibTmux.{Ref, Selection, Server, Snapshot}

  @doc "Runs the pane smoke test, or validates a pane reference with --pure."
  def main(["--pure"]) do
    {:ok, ref} = Ref.new(:pane, "%0", "demo")
    :ok = Ref.validate(ref, "demo")
    IO.puts("pure: ok")
  end

  def main([]) do
    {:ok, _} = Application.ensure_all_started(:libtmux)
    tmux = System.find_executable("tmux") || raise "tmux is not on PATH"

    pane =
      Server.with_owned([executable: tmux], fn server ->
        Server.new_session!(server, "demo", command: {:argv, ["/bin/cat"]})
        server |> Server.snapshot!() |> Snapshot.panes() |> Selection.one!()
      end)

    IO.puts(pane.id)
  end
end
```

<!-- /example -->

Fetch the published dependency and run the CLI from the project. Mix builds
the helper during dependency compilation. The last line is `%0`.

```console
$ mix deps.get && MIX_ENV=prod mix run -e 'PaneDemo.main([])'
```

## Move the release

Assemble the release and move the whole release directory to a fresh
location. The default release includes ERTS, so the target needs a
compatible Linux system and tmux on `PATH`. `eval` runs a one-off VM, and
the program explicitly starts the library.

```console
$ MIX_ENV=prod mix release && \
    delivery=$(mktemp -d) && \
    mv _build/prod/rel/pane_demo "$delivery/release" && \
    (cd "$delivery" && ./release/bin/pane_demo eval 'PaneDemo.main([])')
```

The last line is `%0`.

Deploy the entire release directory. For the pinned libtmux 0.1.3 dependency,
the helper lives under `priv/native` in the installed application.

## Plain escript with libtmux 0.1.3

Build the escript in the project and move the single file to the same
delivery directory. Erlang's `escript` command must be on `PATH`.

```console
$ MIX_ENV=prod mix escript.build && \
    mv pane_demo "$delivery/pane_demo" && \
    (cd "$delivery" && ./pane_demo --pure)
```

The last line is `pure: ok`.

Run the native command against that same file. It exits with status 1.

```console
$ (cd "$delivery" && ./pane_demo)
```

```text
** (LibTmux.Error) launch_failed during spawn
```

The pure check runs from the archive. To start tmux, libtmux 0.1.3 looks
for an executable helper on the filesystem. The relocated Mix release
supplies that file. For a CLI built from this checkout, follow one of the
two escript deployment recipes above.

See [Building and distribution](ownership.md#building-and-distribution)
for the helper's build contract.
