Skip to content

Use vim.fn.system or vim.system to run copyfile and symlink #398

@ReKylee

Description

@ReKylee

Using ":exec !cmake -E ..." forces the program to use the current nvim shell, but for users with wsl for example it's not always possible or desired.
Instead, something like this could be used to run the cmake commands using the OS's PATH and found cmake.
I don't have a better solution atm, but I use WSL as my shell with cmake installed on windows, and both commands fail with no logging or printing which is so tiresome to debug.
This can definitely be more optimized, and it should be before it's used, but the idea is to log the responses to the scratch buffer or somewhere and to execute with vim.system or vim.fn.system.

local function run_cmake_e(args, label)
  local command = vim.list_extend({ "cmake", "-E" }, args)

  vim.notify(
    string.format("[cmake-tools] %s: %s", label, vim.inspect(command)),
    vim.log.levels.DEBUG
  )

  local result = vim.system(command, { text = true }):wait()

  if result.code ~= 0 then
    vim.notify(
      string.format(
        "[cmake-tools] %s failed with code %d\nstdout:\n%s\nstderr:\n%s",
        label,
        result.code,
        result.stdout or "",
        result.stderr or ""
      ),
      vim.log.levels.ERROR
    )

    return false
  end

  vim.notify(
    string.format("[cmake-tools] %s succeeded", label),
    vim.log.levels.DEBUG
  )

  return true
end

function utils.copyfile(src, target)
  vim.notify("[cmake-tools copyfile] src: " .. src, vim.log.levels.DEBUG)
  vim.notify("[cmake-tools copyfile] target: " .. target, vim.log.levels.DEBUG)

  if not utils.file_exists(src) then
    vim.notify("[cmake-tools copyfile] source does not exist, skipping", vim.log.levels.WARN)
    return false
  end

  return run_cmake_e({
    "copy",
    src,
    target,
  }, "copyfile")
end

function utils.softlink(src, target)
  vim.notify("[cmake-tools softlink] src: " .. src, vim.log.levels.DEBUG)
  vim.notify("[cmake-tools softlink] target: " .. target, vim.log.levels.DEBUG)

  if not utils.file_exists(src) then
    vim.notify("[cmake-tools softlink] source does not exist, skipping", vim.log.levels.WARN)
    return false
  end

  local stat = vim.loop.fs_lstat(target)

  if stat then
    if stat.type == "link" then
      local current = vim.loop.fs_readlink(target)

      if current == src then
        vim.notify("[cmake-tools softlink] link already points to source", vim.log.levels.DEBUG)
        return true
      end

      local ok, err = os.remove(target)
      if not ok then
        vim.notify("[cmake-tools softlink] failed to remove stale link: " .. tostring(err), vim.log.levels.ERROR)
        return false
      end
    else
      local ok, err = os.remove(target)
      if not ok then
        vim.notify("[cmake-tools softlink] failed to remove existing target: " .. tostring(err), vim.log.levels.ERROR)
        return false
      end
    end
  end

  return run_cmake_e({
    "create_symlink",
    src,
    target,
  }, "softlink")
end```

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions