Thursday, August 27, 2009

Code Launcher

Requires: Windows (2000 and higher) and Ruby

When I first started playing with lots of languages two years ago, I built a simple extensible ruby script for myself that compiles/executes source code. I called it "Code Launcher." I was using Textpad mostly at that time, so I set up Textpad to run codeLauncher on the active file with the press of "ctrl-1". "Code Launcher" has a different effect depending on the extension of the currently opened file. For example, I have HTML files open in firefox, ruby code run through a batch file (so it can have a guaranteed pause), and java code compiled and then run through a batch file. It's a simple idea, but it was also a really useful idea that I still use for quick coding. Here's the code for it... the nicest thing about it is that you can add and take from it freely and quickly. Build in special cases for yourself and whatnot.

  1. # This program was created for use in TextPad, so that I could add this  
  2. # as a user tool which would do exactly what I wanted to the current open file.  
  3. # This way, I didn't need a different user tool (and different key shortcut)   
  4. # for every different kind of file.  I did this in Ruby because I had been learning   
  5. # it and liking it a lot at the time.  
  6. #  
  7. # The current problem with it is that there can not be more than one parameter.  
  8. #  
  9. # This program requires ruby 1.8 and AutoIt3 installed.  
  10. # 03/20/07  
  11.   
  12. require 'win32ole'  
  13.   
  14. class CodeLauncher  
  15.     def initialize( fileParam, promptForParams = false, browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" )  
  16.         params = ""  
  17.         exit if( !fileParam )  
  18.   
  19.         #Get parameters if parameters are asked for  
  20.         if( promptForParams )   
  21.             require 'getParams.rb'  
  22.             app = FXApp.new("Get Parameters""FXRuby")  
  23.             gp = GetParams.new(app)  
  24.             app.create  
  25.             app.run  
  26.             params = gp.param  
  27.             params = "\" \""+params+"\""  
  28.         end  
  29.   
  30.         #parse out the file name  
  31.         if( fileParam =~ /(.*)\\(.*)\Z/ )  
  32.             fileDir = $1  
  33.             fileName = $2  
  34.         else  
  35.             exit  
  36.         end  
  37.         extension = $2 if( fileName =~ /(.*)\.(.*)\Z/ )  
  38.   
  39.         #Special Cases (if any, for quick parameter testing)  
  40.   
  41.         #Extension based general executions/compilations  
  42.   
  43.         # Ruby, simpy open using a batch file (for the pause effect)  
  44.         if( extension == "rb" || extension == "rbw")  
  45.             runInCmd( fileDir, fileName, params, true )  
  46.         end  
  47.   
  48.         # Java, compile the code an run in a batch file (for the pause effect)  
  49.         if( extension == "java" )  
  50.             runInCmd( fileDir, "javac\" \""+fileName, ""false )  
  51.             runInCmd( fileDir, "java\" \""+fileName.chomp(".java"), params, true )  
  52.         end  
  53.   
  54.         # HTML, open the page in the browser  
  55.         if( extension == "html" )  
  56.             openInBrowser( browser, fileDir+"\\"+fileName )  
  57.         end  
  58.   
  59.         # PHP, open the page in the browser  
  60.         if( extension == "php" )  
  61.             openInBrowser( browser, fileDir+"\\"+fileName )  
  62.         end  
  63.   
  64.         # JSP, open the page in the browser  
  65.         if( extension == "jsp" )  
  66.             openInBrowser( browser, fileDir+"\\"+fileName )  
  67.         end  
  68.   
  69.         # batch, simply open the batch file  
  70.         if( extension == "bat" )  
  71.             runInCmd( fileDir, fileName, params, false )  
  72.         end  
  73.     end  
  74.   
  75.     #Run the code  
  76.     def runAutoIT3( cmdString )  
  77.         au3 = WIN32OLE.new("AutoItX3.Control")  
  78.         au3.opt("WinTextMatchMode", 2)  
  79.         au3.RunWait( cmdString )  
  80.     end  
  81.   
  82.     def openInBrowser( browser, url )  
  83.         runAutoIT3( browser+" \""+url+"\"" )  
  84.     end  
  85.   
  86.     def runInCmd( fileDir, command, params, leaveOpen )  
  87.         cmdString = ""  
  88.         if( leaveOpen )  
  89.             cmdString = "cmd.exe /c \"codeLauncherPause.bat \"" + fileDir + "\" \""+ command + params + "\"\""  
  90.         else  
  91.             cmdString = "cmd.exe /c \"cd " + fileDir + " && \"" + command + params + "\"\""  
  92.         end  
  93.         runAutoIT3( cmdString )  
  94.     end  
  95. end  
  96.   
  97. if __FILE__ == $0  
  98.     #Get Parameters  
  99.       
  100.     filename = ARGV[0]  
  101.     exit if( !filename )  
  102.     browser = ARGV[1]  
  103.       
  104.     browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" if( !browser )  
  105.       
  106.     #execute  
  107.     CodeLauncher.new( filename, false, browser )  
  108.     exit  
  109. end  
  1. cd %1  
  2. %2 %3  
  3. pause  
And setting CodeLauncher to be run through "ctrl-1" in Textpad is pretty easy. Go to configure, then select preferences, then select tools. Add a DOS Command, name it whatever you want. I called mine Code Launcher.

Click Apply, then expand Tools. For Parameters, type "codeLauncher.rb $File". For Initial Folder, enter the full path to the codeLauncher.rb script. Make sure "Close DOS window on exit" is checked. Then click OK. You're all set.



As a parting note, make sure whatever you're using to develop code with does code launching (maybe build/execute is a better term) and make sure you tailor fit it to your usage. Any decent development environment does... F5 is a common key to try. This is really basic funcitonality that makes a developer's life tons easier.

No comments: