Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
fs.lua
Go to the documentation of this file.
1local function ls(src_path)
2 local ls_cmd = io.popen('ls -1 '..src_path)
3 local ls_files = {}
4
5 if ls_cmd then
6 repeat
7 local line = ls_cmd:read()
8 ls_files[#ls_files + 1] = line
9 until not line
10 ls_cmd:close()
11 end
12
13 return ls_files
14end
15
16local function clear(src_path)
17 if os.execute('rm --version > /dev/null 2> /dev//null') then
18 os.execute('mkdir -p '..src_path)
19 os.execute('rm -Rf '..src_path..'/*')
20 else
21 src_path = src_path:gsub('/', '\\')
22 os.execute('mkdir '..src_path)
23 os.execute('rmdir /s /q '..src_path..'\\*')
24 end
25end
26
27local function move(src_in, dist_out)
28 local src_file = io.open(src_in, "rb")
29 local dist_file = io.open(dist_out, "wb")
30
31 if src_file and dist_file then
32 repeat
33 local buffer = src_file:read(1024)
34 if buffer then
35 dist_file:write(buffer)
36 end
37 until not buffer
38 end
39
40 if src_file then
41 src_file:close()
42 end
43 if dist_file then
44 dist_file:close()
45 end
46end
47
48local function moveLua(src_in, dist_path, dist_file)
49 local deps = {}
50 local pattern = "local ([%w_%-]+) = require%('src/(.-)'%)"
51 local src_file = io.open(src_in, "r")
52 local dist_file_normalized = src_in:gsub('/', '_'):gsub('^src_', '')
53 local dist_out = dist_path:gsub('/$', '')..'/'..(dist_file or dist_file_normalized)
54 local dist_file = io.open(dist_out, "w")
55
56 if src_file and dist_file then
57 repeat
58 local line = src_file:read()
59 if line then
60 local line_require = { line:match(pattern) }
61 if line_require and #line_require > 0 then
62 local var_name = line_require[1]
63 local module_path = line_require[2]
64 deps[#deps + 1] = 'src/'..module_path..'.lua'
65 dist_file:write('local '..var_name..' = require(\''..module_path:gsub('/', '_')..'\')\n')
66 else
67 dist_file:write(line, '\n')
68 end
69 end
70 until not line
71 end
72
73 if src_file then
74 src_file:close()
75 end
76 if dist_file then
77 dist_file:close()
78 end
79
80 return deps
81end
82
83local function build(src_in, dist_path)
84 local main = true
85 local deps = {}
86 local deps_builded = {}
87
88 repeat
89 if src_in:sub(-4) == '.lua' then
90 local index = 1
91 local index_deps = #deps
92 local file_name = main and 'main.lua'
93 local new_deps = moveLua(src_in, dist_path, file_name)
94 while index <= #new_deps do
95 deps[index_deps + index] = new_deps[index]
96 index = index + 1
97 end
98 end
99
100 main = false
101 src_in = nil
102 local index = 1
103 while index <= #deps and not src_in do
104 local dep = deps[index]
105 if not deps_builded[dep] then
106 deps_builded[dep] = true
107 src_in = dep
108 end
109 index = index + 1
110 end
111 until not src_in
112end
113
114local P = {
115 ls = ls,
116 move = move,
117 moveLua = moveLua,
118 build = build,
119 clear = clear
120}
121
122return P
local function move(src_in, dist_out)
local function moveLua(src_in, dist_path, dist_file)
local function build(src_in, dist_path)
local function ls(src_path)
local function clear(src_path)
local os
Definition main.lua:1
local function line(x1, y1, x2, y2)