Skip to content

Commit

Permalink
Added config for single or multiple projects under 1 or many config f…
Browse files Browse the repository at this point in the history
…iles
  • Loading branch information
Avril112113 committed Apr 24, 2024
1 parent ad70ae5 commit b98e179
Show file tree
Hide file tree
Showing 9 changed files with 481 additions and 148 deletions.
37 changes: 37 additions & 0 deletions tool/BuildActions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
local BuildActions = {}


--- Called before any of the build process starts.
---@param config SSSWTool.Config
function BuildActions.pre_build(config)
end

--- Called before a file is parsed.
---@param config SSSWTool.Config
---@param path string
function BuildActions.pre_parse(config, path)
end

--- Called after a file is parsed but before it's transformed.
---@param config SSSWTool.Config
---@param path string
---@param ast ASTNodeSource
function BuildActions.post_parse(config, path, ast)
end

--- Called after a file is transformed.
---@param config SSSWTool.Config
---@param path string
---@param ast ASTNodeSource
function BuildActions.post_transform(config, path, ast)
end

--- Called after the entire build process finishes.
--- This is the very last build action to be called.
--- This is still called before `script.lua` is copied to it's output directory.
---@param config SSSWTool.Config
function BuildActions.post_build(config)
end


return BuildActions
135 changes: 0 additions & 135 deletions tool/build.lua

This file was deleted.

12 changes: 10 additions & 2 deletions tool/cli.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "tool.cli_print"
local Build = require "tool.build"

local MultiProject = require "tool.multi_project"


local CLI = {}
Expand Down Expand Up @@ -59,7 +60,14 @@ CLI.actions = {
handler = function(args, pos)
local addon_dir = args[pos] or "./"
pos = pos + 1
Build.build(addon_dir)
local multi_project, err = MultiProject.new(addon_dir .. "/ssswtool.json")
if not multi_project or err then
print_error(err or "FAIL project ~= nil")
return -1
end
multi_project:build()
-- TODO: Check for any projects that failed to build and return -1 if so.
return 0
end,
},
}
Expand Down
3 changes: 2 additions & 1 deletion tool/cli_print.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ local function _print(mode, ...)
for i=1,select("#", ...) do
values[i] = tostring(values[i])
end
print(("%s[%s%s%s]:%s%s %s"):format(Colors.fix, MODE_COLORS[mode], mode, Colors.fix, Colors.reset, string.rep(" ", 5-#mode), table.concat(values, "\t")))
local s = table.concat(values, "\t"):gsub("\n", "%0" .. string.rep(" ", 9))
print(("%s[%s%s%s]:%s%s %s"):format(Colors.fix, MODE_COLORS[mode], mode, Colors.fix, Colors.reset, string.rep(" ", 5-#mode), s))
end

---@param ... any
Expand Down
63 changes: 63 additions & 0 deletions tool/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local json = require "json"

local Utils = require "SelenScript.utils"


---@class SSSWTool.Config
---@field _validate fun(config:SSSWTool.Config, data:table)?
local Config = {}
Config.__index = Config


---@param default table?
---@param validate fun(config:SSSWTool.Config, data:table)?
function Config.new(default, validate)
local self = setmetatable({}, Config)
self._validate = validate
self.data = default and Utils.shallowcopy(default) or {}
if validate then
local ok, err = pcall(validate, self, self.data)
if not ok then
error(("Failed to validate config '%s'\n%s"):format("<DEFAULT>", err:gsub(".-:.-: ", "")))
end
end
return self
end

--- Siliently ignores file not found errors and does not reset config.
---@param path string
function Config:read(path)
local f, msg, code = io.open(path, "r")
if not f then
if code == 2 then
return true
end
return false, msg, code
end
local src = f:read("*a")
local ok, data = pcall(json.decode, src)
if not ok then
return false, ("Failed to parse config '%s'\n%s"):format(path, data:gsub(".-:.-: ", ""))
end
if self._validate then
local ok, err = pcall(self._validate, self, data)
if not ok then
return false, ("Failed to validate config '%s'\n%s"):format(path, err:gsub(".-:.-: ", ""))
end
end
self.data = data
return true
end

-- ---@param path string
-- function Config:write(path)
-- local f, msg, code = io.open(path, "w")
-- if not f then
-- return false, msg, code
-- end
-- error("TODO")
-- return true
-- end


return Config
84 changes: 84 additions & 0 deletions tool/multi_project.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
local Utils = require "SelenScript.utils"

local Config = require "tool.config"
local Project = require "tool.project"


---@class SSSWTool.MultiProject
---@field config_path string # Normalized path to the project config file.
---@field project_path string # Normalized path to the project.
---@field projects (SSSWTool.Project|SSSWTool.MultiProject)[]
---@field config SSSWTool.Config
local MultiPorject = {}
MultiPorject.__index = MultiPorject


---@param config_path string
function MultiPorject.new(config_path)
local self = setmetatable({}, MultiPorject)
self.config_path = config_path:gsub("\\", "/"):gsub("^%./", ""):gsub("/$", "")
self.project_path = self.config_path:match("^(.*)/") or error("Failed to get parent dir from config path.")

self.projects = {}
self.config = Config.new()
local ok, err, code = self.config:read(config_path)
if not ok then
return nil, err, code
end
if type(self.config.data) == "table" and type(self.config.data[1]) == "table" then
print_info(("Multi Project config '%s'"):format(config_path))
for i, v in ipairs(self.config.data) do
if type(v) == "string" then
self:createMultiProject(self.project_path .. "/" .. v, ("'%s' index %s"):format(config_path, i))
else
Utils.merge(Project.getDefaultConfig(self, false), v, false)
self:createProject(v, ("'%s' index %s"):format(config_path, i))
end
end
else
Utils.merge(Project.getDefaultConfig(self, true), self.config.data, false)
self:createProject(self.config.data, config_path)
end
return self
end

---@param project_config SSSWTool.Project.Config
---@param project_path string
function MultiPorject:createProject(project_config, project_path)
local project, err = Project.new(self, project_config)
if not project or err then
if err then
print_error(("%s: %s"):format(project_config.name or project_path, err:gsub(".-:.-: ", "")))
return nil
else
error("ERROR project == nil")
end
else
print_info(("Loaded config for %s"):format(project_config.name or project_path))
end
table.insert(self.projects, project)
return project
end

---@param config_path string
---@param project_path string
function MultiPorject:createMultiProject(config_path, project_path)
local multiproject = MultiPorject.new(config_path)
table.insert(self.projects, multiproject)
return multiproject
end

---@return {[1]:SSSWTool.Project|SSSWTool.MultiProject,[2]:any[]}[]
function MultiPorject:build()
local results = {}
for _, project in ipairs(self.projects) do
print()
print_info(("Buidling '%s'"):format(project.config.name))
local result = {project:build()}
table.insert(results, {project, result})
end
return results
end


return MultiPorject
Loading

0 comments on commit b98e179

Please sign in to comment.