Advertisement
Tatantyler

Installer Maker

Oct 25th, 2012
652
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.70 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local creditHandle = io.open(shell.dir().."/credits.conf", "r")
  4. local credits = {}
  5. for line in creditHandle:lines() do
  6.     table.insert(credits, line)
  7. end
  8. creditHandle:close()
  9.  
  10. local fileListHandle = io.open(shell.dir().."/files.conf", "r")
  11. local files = {}
  12. local filePaths = {}
  13. for line in fileListHandle:lines() do
  14.     table.insert(files, line)
  15. end
  16. fileListHandle:close()
  17. for i,v in ipairs(files) do
  18.     local path = shell.resolveProgram(v)
  19.     if path then
  20.         filePaths[i] = path
  21.     else
  22.         filePaths[i] = fs.combine(shell.dir(), v)
  23.     end
  24. end
  25. local confHandle = fs.open(shell.dir().."/name.conf", "r")
  26. local name = confHandle.readAll()
  27. confHandle.close()
  28.  
  29. local fileContents = {}
  30. local dirs = {}
  31.  
  32. for i,v in ipairs(filePaths) do
  33.     local dirStart = string.find(v, shell.dir())+string.len(shell.dir())+1
  34.     local path = string.sub(v, dirStart)
  35.     if fs.isDir(v) then
  36.         dirs[path] = true
  37.     else
  38.         local handle = fs.open(v, "r")
  39.         fileContents[path] = handle.readAll()
  40.         handle.close()
  41.     end
  42. end
  43.  
  44. local installerProgram = [[
  45. local function unpackFiles(files, dirs, destination)
  46.     for i,v in pairs(dirs) do
  47.         print("Making folder: "..fs.combine(destination, i))
  48.         fs.makeDir(fs.combine(destination, i))
  49.     end
  50.     for i,v in pairs(files) do
  51.         local namestart = string.find(i, fs.getName(i))
  52.         namestart = namestart-1
  53.         local basePath = string.sub(i, 1, namestart)
  54.         if not fs.exists(fs.combine(destination, basePath)) then
  55.             fs.makeDir(fs.combine(destination, basePath))
  56.         end
  57.         print("Making file: "..fs.combine(destination, i))
  58.         local handle = fs.open( fs.combine(destination, i) , "w")
  59.         handle.write(v)
  60.         handle.close()
  61.     end
  62. end
  63.  
  64. local function drawLine(char)
  65.     local maxX = term.getSize()
  66.     print(" "..string.rep(char, maxX-2))
  67. end
  68.  
  69. local function printCentered(input)
  70.     local maxX = term.getSize()
  71.     print(string.rep(" ", math.floor((maxX - string.len(input))/2))..input)
  72. end
  73.  
  74. local function printColored(...)
  75.     for i=1, arg["n"], 2 do
  76.         term.setTextColor(arg[i])
  77.         write(arg[i+1])
  78.     end
  79.     print("")
  80.     term.setTextColor(colors.white)
  81. end
  82.  
  83. local files = ]]..textutils.serialize(fileContents)..[[
  84.  
  85. local credits = ]]..textutils.serialize(credits)..[[
  86.  
  87. local program = ]].."\""..name.."\""..[[
  88.  
  89. local dirs = ]]..textutils.serialize(dirs)..[[
  90.  
  91. term.clear()
  92. term.setCursorPos(1,1)
  93. printCentered("Installer")
  94. drawLine("=")
  95. print(" ")
  96. printColored(colors.white, "You are installing: ", colors.lime, program)
  97. term.setCursorPos(1,18)
  98. term.setTextColor(colors.lightBlue)
  99. printCentered("Press any key to continue")
  100. term.setTextColor(colors.white)
  101. os.pullEvent("char")
  102. term.clear()
  103. term.setCursorPos(1,1)
  104. printCentered("Installer")
  105. drawLine("=")
  106. print(" ")
  107. print("Credits: ")
  108. textutils.pagedTabulate(colors.lime, credits)
  109. term.setCursorPos(1,18)
  110. term.setTextColor(colors.lightBlue)
  111. printCentered("Press any key to continue")
  112. term.setTextColor(colors.white)
  113. os.pullEvent("char")
  114. term.clear()
  115. term.setCursorPos(1,1)
  116. printCentered("Installer")
  117. drawLine("=")
  118. print(" ")
  119. printColored(colors.white, "Do you want to install ", colors.lime, program, colors.white, "?")
  120. print(" ")
  121. local installOption = false
  122. while true do
  123.     local x,y = term.getCursorPos()
  124.     term.setCursorPos(1, y-1)
  125.     term.clearLine()
  126.     if installOption then
  127.         printCentered("[Yes] | No")
  128.     else
  129.         printCentered("Yes | [No]")
  130.     end
  131.     local event, key = os.pullEvent("key")
  132.     if key == 203 then
  133.         installOption = true
  134.     elseif key == 205 then
  135.         installOption = false
  136.     elseif key == 28 then
  137.         break
  138.     end
  139. end
  140.  
  141. if installOption then
  142.     unpackFiles(files, dirs, shell.dir())
  143. end
  144. term.clear()
  145. term.setCursorPos(1,1)
  146. ]]
  147.  
  148. local handle = fs.open(shell.dir().."/"..args[1], "w")
  149. handle.write(installerProgram)
  150. handle.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement