Configure synchronization of files and directories through a plugin.
| 1 | local M = { |
| 2 | opts = require("syncmap.default"), |
| 3 | } |
| 4 | |
| 5 | ---Correctly extracts reverse_sync_on_spawn |
| 6 | ---@param row SyncmapWatchItem |
| 7 | |
| 8 | if row.reverse_sync_on_spawn == nil then |
| 9 | if M.opts.reverse_sync_on_startup == nil then |
| 10 | return true |
| 11 | else |
| 12 | return M.opts.reverse_sync_on_startup |
| 13 | end |
| 14 | else |
| 15 | return row.reverse_sync_on_spawn |
| 16 | end |
| 17 | end |
| 18 | |
| 19 | ---Checks if a file exists |
| 20 | ---@param path string |
| 21 | local |
| 22 | local f = io.open(path, "r") |
| 23 | if f then |
| 24 | f:close() |
| 25 | return true |
| 26 | end |
| 27 | return false |
| 28 | end |
| 29 | |
| 30 | ---Checks if a path is a directory |
| 31 | ---@param path string |
| 32 | local |
| 33 | local stat = vim.uv.fs_stat(path) |
| 34 | return stat and stat.type == "directory" |
| 35 | end |
| 36 | |
| 37 | --- Ensures trailing slash |
| 38 | ---@param path string |
| 39 | local |
| 40 | if path:sub(-1) ~= "/" then |
| 41 | return path .. "/" |
| 42 | end |
| 43 | return path |
| 44 | end |
| 45 | |
| 46 | ---@param s string |
| 47 | |
| 48 | return "--exclude-from='" .. s .. "'" |
| 49 | end |
| 50 | |
| 51 | ---Gets the exclude from using parent pattern |
| 52 | ---@param m SyncmapWatchItem |
| 53 | |
| 54 | if m.exclude_from == nil then |
| 55 | if M.opts.exclude_from ~= nil then |
| 56 | m.exclude_from = M.opts.exclude_from |
| 57 | return M.string_to_exclude_from(M.opts.exclude_from) |
| 58 | end |
| 59 | return nil |
| 60 | else |
| 61 | return M.string_to_exclude_from(m.exclude_from) |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | ---Makes sure to extract flags correctly |
| 66 | ---@param m SyncmapWatchItem |
| 67 | ---@return RsyncFlag[] |
| 68 | |
| 69 | local flags = m.rsync |
| 70 | if flags == nil then |
| 71 | flags = M.opts.rsync |
| 72 | end |
| 73 | local exclude_from = M.extract_exclude_from(m) |
| 74 | if m.exclude_from == nil then |
| 75 | return flags |
| 76 | end |
| 77 | local src = vim.fn.expand(m[1]) |
| 78 | local stat = vim.uv.fs_stat(src) |
| 79 | if stat and stat.type == "directory" then |
| 80 | if file_exists(with_trailing_slash(src) .. m.exclude_from) then |
| 81 | -- table.insert(flags, exclude_from) |
| 82 | end |
| 83 | end |
| 84 | return flags |
| 85 | end |
| 86 | |
| 87 | ---@param m SyncmapWatchItem |
| 88 | |
| 89 | local flags = M.extract_flags(m) |
| 90 | ---@type RsyncParams |
| 91 | return { |
| 92 | src = vim.fn.expand(m[1]), |
| 93 | dst = vim.fn.expand(m[2]), |
| 94 | flags = flags, |
| 95 | reverse_sync_on_spawn = M.extract_reverse(m), |
| 96 | } |
| 97 | end |
| 98 | |
| 99 | ---@param str string |
| 100 | ---@param sep string |
| 101 | ---@return string[] |
| 102 | |
| 103 | local result = {} |
| 104 | for part in string.gmatch(str, "([^" .. sep .. "]+)") do |
| 105 | table.insert(result, part) |
| 106 | end |
| 107 | return result |
| 108 | end |
| 109 | |
| 110 | return M |