diff options
| author | ruki <[email protected]> | 2025-09-27 22:05:53 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-27 22:05:53 +0800 |
| commit | dd07af97f1b28ed18a9cd4a66dfd574f8fc795fb (patch) | |
| tree | e949590d1ba44e536f4b08cd56c60e25fa8dd385 /xmake/core/base/os.lua | |
| parent | 2bd081f1a2f21c315184a30e664b3537cfce2965 (diff) | |
| parent | 947491d0df87eafcbf8f560a1377f49a7b60a2db (diff) | |
Merge pull request #6854 from xmake-io/opti
improve os.curdir/os.cd
Diffstat (limited to 'xmake/core/base/os.lua')
| -rw-r--r-- | xmake/core/base/os.lua | 43 |
1 files changed, 27 insertions, 16 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) |
