Skip to content

Instantly share code, notes, and snippets.

@GoNZooo
Last active April 23, 2023 09:14
Show Gist options
  • Save GoNZooo/2c62ca618aef1aeefc562e416fdde668 to your computer and use it in GitHub Desktop.
Save GoNZooo/2c62ca618aef1aeefc562e416fdde668 to your computer and use it in GitHub Desktop.
A basic setup for being able to interact with your `iex` sessions live via RPC calls
local M = {}
local erl_call = "erl -noshell -sname neovim -eval"
M.beam_dev_helper_rpc = function(node, module, function_name)
return "rpc:call(" .. node .. ", " .. module .. ", " .. function_name .. ", []), halt(0)."
end
M.execute_erlang_rpc_call = function(node, module, function_name)
local command = erl_call .. " \"" .. M.beam_dev_helper_rpc(node, module, function_name) .. "\""
vim.cmd(":silent !" .. command)
end
function execute(function_name)
local filetype = vim.bo.filetype
if filetype == "erlang" then
M.execute_erlang_rpc_call("term@archlinux", "terminal_dev_helpers", function_name)
elseif filetype == "elixir" then
M.execute_erlang_rpc_call("term@archlinux", "'Elixir.TerminalDevHelpers'", function_name)
end
end
M.compile = function()
execute("compile")
end
M.run_tests = function()
execute("run_tests")
end
return M
defmodule TerminalDevHelpers.Runner do
use GenServer
def start_link([]) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
@impl true
def init(:ok) do
{:ok, :no_state}
end
def execute(f) do
GenServer.cast(__MODULE__, {:execute, f})
end
@impl true
def handle_cast({:execute, f}, state) do
f.()
{:noreply, state}
end
end
defmodule TerminalDevHelpers do
require Logger
def compile() do
TerminalDevHelpers.Runner.execute(fn ->
IEx.Helpers.recompile()
end)
end
def run_tests() do
TerminalDevHelpers.Runner.execute(fn ->
Application.ensure_all_started(:ex_unit)
Code.eval_file("test/test_helper.exs", File.cwd!())
ExUnit.configure(exclude: [], include: [])
Mix.Task.clear()
IEx.Helpers.recompile()
reload_tests()
ExUnit.Server.modules_loaded(true)
ExUnit.run()
end)
end
defp reload_tests() do
test_files = Path.wildcard("test/**/*_test.exs")
case Kernel.ParallelCompiler.compile(test_files) do
{:ok, _modules, _warnings} ->
:ok
{:error, errors, warnings} ->
Enum.each(warnings, fn {file, _line_number, message} ->
Logger.warn("Compilation warning in #{file}: #{message}")
end)
Enum.each(errors, fn {file, _line_number, message} ->
Logger.error("Compilation error in #{file}: #{message}")
end)
Mix.raise("Compilation failed")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment