SquidDev

LuaCombiner.lua

May 30th, 2014
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. local Args = {...}      --Read Args
  2.  
  3. local Files = { }       --All Files to include
  4.  
  5. local OutHandle = nil   --File Handle to write to
  6.  
  7. local getDir = fs.getDir or function(str)
  8.     return str:match("(.*/)")
  9. end
  10.  
  11. local UseLocals = false
  12. for _, v in ipairs(Args) do
  13.     if v=="-l" then
  14.         UseLocals = true
  15.     end
  16. end
  17.  
  18. --Basic error functions
  19. local function PrintError(Message, ...)
  20.     printError(string.format(Message, unpack({...})))
  21. end
  22. local function Error(Message, ...)
  23.     error(string.format(Message, unpack({...})))
  24. end
  25.  
  26. --Require and load
  27. local RequireFile
  28.  
  29. --File functions
  30. local function LoadFile(Path)
  31.     --Read the file
  32.     local File = fs.open(Path, "r")
  33.     if not File then
  34.         Error("Could not find %s", Path)
  35.     end
  36.  
  37.     local Dir = getDir(Path)
  38.     local ThisFile = {
  39.         Path = Path,
  40.         Dir = Dir
  41.     }
  42.  
  43.     File[Path] = ThisFile
  44.  
  45.     local Line = File.readLine()
  46.     local Contents = ""
  47.  
  48.     while Line ~= nil do
  49.         local Annotation = Line:gmatch("%-%-@[^\n]*")()
  50.  
  51.         if Annotation == nil then
  52.             Contents = Contents .. Line .. "\n"
  53.         else
  54.             Annotation =  Annotation:sub(4)
  55.             local Index = Annotation:find("%s")
  56.  
  57.             if not Index then
  58.                 Error("Failed to execute Annotation for File '%s'", File.Path)
  59.             end
  60.  
  61.             local AnnotationName = Annotation:sub(1, Index - 1)
  62.             if AnnotationName == "require" or Annotation == "import" then
  63.                 local RequirePath = Annotation:sub(Index + 1):gsub("%s", "")
  64.  
  65.                 if not RequirePath then
  66.                     Error("Failed to execute '%s' for file '%s' (missing a file path)", AnnotationName, Path)
  67.                 end
  68.  
  69.                 RequirePath = fs.combine(Dir, RequirePath)
  70.  
  71.                 if Files[RequirePath] == nil then
  72.                     RequireFile(RequirePath)
  73.                 end
  74.             elseif AnnotationName == "name" then
  75.                 local AliasName = Annotation:sub(Index + 1):gsub("%s", "")
  76.  
  77.                 if not AliasName then
  78.                     Error("Failed to execute 'name for file '%s' (missing a name)", Path)
  79.                 end
  80.  
  81.                 ThisFile.Alias = AliasName
  82.             elseif AnnotationName == "include" then
  83.                 local IncludePath = Annotation:sub(Index + 1):gsub("%s", "")
  84.  
  85.                 if not IncludePath then
  86.                     Error("Failed to execute 'include' for file '%s' (missing a file path)", Path)
  87.                 end
  88.  
  89.                 IncludePath = fs.combine(Dir, IncludePath)
  90.  
  91.                 local IncludeFile = Files[IncludePath]
  92.                 if IncludeFile == nil then
  93.                     IncludeFile = LoadFile(IncludePath)
  94.                 end
  95.  
  96.                 Contents = Contents .. "--" .. IncludePath .. "\n" .. IncludeFile.Contents
  97.             else
  98.                 PrintError("Unknown annotation '%s' in file '%s'", AnnotationName, Path)
  99.             end
  100.         end
  101.  
  102.         Line = File.readLine()
  103.     end
  104.  
  105.     File.close()
  106.  
  107.     ThisFile.Contents = Contents
  108.     return ThisFile
  109. end
  110.  
  111. function RequireFile(Path)
  112.     local File = LoadFile(Path)
  113.  
  114.     OutHandle.writeLine("--"..Path)
  115.  
  116.     if File.Alias == nil then
  117.         OutHandle.writeLine("do")
  118.         OutHandle.write(File.Contents)
  119.         OutHandle.writeLine("end")
  120.     else
  121.         local Line = File.Alias .. "=_W(function()"
  122.         if UseLocals then
  123.             Line = "local "..Line
  124.         end
  125.         OutHandle.writeLine(Line)
  126.         OutHandle.writeLine(File.Contents)
  127.         OutHandle.writeLine("end)")
  128.     end
  129. end
  130.  
  131. OutHandle = fs.open(fs.combine(shell.dir(), Args[2]), "w")
  132. OutHandle.write([[
  133. local function _W(f)
  134.  local m={}
  135.  setmetatable(m, {__index = getfenv()})
  136.  setfenv(f,m)
  137.  f()
  138.  local t={}
  139.  for k,v in pairs(m) do
  140.   t[k] = v
  141.  end
  142.  return t
  143. end
  144. ]])
  145.  
  146. OutHandle.writeLine(LoadFile(fs.combine(shell.dir(), Args[1])).Contents)
  147. OutHandle.close()
Advertisement
Add Comment
Please, Sign In to add comment