The path operation module implements cross-platform path operations, which is a custom module of xmake.
::: tip API
path.new(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string |
local p = path.new("/tmp/file.txt")
print(p:filename())
The result is: file.txt
::: tip API
path.join(paths: <string|array>, ...)
:::
| Parameter | Description |
|---|---|
| paths | Path string or array |
| ... | Variable arguments, can pass multiple path strings |
Adding multiple path items by splicing. Due to the path difference of windows/unix style, using api to append paths is more cross-platform, for example:
print(path.join("$(tmpdir)", "dir1", "dir2", "file.txt"))
The above splicing on Unix is equivalent to: $(tmpdir)/dir1/dir2/file.txt, and on Windows is equivalent to: $(tmpdir)\\dir1\\dir2\\file.txt
If you find this cumbersome and not clear enough, you can use: path.translate to format the conversion path string to the format supported by the current platform.
::: tip API
path.translate(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to convert |
Formatting converts the specified path string to the path style supported by the current platform, and supports the path string parameter of the windows/unix format to be passed in, even mixed, such as:
print(path.translate("$(tmpdir)/dir/file.txt"))
print(path.translate("$(tmpdir)\\dir\\file.txt"))
print(path.translate("$(tmpdir)\\dir/dir2//file.txt"))
The path strings of the above three different formats, after being standardized by translate, will become the format supported by the current platform, and the redundant path separator will be removed.
::: tip API
path.basename(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string |
print(path.basename("$(tmpdir)/dir/file.txt"))
The result is: file
::: tip API
path.filename(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string |
print(path.filename("$(tmpdir)/dir/file.txt"))
The result is: file.txt
::: tip API
path.extension(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string |
print(path.extension("$(tmpdir)/dir/file.txt"))
The result is: .txt
::: tip API
path.directory(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string |
print(path.directory("$(tmpdir)/dir/file.txt"))
The result is: $(tmpdir)/dir
::: tip API
path.relative(path: <string>, rootdir: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to convert |
| rootdir | Root directory for relative conversion |
print(path.relative("$(tmpdir)/dir/file.txt", "$(tmpdir)"))
The result is: dir/file.txt
The second parameter is to specify the relative root directory. If not specified, the default is relative to the current directory:
os.cd("$(tmpdir)")
print(path.relative("$(tmpdir)/dir/file.txt"))
The result is the same.
::: tip API
path.absolute(path: <string>, rootdir: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to convert |
| rootdir | Root directory for absolute conversion |
print(path.absolute("dir/file.txt", "$(tmpdir)"))
The result is: $(tmpdir)/dir/file.txt
The second parameter is to specify the relative root directory. If not specified, the default is relative to the current directory:
os.cd("$(tmpdir)")
print(path.absolute("dir/file.txt"))
The result is the same.
::: tip API
path.is_absolute(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to check |
if path.is_absolute("/tmp/file.txt") then
-- if it is an absolute path
end
::: tip API
path.split(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to split |
print(path.split("/tmp/file.txt"))
The result is: { "tmp", "file.txt" }
/::: tip API
path.sep()
:::
| Parameter | Description |
|---|---|
| None | No parameters |
print(path.sep())
The result is: /
::: tip API
path.islastsep(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to check |
if (path.islastsep("/tmp/dir/")) then
-- if the last character is a separator
end
::: tip API
path.splitenv(envpath: <string>)
:::
| Parameter | Description |
|---|---|
| envpath | Environment variable path string |
local pathes = path.splitenv(vformat("$(env PATH)"))
-- for windows
local pathes = path.splitenv("C:\\Windows;C:\\Windows\\System32")
-- got { "C:\\Windows", "C:\\Windows\\System32" }
-- for *nix
local pathes = path.splitenv("/usr/bin:/usr/local/bin")
-- got { "/usr/bin", "/usr/local/bin" }
The result is an array of strings, each item is a path in the input string.
::: tip API
path.joinenv(paths: <array>)
:::
| Parameter | Description |
|---|---|
| paths | Array of path strings |
print(path.joinenv({"/tmp/dir", "/tmp/dir2"}))
The result is: /tmp/dir;/tmp/dir2 (on Windows)
::: tip API
path.envsep()
:::
| Parameter | Description |
|---|---|
| None | No parameters |
print(path.envsep())
The result is: ;
::: tip API
path.cygwin_path(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Windows path string to convert |
print(path.cygwin_path("C:\\Windows"))
The result is: /C/Windows
::: tip API
path.pattern(path: <string>)
:::
| Parameter | Description |
|---|---|
| path | Path string to convert |
print(path.pattern("/tmp/file.txt"))
The result is: /[tT][mM][pP]/[fF][iI][lL][eE]%.[tT][xX][tT]