-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProjectLoader.lua
More file actions
74 lines (62 loc) · 2.23 KB
/
ProjectLoader.lua
File metadata and controls
74 lines (62 loc) · 2.23 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
-- ProjectLoader.lua
ProjectLoader = class()
function ProjectLoader.exists(projectName)
local d = "Info.plist"
local v = ProjectLoader.read(d,projectName,true)
return (v~=nil)
end
function ProjectLoader.readAll(projectName)
local files = ProjectLoader.list(projectName)
local contents = {}
for _,file in ipairs(files) do
contents[file] = ProjectLoader.read(file,projectName)
end
return contents
end
function ProjectLoader.read(filename,projectName,retNil)
local home = os.getenv("HOME")
local dir = home.."/Documents/"..projectName..".codea/"
local fname = dir..filename
local info_file = io.open(fname,"r")
if info_file == nil and retNil then return nil end
assert(info_file~=nil,"Could not open "..fname.." in "..projectName)
local contents = ""
for lin in io.lines(fname) do
contents = contents..lin.."\n"
end
return contents
end
function ProjectLoader.list(projectName)
local info_fname = "Info.plist"
local contents = ProjectLoader.read(info_fname,projectName)
-- find the buffer order
local bufferKey = "<key>Buffer Order</key>"
local idx = contents:find(bufferKey)
assert(idx~=nil,"Could not find buffer order in "..info_fname.." in "..projectName)
contents = contents:sub(idx)
-- find the end of the buffer order
local idx2 = contents:find("</array>")
assert(idx2~=nil,"Could not find the end of the buffer order in "..info_fname.." in "..projectName)
contents = contents:sub(1,idx2)
local buffers = {}
for buff in contents:gmatch("<string>([%a%s%d]+)</string>") do
table.insert(buffers,buff..".lua")
end
table.insert(buffers,info_fname)
-- does data plist exist?
local d = "Data.plist"
local v = ProjectLoader.read(d,projectName,true)
if v then table.insert(buffers,d) end
return buffers
end
function ProjectLoader.save(fileContents,projectName)
local home = os.getenv("HOME")
local dir = home.."/Documents/"..projectName..".codea/"
for filename,conts in pairs(fileContents) do
local fname = dir..filename
local file = assert(io.open(fname,"w"))
--print(fname,file)
file:write(conts)
file:close()
end
end