diff --git a/lua/cp/scraper.lua b/lua/cp/scraper.lua index ecd3fa0..7ddca0a 100644 --- a/lua/cp/scraper.lua +++ b/lua/cp/scraper.lua @@ -44,6 +44,10 @@ local function run_scraper(platform, subcommand, args, opts) return { success = false, error = msg } end + if subcommand == 'submit' then + utils.setup_nix_submit_env() + end + local plugin_path = utils.get_plugin_path() local cmd if subcommand == 'submit' then diff --git a/lua/cp/utils.lua b/lua/cp/utils.lua index 578ca6c..ac55dbb 100644 --- a/lua/cp/utils.lua +++ b/lua/cp/utils.lua @@ -123,6 +123,71 @@ function M.get_python_submit_cmd(module, plugin_path) end local python_env_setup = false +local _nix_submit_attempted = false + +---@return boolean +local function discover_nix_submit_cmd() + local cache_dir = vim.fn.stdpath('cache') .. '/cp-nvim' + local cache_file = cache_dir .. '/nix-submit' + + local f = io.open(cache_file, 'r') + if f then + local cached = f:read('*l') + f:close() + if cached and vim.fn.executable(cached) == 1 then + _nix_submit_cmd = cached + return true + end + end + + local plugin_path = M.get_plugin_path() + vim.notify('[cp.nvim] Building submit environment with nix...', vim.log.levels.INFO) + vim.cmd.redraw() + local result = vim + .system( + { 'nix', 'build', plugin_path .. '#submitEnv', '--no-link', '--print-out-paths' }, + { text = true } + ) + :wait() + + if result.code ~= 0 then + logger.log('nix build #submitEnv failed: ' .. (result.stderr or ''), vim.log.levels.WARN) + return false + end + + local store_path = result.stdout:gsub('%s+$', '') + local submit_cmd = store_path .. '/bin/cp-nvim-submit' + + if vim.fn.executable(submit_cmd) ~= 1 then + logger.log('nix submit cmd not executable at ' .. submit_cmd, vim.log.levels.WARN) + return false + end + + vim.fn.mkdir(cache_dir, 'p') + f = io.open(cache_file, 'w') + if f then + f:write(submit_cmd) + f:close() + end + + _nix_submit_cmd = submit_cmd + return true +end + +---@return boolean +function M.setup_nix_submit_env() + if _nix_submit_cmd then + return true + end + if _nix_submit_attempted then + return false + end + _nix_submit_attempted = true + if vim.fn.executable('nix') == 1 then + return discover_nix_submit_cmd() + end + return false +end ---@return boolean local function discover_nix_python()