Skip to content

Commit

Permalink
Simplify and fix direxists
Browse files Browse the repository at this point in the history
  • Loading branch information
zauguin authored and josephwright committed Aug 15, 2021
1 parent ae92a5a commit 71d0632
Showing 1 changed file with 16 additions and 30 deletions.
46 changes: 16 additions & 30 deletions l3build-file-functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,28 @@ function cleandir(dir)
return rm(dir, "**")
end

function direxists(dir)
return attributes(dir, "mode") == "directory"
end

function fileexists(file)
local f = open(file, "r")
if f ~= nil then
f:close()
return true
else
return false -- also file exits and is not readable
end
end

-- Copy files 'quietly'
function cp(glob, source, dest)
local errorlevel
for _,p in ipairs(tree(source, glob)) do
-- p_src is a path relative to `source` whereas
-- p_cwd is the counterpart relative to the current working directory
if os_type == "windows" then
if attributes(p.cwd, "mode") == "directory" then
if direxists(p.cwd) then
errorlevel = execute(
'xcopy /y /e /i "' .. unix_to_win(p.cwd) .. '" "'
.. unix_to_win(dest .. '/' .. p.src) .. '" > nul'
Expand All @@ -241,31 +255,6 @@ function cp(glob, source, dest)
return 0
end

-- OS-dependent test for a directory
function direxists(dir)
local errorlevel
if os_type == "windows" then
errorlevel =
execute("if not exist \"" .. unix_to_win(dir) .. "\" exit 1")
else
errorlevel = execute("[ -d '" .. dir .. "' ]")
end
if errorlevel ~= 0 then
return false
end
return true
end

function fileexists(file)
local f = open(file, "r")
if f ~= nil then
f:close()
return true
else
return false -- also file exits and is not readable
end
end

-- Generate a table containing all file names of the given glob or all files
-- if absent
function filelist(path, glob)
Expand Down Expand Up @@ -309,16 +298,13 @@ function tree(src_path, glob)
local function always_true()
return true
end
local function is_dir(file)
return attributes(file, "mode") == "directory"
end
---@type table<integer,tree_entry_t>
local result = { {
src = ".",
cwd = src_path,
} }
for glob_part, sep in glob:gmatch("([^/]+)(/?)/*") do
local accept = sep == "/" and is_dir or always_true
local accept = sep == "/" and direxists or always_true
---Feeds the given table according to `glob_part`
---@param p tree_entry_t path counterpart relative to the current working directory
---@param table table
Expand Down

0 comments on commit 71d0632

Please sign in to comment.