Adopt WindSeed conventions for consistency

This commit is contained in:
luaneko
2022-12-21 00:11:43 +11:00
parent ff83edab8d
commit f4b1fa84e1
3 changed files with 97 additions and 77 deletions

View File

@ -15,37 +15,11 @@ local function try_format(...)
end end
end end
-- Writes logs to a file and prints pretty status messages. --- Writes logs to a file and prints pretty status messages.
local Logger = { local Logger = setmetatable({
-- Constructs a new `Logger`. __metatable = "Logger",
new = function(mt, path)
path = path or vim.fn.stdpath("cache") .. "/dep.log"
-- clear and open log file
local handle = vim.loop.fs_open(path, "w", 0x1b4) -- 0664
local pipe = vim.loop.new_pipe()
pipe:open(handle)
return setmetatable({
path = path,
handle = handle,
pipe = pipe,
silent = false,
-- TODO: This looks good for me ;) but it should have proper vim color mapping for other people.
stage_colors = {
skip = "Comment",
clean = "Boolean",
install = "MoreMsg",
update = "WarningMsg",
delete = "Directory",
error = "ErrorMsg",
},
}, mt)
end,
__index = { __index = {
-- Prints a message associated with a stage. --- Prints a message associated with a stage.
log = function(self, stage, message, ...) log = function(self, stage, message, ...)
-- calling function -- calling function
local source = debug.getinfo(2, "Sl").short_src local source = debug.getinfo(2, "Sl").short_src
@ -77,7 +51,7 @@ local Logger = {
end) end)
end, end,
-- Closes the log file handle. --- Closes the log file handle.
close = function(self) close = function(self)
if self.pipe then if self.pipe then
self.pipe:close() self.pipe:close()
@ -90,9 +64,36 @@ local Logger = {
end end
end, end,
}, },
} }, {
--- Constructs a new `Logger`.
__call = function(mt, path)
path = path or vim.fn.stdpath("cache") .. "/dep.log"
-- clear and open log file
local handle = vim.loop.fs_open(path, "w", 0x1b4) -- 0664
local pipe = vim.loop.new_pipe()
pipe:open(handle)
return setmetatable({
path = path,
handle = handle,
pipe = pipe,
silent = false,
-- TODO: This looks good for me ;) but it should have proper vim color mapping for other people.
stage_colors = {
skip = "Comment",
clean = "Boolean",
install = "MoreMsg",
update = "WarningMsg",
delete = "Directory",
error = "ErrorMsg",
},
}, mt)
end,
})
return { return {
Logger = Logger, Logger = Logger,
global = Logger:new(), global = Logger(),
} }

View File

@ -6,8 +6,8 @@
-- --
-- https://opensource.org/licenses/MIT -- https://opensource.org/licenses/MIT
-- --
local require, type, setmetatable, error, assert, math, os, debug = local require, type, setmetatable, error, table, assert, math, os, debug =
require, type, setmetatable, error, assert, math, os, debug require, type, setmetatable, error, table, assert, math, os, debug
local logger = require("dep.log").global local logger = require("dep.log").global
local function parse_name_from_id(id) local function parse_name_from_id(id)
@ -23,33 +23,11 @@ local function is_nonempty_str(s)
return type(s) == "string" and #s ~= 0 return type(s) == "string" and #s ~= 0
end end
-- Package information. --- Package information.
local Package = { local Package = setmetatable({
-- Constructs a new `Package` with the given identifier. __metatable = "Package",
new = function(mt, id)
local name = parse_name_from_id(id)
return setmetatable({
id = id,
name = name,
url = "https://github.com/" .. id .. ".git",
enabled = true,
exists = false,
added = false,
configured = false,
loaded = false,
dependencies = {},
dependents = {},
subtree_configured = false,
subtree_loaded = false,
on_setup = {},
on_config = {},
on_load = {},
perf = { hooks = {} },
}, mt)
end,
__index = { __index = {
-- Runs all registered hooks of the given type. --- Runs all registered hooks of the given type.
run_hooks = function(self, hook) run_hooks = function(self, hook)
local hooks = self["on_" .. hook] local hooks = self["on_" .. hook]
if not hooks or #hooks == 0 then if not hooks or #hooks == 0 then
@ -80,20 +58,36 @@ local Package = {
return true return true
end, end,
}, },
} }, {
--- Constructs a new `Package` with the given identifier.
-- Manages a set of packages. __call = function(mt, id)
local PackageStore = { local name = parse_name_from_id(id)
-- Constructs a new `PackageStore`. return setmetatable({
new = function(mt) id = id,
-- hash part of store maps package ids to packages name = name,
-- array part of store is a list of packages url = "https://github.com/" .. id .. ".git",
-- all packages in a store are unique based on their id enabled = true,
return setmetatable({}, mt) exists = false,
added = false,
configured = false,
loaded = false,
dependencies = {},
dependents = {},
subtree_configured = false,
subtree_loaded = false,
on_setup = {},
on_config = {},
on_load = {},
perf = { hooks = {} },
}, mt)
end, end,
})
--- Manages a set of packages.
local PackageStore = setmetatable({
__metatable = "PackageStore",
__index = { __index = {
-- Links the given packages such that the parent must load before the child. --- Links the given packages such that the parent must load before the child.
link_dependency = function(self, parent, child) link_dependency = function(self, parent, child)
if not parent.dependents[child.id] then if not parent.dependents[child.id] then
parent.dependents[child.id] = child parent.dependents[child.id] = child
@ -106,7 +100,7 @@ local PackageStore = {
end end
end, end,
-- Ensures the given package spec table is valid. --- Ensures the given package spec table is valid.
validate_spec = function(self, spec) validate_spec = function(self, spec)
assert(spec[1] ~= nil, "package id missing from spec") assert(spec[1] ~= nil, "package id missing from spec")
assert(type(spec[1]) == "string", "package id must be a string") assert(type(spec[1]) == "string", "package id must be a string")
@ -132,7 +126,7 @@ local PackageStore = {
assert(spec[2] == nil or type(spec[2]) == "function", "package loader must be a function") assert(spec[2] == nil or type(spec[2]) == "function", "package loader must be a function")
end, end,
-- Creates or updates a package given the following spec table, and returns that package. --- Creates or updates a package from the given spec table, and returns that package.
add_spec = function(self, spec, scope) add_spec = function(self, spec, scope)
self:validate_spec(spec) self:validate_spec(spec)
scope = scope or {} scope = scope or {}
@ -141,7 +135,7 @@ local PackageStore = {
local pkg = self[id] local pkg = self[id]
if not pkg then if not pkg then
pkg = Package:new() pkg = Package(id)
self[id], self[#self + 1] = pkg, pkg self[id], self[#self + 1] = pkg, pkg
end end
@ -169,7 +163,7 @@ local PackageStore = {
end end
end, end,
-- Adds the given list of specs. --- Adds the given list of specs.
add_specs = function(self, specs, scope) add_specs = function(self, specs, scope)
assert(type(specs) == "table", "package list must be a table") 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.pin == nil or type(specs.pin) == "boolean", "package list pin must be a boolean")
@ -203,7 +197,7 @@ local PackageStore = {
end end
end, end,
-- Ensures there are no circular dependencies in this package store. --- Ensures there are no circular dependencies in this package store.
ensure_acyclic = function(self) ensure_acyclic = function(self)
-- tarjan's strongly connected components algorithm -- tarjan's strongly connected components algorithm
local idx, indices, lowlink, stack = 0, {}, {}, {} local idx, indices, lowlink, stack = 0, {}, {}, {}
@ -263,4 +257,17 @@ local PackageStore = {
end end
end, end,
}, },
}, {
--- Constructs a new `PackageStore`.
__call = function(mt)
-- hash part of store maps package ids to packages
-- array part of store is a list of packages
-- all packages in a store are unique based on their id
return setmetatable({}, mt)
end,
})
return {
Package = Package,
PackageStore = PackageStore,
} }

12
lua/dep2.lua Normal file
View File

@ -0,0 +1,12 @@
--
-- Copyright (c) 2022 chiya.dev
--
-- Use of this source code is governed by the MIT License
-- which can be found in the LICENSE file and at:
--
-- https://opensource.org/licenses/MIT
--
local logger = require("dep.log").global
local store = require("dep.package").PackageStore()
-- placeholder for refactoring