From 7097548bf40eefff95986ec32f01872ed341aeb2 Mon Sep 17 00:00:00 2001 From: Adam Hellberg Date: Sun, 27 Aug 2023 12:23:54 +0200 Subject: [PATCH] [utils]: feat add windows support fixes #25 --- fnl/tangerine/utils/README.md | 14 ++++---- fnl/tangerine/utils/fs.fnl | 4 ++- fnl/tangerine/utils/path.fnl | 22 ++++++++++--- lua/tangerine/utils/fs.lua | 13 +++++--- lua/tangerine/utils/path.lua | 60 +++++++++++++++++++++++------------ 5 files changed, 78 insertions(+), 35 deletions(-) diff --git a/fnl/tangerine/utils/README.md b/fnl/tangerine/utils/README.md index 40d4444..383b2ab 100644 --- a/fnl/tangerine/utils/README.md +++ b/fnl/tangerine/utils/README.md @@ -52,12 +52,14 @@ utils[env] ```fennel :path { :goto-output (function 1) - :resolve (function 2) - :shortname (function 3) - :source (function 4) - :target (function 5) - :transform-path (function 6) - :wildcard (function 7) + :gsub (function 2) + :match (function 3) + :resolve (function 4) + :shortname (function 5) + :source (function 6) + :target (function 7) + :transform-path (function 8) + :wildcard (function 9) } ``` diff --git a/fnl/tangerine/utils/fs.fnl b/fnl/tangerine/utils/fs.fnl index edcc355..217e1d0 100644 --- a/fnl/tangerine/utils/fs.fnl +++ b/fnl/tangerine/utils/fs.fnl @@ -16,7 +16,9 @@ (lambda fs.write [path content] "writes string of 'content' to 'path'." - (local dir (path:match "(.*/)")) + (local dir (if (= _G.jit.os "Windows") + (path:match "(.*[/\\])") + (path:match "(.*/)"))) (if (= 0 (vim.fn.isdirectory dir)) (vim.fn.mkdir dir :p)) (with-open [file (assert (io.open path :w))] diff --git a/fnl/tangerine/utils/path.fnl b/fnl/tangerine/utils/path.fnl index fce2180..1e30eeb 100644 --- a/fnl/tangerine/utils/path.fnl +++ b/fnl/tangerine/utils/path.fnl @@ -9,11 +9,25 @@ ;; -------------------- ;; ;; Utils ;; ;; -------------------- ;; +(local windows? (= _G.jit.os "Windows")) + +(lambda p.match [path pattern] + "matches 'path' against 'pattern' with support for windows." + (if windows? + (path:match (pattern:gsub "/" "[\\/]")) + (path:match pattern))) + +(lambda p.gsub [path pattern repl] + "substitutes 'pattern' in 'path' for 'repl' with support for windows." + (if windows? + (path:gsub (pattern:gsub "/" "[\\/]") repl) + (path:gsub pattern repl))) + (lambda p.shortname [path] "shortens absolute 'path' for better readability." - (or (path:match ".+/fnl/(.+)") - (path:match ".+/lua/(.+)") - (path:match ".+/(.+/.+)"))) + (or (p.match path ".+/fnl/(.+)") + (p.match path ".+/lua/(.+)") + (p.match path ".+/(.+/.+)"))) (lambda p.resolve [path] "resolves 'path' to POSIX complaint path." @@ -36,7 +50,7 @@ path (path:gsub (.. "%." ext1 "$") (.. "." ext2))] (if (path:find from) (path:gsub from to) - (path:gsub (.. "/" ext1 "/") (.. "/" ext2 "/"))))) + (p.gsub path (.. "/" ext1 "/") (.. "/" ext2 "/"))))) (lambda p.target [path] "converts fnl:'path' to valid target path." diff --git a/lua/tangerine/utils/fs.lua b/lua/tangerine/utils/fs.lua index ee9686d..e04d11b 100644 --- a/lua/tangerine/utils/fs.lua +++ b/lua/tangerine/utils/fs.lua @@ -22,7 +22,12 @@ end fs.write = function(path, content) _G.assert((nil ~= content), "Missing argument content on fnl/tangerine/utils/fs.fnl:17") _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/fs.fnl:17") - local dir = path:match("(.*/)") + local dir + if (_G.jit.os == "Windows") then + dir = path:match("(.*[/\\])") + else + dir = path:match("(.*/)") + end if (0 == vim.fn.isdirectory(dir)) then vim.fn.mkdir(dir, "p") else @@ -36,13 +41,13 @@ fs.write = function(path, content) return error(..., 0) end end - local function _5_() + local function _6_() return file:write(content) end - return close_handlers_8_auto(_G.xpcall(_5_, (package.loaded.fennel or debug).traceback)) + return close_handlers_8_auto(_G.xpcall(_6_, (package.loaded.fennel or debug).traceback)) end fs.remove = function(path, ...) - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/fs.fnl:25") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/fs.fnl:27") for _, path0 in ipairs({path, ...}) do for _0, file in ipairs(vim.fn.glob((path0 .. "/*"), 0, 1)) do os.remove(file) diff --git a/lua/tangerine/utils/path.lua b/lua/tangerine/utils/path.lua index c0a0707..bd0c9f3 100644 --- a/lua/tangerine/utils/path.lua +++ b/lua/tangerine/utils/path.lua @@ -1,41 +1,61 @@ local env = require("tangerine.utils.env") local p = {} +local windows_3f = (_G.jit.os == "Windows") +p.match = function(path, pattern) + _G.assert((nil ~= pattern), "Missing argument pattern on fnl/tangerine/utils/path.fnl:14") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:14") + if windows_3f then + return path:match(pattern:gsub("/", "[\\/]")) + else + return path:match(pattern) + end +end +p.gsub = function(path, pattern, repl) + _G.assert((nil ~= repl), "Missing argument repl on fnl/tangerine/utils/path.fnl:20") + _G.assert((nil ~= pattern), "Missing argument pattern on fnl/tangerine/utils/path.fnl:20") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:20") + if windows_3f then + return path:gsub(pattern:gsub("/", "[\\/]"), repl) + else + return path:gsub(pattern, repl) + end +end p.shortname = function(path) - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:12") - return (path:match(".+/fnl/(.+)") or path:match(".+/lua/(.+)") or path:match(".+/(.+/.+)")) + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:26") + return (p.match(path, ".+/fnl/(.+)") or p.match(path, ".+/lua/(.+)") or p.match(path, ".+/(.+/.+)")) end p.resolve = function(path) - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:18") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:32") return vim.fn.resolve(vim.fn.expand(path)) end local vimrc_out = (env.get("target") .. "tangerine_vimrc.lua") local function esc_regex(str) - _G.assert((nil ~= str), "Missing argument str on fnl/tangerine/utils/path.fnl:28") + _G.assert((nil ~= str), "Missing argument str on fnl/tangerine/utils/path.fnl:42") return str:gsub("[%%%^%$%(%)%[%]%{%}%.%*%+%-%?]", "%%%1") end -p["transform-path"] = function(path, _1_, _3_) - local _arg_2_ = _1_ - local key1 = _arg_2_[1] - local ext1 = _arg_2_[2] +p["transform-path"] = function(path, _3_, _5_) local _arg_4_ = _3_ - local key2 = _arg_4_[1] - local ext2 = _arg_4_[2] - _G.assert((nil ~= ext2), "Missing argument ext2 on fnl/tangerine/utils/path.fnl:32") - _G.assert((nil ~= key2), "Missing argument key2 on fnl/tangerine/utils/path.fnl:32") - _G.assert((nil ~= ext1), "Missing argument ext1 on fnl/tangerine/utils/path.fnl:32") - _G.assert((nil ~= key1), "Missing argument key1 on fnl/tangerine/utils/path.fnl:32") - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:32") + local key1 = _arg_4_[1] + local ext1 = _arg_4_[2] + local _arg_6_ = _5_ + local key2 = _arg_6_[1] + local ext2 = _arg_6_[2] + _G.assert((nil ~= ext2), "Missing argument ext2 on fnl/tangerine/utils/path.fnl:46") + _G.assert((nil ~= key2), "Missing argument key2 on fnl/tangerine/utils/path.fnl:46") + _G.assert((nil ~= ext1), "Missing argument ext1 on fnl/tangerine/utils/path.fnl:46") + _G.assert((nil ~= key1), "Missing argument key1 on fnl/tangerine/utils/path.fnl:46") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:46") local from = ("^" .. esc_regex(env.get(key1))) local to = esc_regex(env.get(key2)) local path0 = path:gsub(("%." .. ext1 .. "$"), ("." .. ext2)) if path0:find(from) then return path0:gsub(from, to) else - return path0:gsub(("/" .. ext1 .. "/"), ("/" .. ext2 .. "/")) + return p.gsub(path0, ("/" .. ext1 .. "/"), ("/" .. ext2 .. "/")) end end p.target = function(path) - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:41") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:55") local vimrc = env.get("vimrc") if (path == vimrc) then return vimrc_out @@ -44,7 +64,7 @@ p.target = function(path) end end p.source = function(path) - _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:48") + _G.assert((nil ~= path), "Missing argument path on fnl/tangerine/utils/path.fnl:62") local vimrc = env.get("vimrc") if (path == vimrc_out) then return vimrc @@ -64,8 +84,8 @@ p["goto-output"] = function() end end p.wildcard = function(dir, pat) - _G.assert((nil ~= pat), "Missing argument pat on fnl/tangerine/utils/path.fnl:73") - _G.assert((nil ~= dir), "Missing argument dir on fnl/tangerine/utils/path.fnl:73") + _G.assert((nil ~= pat), "Missing argument pat on fnl/tangerine/utils/path.fnl:87") + _G.assert((nil ~= dir), "Missing argument dir on fnl/tangerine/utils/path.fnl:87") return vim.fn.glob((dir .. pat), 0, 1) end return p