Guest User

Untitled

a guest
Mar 19th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.85 KB | None | 0 0
  1. #sublime text 2 plugin
  2. #xorc1zt
  3. #2012
  4.  
  5. import sublime
  6. import sublime_plugin
  7. import subprocess
  8. import os
  9.  
  10. COMPILER_READY  = False
  11. compilerprocess = None
  12.  
  13. def has_file_ext(view, ext):
  14.     """Returns ``True`` if view has file extension ``ext``.
  15.    ``ext`` may be specified with or without leading ``.``.
  16.    """
  17.     if not view.file_name(): return False
  18.     if not ext.strip().replace('.', ''): return False
  19.    
  20.     if not ext.startswith('.'):
  21.         ext = '.' + ext
  22.  
  23.     return view.file_name().endswith(ext)
  24.  
  25. def SendCommandLine( process, bytestring ):
  26.   process.stdin.write( bytestring )
  27.   process.stdin.flush()
  28.  
  29. def GetOutputSucces( process ):
  30.   while True:
  31.     line = process.stdout.readline()
  32.  
  33.     print( line )
  34.    
  35.     if line == b'SUCCESS\r\n':
  36.       break
  37.  
  38. def GetOutputComplete( process ):
  39.   while True:
  40.     line = process.stdout.readline()
  41.  
  42.     print( line )
  43.    
  44.     if line == b'OUTPUT\tCOMPLETE\r\n':
  45.       break
  46.  
  47. def GetStatus( process ):
  48.     while True:
  49.         line = process.stdout.readline()
  50.         print( line )
  51.  
  52.         if line == b'READY\r\n':
  53.             retval = 1
  54.             break
  55.         if line.find( b'ERROR' ) != -1:
  56.             retval = 2
  57.             break
  58.  
  59.     return retval
  60.  
  61. def StartCompiler():
  62.     global COMPILER_READY
  63.  
  64.     process = subprocess.Popen(['pbcompiler','/STANDBY'],
  65.                         stdin=subprocess.PIPE,
  66.                         stdout=subprocess.PIPE,
  67.                         stderr=subprocess.STDOUT,
  68.                         shell=False
  69.                         )
  70.  
  71.     status = GetStatus( process )
  72.  
  73.     if status == 1:
  74.         COMPILER_READY = True
  75.     elif status == 2:
  76.         COMPILER_READY = False
  77.         return False
  78.  
  79.     return process
  80.  
  81. def StopCompiler( process ):
  82.     SendCommandLine( process, b'END\n' )
  83.  
  84. def DoCompilation( process ):
  85.     view        = sublime.active_window().active_view()
  86.     settings    = view.settings()
  87.     sourcename  = view.file_name()
  88.     targetname  = sourcename.strip().replace('.pb', '.exe')
  89.     SendCommandLine( process, b'SOURCE\t'+sourcename+b'\n')
  90.     SendCommandLine( process, b'TARGET\t'+targetname+b'\n')
  91.     SendCommandLine( process, b'COMPILE\n')
  92.     line = process.stdout.readline()
  93.     print(line)
  94.  
  95.     if line == b'SUCCESS\r\n':
  96.         print("COMPILATIONSUCCES")
  97.  
  98.     print(b'SOURCE\t'+sourcename+b'\n')
  99.     print(b'SOURCE\t'+targetname+b'\n')
  100.     #GetOutputComplete( process )
  101.  
  102. class MakepbCommand(sublime_plugin.ApplicationCommand):
  103.     """Compile a purebasic file with flags
  104.    """
  105.     def is_enabled(self):
  106.         return has_file_ext( sublime.active_window().active_view(), '.pb' )
  107.  
  108.     def run(self, command=''):
  109.         global COMPILER_READY
  110.         global compilerprocess
  111.  
  112.         if( compilerprocess == None ) or ( compilerprocess.poll()!=None ) :
  113.             compilerprocess = StartCompiler()
  114.  
  115.         if COMPILER_READY:
  116.             DoCompilation( compilerprocess )
  117.        
  118.         if command !='':
  119.             SendCommandLine( compilerprocess, command )
  120.             GetOutputComplete( compilerprocess )
  121.         #StopCompiler( compilerprocess)
  122.  
  123. class buildpbCommand(sublime_plugin.WindowCommand):
  124.     """Compile a purebasic file with flags
  125.    """
  126.     def is_enabled(self):
  127.         return has_file_ext( self.window.active_view(), '.pb' )
  128.  
  129.     def run(self, cmd = [], file_regex = "", line_regex = "", working_dir = "",
  130.             encoding = "utf-8", env = {}, quiet = False, kill = False,
  131.             # Catches "path" and "shell"
  132.             **kwargs):
  133.         view = self.window.active_view()
  134.         targetname = view.file_name()
  135.  
  136.         print("sdfssdfsdf")
  137.  
  138.         if not view.settings().get('pbcompiler_autoexec') and not view.settings().get('pbcompiler_dll'):
  139.             cmd.append("/EXE")
  140.             cmd.append(targetname.strip().replace('.pb', '.exe'))
  141.  
  142.         if view.settings().get('pbcompiler_debugger') and not view.settings().get('pbcompiler_dll'):
  143.             cmd.append("/DEBUGGER")
  144.  
  145.         if view.settings().get('pbcompiler_dll'):
  146.             cmd.append("/DLL")
  147.             cmd.append("/EXE")
  148.             cmd.append(targetname.strip().replace('.pb', '.dll'))
  149.  
  150.         if view.settings().get('pbcompiler_console'):
  151.             cmd.append("/CONSOLE")
  152.            
  153.         if view.settings().get('pbcompiler_quiet'):
  154.             cmd.append("/QUIET")
  155.            
  156.         if view.settings().get('pbcompiler_commented'):
  157.             cmd.append("/COMMENTED")
  158.            
  159.         if view.settings().get('pbcompiler_inlineasm'):
  160.             cmd.append("/INLINEASM")
  161.            
  162.         if view.settings().get('pbcompiler_linenumbering'):
  163.             cmd.append("/LINENUMBERING")
  164.            
  165.         if view.settings().get('pbcompiler_dynamiccpu'):
  166.             cmd.append("/DYNAMICCPU")
  167.            
  168.         if view.settings().get('pbcompiler_xp'):
  169.             cmd.append("/XP")
  170.  
  171.         if view.settings().get('pbcompiler_administrator'):
  172.             cmd.append("/ADMINISTRATOR")
  173.    
  174.         if view.settings().get('pbcompiler_user'):
  175.             cmd.append("/USER")
  176.    
  177.         if view.settings().get('pbcompiler_thread'):
  178.             cmd.append("/THREAD")
  179.  
  180.         if view.settings().get('pbcompiler_unicode'):
  181.             cmd.append("/UNICODE")
  182.  
  183.         if view.settings().get('pbcompiler_subsystem')==1:
  184.             cmd.append("/SUBSYSTEM")
  185.             cmd.append("opengl")
  186.         elif view.settings().get('pbcompiler_subsystem')==2:
  187.             cmd.append("/SUBSYSTEM")
  188.             cmd.append("winnt")
  189.  
  190.         args =  {
  191.                 "cmd": cmd,
  192.                 "file_regex": file_regex,
  193.                 "line_regex": line_regex,
  194.                 "working_dir": working_dir,        
  195.                 "encoding": encoding,
  196.                 "env": env,
  197.                 "quiet": quiet,
  198.                 "kill": kill,
  199.                 }
  200.  
  201.  
  202.         self.window.run_command("exec", args )
Advertisement
Add Comment
Please, Sign In to add comment