Packages
nerves_runtime
0.5.0
0.13.13
0.13.12
0.13.11
retired
0.13.10
0.13.9
0.13.8
0.13.7
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.10
0.11.9
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
retired
0.9.5
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.0
0.7.0
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.3
0.5.2
0.5.1
0.5.0
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.1
0.3.0
0.2.0
0.1.2
0.1.1
0.1.0
Small, general runtime utilities for Nerves devices
Current section
Files
Jump to
Current section
Files
lib/nerves_runtime/helpers.ex
defmodule Nerves.Runtime.Helpers do
# This is the path on all official Nerves systems
@iex_exs_path "/root/.iex.exs"
@moduledoc """
Helper functions for making the IEx prompt a little friendlier to use
with Nerves. It is intended to be imported to minimize typing:
iex> use Nerves.Runtime.Helpers
For development, you may want to run
iex> Nerves.Runtime.Helpers.install
on the target so that the helpers get automatically imported on each boot.
Helpers include:
* `cmd/1` - runs a shell command and prints the output
* `hex/1` - inspects a value with integers printed as hex
* `reboot/0` - reboots gracefully
* `reboot!/0` - reboots immediately
Help for all of these can be found by running:
iex> h(Nerves.Runtime.Helpers.cmd/1)
"""
defmacro __using__(_) do
quote do
import Nerves.Runtime.Helpers, except: [install: 0]
IO.puts("Nerves.Runtime.Helpers imported. Run h(Nerves.Runtime.Helpers) for more info")
end
end
@doc """
Install the helpers so that they're autoloaded on subsequent reboots.
"""
def install() do
case File.exists?(@iex_exs_path) do
false ->
File.write!(@iex_exs_path, "use Nerves.Runtime.Helpers")
IO.puts("Helpers installed and will be loaded on next reboot. To use")
IO.puts("them now, run `use Nerves.Runtime.Helpers`")
true ->
IO.puts("#{@iex_exs_path} already exists.")
IO.puts("Please manually add 'use Nerves.Runtime.Helpers' if it's not already there.")
end
end
@doc """
Run a command using :os.cmd/1 and run its output
through IO.puts so that newlines get printed nicely.
"""
def cmd(str) when is_binary(str) do
cmd(to_charlist(str))
end
def cmd(str) when is_list(str) do
:os.cmd(str) |> IO.puts
end
@doc """
Shortcut to reboot a board. This is a graceful reboot, so it takes
some time before the real reboot.
"""
defdelegate reboot(), to: Nerves.Runtime
@doc """
Remote immediately without a graceful shutdown. This is for the
impatient.
"""
def reboot!() do
Nerves.Runtime.reboot()
:erlang.halt()
end
@doc """
Inspect a value with all integers printed out in hex. This is useful
for one-off hex conversions. If you're doing a lot of work that requires
hexadecimal output, you should consider running:
`IEx.configure(inspect: [base: :hex])`
The drawback of doing the above is that strings print out as hex binaries.
"""
def hex(value) do
inspect(value, base: :hex)
end
end