Skip to content

Commit

Permalink
Generally improved path handling stability and results using AVPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Avril112113 committed Apr 26, 2024
1 parent b98e179 commit c2370a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 2 additions & 0 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ package.path = ("{TP}/?.lua;{TP}/?/init.lua;{SSP}/libs/?.lua;{SSP}/libs/?/init.l
package.cpath = ("{TP}/?.{EXT};{SSP}/libs/?.{EXT};"):gsub("{TP}", TOOL_PATH):gsub("{SSP}", SELENSCRIPT_PATH):gsub("{EXT}", binary_ext)

local CLI = require "tool.cli"
local AVPath = require "avpath"
AVPath.SEPERATOR = "/"

os.exit(CLI.process(args) or 0)
7 changes: 4 additions & 3 deletions tool/multi_project.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Utils = require "SelenScript.utils"
local AVPath = require "avpath"

local Config = require "tool.config"
local Project = require "tool.project"
Expand All @@ -16,8 +17,8 @@ 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.config_path = AVPath.norm(config_path)
self.project_path = AVPath.base(config_path)

self.projects = {}
self.config = Config.new()
Expand Down Expand Up @@ -73,7 +74,7 @@ function MultiPorject:build()
local results = {}
for _, project in ipairs(self.projects) do
print()
print_info(("Buidling '%s'"):format(project.config.name))
print_info(("Building '%s'"):format(project.config.name or AVPath.relative(project.config_path, self.project_path)))
local result = {project:build()}
table.insert(results, {project, result})
end
Expand Down
14 changes: 8 additions & 6 deletions tool/project.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local lfs = require "lfs"
local AVPath = require "avpath"

local Parser = require "SelenScript.parser.parser"
local Transformer = require "SelenScript.transformer.transformer"
Expand Down Expand Up @@ -105,8 +106,8 @@ end
function Project:findSrcFile(path)
local searched = {}
local function check(base)
local src_path = (base .. "/" .. path):gsub("\\", "/"):gsub("^%./", ""):gsub("/$", "")
local full_path = self.multiproject.project_path .. "/" .. src_path
local src_path = AVPath.join{base, path}
local full_path = AVPath.join{self.multiproject.project_path, src_path}
table.insert(searched, ("no file '%s'"):format(full_path))
if path_is(full_path, "file") then
return full_path, src_path
Expand All @@ -131,20 +132,21 @@ function Project:findModFile(modpath, path)
path = path or "?.lua;?/init.lua;"
local path_parts = {}
local srcs = type(self.config.src) == "string" and {self.config.src} or self.config.src
---@cast srcs string[]
---@diagnostic disable-next-line: param-type-mismatch
for _, src in ipairs(srcs) do
if #src > 0 then
local new_repl = path:gsub(
"%?",
((self.multiproject.project_path .. "/" .. src .. "/?"):gsub("\\", "/"):gsub("^%./", ""):gsub("%/./", "/"):gsub("/$", ""))
AVPath.join{self.multiproject.project_path, src, "?"}
)
table.insert(path_parts, new_repl)
end
end
local full_path, err = package.searchpath(modpath, table.concat(path_parts, ";"))
if full_path then
full_path = full_path:gsub("\\", "/"):gsub("^%./", ""):gsub("/%./", "/"):gsub("/$", "")
local src_path = full_path:sub(#self.multiproject.project_path+2, -1)
full_path = AVPath.norm(full_path)
local src_path = AVPath.relative(full_path, self.multiproject.project_path)
return full_path, src_path, nil
end
return nil, nil, err
Expand Down Expand Up @@ -231,7 +233,7 @@ function Project:build()
if path:match("^<SSSWTOOL>/") then
return path
end
return ("../%s"):format(path:sub(#self.multiproject.project_path+2, -1))
return ("../%s"):format(AVPath.relative(path, self.multiproject.project_path))
end
})

Expand Down
10 changes: 6 additions & 4 deletions tool/transform_combiner.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
local modpath = ...
---@diagnostic disable-next-line: param-type-mismatch
local modfolderpath = package.searchpath(modpath, package.path):gsub("[\\/][^\\/]*$", "")
local REQUIRE_SRC_FILE = modfolderpath .. "/src/require.lua"

local AVPath = require "avpath"

local Utils = require "SelenScript.utils"
local ASTHelpers = require "SelenScript.transformer.ast_helpers"
local ASTNodes = ASTHelpers.Nodes
local AST = require "SelenScript.parser.ast" -- Used for debugging.

---@diagnostic disable-next-line: param-type-mismatch
local REQUIRE_SRC_FILE = AVPath.join{package.searchpath(modpath, package.path), "../src/require.lua"}


---@class SSSWTool.Transformer_Combiner : SSSWTool.Transformer
---@field required_files table<string,ASTNodeSource|false>
Expand Down Expand Up @@ -96,7 +98,7 @@ function TransformerDefs:index(node)
print_error(("Failed to find '%s'%s"):format(modpath, err))
return ASTNodes.LongComment(node, nil, ("Failed to find '%s'"):format(modpath))
else
filepath = filepath:gsub("\\", "/")
filepath = AVPath.norm(filepath)
if self.required_files[filepath] == nil then
print_info(("Parsing '%s'"):format(filepath_local))
local ast, errors, comments = self.parser:parse(Utils.readFile(filepath), filepath)
Expand Down
11 changes: 6 additions & 5 deletions tool/transform_swaddon_tracing.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
local modpath = ...
---@diagnostic disable-next-line: param-type-mismatch
local modfolderpath = package.searchpath(modpath, package.path):gsub("[\\/][^\\/]*$", "")
local TRACING_PREFIX_SRC_FILE = modfolderpath .. "/src/tracing.lua"

local AVPath = require "avpath"

local Utils = require "SelenScript.utils"
local ASTHelpers = require "SelenScript.transformer.ast_helpers"
Expand All @@ -11,6 +9,9 @@ local ASTNodes = ASTHelpers.Nodes
local Emitter = require "SelenScript.emitter.emitter"
local AST = require "SelenScript.parser.ast"

---@diagnostic disable-next-line: param-type-mismatch
local TRACING_PREFIX_SRC_FILE = AVPath.join{package.searchpath(modpath, package.path), "../src/tracing.lua"}


--- Used for converting AST nodes into strings
local emitter = Emitter.new("lua", {})
Expand Down Expand Up @@ -186,9 +187,9 @@ function TransformerDefs:funcbody(node)
local local_file_path = "<UNKNOWN>"
if local_source_node.file then
if local_source_node.file:find("^<SSSWTOOL>[\\/]") then
local_file_path = local_source_node.file:gsub("\\", "/")
local_file_path = AVPath.norm(local_source_node.file)
else
local_file_path = local_source_node.file:sub(#self.multiproject.project_path+2):gsub("\\", "/")
local_file_path = AVPath.relative(local_source_node.file, self.multiproject.project_path)
end
end
self:_add_trace_info(node, name, start_line, start_column, local_file_path)
Expand Down

0 comments on commit c2370a3

Please sign in to comment.