Skip to content

Commit

Permalink
fix: support local files as plugin spec
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 29, 2022
1 parent 32fa5f8 commit 0233460
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
35 changes: 30 additions & 5 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ M.dirty = false
---@field plugins table<string, LazyPlugin>
---@field funs? table<string, string[]>
local Spec = {}
M.Spec = Spec

---@param spec? LazySpec
function Spec.new(spec)
local self = setmetatable({}, { __index = Spec })
self.plugins = {}
self.modname = nil
self.modpath = nil
if spec then
self:normalize(spec)
end
return self
end

---@param modname string
---@param modpath string
Expand All @@ -70,17 +83,29 @@ end

---@param plugin LazyPlugin
function Spec:add(plugin)
if type(plugin[1]) ~= "string" then
local pkg = plugin[1]
if type(pkg) ~= "string" then
Util.error("Invalid plugin spec " .. vim.inspect(plugin))
end
plugin.uri = plugin.uri or ("https://github.com/" .. plugin[1] .. ".git")
plugin._ = {}

if not plugin.uri then
local c = pkg:sub(1, 1)
if c == "~" then
plugin.uri = vim.loop.os_getenv("HOME") .. pkg:sub(2)
elseif c == "/" then
plugin.uri = pkg
elseif pkg:sub(1, 4) == "http" or pkg:sub(1, 3) == "ssh" then
plugin.uri = pkg
else
plugin.uri = ("https://github.com/" .. pkg .. ".git")
end
end

-- PERF: optimized code to get package name without using lua patterns
if not plugin.name then
local name = plugin[1]:sub(-4) == ".git" and plugin[1]:sub(1, -5) or plugin[1]
local name = pkg:sub(-4) == ".git" and pkg:sub(1, -5) or pkg
local slash = name:reverse():find("/", 1, true) --[[@as number?]]
plugin.name = slash and name:sub(#name - slash + 2) or plugin[1]:gsub("%W+", "_")
plugin.name = slash and name:sub(#name - slash + 2) or pkg:gsub("%W+", "_")
end

M.process_local(plugin)
Expand Down
29 changes: 29 additions & 0 deletions tests/core/plugin_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local Config = require("lazy.core.config")
local Plugin = require("lazy.core.plugin")

local assert = require("luassert")

Config.setup()

describe("plugin spec", function()
local tests = {
{ { "~/foo" }, { [1] = "~/foo", name = "foo", uri = vim.fn.fnamemodify("~/foo", ":p") } },
{ { "/tmp/foo" }, { [1] = "/tmp/foo", name = "foo", uri = "/tmp/foo" } },
{ { "foo/bar" }, { [1] = "foo/bar", name = "bar", uri = "https://github.com/foo/bar.git" } },
{ { "foo/bar", name = "foobar" }, { [1] = "foo/bar", name = "foobar", uri = "https://github.com/foo/bar.git" } },
{ { "foo/bar", uri = "123" }, { [1] = "foo/bar", name = "bar", uri = "123" } },
{ { "https://foobar" }, { [1] = "https://foobar", name = "foobar", uri = "https://foobar" } },
{ { "ssh://foobar" }, { [1] = "ssh://foobar", name = "foobar", uri = "ssh://foobar" } },
{ "foo/bar", { [1] = "foo/bar", name = "bar", uri = "https://github.com/foo/bar.git" } },
{ { { { "foo/bar" } } }, { [1] = "foo/bar", name = "bar", uri = "https://github.com/foo/bar.git" } },
}

for _, test in ipairs(tests) do
it("parses uri " .. vim.inspect(test[1]):gsub("%s+", " "), function()
local spec = Plugin.Spec.new(test[1])
local plugins = vim.tbl_values(spec.plugins)
assert.equal(1, #plugins)
assert.same(test[2], plugins[1])
end)
end
end)

0 comments on commit 0233460

Please sign in to comment.