Advertisement
_LINKI

GreyScript ProjectBuilder + Debugger v0.5

Jul 23rd, 2019
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.32 KB | None | 0 0
  1. // Name - GSProjectBuilder
  2. // Author - LINKI
  3. // Version - 0.5
  4.  
  5. help = "<color=blue><b>GSProjectBuilder</b></color>\n<color=green>   Author</color><color=grey> - </color><color=yellow>LINKI</color>\n<color=green>   Version<color=grey> - </color>0.5</color>\n<color=blue><b>Supported commands:</b></color>\n<color=yellow>   help - <i>print this text</i></color>\n<color=yellow>   init [Path/ProjectName] - <i>create project folder/files</i></color>\n<color=yellow>   remove [ProjectPath] - <i>remove project</i></color>\n<color=yellow>   remove-all [ProjectsPath] - <i>remove all projects in projects folder</i></color>\n<color=yellow>   list [ProjectsFolder] - <i>print all projects in folder</i></color>\n<color=yellow>   compile [ProjectPath] (opt: MainSourceName) - <i>just compile project</i></color>\n<color=yellow>   run [ProjectPath] (opt: params) (opt: MainSourceName) - <i>compile and run project</i></color>\n<color=yellow>   run-debug [ProjectPath] (opt:params) (opt: MainSourceName) - <i>compile project and run debug mode</i></color>\n<color=yellow>   debug [BinaryExec/ProjectPath] - <i>Run project/BinaryExec in debug mode</i></color>"
  6. if params.len < 1 then exit(help)
  7.  
  8. shell = get_shell()
  9. pc = shell.host_computer()
  10.  
  11. error = function(msg, quit = true)
  12.     if quit then ex = @exit else ex = @print
  13.     ex("<color=red><b>" + msg + "</b></color>")
  14. end function
  15. info = function(msg, prefix = "(i)")
  16.     print("<color=yellow><i>" + prefix + msg + "</i></color>")
  17. end function
  18. errorInfo = function(msg, infomsg)
  19.     error(msg, false)
  20.     info(infomsg)
  21.     exit
  22. end function
  23. general = function(msg)
  24.     print("<color=blue>" + msg + "</color>")
  25. end function
  26.  
  27. whoiam = function()
  28.     user = active_user
  29.    
  30.     //root check
  31.     if user == "root" then
  32.         root = true
  33.     else
  34.         result = pc.create_user("whoiam", "-")
  35.         if result == true then
  36.             root = true
  37.             pc.delete_user("whoiam", true)
  38.         else
  39.             root = false
  40.         end if
  41.     end if
  42.     if root then root = "true" else root = "false"
  43.     return [user, root]
  44. end function
  45. runExec = function(exec, runParams = null, debug = false)
  46.     person = whoiam()
  47.     if debug then injectDebug()
  48.    
  49.     info("Runned from " + person[0] + ", root = " + person[1])
  50.     info("Run exec - " + exec)
  51.     stime = time()
  52.     info("StartTime: " + stime)
  53.    
  54.     //shell.launch(exec, runParams)
  55.     if debug then
  56.         if runParams then
  57.             shell.debug_launch(exec, runParams)
  58.         else
  59.             shell.debug_launch(exec)
  60.         end if
  61.     else
  62.         if runParams then
  63.             shell.launch(exec, runParams)
  64.         else
  65.             shell.launch(exec)
  66.         end if
  67.     end if
  68.    
  69.     etime = time()
  70.     info("EndTime: " + etime)
  71.     info("ElapsedTime: " + (etime - stime))
  72. end function
  73. injectDebug = function()
  74.     print("<color=yellow><b>[DebugMode]</b></color>")
  75.    
  76.     shell = get_shell
  77.     pc = shell.host_computer
  78.     router = get_router
  79.     file = pc.File("/")
  80.     crypto = include_lib("/lib/crypto.so")
  81.    
  82.     debugInject = function(sType)
  83.         inject = { }
  84.         for f in sType.values[0]
  85.             inject["debug_" + f.key] = @f.value
  86.         end for
  87.         for f in inject
  88.             sType.values[0][f.key] = @f.value
  89.         end for
  90.         sType.values[0]["info"] = @info
  91.     end function
  92.     debugInject(shell)
  93.     debugInject(pc)
  94.     debugInject(router)
  95.     debugInject(file)
  96.     if crypto then debugInject(crypto)
  97.    
  98.     shell.values[0].connect_service = function(self, ipAddress, port, user, password, serviceId)
  99.         s = ipAddress + ":" + port + ", " + user + ":" + password + ", id="
  100.         result = self.debug_connect_service(ipAddress, port, user, password, serviceId)
  101.         if not serviceId then serviceId = "NotSet"
  102.         s = s + serviceId
  103.         if result then
  104.             self.info("Connected[" + typeof(result) + "] to - " + s)
  105.         else
  106.             self.info("Bad attempt connect to - " + s)
  107.         end if
  108.         return result
  109.     end function
  110.     shell.values[0].start_terminal = function(self)
  111.         self.info("Terminal started")
  112.         return self.debug_start_terminal(self)
  113.     end function
  114.     shell.values[0].scp_upload = function(self, path_orig, path_dest, remote_shell)
  115.         self.info("Send file '" + path_orig + "' to '" + path_dest + "'[reciever]")
  116.         return self.debug_scp_upload(path_orig, path_dest, remote_shell)
  117.     end function
  118.     shell.values[0].build = function(self, pathSource, pathBinary)
  119.         result = self.debug_build(pathSource, pathBinary)
  120.         if result then
  121.             self.info("Compile error in '" + pathSource + "' : " + result)
  122.         else
  123.             self.info("Compiled '" + pathSource + "' to '" + pathBinary + "'")
  124.         end if
  125.         return result
  126.     end function
  127.     shell.values[0].launch = function(self, path, params)
  128.         self.info("Launch '" + path + "' with params=""" + params + """")
  129.         if params then
  130.             return self.debug_launch(path, params)
  131.         else
  132.             return self.debug_launch(path)
  133.         end if
  134.     end function
  135.     shell.values[0].host_computer = function(self)
  136.         pc = self.debug_host_computer()
  137.         self.info("Received Сomputer object, lanIP = " + pc.debug_lan_ip)
  138.         return pc
  139.     end function
  140.    
  141.     pc.values[0].get_ports = function(self)
  142.         result = self.debug_get_ports()
  143.         list = []
  144.         for item in result
  145.             s = item.port_number
  146.             if item.is_closed then s = s + "(closed)"
  147.             list.push(s)
  148.         end for
  149.         self.info("Received ports = " + list)
  150.         return result
  151.     end function
  152.     pc.values[0].File = function(self, path)
  153.         file = self.debug_File(path)
  154.         if file then
  155.             self.info("Received file - '" + path + "'")
  156.         else
  157.             self.info("Bad attempt to get file - '" + path + "'")
  158.         end if
  159.         return file
  160.     end function
  161.     pc.values[0].create_folder = function(self, path, nameFolder)
  162.         result = self.debug_create_folder(path, nameFolder)
  163.         if result then
  164.             self.info("Created folder - '" + path + "/" + name + "'")
  165.         else
  166.             self.info("Bad attempt create folder - '" + path + "/" + name + "'")
  167.         end if
  168.         return result
  169.     end function
  170.     pc.values[0].current_path = function(self)
  171.         result = self.debug_current_path()
  172.         self.info("Received current path - '" + result + "'")
  173.         return result
  174.     end function
  175.     pc.values[0].is_network_active = function(self)
  176.         result = self.debug_is_network_active()
  177.         if result then s = "true" else "false"
  178.         self.info("Received network state = " + s)
  179.         return result
  180.     end function
  181.     pc.values[0].lan_ip = function(self)
  182.         result = self.debug_lan_ip()
  183.         self.info("Received lanIP = " + result)
  184.         return result
  185.     end function
  186.     pc.values[0].touch = function(self, path, nameFile)
  187.         result = self.debug_touch(path, nameFile)
  188.         if result == true then
  189.             self.info("Created file - '" + path + "/" + nameFile + "'")
  190.         else
  191.             self.info("Bad attempt create file - '" + path + "/" + nameFile + "', msg = " + result)
  192.         end if
  193.         return result
  194.     end function
  195.     pc.values[0].show_procs = function(self)
  196.         result = self.debug_show_procs()
  197.         self.info("Received procs")
  198.         return result
  199.     end function
  200.     pc.values[0].network_devices = function(self)
  201.         result = self.debug_network_devices()
  202.         self.info("Received netDevices")
  203.         return result
  204.     end function
  205.     pc.values[0].change_password = function(self, user, password)//Root
  206.         result = self.debug_change_password(user, password)
  207.         if result == true then
  208.             self.info("Changed password - " + user + ":" + password)
  209.         else
  210.             self.info("Bad attempt change password - " + user + ":" + password + ", msg = " + result)
  211.         end if
  212.         return result
  213.     end function
  214.     pc.values[0].create_user = function(self, user, password)//Root
  215.         result = self.debug_create_user(user, password)
  216.         if result == true then
  217.             self.info("Created user - " + user + ":" + password)
  218.         else
  219.             self.info("Bad attempt create user - " + user + ":" + password + ", msg = " + result)
  220.         end if
  221.         return result
  222.     end function
  223.     pc.values[0].delete_user = function(self, user, removeHome)//Root
  224.         result = self.debug_delete_user(user, removeHome)
  225.         if result == true then
  226.             s = ""
  227.             if removeHome then s = ", removed home folder"
  228.             self.info("Deleted user - " + user + s)
  229.         else
  230.             self.info("Bad attempt delete user - " + user + ", msg = " + result)
  231.         end if
  232.         return result
  233.     end function
  234.     pc.values[0].create_group = function(self, username, groupname)//Root
  235.         result = self.debug_create_group(user, groupname)
  236.         if result == true then
  237.             self.info("Added user - '" + username + "' to group - '" + groupname + "'")
  238.         else
  239.             self.info("Bad attempt add user '" + user + "' to group - '" + groupname + "', msg = " + result)
  240.         end if
  241.         return result
  242.     end function
  243.     pc.values[0].delete_group = function(self, username, groupname)//Root
  244.         result = self.debug_delete_group(user, groupname)
  245.         if result == true then
  246.             self.info("Deleted user - '" + username + "' from group - '" + groupname + "'")
  247.         else
  248.             self.info("Bad attempt delete user '" + user + "' from group - '" + groupname + "', msg = " + result)
  249.         end if
  250.         return result
  251.     end function
  252.     pc.values[0].groups = function(self, username)
  253.         result = self.debug_groups(username)
  254.         self.info("Received user - '" + username + "' groups")
  255.         return result
  256.     end function
  257.    
  258.     router.values[0].public_ip = function(self)
  259.         result = self.debug_public_ip()
  260.         self.info("Received publicIP = " + result)
  261.         return result
  262.     end function
  263.     router.values[0].local_ip = function(self)
  264.         result = self.debug_local_ip()
  265.         self.info("Received localIP = " + result)
  266.         return result
  267.     end function
  268.     router.values[0].ping_port = function(self, port)
  269.         result = self.debug_ping_port(port)
  270.         if result then
  271.             self.info("Ping port " + port + ", result = OK")
  272.         else
  273.             self.info("Ping port " + port + ", result = BAD")
  274.         end if
  275.         return result
  276.     end function
  277.     router.values[0].port_info = function(self, port)
  278.         result = self.debug_port_self.info(port)
  279.         self.info("Received port info, port - " + port.port_number)
  280.         return result
  281.     end function
  282.     router.values[0].used_ports = function(self)
  283.         result = self.debug_used_ports()
  284.         list = []
  285.         for item in result
  286.             s = item.port_number
  287.             if item.is_closed then s = s + "(closed)"
  288.             list.push(s)
  289.         end for
  290.         self.info("Received used ports = " + list)
  291.         return result
  292.     end function
  293.     router.values[0].computer_ports = function(self, address)
  294.         result = self.debug_computer_ports(address)
  295.         if result then
  296.             list = []
  297.             for item in result
  298.                 s = item.port_number
  299.                 if item.is_closed then s = s + "(closed)"
  300.                 list.push(s)
  301.             end for
  302.             self.info("Received ports on computer - '" + address + "', ports = " + list)
  303.         else
  304.             self.info("Bad attempt receive ports on computer - " + address)
  305.         end if
  306.         return result
  307.     end function
  308.     router.values[0].computers_lan_ip = function(self)
  309.         result = self.debug_computers_lan_ip()
  310.         self.info("Received computers lanIPs, result = " + result)
  311.         return result
  312.     end function
  313.     router.values[0].essid_name = function(self)
  314.         result = self.debug_essid_name()
  315.         self.info("Received essid name = " + result)
  316.         return result
  317.     end function
  318.     router.values[0].bssid_name = function(self)
  319.         result = self.debug_bssid_name()
  320.         self.info("Received bssid name = " + result)
  321.         return result
  322.     end function
  323.    
  324.     file.values[0].copy = function(self, path, newName)
  325.         result = self.debug_copy(path, newName)
  326.         if result == true then
  327.             self.info("File - '" + self.debug_path + "' copyed to '" + path + "/" + newName + "'")
  328.         else
  329.             self.info("Bad attempt copy file - '" + self.debug_path + "' to '" + path + "/" + newName + "'")
  330.         end if
  331.         return result
  332.     end function
  333.     file.values[0].move = function(self, path, newName)
  334.         old = self.debug_path
  335.         result = self.debug_move(path, newName)
  336.         if result == true then
  337.             self.info("File - '" + old + "' moved to '" + path + "/" + newName + "'")
  338.         else
  339.             self.info("Bad attempt move file - '" + old + "' to '" + path + "/" + newName + "', msg = " + result)
  340.         end if
  341.         return result
  342.     end function
  343.     file.values[0].rename = function(self, newName)
  344.         old = self.debug_name
  345.         self.debug_rename(newName)
  346.         self.info("File renamed from - '" + old + "' to '" + newName + "'")
  347.         return result
  348.     end function
  349.     file.values[0].path = function(self)
  350.         result = self.debug_name()
  351.         self.info("Received path - '" + result + "' from file/folder")
  352.         return result
  353.     end function
  354.     file.values[0].parent = function(self)
  355.         result = self.debug_name()
  356.         self.info("Received parent folder - '" + result.debug_path + "' from file/folder - '" + self.debug_path + "'")
  357.         return result
  358.     end function
  359.     file.values[0].name = function(self)
  360.         result = self.debug_name()
  361.         self.info("Received name - '" + result + "' from file/folder - '" + self.debug_path + "'")
  362.         return result
  363.     end function
  364.     file.values[0].is_folder = function(self)
  365.         result = self.debug_is_folder()
  366.         if result then s = "true" else s = "false"
  367.         self.info("Received 'is_folder' from file/folder - '" + self.debug_path + "', result = " + s)
  368.         return result
  369.     end function
  370.     file.values[0].content = function(self)
  371.         result = self.debug_content()
  372.         if result then
  373.             self.info("Received content from file - '" + self.debug_path + "'")
  374.         else
  375.             self.info("Bad attempt receive content from file - '" + self.debug_path + "'")
  376.         end if
  377.         return result
  378.     end function
  379.     file.values[0].set_content = function(self, content)
  380.         result = self.debug_set_content(content)
  381.         if result == true then
  382.             self.info("Set content in file - '" + self.debug_path + "'")
  383.         else
  384.             self.info("Bad attempt set content in file - '" + self.debug_path + "', msg = " + result)
  385.         end if
  386.         return result
  387.     end function
  388.     file.values[0].is_binary = function(self)
  389.         result = self.debug_is_binary()
  390.         if result then s = "true" else s = "false"
  391.         self.info("Received 'is_binary' from file/folder - '" + self.debug_path + "', result = " + s)
  392.         return result
  393.     end function
  394.     file.values[0].has_permission = function(self, typePerm)
  395.         result = self.debug_has_permission(typePerm)
  396.         if result then s = "true" else s = "false"
  397.         self.info("Received 'has_permission' - '" + typePerm + "' from file/folder - '" + self.debug_path + "', result = " + s)
  398.         return result
  399.     end function
  400.     file.values[0].delete = function(self)
  401.         result = self.debug_delete()
  402.         if result then
  403.             self.info("File/Folder - '" + self.debug_path + "' deleted")
  404.         else
  405.             self.info("Bad attempt delete file/folder - '" + self.debug_path + "', msg = " + result)
  406.         end if
  407.         return result
  408.     end function
  409.     file.values[0].get_folders = function(self)
  410.         result = self.debug_get_folders()
  411.         if result then
  412.             self.info("Received folders(" + result.len + ") from folder - '" + self.debug_path + "'")
  413.         else
  414.             self.info("Bad attempt receive folders from file - '" + self.debug_path + "' :)")
  415.         end if
  416.         return result
  417.     end function
  418.     file.values[0].get_files = function(self)
  419.         result = self.debug_get_files()
  420.         if result then
  421.             self.info("Received files(" + result.len + ") from folder - '" + self.debug_path + "'")
  422.         else
  423.             self.info("Bad attempt receive files from file - '" + self.debug_path + "' :)")
  424.         end if
  425.         return result
  426.     end function
  427.     file.values[0].chmod = function(self, typePerm, isRecursive)
  428.         result = self.debug_chmod(typePerm, isRecursive)
  429.         self.info("Chmoded file/folder - '" + self.debug_path + "', with permissions - '" + typePerm + "'")
  430.         return result
  431.     end function
  432.     file.values[0].permissions = function(self)
  433.         result = self.debug_permissions()
  434.         self.info("Received file/folder permissions - '" + result + "' from '" + self.debug_path + "'")
  435.         return result
  436.     end function
  437.     file.values[0].owner = function(self)
  438.         result = self.debug_owner()
  439.         self.info("Received file/folder owner - '" + result + "' from '" + self.debug_path + "'")
  440.         return result
  441.     end function
  442.     file.values[0].set_owner = function(self, owner, isRecursive)
  443.         result = self.debug_set_owner(owner, isRecursive)
  444.         self.info("Set owner - '" + owner + "' on file - '" + self.debug_path + "'")
  445.         return result
  446.     end function
  447.     file.values[0].group = function(self)
  448.         result = self.debug_group()
  449.         self.info("Received file/folder group - '" + result + "' from '" + self.debug_path + "'")
  450.         return result
  451.     end function
  452.     file.values[0].set_group = function(self, groupname, isRecursive)
  453.         result = self.debug_set_group(groupname, isRecursive)
  454.         self.info("Set group - '" + groupname + "' on file/folder - '" + self.debug_path + "'")
  455.         return result
  456.     end function
  457.     file.values[0].size = function(self)
  458.         result = self.debug_size(self)
  459.         self.info("Received size file/folder - '" + self.debug_path + "' = " + result)
  460.         return result
  461.     end function
  462.    
  463.     crypto.values[0].decipher = function(self, user, encryptPass)
  464.         self.info("Loaded decipher with data = " + user + ":" + encryptPass)
  465.         result = self.debug_decipher(user, encryptPass)
  466.         self.info("Result decipher - " + encryptPass + "==" + result)
  467.         return result
  468.     end function
  469.     crypto.values[0].aircrack = function(self, pathFile)
  470.         self.info("Loaded aircrack with file - '" + pathFile + "'")
  471.         result = self.debug_aircrack(pathFile)
  472.         self.info("Result aircrack = " + result)
  473.         return result
  474.     end function
  475.     crypto.values[0].airmon = function(self, option, device)
  476.         result = self.debug_airmon(option, device)
  477.         self.info("Set airmon config - """ + option + " " + device + """")
  478.         return result
  479.     end function
  480.    
  481.     info("Functions injected")
  482. end function
  483.  
  484. comand = params[0]
  485. if comand == "init" and params.len > 1 then
  486.     path = params[1]
  487.     if path.indexOf("/") == null then error("Bad path - '" + path + "'")
  488.     name = path.split("/")[-1]
  489.     folderPath = parent_path(path)
  490.    
  491.     folder = pc.File(folderPath)
  492.     if not folder or not folder.is_folder or not folder.has_permission("w") then
  493.         folder = pc.File(pc.current_path + "/" + folderPath)
  494.         if not folder or not folder.is_folder or not folder.has_permission("w") then errorInfo("Could not open folder - '<i>" + folderPath + "</i>'","Check path and permision(w) folder")
  495.     end if
  496.    
  497.     pc.create_folder(folder.path, name)
  498.     projectPath = folder.path + "/" + name
  499.     projectFolder = pc.File(projectPath)
  500.     if not projectFolder or not projectFolder.is_folder or not projectFolder.has_permission("w") then errorInfo("Could not create/open folder - '<i>" + projectPath + "</i>'","Check path and permision(w) folder")
  501.    
  502.     pc.create_folder(projectPath, "build")
  503.     pc.touch(projectPath, "Main.src")
  504.     info("Created project - '" + projectPath + "'")
  505.     exit
  506. else if comand == "remove" and params.len > 1 then
  507.     path = params[1]
  508.     folder = pc.File(path)
  509.     if not folder or not folder.is_folder or not folder.has_permission("w") then
  510.         folder = pc.File(pc.current_path + "/" + path)
  511.         if not folder or not folder.is_folder or not folder.has_permission("w") then errorInfo("Could not open folder - '<i>" + path + "</i>'", "Check path and permision(w) folder")
  512.     end if
  513.     path = folder.path
  514.     folder.delete()
  515.     info("Removed project - '" + path + "'")
  516.     exit
  517. else if comand == "remove-all" and params.len > 1 then
  518.     path = params[1]
  519.     folder = pc.File(path)
  520.     if not folder or not folder.is_folder or not folder.has_permission("w") then
  521.         folder = pc.File(pc.current_path + "/" + path)
  522.         if not folder or not folder.is_folder or not folder.has_permission("w") then errorInfo("Could not open folder - '<i>" + path + "</i>'", "Check path and permision(w) folder")
  523.     end if
  524.     path = folder.path
  525.    
  526.     i = 0
  527.     for f in folder.get_folders()
  528.         if not f.has_permission("w") then
  529.             info("Could not delete project - '" + f.path + "'")
  530.         else
  531.             f.delete()
  532.             info("Removed project - '" + f.path + "'")
  533.             i = i + 1
  534.         end if
  535.     end for
  536.     info("Removed " + i + " projects in folder - '" + folder.path + "'")
  537.     exit
  538. else if comand == "list" and params.len > 1 then
  539.     path = params[1]
  540.     folder = pc.File(path)
  541.     if not folder or not folder.is_folder or not folder.has_permission("w") then
  542.         folder = pc.File(pc.current_path + "/" + path)
  543.         if not folder or not folder.is_folder or not folder.has_permission("w") then errorInfo("Could not open folder - '<i>" + path + "</i>'", "Check path and permision(w) folder")
  544.     end if
  545.     path = folder.path
  546.    
  547.     s = "NAME SIZE FILES OWNER"
  548.     for f in folder.get_folders
  549.         s = s + "\n" + f.name + " " + f.size + " " + f.get_files.len + " " + f.owner
  550.     end for
  551.     exit(format_columns(s))
  552. else if comand == "compile" and params.len > 1 then
  553.     run = false
  554.     debug = false
  555.     if params.len > 2 then mainName = params[2] else mainName = "Main"
  556. else if comand == "run" or comand == "run-debug" and params.len > 1 then
  557.     run = true
  558.     if comand == "run-debug" then debug = true else debug = false
  559.     if params.len > 2 then mainName = params[2] else mainName = "Main"
  560.     if params.len > 3 then runParams = params[3:].join(" ") else runParams = null
  561. else if comand == "debug" and params.len > 1 then
  562.     path = params[1]
  563.     if params.len > 2 then runParams = params[2:].join(" ") else runParams = null
  564.     folder = pc.File(path)
  565.     if not folder or not folder.is_folder or not folder.has_permission("x") then
  566.         folder = pc.File(pc.current_path + "/" + path)
  567.         if not folder or not folder.is_folder or not folder.has_permission("x") then
  568.             file = pc.File(path)
  569.             if not file or not file.is_binary or not file.has_permission("x") then
  570.                 file = pc.File(pc.current_path + "/" + path)
  571.                 if not file or not file.is_binary or not file.has_permission("x") then errorInfo("Could not open file/folder - '<i>" + path + "</i>'", "Check path and permission(x) file/folder")
  572.             end if
  573.         end if
  574.     end if
  575.     if folder then
  576.         if folder.is_folder then
  577.             execPath = folder.path + "/build/" + folder.name
  578.             file = pc.File(execPath)
  579.             if not file or not file.is_binary or not file.has_permission("x") then errorInfo("Could not open file - '<i>" + execPath + "</i>'", "Check path and permission(x) file")
  580.         else
  581.             file = folder
  582.         end if
  583.     end if
  584.     runExec(file.path, runParams, true)
  585.     exit
  586. else
  587.     exit(help)
  588. end if
  589.  
  590. //LoadLibFolder
  591. libsFolderPath = "/lib"
  592. libsFolder = pc.File(libsFolderPath)
  593. if not libsFolder or not libsFolder.is_folder or not libsFolder.has_permission("r") then errorInfo("Libs folder not found", "Check permision(r) and folder path - '" + libsFolderPath + "'")
  594.  
  595. //LoadProjectFolder
  596. projectFolderPath = params[1]
  597. projectFolder = pc.File(projectFolderPath)
  598. if not projectFolder or not projectFolder.is_folder or not projectFolder.has_permission("r") or not projectFolder.has_permission("w") then exit("Could not open ProjectFolder", "Check permissions(rw) and folder path - '" + projectFolderPath + "'")
  599. projectFolderPath = projectFolder.path
  600. projectName = projectFolder.name
  601.  
  602. //LoadMainFile
  603. mainFile = pc.File(projectFolderPath + "/" + mainName)
  604. if not mainFile or mainFile.is_folder or mainFile.is_binary then
  605.     mainFile = pc.File(projectFolderPath + "/" + mainName + ".src")
  606.     if not mainFile or mainFile.is_folder or mainFile.is_binary or not mainFile.has_permission("r") then errorInfo("Main file not found", "Check permission(r) and path - '" + mainFilePath + "'")
  607. end if
  608. mainFile = { "File" : mainFile }
  609.  
  610. //UtilFunctions
  611. validFile = function(file)
  612.     return file and (file.is_folder or not file.is_binary) and file.has_permission("r")
  613. end function
  614. loadFile = function(name)
  615.     if name and name[-1] == "*" then
  616.         name = name[0:-2]
  617.         if not name then name = "/"
  618.     else if name == "*" then
  619.         return projectFolder
  620.     end if
  621.    
  622.     file = pc.File(projectFolderPath + "/" + name)
  623.     if not validFile(file) then
  624.         file = pc.File(projectFolderPath + "/" + name + ".src")
  625.         if not validFile(file) then
  626.             file = pc.File(libsFolderPath + "/" + name)
  627.             if not validFile(file) then
  628.                 file = pc.File(libsFolderPath + "/" + name + ".src")
  629.                 if not validFile(file) then
  630.                     file = pc.File(name)
  631.                     if not validFile(file) then
  632.                         file = pc.File(name + ".src")
  633.                         if not validFile(file) then errorInfo("File/Folder - '<i>" + name + "</i>' not found!", "Check permission(read) and file/folder path")
  634.                     end if
  635.                 end if
  636.             end if
  637.         end if
  638.     end if
  639.     return { "File" : file }
  640. end function
  641. loadFilesInFolder = function(folder)
  642.     files = []
  643.     for file in folder.get_files
  644.         if validFile(file) then
  645.             if file.is_folder then
  646.                 files = files + loadFilesInFolder(file)
  647.             else
  648.                 files.push({ "File" : file })
  649.             end if
  650.         end if
  651.     end for
  652.     return files
  653. end function
  654. loadDepends = function(fileWrap)
  655.     importFiles = []; code = fileWrap.File.content.trim()
  656.    
  657.     index = code.indexOf(char(10))
  658.     if index != null then
  659.         fline = code[:index].trim()
  660.         if fline[0] == "#" then
  661.             fileWrap.Content = code[index:].trim()
  662.             splited = fline.split(" ")
  663.             if splited and splited.len > 1 then
  664.                 name = splited[0]
  665.                 args = splited[1:]
  666.                 if name == "#import" then
  667.                     for arg in args
  668.                         file = loadFile(arg)
  669.                         if file.File.is_folder then
  670.                             importFiles = importFiles + loadFilesInFolder(file.File)
  671.                         else
  672.                             importFiles.push(file)
  673.                         end if
  674.                     end for
  675.                 end if
  676.             end if
  677.         end if
  678.     end if
  679.    
  680.     if not fileWrap.hasIndex("Content") then fileWrap.Content = code
  681.     fileWrap.Content = fileWrap.Content.replace("\n", """+char(10)+""").replace("\t", """+char(9)+""")
  682.     fileWrap.Depends = importFiles
  683. end function
  684. build = []
  685. setLevels = function(mainFile, level = 0)
  686.     if not mainFile.hasIndex("Level") or mainFile.Level < level then mainFile.Level = level
  687.     if not mainFile.hasIndex("Depends") then
  688.         build.push(mainFile)
  689.         loadDepends(mainFile)
  690.     end if
  691.    
  692.     for file in mainFile.Depends
  693.         if mainFile.File.path == file.File.path then error("Self recursion found in file - '<i>" + mainFile.File.path + "</i>'")
  694.         if file.hasIndex("Depends") then
  695.             for fd in file.Depends
  696.                 if mainFile.File.path == fd.File.path then error("Recursion found in files: '<i>" + mainFile.File.path + "</i>' and '<i>" + fd.File.path + "</i>'")
  697.             end for
  698.         end if
  699.         if not file.hasIndex("Level") or file.Level < level + 1 then setLevels(file, level + 1)
  700.     end for
  701. end function
  702. //SetLevels&LoadDepends
  703. setLevels(mainFile)
  704. //SortBuild
  705. build.sort("Level")
  706. build.reverse()
  707.  
  708. //ImportFiles
  709. info("Imports:")
  710. content = ""; i = 0
  711. while i < build.len
  712.     info("Imported - '" + build[i].File.path + "'", "")
  713.     content = content + build[i].Content + "\n"
  714.     i = i + 1
  715. end while
  716. content = content[0:-2]
  717. info("Imported " + build.len + " files")
  718.  
  719. //Create/Get BuildFolder
  720. buildPath = projectFolder.path + "/build"
  721. if not pc.File(buildPath) then pc.create_folder(projectFolder.path, "build")
  722.  
  723. //CreateOutFile
  724. outFileName = projectFolder.name + ".src"
  725. outFilePath = buildPath + "/" + outFileName
  726. outFile = pc.File(outFilePath)
  727. if not outFile or outFile.is_folder or outFile.is_binary then
  728.     pc.touch(buildPath, outFileName)
  729.     outFile = pc.File(outFilePath)
  730. end if
  731. if not outFile or outFile.is_folder then error("Could not create file - '<i>" + outFilePath + "</i>'")
  732. outFile.set_content(content)
  733.  
  734. //Compile
  735. msg = shell.build(outFile.path, buildPath)
  736. if msg then error("Compile error : " + msg)
  737. info("Project compiled to '" + buildPath + "'")
  738.  
  739. //Run
  740. if run then
  741.     print
  742.     runExec(buildPath + "/" + projectFolder.name, runParams, debug)
  743. end if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement