-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add support for global deployment file #15
Changes from 5 commits
4bf4b44
1d7250c
0fca43c
20d6d72
9962e59
e10c6b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,29 @@ local config = require("transfer.config") | |
|
||
local M = {} | ||
|
||
-- reloads the buffer after a transfer | ||
-- refreshes the neo-tree if the buffer is a neo-tree | ||
-- @param bufnr number | ||
-- @return void | ||
local function file_exists(path) | ||
return vim.fn.filereadable(path) == 1 | ||
end | ||
|
||
local function load_config(path) | ||
if file_exists(path) then | ||
local success, result = pcall(dofile, path) | ||
if success then | ||
return result | ||
else | ||
vim.notify( | ||
"Error loading config file: " .. path .. "\n" .. result, | ||
vim.log.levels.ERROR, | ||
{ title = "Transfer.nvim" } | ||
) | ||
end | ||
end | ||
return {} | ||
end | ||
|
||
---reloads the buffer after a transfer | ||
---refreshes the neo-tree if the buffer is a neo-tree | ||
---@param bufnr number | ||
local function reload_buffer(bufnr) | ||
local filetype = vim.api.nvim_buf_get_option(bufnr, "filetype") | ||
if filetype == "neo-tree" then | ||
|
@@ -22,9 +41,9 @@ local function reload_buffer(bufnr) | |
end | ||
end | ||
|
||
-- convert the given local absolute path to a relative project root path | ||
-- @param absolute_path string | ||
-- @return string | ||
---convert the given local absolute path to a relative project root path | ||
---@param absolute_path string | ||
---@return string | ||
local function normalize_local_path(absolute_path) | ||
local cwd = vim.loop.cwd() | ||
local found, found_end = string.find(absolute_path, cwd, 1, true) | ||
|
@@ -35,10 +54,10 @@ local function normalize_local_path(absolute_path) | |
return string.gsub(absolute_path, "^/", "") | ||
end | ||
|
||
-- check if the given path matches the given pattern | ||
-- @param path string | ||
-- @param pattern string | ||
-- @return boolean | ||
---check if the given path matches the given pattern | ||
---@param path string | ||
---@param pattern string | ||
---@return boolean | ||
local function path_matches(path, pattern) | ||
pattern = string.gsub(pattern, "/$", "") | ||
path = string.gsub(path, "/$", "") | ||
|
@@ -56,10 +75,10 @@ local function path_matches(path, pattern) | |
return false | ||
end | ||
|
||
-- get the remote path for scp | ||
-- @param deployment table | ||
-- @param remote_file string | ||
-- @return string | ||
---get the remote path for scp | ||
---@param deployment table | ||
---@param remote_file string | ||
---@return string | ||
local function build_scp_path(deployment, remote_file) | ||
local remote_path = "scp://" | ||
if deployment.username then | ||
|
@@ -70,10 +89,10 @@ local function build_scp_path(deployment, remote_file) | |
return remote_path | ||
end | ||
|
||
-- get the excluded paths for the given directory | ||
-- @param deployment table | ||
-- @param dir string | ||
-- @return table | ||
---get the excluded paths for the given directory | ||
---@param deployment table | ||
---@param dir string | ||
---@return table | ||
function M.excluded_paths_for_dir(deployment, dir) | ||
local excludedPaths = {} | ||
if deployment and deployment.excludedPaths and #deployment.excludedPaths > 0 then | ||
|
@@ -97,15 +116,23 @@ function M.excluded_paths_for_dir(deployment, dir) | |
return excludedPaths | ||
end | ||
|
||
-- get the remote path for scp | ||
-- @param local_path string | ||
-- @return string | ||
---get the remote path for scp | ||
---@param local_path string | ||
---@return string? | ||
function M.remote_scp_path(local_path) | ||
local cwd = vim.loop.cwd() | ||
local config_file = cwd .. "/.nvim/deployment.lua" | ||
if vim.fn.filereadable(config_file) ~= 1 then | ||
local project_config_file = vim.loop.cwd() .. "/.nvim/deployment.lua" | ||
local global_config_file = vim.fn.stdpath("data") .. "/deployment.lua" | ||
local project_deployment_conf = load_config(project_config_file) | ||
local global_deployment_conf = load_config(global_config_file) | ||
local merged_deployment_conf = global_deployment_conf | ||
-- Merge configurations, project overrides global | ||
for name, deployment in pairs(project_deployment_conf) do | ||
merged_deployment_conf[name] = deployment | ||
end | ||
|
||
if vim.tbl_isempty(merged_deployment_conf) then | ||
vim.notify( | ||
"No deployment config found in \n" .. config_file .. "\n\nRun `:TransferInit` to create it", | ||
"No deployment config found in \n" .. project_config_file .. " or " .. global_config_file, | ||
vim.log.levels.WARN, | ||
{ | ||
title = "Transfer.nvim", | ||
|
@@ -115,12 +142,12 @@ function M.remote_scp_path(local_path) | |
) | ||
return nil | ||
end | ||
local deployment_conf = dofile(config_file) | ||
|
||
-- remove cwd from local file path | ||
local_path = normalize_local_path(local_path) | ||
|
||
local skip_reason | ||
for name, deployment in pairs(deployment_conf) do | ||
for name, deployment in pairs(merged_deployment_conf) do | ||
local skip = false | ||
if deployment.excludedPaths ~= nil then | ||
for _, excluded in pairs(deployment.excludedPaths) do | ||
|
@@ -183,9 +210,9 @@ function M.remote_scp_path(local_path) | |
return nil | ||
end | ||
|
||
-- get the remote path for rsync | ||
-- @param local_path string | ||
-- @return string | ||
---get the remote path for rsync | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason to change comment style? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bug in your highlighting. Treesitter itself and other popular lua plugins use --space in their codebase. |
||
---@param local_path string | ||
---@return string | ||
function M.remote_rsync_path(local_path) | ||
local remote_path, deployment = M.remote_scp_path(local_path) | ||
if remote_path == nil then | ||
|
@@ -198,9 +225,9 @@ function M.remote_rsync_path(local_path) | |
return remote_path, deployment | ||
end | ||
|
||
-- upload the given file | ||
-- @param local_path string | ||
-- @return void | ||
---upload the given file | ||
---@param local_path string | ||
---@return void | ||
function M.upload_file(local_path) | ||
if local_path == nil then | ||
local_path = vim.fn.expand("%:p") | ||
|
@@ -249,8 +276,8 @@ function M.upload_file(local_path) | |
}) | ||
end | ||
|
||
-- Replace local file with remote copy | ||
-- @param local_path string|nil | ||
---Replace local file with remote copy | ||
---@param local_path string|nil | ||
function M.download_file(local_path) | ||
if local_path == nil then | ||
local_path = vim.fn.expand("%:p") | ||
|
@@ -305,9 +332,9 @@ function M.download_file(local_path) | |
}) | ||
end | ||
|
||
-- Sync local and remote directory | ||
-- @param dir string | ||
-- @param upload boolean | ||
---Sync local and remote directory | ||
---@param dir string | ||
---@param upload boolean | ||
function M.sync_dir(dir, upload) | ||
local remote_path, deployment = M.remote_rsync_path(dir) | ||
if remote_path == nil then | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep the format "-- comment", not "---comment"