irongit

Configure synchronization of files and directories through a plugin.

110 lines2.3 KBLua
1local M = {
2 opts = require("syncmap.default"),
3}
4
5---Correctly extracts reverse_sync_on_spawn
6---@param row SyncmapWatchItem
7function M.extract_reverse(row)
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
17end
18
19---Checks if a file exists
20---@param path string
21local function file_exists(path)
22 local f = io.open(path, "r")
23 if f then
24 f:close()
25 return true
26 end
27 return false
28end
29
30---Checks if a path is a directory
31---@param path string
32local function is_dir(path)
33 local stat = vim.uv.fs_stat(path)
34 return stat and stat.type == "directory"
35end
36
37--- Ensures trailing slash
38---@param path string
39local function with_trailing_slash(path)
40 if path:sub(-1) ~= "/" then
41 return path .. "/"
42 end
43 return path
44end
45
46---@param s string
47function M.string_to_exclude_from(s)
48 return "--exclude-from='" .. s .. "'"
49end
50
51---Gets the exclude from using parent pattern
52---@param m SyncmapWatchItem
53function M.extract_exclude_from(m)
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
63end
64
65---Makes sure to extract flags correctly
66---@param m SyncmapWatchItem
67---@return RsyncFlag[]
68function M.extract_flags(m)
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
85end
86
87---@param m SyncmapWatchItem
88function M.row_to_rsync_params(m)
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 }
97end
98
99---@param str string
100---@param sep string
101---@return string[]
102function M.split(str, sep)
103 local result = {}
104 for part in string.gmatch(str, "([^" .. sep .. "]+)") do
105 table.insert(result, part)
106 end
107 return result
108end
109
110return M