Search code examples
xcodexcode4

Unlock Xcode Project File for Find and Replace


I make common use of Xcode's workspace Find and Replace. It's very powerful when combined with regex.

However, I've got a workspace with 30-40 projects and a large change to the build settings has just been required. I expected to hop into Xcode, and put some regex to use to change the build setting for all projects when I was hit with a block. All my Xcode project files are locked. So the regex find works great, but I have no way of replacing. I can't even preview it. It is only affecting project files that I can see. I opened Finder to check permissions and they are wide open and the OSX file lock is off on all of them. So this seems to be an Xcode locking issue. I tried to lock/unlock the project files the way you would a normal file (File > Unlock...) in Xcode but these options are grayed out for projects.

Anyone encounter this before? Any possible solutions before I have to hand update build settings for 30+ projects?


Solution

  • I did some more digging on this question to confirm or refute my hypothesis from the comments above. Xcode does indeed work directly against the on-disk data for the files involved in find/replace activities. Filesystem events suggest that the find/replace operation opens the .pbxproj file and begins regex activities from top to bottom working through the file and group references to the build configurations and settings sections ultimately applying the search to the on-disk data pointed to by each of the entries in the .pbxproj

    Naturally, depending on the regex the user enters, it would be entirely possible to match content exactly at the cursor's current location in the search and trigger a replacement operation that would destroy the current search's context. This would have the most likely effect of terminating the search prematurely, but could also trigger an infinitely long find/replace operation (ex. find '.' replace with 'aa') if the search wasn't well contained. To stem off the entire class of 'self eating snake' problems when it comes to this Xcode find/replace behavior, it seems that Apple has elected to lock the .pbxproj for the duration of the search operation. While this does mean that you can't use Xcode's Find/Replace regex tools, there are a number of great text editors out there (even free ones!) that allow you to do regex find/replace via a Multi-file search (TextWrangler and BBEdit). These editors allow you to filter your search to a file pattern (say *.pbxproj) in subfolders of your master project. Depending on how your projects are organized on disk, pick a search starting at a common ancestor and you can walk your way through the find/replace operation just as you would in Xcode. In fact, the same regex strings you identified in Xcode can be used in these tools to affect the change(s) you need.

    As with most massive or autonomous edits, make sure you keep backups of the files you intend to edit (this goes at least doubly so for you since your projects are entirely local, non-source controlled projects) just in case you need to rollback your changes. Good luck and let us know which avenue you wound up taking and how things went.