summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2017-07-19 11:53:38 +0800
committerruki <[email protected]>2017-07-19 11:53:38 +0800
commit8119b93a66582b3b2f0443f5984b7a0336c30c8f (patch)
tree6dc7a99d64e47189b38a347f4ba7fb3a8055a75e
parent75ddc718c55c578c53f22ac925bc1b9df4cf7ded (diff)
improve os.cd in core
-rw-r--r--xmake/core/base/os.lua16
-rw-r--r--xmake/core/project/project.lua12
-rw-r--r--xmake/core/sandbox/modules/import/core/project/project.lua8
-rw-r--r--xmake/core/sandbox/modules/os.lua11
4 files changed, 22 insertions, 25 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 94c14ebdc..557b3d7a7 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -388,6 +388,9 @@ function os.cd(dir)
-- check
assert(dir)
+ -- the previous directory
+ local oldir = os.curdir()
+
-- change to the previous directory?
if dir == "-" then
-- exists the previous directory?
@@ -396,31 +399,28 @@ function os.cd(dir)
os._PREDIR = nil
else
-- error
- return false, string.format("not found the previous directory %s", os.strerror())
+ return nil, string.format("not found the previous directory %s", os.strerror())
end
end
-- is directory?
if os.isdir(dir) then
- -- get the current directory
- local current = os.curdir()
-
-- change to directory
if not os.chdir(dir) then
- return false, string.format("cannot change directory %s %s", dir, os.strerror())
+ return nil, string.format("cannot change directory %s %s", dir, os.strerror())
end
-- save the previous directory
- os._PREDIR = current
+ os._PREDIR = oldir
-- not exists?
else
- return false, string.format("cannot change directory %s, not found this directory %s", dir, os.strerror())
+ return nil, string.format("cannot change directory %s, not found this directory %s", dir, os.strerror())
end
-- ok
- return true
+ return oldir
end
-- create directories
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index a6f2b6c4a..31c19ee86 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -363,8 +363,8 @@ function project._load_targets()
assert(interp)
-- enter the project directory
- local ok, errors = os.cd(os.projectdir())
- if not ok then
+ local oldir, errors = os.cd(os.projectdir())
+ if not oldir then
return nil, errors
end
@@ -375,7 +375,7 @@ function project._load_targets()
end
-- leave the project directory
- ok, errors = os.cd("-")
+ local ok, errors = os.cd(oldir)
if not ok then
return nil, errors
end
@@ -428,8 +428,8 @@ function project._load_options(disable_filter)
assert(interp)
-- enter the project directory
- local ok, errors = os.cd(os.projectdir())
- if not ok then
+ local oldir, errors = os.cd(os.projectdir())
+ if not oldir then
return nil, errors
end
@@ -440,7 +440,7 @@ function project._load_options(disable_filter)
end
-- leave the project directory
- ok, errors = os.cd("-")
+ local ok, errors = os.cd(oldir)
if not ok then
return nil, errors
end
diff --git a/xmake/core/sandbox/modules/import/core/project/project.lua b/xmake/core/sandbox/modules/import/core/project/project.lua
index 8ea938d0d..0c536e700 100644
--- a/xmake/core/sandbox/modules/import/core/project/project.lua
+++ b/xmake/core/sandbox/modules/import/core/project/project.lua
@@ -59,8 +59,8 @@ function sandbox_core_project.check()
assert(instance)
-- enter the project directory
- local ok, errors = os.cd(os.projectdir())
- if not ok then
+ local oldir, errors = os.cd(os.projectdir())
+ if not oldir then
raise(errors)
end
@@ -92,7 +92,7 @@ function sandbox_core_project.check()
end
-- check all options
- ok, errors = process.runjobs(instance:fork(checktask):script(), #options, 4)
+ local ok, errors = process.runjobs(instance:fork(checktask):script(), #options, 4)
if not ok then
raise(errors)
end
@@ -101,7 +101,7 @@ function sandbox_core_project.check()
environment.leave("toolchains")
-- leave the project directory
- local ok, errors = os.cd("-")
+ ok, errors = os.cd(oldir)
if not ok then
raise(errors)
end
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 850618a57..b8c8c5ef3 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -153,17 +153,14 @@ function sandbox_os.cd(dir)
-- format it first
dir = vformat(dir)
- -- the previous directory
- local olddir = os.curdir()
-
- -- done
- local ok, errors = os.cd(dir)
- if not ok then
+ -- enter this directory
+ local oldir, errors = os.cd(dir)
+ if not oldir then
os.raise(errors)
end
-- ok
- return olddir
+ return oldir
end
-- create directories