Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Mar 12, 2024
1 parent 26d814c commit 4880b0c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 63 deletions.
25 changes: 3 additions & 22 deletions scripts/glob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,14 @@ local function compile(pattern)
end

local function pattern_preprocess(root, pattern)
local ispath = pathutil.is(pattern)
if ispath and pattern.accepted then
local value = pattern.value
local ignore
if value:match "^!" then
ignore = true
value = value:sub(2)
end
return value, ignore
end

local path = ispath and pattern.value or tostring(pattern)

local ispath, path = pathutil.tovalue(pattern)
local ignore
if path:match "^!" then
ignore = true
path = path:sub(2)
end
path = fsutil.absolute(root, path)

if ispath then
if ignore then
pattern.value = "!"..path
else
pattern.value = path
end
pattern.accepted = true
if not ispath then
path = fsutil.absolute(root, path)
end
return path, ignore
end
Expand Down
32 changes: 16 additions & 16 deletions scripts/pathutil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ local function path_normalize(base, path)
return path:gsub("\\", "/")
end

local function is(path)
return mt == getmetatable(path)
end

local function accept(base, path)
if mt == getmetatable(path) then
if is(path) then
if not path.accepted then
path.value = path_normalize(base, path.value)
path.accepted = true
Expand All @@ -30,24 +34,20 @@ local function accept(base, path)
return path
end

local function tostring(base, path)
if mt == getmetatable(path) then
if not path.accepted then
path.value = path_normalize(base, path.value)
path.accepted = true
end
local function tostr(base, path)
if is(path) then
assert(path.accepted, "Cannot be used before accept.")
return path.value
end
return path_normalize(base, path)
end

local function is(path)
return mt == getmetatable(path)
end

function mt:__tostring()
assert(self.accepted, "Cannot be used before accept.")
return self.value
local function tovalue(path)
if is(path) then
assert(path.accepted, "Cannot be used before accept.")
return true, path.value
end
return false, tostring(path)
end

function mt.__concat(lft, rhs)
Expand All @@ -67,7 +67,7 @@ end

return {
create = create,
tostring = tostring,
tostr = tostr,
tovalue = tovalue,
accept = accept,
is = is,
}
20 changes: 10 additions & 10 deletions scripts/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,25 @@ end
local function path2string(root, v)
local vt = type(v)
if vt == "string" then
return pathutil.tostring(root, v)
return pathutil.tostr(root, v)
elseif vt == "userdata" then
return pathutil.tostring(root, v)
return pathutil.tostr(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
return pathutil.tostring(root, v)
return pathutil.tostr(root, v)
end
end
end

local function push_paths(t, v, root)
local vt = type(v)
if vt == "string" then
t[#t+1] = pathutil.tostring(root, v)
t[#t+1] = pathutil.tostr(root, v)
elseif vt == "userdata" then
t[#t+1] = pathutil.tostring(root, v)
t[#t+1] = pathutil.tostr(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
t[#t+1] = pathutil.tostring(root, v)
t[#t+1] = pathutil.tostr(root, v)
else
for i = 1, #v do
push_paths(t, v[i], root)
Expand All @@ -118,17 +118,17 @@ local function push_mix(t, v, root)
local vt = type(v)
if vt == "string" then
if v:sub(1, 1) == "@" then
t[#t+1] = pathutil.tostring(root, v:sub(2))
t[#t+1] = pathutil.tostr(root, v:sub(2))
else
t[#t+1] = v:gsub("@{([^}]*)}", function (s)
return pathutil.tostring(root, s)
return pathutil.tostr(root, s)
end)
end
elseif vt == "userdata" then
t[#t+1] = pathutil.tostring(root, v)
t[#t+1] = pathutil.tostr(root, v)
elseif vt == "table" then
if getmetatable(v) ~= nil then
t[#t+1] = pathutil.tostring(root, v)
t[#t+1] = pathutil.tostr(root, v)
else
for i = 1, #v do
push_mix(t, v[i], root)
Expand Down
18 changes: 3 additions & 15 deletions scripts/writer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,7 @@ local function tbl_insert(t, pos, a)
end

local function normalize_rootdir(workdir, rootdir)
if type(rootdir) == "table" then
if getmetatable(rootdir) == nil then
rootdir = rootdir[#rootdir]
else
--TODO
rootdir = tostring(rootdir)
return fsutil.absolute(WORKDIR, rootdir)
end
end
return fsutil.normalize(workdir, rootdir or ".")
return pathutil.tostr(workdir, rootdir)
end

local function reslove_attributes(g, l)
Expand Down Expand Up @@ -647,7 +638,7 @@ function GEN.copy(attribute, name)
local inputs = attribute.inputs or {}
local outputs = attribute.outputs or {}
for i = 1, #inputs do
inputs[i] = pathutil.tostring(attribute.rootdir, inputs[i])
inputs[i] = pathutil.tostr(attribute.rootdir, inputs[i])
end
local implicit_inputs = getImplicitInputs(name, attribute)
log.assert(#inputs == #outputs, "`%s`: The number of input and output must be the same.", name)
Expand Down Expand Up @@ -985,10 +976,7 @@ end

function api:import(path)
local ws = self
local fullpath = pathutil.tostring(ws.workdir, path)
if fs.is_directory(fullpath) then
fullpath = fsutil.join(fullpath, "make.lua")
end
local fullpath = pathutil.tostr(ws.workdir, path)
if visited[fullpath] then
return
end
Expand Down

0 comments on commit 4880b0c

Please sign in to comment.