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.
- # This program was created for use in TextPad, so that I could add this
- # as a user tool which would do exactly what I wanted to the current open file.
- # This way, I didn't need a different user tool (and different key shortcut)
- # for every different kind of file. I did this in Ruby because I had been learning
- # it and liking it a lot at the time.
- #
- # The current problem with it is that there can not be more than one parameter.
- #
- # This program requires ruby 1.8 and AutoIt3 installed.
- # 03/20/07
- require 'win32ole'
- class CodeLauncher
- def initialize( fileParam, promptForParams = false, browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" )
- params = ""
- exit if( !fileParam )
- #Get parameters if parameters are asked for
- if( promptForParams )
- require 'getParams.rb'
- app = FXApp.new("Get Parameters", "FXRuby")
- gp = GetParams.new(app)
- app.create
- app.run
- params = gp.param
- params = "\" \""+params+"\""
- end
- #parse out the file name
- if( fileParam =~ /(.*)\\(.*)\Z/ )
- fileDir = $1
- fileName = $2
- else
- exit
- end
- extension = $2 if( fileName =~ /(.*)\.(.*)\Z/ )
- #Special Cases (if any, for quick parameter testing)
- #Extension based general executions/compilations
- # Ruby, simpy open using a batch file (for the pause effect)
- if( extension == "rb" || extension == "rbw")
- runInCmd( fileDir, fileName, params, true )
- end
- # Java, compile the code an run in a batch file (for the pause effect)
- if( extension == "java" )
- runInCmd( fileDir, "javac\" \""+fileName, "", false )
- runInCmd( fileDir, "java\" \""+fileName.chomp(".java"), params, true )
- end
- # HTML, open the page in the browser
- if( extension == "html" )
- openInBrowser( browser, fileDir+"\\"+fileName )
- end
- # PHP, open the page in the browser
- if( extension == "php" )
- openInBrowser( browser, fileDir+"\\"+fileName )
- end
- # JSP, open the page in the browser
- if( extension == "jsp" )
- openInBrowser( browser, fileDir+"\\"+fileName )
- end
- # batch, simply open the batch file
- if( extension == "bat" )
- runInCmd( fileDir, fileName, params, false )
- end
- end
- #Run the code
- def runAutoIT3( cmdString )
- au3 = WIN32OLE.new("AutoItX3.Control")
- au3.opt("WinTextMatchMode", 2)
- au3.RunWait( cmdString )
- end
- def openInBrowser( browser, url )
- runAutoIT3( browser+" \""+url+"\"" )
- end
- def runInCmd( fileDir, command, params, leaveOpen )
- cmdString = ""
- if( leaveOpen )
- cmdString = "cmd.exe /c \"codeLauncherPause.bat \"" + fileDir + "\" \""+ command + params + "\"\""
- else
- cmdString = "cmd.exe /c \"cd " + fileDir + " && \"" + command + params + "\"\""
- end
- runAutoIT3( cmdString )
- end
- end
- if __FILE__ == $0
- #Get Parameters
- filename = ARGV[0]
- exit if( !filename )
- browser = ARGV[1]
- browser = "C:\\Program Files\\Mozilla Firefox\\firefox.exe" if( !browser )
- #execute
- CodeLauncher.new( filename, false, browser )
- exit
- end
- cd %1
- %2 %3
- pause
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:
Post a Comment