1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
local path = {}
path.sep = package.config:sub(1, 1)
path.is_windows = path.sep == "\\"
local function normalize(p)
local out = p:gsub("\\", "/")
out = out:gsub("/+", "/")
if path.is_windows then
out = out:gsub("/", "\\")
end
return out
end
function path.join(...)
local parts = {...}
local out = ""
for _, part in ipairs(parts) do
part = tostring(part)
if part ~= "" then
if out == "" then
out = part
else
out = out .. "/" .. part
end
end
end
return normalize(out)
end
function path.dirname(p)
p = tostring(p):gsub("\\", "/")
local d = p:match("^(.*)/[^/]*$")
if d == nil or d == "" then return "." end
return normalize(d)
end
function path.basename(p)
p = tostring(p):gsub("\\", "/")
return p:match("([^/]+)$") or p
end
function path.exists(p)
local f = io.open(p, "rb")
if f then f:close(); return true end
return false
end
function path.read_file(p)
local f = assert(io.open(p, "rb"))
local data = f:read("*a")
f:close()
return data
end
function path.write_file(p, data)
local f = assert(io.open(p, "wb"))
f:write(data)
f:close()
end
function path.remove_file(p)
if not p or p == "" then return end
os.remove(p)
end
function path.shell_quote(s)
s = tostring(s)
if path.is_windows then
return '"' .. s:gsub('"', '\\"') .. '"'
end
return "'" .. s:gsub("'", "'\\''") .. "'"
end
function path.repo_root(script_path)
local tools_dir = path.dirname(script_path or arg[0] or "tools/test.lua")
local root = path.dirname(tools_dir)
if path.exists(path.join(root, "gpu.odin")) then
return root
end
return "."
end
function path.mkdir_p(dir)
local cmd
if path.is_windows then
cmd = "if not exist " .. path.shell_quote(dir) .. " mkdir " .. path.shell_quote(dir)
else
cmd = "mkdir -p " .. path.shell_quote(dir)
end
return os.execute(cmd)
end
return path