-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config):
import
option for splitting rocks toml configs (#565)
* feat: Add support for config imports * Add tests for import feature * Use newer version of toml-edit and add more tests * Update flake.lock * Update README.md * Update lua/rocks/operations/helpers/multi_mut_rocks_toml_wrapper.lua --------- Co-authored-by: Marc Jakobi <marc@jakobi.dev> Co-authored-by: Marc Jakobi <mrcjkb89@outlook.com>
- Loading branch information
1 parent
6f3f611
commit c90f451
Showing
18 changed files
with
499 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lua/rocks/operations/helpers/multi_mut_rocks_toml_wrapper.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
local config = require("rocks.config.internal") | ||
local fs = require("rocks.fs") | ||
|
||
---@class MutRocksTomlRefWithPath | ||
---@field config MutRocksTomlRef Config metatable | ||
---@field path? string The path to the configuration | ||
|
||
---@class MultiMutRocksTomlWrapper | ||
---@field cache table<string, MultiMutRocksTomlWrapper> Cache for nested metatables | ||
---@field configs MutRocksTomlRefWithPath[] A list of rocks toml configs | ||
local MultiMutRocksTomlWrapper = {} | ||
MultiMutRocksTomlWrapper.__index = function(self, key) | ||
-- Give preference to class methods/fields | ||
if MultiMutRocksTomlWrapper[key] then | ||
return MultiMutRocksTomlWrapper[key] | ||
end | ||
-- Find the key within the config tables | ||
for _, tbl in ipairs(self.configs) do | ||
if tbl.config[key] ~= nil then | ||
if type(tbl.config[key]) == "table" then | ||
if not self.cache[key] then | ||
self.cache[key] = MultiMutRocksTomlWrapper.new(vim.iter(self.configs) | ||
:filter(function(v) | ||
return type(v.config[key]) == "table" | ||
end) | ||
:fold({}, function(acc, v) | ||
table.insert(acc, { config = v.config[key], path = v.path }) | ||
return acc | ||
end)) | ||
end | ||
return self.cache[key] | ||
else | ||
return tbl.config[key] | ||
end | ||
end | ||
end | ||
return nil | ||
end | ||
MultiMutRocksTomlWrapper.__newindex = function(self, key, value) | ||
local insert_index = 1 | ||
for i, tbl in ipairs(self.configs) do | ||
-- Insert into base config by default | ||
if tbl.path == config.config_path then | ||
insert_index = i | ||
end | ||
if tbl.config[key] ~= nil then | ||
tbl.config[key] = value | ||
return | ||
end | ||
end | ||
-- If key not found in any table, add it to the first table | ||
self.configs[insert_index].config[key] = value | ||
end | ||
|
||
--- Write to all rocks toml config files in an async context | ||
---@type async fun(self: MultiMutRocksTomlWrapper) | ||
function MultiMutRocksTomlWrapper:write() | ||
for _, tbl in ipairs(self.configs) do | ||
if tbl.path ~= nil then | ||
fs.write_file_await(tbl.path, "w", tostring(tbl.config)) | ||
end | ||
end | ||
end | ||
|
||
--- Function to create a new wrapper | ||
---@param configs MutRocksTomlRefWithPath[] A list of rocks toml configs | ||
---@return MultiMutRocksTomlWrapper | ||
function MultiMutRocksTomlWrapper.new(configs) | ||
assert(#configs > 0, "Must provide at least one rocks toml config") | ||
local self = { cache = {}, configs = configs } | ||
setmetatable(self, MultiMutRocksTomlWrapper) | ||
return self | ||
end | ||
|
||
return MultiMutRocksTomlWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.