Now that Premake 4.0 is out the door, I am taking a pass through my website backlog before beginning a crash course in Mac OS X programming. I thought I might get a jump start on the latter by configuring Xcode — "Apple's premiere development environment for Mac OS X" — for Drupal development, and ramp up on this new-to-me IDE while working on the familiar Drupal codebase. I'm glad I did, for it all turned out rather well.
PHP isn't a complicated development environment. There is no concept of targets or compiling, so it is really just a matter of configuring the project browser and text editor.
I'm running Xcode 3.1, which came as part of the iPhone dev kit. As such, these instructions are likely to be specific to Xcode 3.0 or later (if someone tries it with 2.x let me know how it works out, and if you needed to change anything).
The first step is to register the Drupal file extensions, such as .module and .install, as PHP file types. This will enable Xcode to provide the proper syntax coloring (and possibly other language specific features; I'm still figuring that part out).
I've done the legwork on this one and, after a bit of poking around in the Xcode internals, put together a Drupal specification file. Download drupal.pbfilespec and place it in ~/Library/Application Support/Developer/Shared/Xcode/Specifications. This is a standard OS X property list, which you can view or modify with the Property List Editor. Depending on your codebase, you may want to add more file extensions, such as .test. One curious caveat: if you change this file you will need to not only restart Xcode, but also recreate any existing projects in order to pick up the changes. Fortunately, creating the project is simple and quick, as you'll see below.
The next step is setting up the code formatting to match Drupal's two-space indentation standard. Unfortunately, Xcode is not flexible in this regard: there is a global setting, accessible at Preferences... > Indentation, and a per-file setting, available at View > Text > Tab Settings.... Pick your poison. Here I miss Visual Studio, which enables different settings per language.
I happen to be a big fan of tabs myself, and since Drupal development is only a small part of my work I've taken the per-file approach. To make sure I stick to the standards I use Drupal's Coder module, which I recommend.
That's all the setup. Now you can create a new project in Xcode using File > New Project.... Choose the Other group and Empty Project. Save it where you want it. Then choose Project > Add to Project... and choose all the files and folders at the root level of your Drupal installation (or whatever subset makes sense to you) and click Add. You can keep the defaults for the References dialog -- to be honest I'm not sure what all these settings mean quite yet, but the defaults are working fine for me.
Then, in the Xcode project outline, select all of the files and folders at the top level of the project. Hit Command+I to bring up the Inspector and set the tab settings as described above. These will be applied recursively to all of the objects in the projects.
And that's it! You might try playing with the different layouts in Preferences > General to see which you like best; I've settled on "All-In-One".
If, like me, you are just getting started with Xcode, these links might be helpful. I'll update the list as I discover new ones and please, if you know of a helpful site post it in the comments below. And be sure to check out the built-in help, which is actually pretty good.
I found these sites useful in figuring out the Xcode internals for this article:
- Xcode's Plugin Interface
- Xcode 3 C# syntax coloring
- Xcode Plugin Development
- Script-Fu syntax highlighting for Xcode
| Attachment | Size |
|---|---|
| drupal.pbfilespec | 453 bytes |

Thanks!
Brilliant intro. I've just started doing drupal dev and use xcode as my default editor, this is gonna help me loads!
Thanks for posting this.
The idea of using Xcode with Drupal was lurking around in the back of my head. I've been having trouble getting Eclipse set due the error "Standard VM not supported on MacOS", even though the standard VM came bundled with Mac OS X 10.5.7.
I'll let you know if i have better success with Xcode.
Starkos,
I've installed Xcode and it appears that the following path does not exist:
~/Library/Application Support/Developer/Shared/Xcode/Specifications
Any suggestions?
Thanks.
Assuming you've got the main part of the path ...Developer/Shared/Xcode go ahead and create the Specifications folder.
I now realize that you must create the directory yourself. I came across this article: Exending Xcode - File & Project Templates, TextMate Style Macros, which says:
When Xcode boots it scans through a few different directories to put together its runtime environment. One of the last directories that it scans is ~/Library/Application Support/Developer/Shared/Xcode. This directory contains user specific Xcode extensions. If this directory does not exist, you will have to create it. This is where you will place project templates, file templates, and macro specifications. (emphasis mine)
Once I placed the file drupal.pbfilespec in this directory and started up Xcode, i was able to see text.script.php and text.script.php.drupal.
However, when reviewing file types, i do not see *.module, *.install nor *.inc. Should i be seeing something like text.script.php.drupal.module?
Thanks, Starkos.
I didn't see your reply until after i posted my additional comment.
Have you actually been able to debug your drupal code in xCode?
I can't figure how to do this. It appears that i may need a target and/or an executable.
In Eclipse you would point to a PHP Executable. Would this be the same in xCode.
What about the target? Would this be index.php?
Would a php project be similar to an Applescript project?
Apple mentions php, but gives no details on setting up a project, on debugging, etc.
Thanks.
Hi Starkos.
I didn't get anywhere after i got drupal.pbfilespec set up on the correct directory.
For example, clicking on index.php in eclipse was enough to get the debugger started. No such luck here. I can't seem to designate index.php as a target nor as an executable.
When I look at all my files from drupal, there is no hierarchy of directories; there just seems to be one flat folder with all the files.
Strange stuff. Hard to believe that xcode won't offer any suggestions on setting a PHP/Drupal project.
At the moment, i'm just using Komodo Edit and $output .= dsm($something).
How about yourself? Have you gotten anywhere?
As far as I know, you can't run/debug PHP from Xcode, you can only use it as an editor (like Komodo Edit).
Use the project outline, in the left sidebar, to navigate the file hierarchy, just like in Eclipse. That weird pane above the editor shows all of the files at or below your current location in the outline. It doesn't make any sense to me, and I just hide it to make more room for the editor—just drag the splitter up to the top of the window.
I'm not doing very much PHP development right now, mostly just tweaks to Drupal, so Xcode has been sufficient for me.
Have you made any more headway, Starkos?
Has anyone tried with Xcode 4? It doesn't seem to be working.
Unfortunately, Xcode4 doesn't support the language spec files any longer, and there doesn't seem to be any replacement for it. So…SOL. At the moment I'm using MonoDevelop, because I happened to have it installed.