Advertisement
billysback

clscmp

Nov 17th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. local root
  2. local output
  3. local outlines = nil
  4. local out_dir
  5.  
  6. local function getLines(dir)
  7.     local lines = {}
  8.     if fs.exists(dir) then
  9.         local file = fs.open(dir, "r")
  10.         local line = file.readLine()
  11.         while line ~= nil do
  12.             lines[#lines + 1] = line
  13.             line = file.readLine()
  14.         end
  15.         file.close()
  16.     end
  17.     return lines
  18. end
  19.  
  20. local function writeClass(dir)
  21.     local fdir = root.."/"..dir
  22.     local import = string.gsub(out_dir.."/"..dir, "/", ".")
  23.    
  24.     local lines = getLines(fdir)
  25.    
  26.     if #lines > 1 then
  27.        
  28.         if outlines ~= nil then
  29.             outlines[#outlines + 1] = "++"..import
  30.             for i=1,#lines do
  31.                 local line = lines[i]
  32.                 if string.sub(line, 1, 8) == "inherit " then
  33.                     local impt = string.sub(line, 9, -1)
  34.                     local imp = classy.import(impt)
  35.                     if imp ~= nil then
  36.                         local chunk = imp[1]
  37.                         for j=1,#chunk do
  38.                             outlines[#outlines + 1] = chunk[j]
  39.                         end
  40.                     end
  41.                 else
  42.                     outlines[#outlines + 1] = lines[i]
  43.                 end
  44.             end
  45.             outlines[#outlines + 1] = "--"..import
  46.         end
  47.    
  48.     end
  49. end
  50.  
  51. local function writeList(odir, list, tab)
  52.     for i=1,#list do
  53.         local name = list[i]
  54.         print(tab..name)
  55.         if fs.isDir(root.."/"..odir..name) then
  56.             local path = odir..name
  57.             writeList(path.."/", fs.list(root.."/"..path), tab.." ")
  58.         else
  59.             writeClass(odir..name)
  60.         end
  61.     end
  62. end
  63.  
  64. local function writeProject(dir, odir)
  65.     root = dir
  66.     outlines = {}
  67.    
  68.    
  69.     local list = fs.list(dir)
  70.     writeList("", list, " ")
  71.    
  72.     if fs.exists(odir..".cmp") then fs.delete(odir..".cmp") end
  73.     output = fs.open(odir..".cmp", "a")
  74.    
  75.     print("Writing to output...")
  76.    
  77.     local tx,ty = term.getSize()
  78.    
  79.     local erase = ""
  80.     for i=1,tx do erase = erase.." " end
  81.    
  82.     local x,y = term.getCursorPos()
  83.    
  84.     for i=1,#outlines do
  85.         local line = outlines[i]
  86.         if line ~= nil and string.len(line) > 0 then
  87.             output.writeLine(outlines[i])
  88.         end
  89.         local p = math.floor(tonumber((i/#outlines)*100))
  90.        
  91.         term.setCursorPos(x, y)
  92.         term.write(erase)
  93.         term.setCursorPos(x, y)
  94.         term.write("%"..p.." done!")
  95.     end
  96.     print("")
  97.     output.close()
  98. end
  99.  
  100. local function getInput()
  101.    
  102.     local tx,ty = term.getSize()
  103.    
  104.     local erase = ""
  105.     for i=1,tx do erase = erase.." " end
  106.  
  107.     local x,y = term.getCursorPos()
  108.     local input = read()
  109.     while input == "" or input == nil do
  110.         term.setCursorPos(x, y)
  111.         term.write(erase)
  112.         term.setCursorPos(x, y)
  113.         input = read()
  114.     end
  115.     return input
  116.    
  117. end
  118.  
  119. print("Enter project directory:")
  120. local root_dir = getInput()
  121. print("Enter output directory:")
  122. out_dir = getInput()
  123.  
  124. print(" ")
  125. print("Writing project...")
  126. writeProject(root_dir, out_dir)
  127. print("Finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement