Advertisement
_LINKI

GreyScript Minifier

Jul 18th, 2019
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. if params.len < 1 then exit("Example: minifier [SourceCodePath]")
  2.  
  3. pc = get_shell.host_computer
  4. codeFilePath = params[0]
  5.  
  6. codeFile = pc.File(codeFilePath)
  7. if not codeFile then exit("File not found")
  8.  
  9. analyzeLine = function(string)
  10.     string = string.trim()
  11.     if not string or string == "" then return null
  12.     if string.indexOf("//") == 0 then return null
  13.    
  14.     string = string.replace("\n", """+char(10)+""")
  15.     string = string.replace("\t", """+char(9)+""")
  16.    
  17.     result = ""
  18.     comment = false
  19.     i = 0
  20.     while i < string.len
  21.         ch1 = string[i]
  22.         if i + 1 < string.len then ch2 = string[i + 1] else ch2 = null
  23.        
  24.         if ch1 == """" then
  25.             if comment then comment = false else comment = true
  26.         else if not comment and ch1 == "/" and ch2 == "/" then
  27.             break
  28.         end if
  29.        
  30.         result = result + ch1
  31.         i = i + 1
  32.     end while
  33.    
  34.     return result.trim()
  35. end function
  36.  
  37. result = ""
  38. statements = codeFile.content.split("\n")
  39. startTime = time
  40. for statement in statements
  41.     statement = analyzeLine(statement)
  42.     if not statement then continue
  43.    
  44.     result = result + statement + ";"
  45. end for
  46. endTime = time
  47. print("ElapsedTime - " + (endTime - startTime))
  48.  
  49. dir = parent_path(codeFile.path)
  50. name = "Minified_" + codeFile.name
  51. outFilePath = dir + "/" + name
  52.  
  53. outFile = pc.File(outFilePath)
  54. if not outFile then
  55.     pc.touch(dir, name)
  56.     outFile = pc.File(outFilePath)
  57. end if
  58. if not outFile then exit("Could not create file - '" + outFilePath + "'")
  59.  
  60. outFile.set_content(result)
  61. print("Minified code saved to - '" + outFilePath + "'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement