Skip to content

Commit

Permalink
support path instance for os
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed May 8, 2022
1 parent 48e5a65 commit c54cee1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
21 changes: 17 additions & 4 deletions core/src/xmake/os/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,23 @@ tb_int_t xm_os_args(lua_State* lua)
// add argument
lua_pushnumber(lua, (tb_int_t)i);
lua_rawget(lua, 1);
size_t size = 0;
tb_char_t const* cstr = luaL_checklstring(lua, -1, &size);
if (cstr && size)
tb_os_args_append(&result, cstr, size, escape, nowrap);
if (lua_istable(lua, -1)) // is path instance?
{
lua_pushstring(lua, "_PATH");
lua_gettable(lua, -2);
size_t size = 0;
tb_char_t const* cstr = luaL_checklstring(lua, -1, &size);
if (cstr && size)
tb_os_args_append(&result, cstr, size, escape, nowrap);
lua_pop(lua, 1);
}
else
{
size_t size = 0;
tb_char_t const* cstr = luaL_checklstring(lua, -1, &size);
if (cstr && size)
tb_os_args_append(&result, cstr, size, escape, nowrap);
}
lua_pop(lua, 1);
}
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/xmake/process/openv.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ tb_int_t xm_process_openv(lua_State* lua)
// pass this argument
argv[1 + argi] = lua_tostring(lua, -1);
}
// is path instance?
else if (lua_istable(lua, -1))
{
lua_pushstring(lua, "_PATH");
lua_gettable(lua, -2);
argv[1 + argi] = lua_tostring(lua, -1);
lua_pop(lua, 1);
}
else
{
// error
Expand Down
24 changes: 21 additions & 3 deletions xmake/core/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ end
--
function os.match(pattern, mode, callback)

-- support path instance
pattern = tostring(pattern)

-- get the excludes
local excludes = pattern:match("|.*$")
if excludes then excludes = excludes:split("|", {plain = true}) end
Expand Down Expand Up @@ -423,12 +426,15 @@ function os.cp(srcpath, dstpath, opt)
-- reserve the source directory structure if opt.rootdir is given
local rootdir = opt and opt.rootdir
if rootdir then
rootdir = tostring(rootdir)
if not path.is_absolute(rootdir) then
rootdir = path.absolute(rootdir)
end
end

-- copy files or directories
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
local srcpathes = os._match_wildcard_pathes(srcpath)
if type(srcpathes) == "string" then
return os._cp(srcpathes, dstpath, rootdir, opt)
Expand All @@ -452,6 +458,8 @@ function os.mv(srcpath, dstpath)
end

-- copy files or directories
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
local srcpathes = os._match_wildcard_pathes(srcpath)
if type(srcpathes) == "string" then
return os._mv(srcpathes, dstpath)
Expand All @@ -475,6 +483,7 @@ function os.rm(filepath)
end

-- remove file or directories
filepath = tostring(filepath)
local filepathes = os._match_wildcard_pathes(filepath)
if type(filepathes) == "string" then
return os._rm(filepathes)
Expand All @@ -491,6 +500,8 @@ end

-- link file or directory to the new symfile
function os.ln(srcpath, dstpath)
srcpath = tostring(srcpath)
dstpath = tostring(dstpath)
if not os.link(srcpath, dstpath) then
return false, string.format("cannot link %s to %s, %s", srcpath, dstpath, os.strerror())
end
Expand All @@ -499,10 +510,11 @@ end

-- change to directory
function os.cd(dir)

-- check
assert(dir)

-- support path instance
dir = tostring(dir)

-- the previous directory
local oldir = os.curdir()

Expand Down Expand Up @@ -551,6 +563,9 @@ function os.mkdir(dir)
return false, string.format("invalid arguments!")
end

-- support path instance
dir = tostring(dir)

-- create directories
local dirs = table.wrap(os._match_wildcard_pathes(dir))
for _, _dir in ipairs(dirs) do
Expand All @@ -569,6 +584,9 @@ function os.rmdir(dir)
return false, string.format("invalid arguments!")
end

-- support path instance
dir = tostring(dir)

-- remove directories
local dirs = table.wrap(os._match_wildcard_pathes(dir))
for _, _dir in ipairs(dirs) do
Expand Down Expand Up @@ -728,7 +746,7 @@ function os.execv(program, argv, opt)
opt = opt or {}

-- is not executable program file?
local filename = program
local filename = tostring(program)
if not os.isexec(program) then

-- parse the filename and arguments, e.g. "xcrun -sdk macosx clang"
Expand Down

0 comments on commit c54cee1

Please sign in to comment.