From c2370a3b10c99c90b3e6637b9e55214f230c3860 Mon Sep 17 00:00:00 2001 From: Avril112113 Date: Fri, 26 Apr 2024 02:05:28 +0100 Subject: [PATCH] Generally improved path handling stability and results using AVPath --- main.lua | 2 ++ tool/multi_project.lua | 7 ++++--- tool/project.lua | 14 ++++++++------ tool/transform_combiner.lua | 10 ++++++---- tool/transform_swaddon_tracing.lua | 11 ++++++----- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/main.lua b/main.lua index 0e39df1..7c1c6b4 100644 --- a/main.lua +++ b/main.lua @@ -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) diff --git a/tool/multi_project.lua b/tool/multi_project.lua index 099d56b..c112eea 100644 --- a/tool/multi_project.lua +++ b/tool/multi_project.lua @@ -1,4 +1,5 @@ local Utils = require "SelenScript.utils" +local AVPath = require "avpath" local Config = require "tool.config" local Project = require "tool.project" @@ -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() @@ -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 diff --git a/tool/project.lua b/tool/project.lua index d85bd1e..7474ebd 100644 --- a/tool/project.lua +++ b/tool/project.lua @@ -1,4 +1,5 @@ local lfs = require "lfs" +local AVPath = require "avpath" local Parser = require "SelenScript.parser.parser" local Transformer = require "SelenScript.transformer.transformer" @@ -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 @@ -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 @@ -231,7 +233,7 @@ function Project:build() if path:match("^/") 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 }) diff --git a/tool/transform_combiner.lua b/tool/transform_combiner.lua index f539463..04db005 100644 --- a/tool/transform_combiner.lua +++ b/tool/transform_combiner.lua @@ -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 @@ -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) diff --git a/tool/transform_swaddon_tracing.lua b/tool/transform_swaddon_tracing.lua index f5ca3fe..f4f8277 100644 --- a/tool/transform_swaddon_tracing.lua +++ b/tool/transform_swaddon_tracing.lua @@ -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" @@ -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", {}) @@ -186,9 +187,9 @@ function TransformerDefs:funcbody(node) local local_file_path = "" if local_source_node.file then if local_source_node.file:find("^[\\/]") 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)