Long-time reader Christian asked via the Suggestion Box for some examples of useful IDE scripting. Specifically, he was wondering how you'd automate a build, and perform a bit of file maintenance after the fact. Well, you're in luck, because I just so happen to have a bit of insights and useful tips on the topic! :-)
To perform a build, you're going to want to use the BuildApp method, provided by the context object. This method takes an integer platform parameter, and it returns to you a string to the built application on disk, and it's duty is to synchronously build the application you're asking for. This is going to be the cornerstone API of any IDE script that wants to automate builds, as you can imagine.
The other helpful API we're going to use is the DoShellCommand method. It takes a shell command as a parameter, and returns to you a string holding the results of the operation. This is what we're going to use to do the file maintenance after the application is built.
My example script is going to be very simple -- it's going to copy a few files from the root volume on Windows to next to the built application. It should be trivial for you to extrapolate more useful applications of this for your own projects. Without further ado:
// Pulled these constants right out of the LR entry for IDE Script
Const kBuildForWindows = 3
Const kBuildForOSX = 8
// Performs a build operation, and then copies a file in from the given path
Sub DoBuild( platform as Integer, copyPath as String )
dim path as String
// Build for the platform given, and get the path to the executable back
path = BuildApp( platform )
if path <>"" then
// Get the folder the executable file lives in
dim parts() as String
parts = Split( path, "\" )
// Strip off the executable name itself
parts.Remove( UBound( parts ) )
// Put them all back together, and include the last path
// separator at the end (so we have a valid folder path)
path = Join( parts, "\" )
// Now you can do your shell command
call DoShellCommand( "copy /b /y " + copyPath + " " + path )
end if
End Sub
DoBuild( kBuildForWindows, "c:\config.sys" )
DoBuild( kBuildForOSX, "c:\autoexec.bat" )
Glorious, isn't it? Probably not, but it should be a good example to start from.
Before I close, I'd like to discuss some interesting pieces of information that are easily overlooked when doing IDE scripts. The first one is the return value from BuildApp -- it gives you the path back to the executable file itself, or it returns an empty string if the build fails. So you can use this to get back an important distinction; did the build succeed or fail. The other tidbit has to do with DoShellCommand. There are some helpful environment variables set up for you by the command: IDE_PATH, PROJECT_FILE and PROJECT_PATH. Unfortunately, as of this writing (2008r3.1), they don't work on Windows (but do on Mac and Linux). The method itself also takes an optional timeout parameter, in case you don't want to wait too long for a command to finally fail.
Hopefully you'll find this little introduction to IDE scripting helpful. We designed it to make automating builds as easy as possible, and that's what we've been using it for internally for a long time. Now, you can do the same!
So how close has the team gotten to a "one step" (ha!) build process for RB?
I'm probably 90% there now on one of my primary projects. I'm actually kind of surprised how much better it feels being able to put all the right files together and build an installer as part of an integrated build process. It also forced me to standardize some steps that were previously a little too much "magic" and not enough "process" :)
@Travis -- Nathan (our amazing build engineer) can probably answer this better than I can, but I know we don't have it down to one step yet. :-P We have a lot of strange build requirements because of the bootstrapping. But it's somewhat automated, that much I am certain of.
Very cool. Thanks Aaron! I'll have to tinker with this this weekend. I have a number of builds where after the app is compiled, I need to open up the bundle and copy some files in there. Automating that would be a great help.
I've been evaluating RB for a little while now and I've been hunting this down for a while. I was wondering if there's a way to execute scripts like that from the command-line. I.E. I'd like to be able to have a cron job run a RBScript for automated builds and whatnot. Hope it's not impertinent to ask here.
Thanks.
@Michael -- there's a stand-alone application in the Extras folder for executing IDE scripts from a command line. From what I recall, it essentially launches the IDE, starts up an IPC connection, and passes the script to the IDE. The source code is available for it, too, from what I recall. So if it doesn't meet your needs, you can modify it however you'd like.
*blink* Woah. Thanks. Seems that I didn't RTFM. *grimace* Sorry 'bout that. I'll give it a shot.