diff options
Diffstat (limited to '')
-rw-r--r-- | LICENSE | 3 | ||||
-rw-r--r-- | README.md | 107 | ||||
-rw-r--r-- | lua/dep.lua | 137 | ||||
-rw-r--r-- | lua/dep/package.lua | 15 | ||||
-rw-r--r-- | lua/dep/proc.lua | 10 |
5 files changed, 105 insertions, 167 deletions
@@ -1,6 +1,7 @@ MIT License -Copyright (c) 2021 chiya.dev +(c) 2021 chiya.dev +(c) 2024 squibid Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -95,6 +95,10 @@ A package must be declared in the following format. -- Defaults to whatever the remote configured as their HEAD, which is usually "master". branch = "develop", + -- [string] Overrides the commit ref to target + -- Defaults to the latest commit on the current branch + commit = "e76cb03", + -- [boolean] Prevents the package from being loaded. disable = true, @@ -287,97 +291,6 @@ require "dep" { } ``` -## Separating code into modules - -Suppose you split your `init.lua` into two files `packages/search.lua` and -`packages/vcs.lua`, which declare the packages [telescope.nvim][6] and [vim-fugitive][7] respectively. - -```lua --- ~/.config/nvim/lua/packages/search.lua: -return { - { - "nvim-telescope/telescope.nvim", - requires = "nvim-lua/plenary.nvim" - } -} -``` - -```lua --- ~/.config/nvim/lua/packages/vcs.lua: -return { - "tpope/vim-fugitive" -} -``` - -Package specifications from other modules can be loaded using the `modules` option. - -```lua -require "dep" { - modules = { - prefix = "packages.", - "search", - "vcs" - } -} - --- the above is equivalent to -require "dep" { - modules = { - "packages.search", - "packages.vcs" - } -} - --- which is equivalent to -local packages = {} - -for _, package in ipairs(require "packages.search") do - table.insert(packages, package) -end - -for _, package in ipairs(require "packages.vcs") do - table.insert(packages, package) -end - -require("dep")(packages) - --- which is ultimately equivalent to -require "dep" { - { - "nvim-telescope/telescope.nvim", - requires = "nvim-lua/plenary.nvim" - }, - "tpope/vim-fugitive" -} - --- all of the above are guaranteed to load plenary.nvim before telescope.nvim. --- order of telescope.nvim and vim-fugitive is consistent but unspecified. -``` - -Entire modules can be marked as disabled, which disables all top-level packages declared in that module. - -```lua -return { - disable = true, - { - "user/package", - disabled = true, -- implied by module - requires = { - { - "user/dependency", - -- disabled = true -- not implied - } - }, - deps = { - { - "user/dependent", - disabled = true -- implied by dependency - } - } - } -} -``` - ## Miscellaneous configuration dep accepts configuration parameters as named fields in the package list. @@ -390,14 +303,10 @@ require "dep" { -- "always": synchronize all packages on startup sync = "new", - -- [array] Specifies the modules to load package specifications from. - -- Defaults to an empty table. - -- Items can be either an array of package specifications, - -- or a string that indicates the name of the module from which the array of package specifications is loaded. - modules = { - -- [string] Prefix string to prepend to all module names. - prefix = "", - }, + -- [function] Callback when dep is (re)loaded + -- if a table is returned it will be read as a table of config specs + load = function() + end -- list of package specs... } diff --git a/lua/dep.lua b/lua/dep.lua index 11802c8..1c143c8 100644 --- a/lua/dep.lua +++ b/lua/dep.lua @@ -11,7 +11,7 @@ local logger = require("dep.log").global local proc = require("dep.proc") local initialized, perf, config_path, base_dir -local packages, root +local packages, root, load local function bench(name, code, ...) local start = os.clock() @@ -78,6 +78,7 @@ local function register(spec, overrides) package.url = spec.url or package.url or ("https://github.com/" .. id .. ".git") package.branch = spec.branch or package.branch package.dir = base_dir .. package.name + package.commit = spec.commit package.pin = overrides.pin or spec.pin or package.pin package.enabled = not overrides.disable and not spec.disable and package.enabled @@ -127,27 +128,6 @@ local function register_recursive(list, overrides) error(string.format("%s (spec=%s)", err, vim.inspect(list[i]))) end end - - if list.modules then - for i = 1, #list.modules do - local name, module = "<unnamed module>", list.modules[i] - - if type(module) == "string" then - if list.modules.prefix then - module = list.modules.prefix .. module - end - - name, module = module, require(module) - end - - name = module.name or name - - local ok, err = pcall(register_recursive, module, overrides) - if not ok then - error(string.format("%s <- %s", err, name)) - end - end - end end local function sort_dependencies() @@ -417,6 +397,16 @@ local function reload() end local function reload_all() + -- recall the load function + if load and type(load) == "function" then + local ok, ret = pcall(load) + if ok and type(ret) == "table" then + register_recursive(ret) + else + logger:log("error", ret) + end + end + for i = 1, #packages do local package = packages[i] package.loaded, package.subtree_loaded = false, false @@ -504,33 +494,55 @@ local function sync(package, cb) log_err(before) cb(err) else - proc.git_fetch(package.dir, "origin", package.branch or "HEAD", function(err, message) - if err then - log_err(message) - cb(err) - else - proc.git_rev_parse(package.dir, "FETCH_HEAD", function(err, after) - if err then - log_err(after) - cb(err) - elseif before == after then - logger:log("skip", string.format("skipped %s", package.id)) - cb(err) - else - proc.git_reset(package.dir, after, function(err, message) - if err then - log_err(message) - else - mark_reconfigure(package) - logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after)) - end - + if package.commit then + proc.git_checkout(package.dir, package.branch, package.commit, function(err, message) + if err then + log_err(message) + cb(err) + else + proc.git_rev_parse(package.dir, package.commit, function(err, after) + if err then + log_err(after) cb(err) - end) - end - end) - end - end) + elseif before == after then + logger:log("skip", string.format("skipped %s", package.id)) + cb(err) + else + mark_reconfigure(package) + logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after)) + end + end) + end + end) + else + proc.git_fetch(package.dir, "origin", package.branch or "HEAD", function(err, message) + if err then + log_err(message) + cb(err) + else + proc.git_rev_parse(package.dir, "FETCH_HEAD", function(err, after) + if err then + log_err(after) + cb(err) + elseif before == after then + logger:log("skip", string.format("skipped %s", package.id)) + cb(err) + else + proc.git_reset(package.dir, after, function(err, message) + if err then + log_err(message) + else + mark_reconfigure(package) + logger:log("update", string.format("updated %s; %s -> %s", package.id, before, after)) + end + + cb(err) + end) + end + end) + end + end) + end end end) else @@ -538,9 +550,21 @@ local function sync(package, cb) if err then logger:log("error", string.format("failed to install %s; reason: %s", package.id, message)) else - package.exists = true - mark_reconfigure(package) - logger:log("install", string.format("installed %s", package.id)) + if package.commit then + proc.git_checkout(package.dir, package.branch, package.commit, function(err, message) + if err then + logger:log("error", string.format("failed to checkout %s; reason: %s", package.id, message)) + else + package.exists = true + mark_reconfigure(package) + logger:log("install", string.format("installed %s", package.id)) + end + end) + else + package.exists = true + mark_reconfigure(package) + logger:log("install", string.format("installed %s", package.id)) + end end cb(err) @@ -850,7 +874,16 @@ return setmetatable({ packages = {} bench("load", function() - root = register("chiyadev/dep") + root = register("squibid/dep") + if config["load"] and type(config["load"]) == "function" then + local ok, ret = pcall(config["load"]) + if ok and type(ret) == "table" then + load = config["load"] + register_recursive(ret) + else + logger:log("error", ret) + end + end register_recursive(config) sort_dependencies() ensure_acyclic() diff --git a/lua/dep/package.lua b/lua/dep/package.lua index 513df8d..e76238d 100644 --- a/lua/dep/package.lua +++ b/lua/dep/package.lua @@ -168,7 +168,6 @@ local PackageStore = setmetatable({ assert(type(specs) == "table", "package list must be a table") assert(specs.pin == nil or type(specs.pin) == "boolean", "package list pin must be a boolean") assert(specs.disable == nil or type(specs.disable) == "boolean", "package list disable must be a boolean") - assert(specs.modules == nil or type(specs.modules) == "table", "package list module list must be a table") scope = scope or {} scope = { @@ -181,20 +180,6 @@ local PackageStore = setmetatable({ for i = 1, #specs do self:add_spec(specs[i], scope) end - - -- recursively add referenced spec list modules - if specs.modules then - local prefix = specs.modules.prefix or "" - for i = 1, #specs.modules do - local name = specs.modules[i] - assert(type(name) == "string", "package list inner module name must be a string") - name = prefix .. name - - local module = require(name) - assert(type(module) == "table", "package list inner module did not return a spec list table") - self:add_specs(module, scope) - end - end end, --- Ensures there are no circular dependencies in this package store. diff --git a/lua/dep/proc.lua b/lua/dep/proc.lua index 4b95baa..cf3aa9c 100644 --- a/lua/dep/proc.lua +++ b/lua/dep/proc.lua @@ -61,4 +61,14 @@ function proc.git_reset(dir, treeish, cb) proc.exec("git", args, dir, git_env, cb) end +function proc.git_checkout(dir, branch, commit, cb) + local args = { "fetch", "--depth=2147483647", "origin", branch } + proc.exec("git", args, dir, git_env, function(err, message) + cb(err, message) + + args = { "checkout", commit } + proc.exec("git", args, dir, git_env, cb) + end) +end + return proc |