diff options
| author | ruki <[email protected]> | 2022-05-07 00:57:57 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-07 00:57:57 +0800 |
| commit | 1e4d1edfff5bb9c8721c5667229664d205ebfe37 (patch) | |
| tree | 7c7c15b23b473b37f9750ed3b94a49f0a9db7313 | |
| parent | a28245d826c4fecaa15c6bd85ebb85f5ec9aa44c (diff) | |
add path instance
| -rw-r--r-- | xmake/core/base/path.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/xmake/core/base/path.lua b/xmake/core/base/path.lua index 8860b2af9..2c54b03a0 100644 --- a/xmake/core/base/path.lua +++ b/xmake/core/base/path.lua @@ -23,6 +23,79 @@ local path = path or {} -- load modules local string = require("base/string") +local table = require("base/table") +local _instance = _instance or {} + +-- new a path +function _instance.new(p) + local instance = table.inherit(_instance) + instance._PATH = p + setmetatable(instance, _instance) + return instance +end + +function _instance:str() + return self._PATH +end + +function _instance:normalize() + return path.new(path.normalize(self:str())) +end + +function _instance:translate(opt) + return path.new(path.translate(self:str(), opt)) +end + +function _instance:filename() + return path.filename(self:str()) +end + +function _instance:basename() + return path.basename(self:str()) +end + +function _instance:extension() + return path.extension(self:str()) +end + +function _instance:directory() + return path.new(path.directory(self:str())) +end + +function _instance:join(...) + local items = {self:str()} + for _, item in ipairs(table.pack(...)) do + if path.instance_of(item) then + table.insert(items, item:str()) + else + table.insert(items, item) + end + end + return path.new(path.join(table.unpack(items))) +end + +function _instance:split(p) + return path.split(self:str(), p) +end + +function _instance:splitenv(p) + return path.splitenv(self:str(), p) +end + +-- concat two paths +function _instance:__concat(other) + return path.new(path.join(self:str(), other:str())) +end + +-- tostring(path) +function _instance:__tostring() + return self:str() +end + +-- todisplay(path) +function _instance:__todisplay() + return "<path: " .. self:str() .. ">" +end -- path.translate: -- - transform the path separator @@ -231,5 +304,22 @@ function path.cygwin_path(p) return p end +-- new a path instance +function path.new(p) + return _instance.new(p) +end + +-- is path instance? +function path.instance_of(p) + return type(p) == "table" and p.normalize and p._PATH +end + +-- register call function +setmetatable(path, { + __call = function (_, ...) + return path.new(...) + end, +}) + -- return module: path return path |
