Repository navigation
Expand file tree
/
Copy pathaudit.lua
More file actions
88 lines (71 loc) · 2.22 KB
/
Copy pathaudit.lua
File metadata and controls
88 lines (71 loc) · 2.22 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
local fs = require("src.util.fs")
local spawn = require("src.agnostic.spawn")
-- audit all files for usage
--- Split a string into an array by newlines
---@param s string the string to split up
---@return string[] lines
local function split_newlines(s)
local lines = {}
for sub in string.gmatch(s, "[^\r\n]+") do
table.insert(lines, sub)
end
return lines
end
--- Convert a filesystem path to a lua module name
---@param path string
---@return string
local function as_module(path)
local new_path = path
-- remove ./
:gsub("%./", "")
-- remove .lua
:gsub(".lua", "")
-- / -> .
:gsub("/", ".")
-- <mod>.init -> mod
:gsub("%.init", "")
return new_path
end
local function audit()
print("No files depend on these files: ")
local ignore = {
"lib",
"_types",
"test",
"rc.lua",
"audit.lua",
"config_rm example.lua",
"src/widgets/applet",
"src/sh/service/services",
"src/cli/commands"
}
spawn("find -name '*.lua'", function(res)
for _, file in ipairs(split_newlines(res)) do
(function()
for _, ignore_item in ipairs(ignore) do
if string.find(file, "./" .. ignore_item) then
return
end
end
local module_name = as_module(file)
spawn("find -name '*.lua' | grep -v \"" .. file .. "\"", function(res)
local has_depends = false
for _, possibly_depends in ipairs(split_newlines(res)) do
local contents = fs.read(possibly_depends)
if not contents then
error("file " .. possibly_depends .. " does not load")
end
if string.find(contents, module_name) then
has_depends = true
break
end
end
if not has_depends then
print(" - " .. module_name)
end
end)
end)()
end
end)
end
audit()