-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
182 lines (148 loc) · 4.51 KB
/
utils.lua
File metadata and controls
182 lines (148 loc) · 4.51 KB
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
local lfs = require"lfs"
local utils = {}
function utils.starts_with(str, start)
return str:sub(1, #start) == start
end
function utils.ends_with(str, ending)
return ending == "" or str:sub(-#ending) == ending
end
function utils.deletedir(dir)
for file in lfs.dir(dir) do
local file_path = dir..'/'..file
if file ~= "." and file ~= ".." then
if lfs.attributes(file_path, 'mode') == 'file' then
os.remove(file_path)
elseif lfs.attributes(file_path, 'mode') == 'directory' then
utils.deletedir(file_path)
end
end
end
lfs.rmdir(dir)
end
function utils.add_to_file_list(dir, file, files)
if lfs.attributes(dir .. "/" .. file,"mode") == "file" and not utils.ends_with(file, ".iml") and not utils.starts_with(file, ".") then
files[#files + 1] = dir .. "/" .. file
elseif lfs.attributes(dir .. "/" .. file,"mode") == "directory" then
files = utils.get_file_list(dir .. "/" .. file, files)
end
return files
end
function utils.get_file_list(dir, files)
for file in lfs.dir(dir) do
if file ~= ".hg" and file ~= "." and file ~= ".." and file ~= "out" and file ~= ".idea" then
files = utils.add_to_file_list(dir, file, files)
end
end
return files
end
function utils.get_test_output_list(dir)
local files = {}
for file in lfs.dir(dir) do
if file ~= "." and file ~= ".." and file ~= "index.html" and file ~= "help.html" and file ~= "proj_info.html" and utils.ends_with(file, ".html") then
files[#files + 1] = file
end
end
return files
end
-- get modules from <BUILD_PORJ>/.idea/modules.xml
-- see if the file exists
function utils.file_exists(file)
local f = io.open(file, "rb")
if f then f:close() end
return f ~= nil
end
-- get all lines from a file, returns an empty
-- list/table if the file does not exist
function utils.lines_from(file)
if not utils.file_exists(file) then return {} end
lines = {}
for line in io.lines(file) do
lines[#lines + 1] = line
end
return lines
end
-- get module names
function utils.module_names(lines)
local module_names = {}
local module_reader = "PROJECT_DIR$/.-\" filepath="
for _,v in pairs(lines) do
if string.find(v, "<module fileurl") then
local part = string.sub(v, string.find(v, module_reader))
local remove_front = string.gsub(part, "PROJECT_DIR$/", "")
local remove_end = string.gsub(remove_front, "\" filepath=", "")
local module_name_and_path = remove_end:gsub("%.%./", "")
local iml_file_split = utils.split(module_name_and_path, "/")
local iml_file = iml_file_split[#iml_file_split]
local module_name = iml_file:gsub("%.iml", "")
module_names[#module_names + 1] = module_name
end
end
return module_names
end
-- get module names
function utils.dependant_module_names(lines)
local module_names = {}
local module_reader = "module%-name=\".-\""
for _,v in pairs(lines) do
if string.find(v, "module%-name") then
local part = string.sub(v, string.find(v, module_reader))
local remove_front = string.gsub(part, "module%-name=\"", "")
local remove_end = string.gsub(remove_front, "\"", "")
module_names[#module_names + 1] = remove_end
end
end
return module_names
end
-- did test pass, takes in lines from test output
function utils.has_passed(lines)
for _,v in pairs(lines) do
if string.find(v, "FAILURES!!!") then
return false
end
end
return true
end
function utils.split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function utils.calc_depth(modules, module_name, depth)
-- get deps of current module
local mod_deps = modules[module_name]
if #mod_deps > 0 then
depth = depth + 1
end
-- iterate
for i,dep_name in ipairs(mod_deps) do
local new_depth = utils.calc_depth(modules, dep_name, depth)
if new_depth > depth then
depth = new_depth
end
end
return depth
end
function utils.reverse_list(list)
rev = {}
for i=#list, 1, -1 do
rev[#rev+1] = list[i]
end
return rev
end
function utils.get_parent_path(path)
local function getParentPath(_path)
pattern1 = "^(.+)//"
pattern2 = "^(.+)\\"
if (string.match(path,pattern1) == nil) then
return string.match(path,pattern2)
else
return string.match(path,pattern1)
end
end
end
return utils