From 0cd9fa1476c499946b636387676df3d0e2723a3c Mon Sep 17 00:00:00 2001 From: phosphene47 Date: Tue, 14 Dec 2021 14:11:41 +1100 Subject: Change directory into the package dir when running hooks This makes running external commands like `make` from hooks easier to do. --- lua/dep.lua | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lua') diff --git a/lua/dep.lua b/lua/dep.lua index ca179c5..93d665a 100755 --- a/lua/dep.lua +++ b/lua/dep.lua @@ -240,6 +240,11 @@ local function run_hooks(package, type) local start = os.clock() + -- chdir into the package directory to make running external commands + -- from hooks easier. + local last_cwd = vim.fn.getcwd() + vim.fn.chdir(package.dir) + for i = 1, #hooks do local ok, err = pcall(hooks[i]) if not ok then @@ -248,6 +253,7 @@ local function run_hooks(package, type) end end + vim.fn.chdir(last_cwd) package.perf[type] = os.clock() - start logger:log( -- cgit v1.2.1 From b7ea828bdd98bca4eb188b2c5e5000a2dfccff05 Mon Sep 17 00:00:00 2001 From: phosphene47 Date: Tue, 14 Dec 2021 14:20:20 +1100 Subject: Ensure cwd is always restored to the original directory on hook failure --- lua/dep.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lua') diff --git a/lua/dep.lua b/lua/dep.lua index 93d665a..2ae3105 100755 --- a/lua/dep.lua +++ b/lua/dep.lua @@ -248,6 +248,8 @@ local function run_hooks(package, type) for i = 1, #hooks do local ok, err = pcall(hooks[i]) if not ok then + vim.fn.chdir(last_cwd) + package.error = true return false, err end -- cgit v1.2.1