Skip to content

Commit

Permalink
Use lua directly to run files
Browse files Browse the repository at this point in the history
  • Loading branch information
JafarAbdi committed Aug 26, 2024
1 parent 81ca4bd commit 7bef982
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 148 deletions.
108 changes: 68 additions & 40 deletions neovim/.config/nvim/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -262,62 +262,90 @@ _G.dap_status = function()
return ""
end

local runners = {
python = function(file_path, root_dir, is_test)
local python_executable = "python3"
if vim.uv.fs_stat(vim.fs.joinpath(root_dir, ".pixi")) ~= nil then
python_executable = vim.fs.joinpath(root_dir, ".pixi", "envs", "default", "bin", "python")
end
if is_test then
if not file_path:match("^test_") and not file_path:match("_test%.py$") then
print(
string.format(
"Test file '%s' doesn't start/end with 'test_'/'_test' and will be ignored by pytest",
file_path
)
)
end
return {
python_executable,
"-m",
"pytest",
"--capture=no",
file_path,
}
end
return {
python_executable,
file_path,
}
end,
bash = function(file_path, root_dir, is_test)
return {
"bash",
file_path,
}
end,
fish = function(file_path, root_dir, is_test)
return {
"fish",
file_path,
}
end,
xml = function(file_path, root_dir, is_test)
return {
"curl",
"-X",
"POST",
"http://127.0.0.1:7777/set_reload_request",
}
end,
lua = function(file_path, root_dir, is_test)
vim.cmd.source(file_path)
end,
}
runners.sh = runners.bash

local run_file = function(is_test)
local filetype = vim.api.nvim_get_option_value("filetype", {})
if not filetype or filetype == "" then
return
end
vim.print(filetype)

local runner = runners[filetype]
if not runner then
return
end

local dirname = vim.fn.expand("%:p:h")
local root_dir = root_dirs[filetype]
if root_dir then
root_dir = root_dir(dirname) or dirname
else
root_dir = dirname
for dir in vim.fs.parents(vim.api.nvim_buf_get_name(0)) do
if vim.env.HOME == dir then
break
end
if vim.fn.isdirectory(dir .. "/.vscode") == 1 then
root_dir = dir
break
end
or function(startpath)
return vim.fs.root(startpath, { ".git" })
end
end
root_dir = root_dir(dirname) or dirname

if
not vim.api.nvim_buf_get_option(0, "readonly") and vim.api.nvim_buf_get_option(0, "modified")
then
vim.cmd.write()
end
local args = {
"--workspace-folder",
root_dir,
"--filetype",
filetype,
"--file-path",
vim.fn.expand("%:p"),
}
local cmd = "build_project.py"
if filetype ~= "python" then
cmd = "pixi"
for _, v in
ipairs(vim.fn.reverse({
"run",
"--manifest-path",
"~/myconfigs/pixi.toml",
"--frozen",
"python3",
"~/.local/bin/build_project.py",
}))
do
table.insert(args, 1, v)
end
end
if is_test then
table.insert(args, "--test")

local cmd = runner(vim.fn.expand("%:p"), root_dir, is_test)
if not cmd then
return
end
term.run(cmd, args, { cwd = root_dir, auto_close = false })
term.run(cmd[1], vim.list_slice(cmd, 2), { cwd = root_dir, auto_close = false })
end

----------------
Expand Down
108 changes: 0 additions & 108 deletions scripts/.local/bin/build_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,86 +17,6 @@
CMAKE_REPLY_DIR: Final = Path(".cmake") / "api" / "v1" / "reply"


def lua(file: Path, args: list, cwd: Path, extra_args: dict, *, is_test: bool) -> None:
"""Run a lua file.
Args:
file: File to run
args: Arguments to pass to the file when running it
cwd: Current working directory
extra_args: Generic arguments to be used by the runner
is_test: Whether the file is a test or not
"""
env = os.environ.copy()
env.pop("NVIMRUNNING", None)
run_command(
["nvim", "--headless", "-n", "-c", "source", str(file), "-c", "qall!", *args],
dry_run=False,
cwd=cwd,
env=env,
)


def bash(file: Path, args: list, cwd: Path, extra_args: dict, *, is_test: bool) -> None:
"""Run a sh file.
Args:
file: File to run
args: Arguments to pass to the file when running it
cwd: Current working directory
extra_args: Generic arguments to be used by the runner
is_test: Whether the file is a test or not
"""
run_command(["bash", str(file), *args], dry_run=False, cwd=cwd)


def fish(file: Path, args: list, cwd: Path, extra_args: dict, *, is_test: bool) -> None:
"""Run a fish file.
Args:
file: File to run
args: Arguments to pass to the file when running it
cwd: Current working directory
extra_args: Generic arguments to be used by the runner
is_test: Whether the file is a test or not
"""
run_command(["fish", str(file), *args], dry_run=False, cwd=cwd)


def python(
file: Path,
args: list,
cwd: Path,
extra_args: dict,
*,
is_test: bool,
) -> None:
"""Run a python file.
Args:
file: File to run
args: Arguments to pass to the file when running it
cwd: Current working directory
extra_args: Generic arguments to be used by the runner
is_test: Whether the file is a test or not
"""
cmd = []
python_path = "python3"
if (pixi_prefix := find_rootdir(".pixi")(file)) is not None:
python_path = pixi_prefix / ".pixi" / "envs" / "default" / "bin" / "python"
if is_test:
if not file.name.startswith("test_") and not file.name.endswith("_test.py"):
logging.error(
f"Test file '{file}' doesn't start/end with 'test_'/'_test' and will be ignored by pytest",
)
return
cmd.extend([python_path, "-m", "pytest", "--capture=no", str(file.name)])
cwd = file.parent
else:
cmd.extend([python_path, str(file)])
run_command(cmd + args, dry_run=False, cwd=cwd)


def rust(file: Path, args: list, cwd: Path, extra_args: dict, *, is_test: bool) -> None:
"""Run a rust file.
Expand Down Expand Up @@ -293,38 +213,10 @@ def cmake(file: Path, args: list, cwd: Path, extra_args: dict) -> None:
return True


def xacro(
file: Path,
args: list,
cwd: Path,
extra_args: dict,
*,
is_test: bool,
) -> None:
"""Run a urdf/xaacro file.
Args:
file: File to run
args: Arguments to pass to the file when running it
cwd: Current working directory
extra_args: Generic arguments to be used by the runner
is_test: Weather the file is a test or not
"""
run_command(
["curl", "-X", "POST", "http://127.0.0.1:7777/set_reload_request"],
dry_run=False,
)


runners: Final = {
"lua": lua,
"python": python,
"rust": rust,
"cpp": cpp,
"fish": fish,
"xml": xacro,
"sh": bash,
"bash": bash,
}


Expand Down

0 comments on commit 7bef982

Please sign in to comment.