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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
Path = {}
function Path:new(p)
local is_absolute = p:sub(1, 1) == "/"
p = p:gsub("/+", "/")
p = p:gsub("(/%./", "/")
p = p:gsub("/%.$", "")
p = p:gsub("/$", "")
p = p:gsub("^%./", "")
p = p:gsub("/[^/]+/%.%.", "")
p = p:gsub("^[^/]+/%.%./", "")
local path = {
segments = p:split('/'),
is_absolute = is_absolute
}
setmetatable(path, self)
self.__index = self
return path
end
function Path:name()
return self.segments[#self.segments]
end
function Path:stem()
return self.segments[#self.segments]:match('[^.]+')
end
function Path:parent()
return self / ".."
end
function Path:__tostring()
return (self.is_absolute and "/" or "") .. table.concat(self.segments, "/")
end
function Path:__div(next)
if next.is_absolute then
error("cannot concat with absolute path")
end
return Path:new(self:__tostring() .. "/" .. tostring(next))
end
function Path:open(mode)
return io.open(tostring(self), mode)
end
function Path:exists()
local f = self:open('r')
if f ~= nil then
f:close()
return true
end
return false
end
function Path:is_directory()
local f = self:open('r')
if not f then
return false
end
local value, err, code = f:read()
f:close()
return code == 21
end
function Path:entries()
if not self:is_directory() then
return function() end
end
local pipe = io.popen('ls' .. ' "' .. tostring(self) .. '"')
local lines = pipe:lines()
return function()
local value = lines()
if value == nil then
pipe:close()
return nil
end
return self / value
end
end
function Path:children()
if not self:is_directory() then
return function() end
end
local pipe = io.popen('find' .. ' "' .. tostring(self) .. '"')
local lines = pipe:lines()
return function()
local value = lines()
if value == nil then
pipe:close()
return nil
end
return Path:new(value)
end
end
function Path:make_directory(opts)
local pipe = io.popen('mkdir ' .. (opts.create_parents and '-p ' or ' ') .. tostring(self))
pipe:close()
end
function Path:copy_to(target)
local pipe = io.popen('cp -r ' .. tostring(self) .. ' ' .. tostring(target))
pipe:close()
end
function Path:remove(opts)
local pipe = io.popen('rm ' ..
(opts.recursive and '-r ' or ' ') ..
(opts.force and '-f ' or ' ') ..
tostring(self)
)
pipe:close()
end
function Path:is_parent_of(child)
return tostring(child):starts_with(tostring(self))
end
function Path:relative_to_parent(path)
if not path:is_parent_of(self) then
return nil
end
return Path:new(tostring(self):sub(#tostring(path) + 2))
end
return {
tests = {
function()
local path = Path:new("some/path/here")
assert.equals("here", path:name())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("here", path:stem())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("some/path", path:parent():__tostring())
end,
function()
local path = Path:new("some/path/here.txt")
assert.equals("some/path/here.txt", path:__tostring())
end,
function()
local path = Path:new("some/path/here")
path = path / 'next.txt'
assert.equals("some/path/here/next.txt", path:__tostring())
end,
function()
local path = Path:new("./src")
assert.equals(true, path:is_directory())
end,
function()
local path = Path:new("./src/blueprint")
assert.equals(false, path:is_directory())
end,
function()
local path = Path:new("./src/blueprint")
assert.equals(true, path:exists())
end,
function()
local path = Path:new("./src/blueprint__")
assert.equals(false, path:exists())
end
}
}
|