diff options
| -rw-r--r-- | xmake/core/base/os.lua | 43 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 21 |
2 files changed, 36 insertions, 28 deletions
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index cecf38dd2..57046813e 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -39,6 +39,7 @@ os._mkdir = os._mkdir or os.mkdir os._rmdir = os._rmdir or os.rmdir os._touch = os._touch or os.touch os._tmpdir = os._tmpdir or os.tmpdir +os._curdir = os._curdir or os.curdir os._fscase = os._fscase or os.fscase os._setenv = os._setenv or os.setenv os._getenvs = os._getenvs or os.getenvs @@ -197,6 +198,14 @@ function os._sched_chdir_set(chdir) os._SCHED_CHDIR = chdir end +-- notify the current directory have been changed +function os._notify_curdir_changed() + os._CURDIR = nil + if os._SCHED_CHDIR then + os._SCHED_CHDIR(os.curdir()) + end +end + -- notify envs have been changed function os._notify_envs_changed(envs) if os._SCHED_CHENVS then @@ -605,41 +614,33 @@ function os.cd(dir) -- support path instance dir = tostring(dir) - -- the previous directory - local oldir = os.curdir() - -- change to the previous directory? + local oldir = os.curdir() if dir == "-" then - -- exists the previous directory? if os._PREDIR then dir = os._PREDIR os._PREDIR = nil else - -- error return nil, string.format("not found the previous directory %s", os.strerror()) end end - -- is directory? - if os.isdir(dir) then + -- no changed? + if dir == oldir then + return oldir + end - -- change to directory + -- do change directory + if os.isdir(dir) then if not os.chdir(dir) then return nil, string.format("cannot change directory %s %s", dir, os.strerror()) end - - -- save the previous directory os._PREDIR = oldir - - -- not exists? else return nil, string.format("cannot change directory %s, not found this directory %s", dir, os.strerror()) end - -- do chdir callback for scheduler - if os._SCHED_CHDIR then - os._SCHED_CHDIR(os.curdir()) - end + os._notify_curdir_changed() return oldir end @@ -695,6 +696,16 @@ function os.rmdir(dir) return true end +-- get the current directory +function os.curdir() + local curdir = os._CURDIR + if curdir == nil then + curdir = os._curdir() + os._CURDIR = curdir + end + return curdir +end + -- get the temporary directory function os.tmpdir(opt) diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 9084ad6ba..c96b32441 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -273,18 +273,15 @@ function scheduler:_co_curdir_update(curdir) return end - -- save the current directory hash + -- save the current directory curdir = curdir or os.curdir() - local curdir_hash = hash.uuid4(path.absolute(curdir)):sub(1, 8) - self._CO_CURDIR_HASH = curdir_hash - - -- save the current directory for each coroutine local co_curdirs = self._CO_CURDIRS if not co_curdirs then co_curdirs = {} self._CO_CURDIRS = co_curdirs end - co_curdirs[running] = {curdir_hash, curdir} + co_curdirs[running] = curdir + self._CO_CURDIR_CURRENT = curdir end -- update the current environments hash of current coroutine @@ -444,10 +441,10 @@ function scheduler:co_resume(co, ...) if running then -- has the current directory been changed? restore it - local curdir = self._CO_CURDIR_HASH + local curdir = self._CO_CURDIR_CURRENT local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil - if olddir and curdir ~= olddir[1] then -- hash changed? - os.cd(olddir[2]) + if olddir and curdir ~= olddir then -- hash changed? + os.cd(olddir) end -- has the current environments been changed? restore it @@ -469,10 +466,10 @@ function scheduler:co_suspend(...) -- has the current directory been changed? restore it local running = assert(self:co_running()) - local curdir = self._CO_CURDIR_HASH + local curdir = self._CO_CURDIR_CURRENT local olddir = self._CO_CURDIRS and self._CO_CURDIRS[running] or nil - if olddir and curdir ~= olddir[1] then -- hash changed? - os.cd(olddir[2]) + if olddir and curdir ~= olddir then -- hash changed? + os.cd(olddir) end -- has the current environments been changed? restore it |
