KISS

Be Among the 20% of the Best!

Renaming Xcode Project

| comments

I’ve used shell commands to rename projects a few times, and it worked better than renaming from Xcode itself. Here are the steps (given we want to rename warnings_test => BestAppEver) (you may need to install a few extra tools with brew install rename ack):

  1. Find all files with name containing the source string:
    1
    2
    3
    4
    5
    6
    7
    
    $ find . -name 'warnings_test*'
    ./warnings_test
    ./warnings_test.xcodeproj
    ./warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_test.xcscheme
    ./warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_testTests.xcscheme
    ./warnings_testTests
    ./warnings_testTests/warnings_testTests.m
    
  2. Close the project in Xcode/AppCode.
  3. Rename those files and directories:
    1
    2
    3
    4
    
    $ find . -name 'warnings_test*' -print0 | xargs -0 rename --subst-all 'warnings_test' 'BestAppEver'
    Can't rename './warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_test.xcscheme' to './BestAppEver.xcodeproj/xcshareddata/xcschemes/BestAppEver.xcscheme': No such file or directory
    Can't rename './warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_testTests.xcscheme' to './BestAppEver.xcodeproj/xcshareddata/xcschemes/BestAppEverTests.xcscheme': No such file or directory
    Can't rename './warnings_testTests/warnings_testTests.m' to './BestAppEverTests/BestAppEverTests.m': No such file or directory
    
    You’ll need to run this command a couple of times, because directories will be renamed first, then files and directories inside those will be renamed on the next iteration. Check with the step 1 if all the files are renamed (should see empty output):
    1
    
    $ find . -name 'warnings_test*' -print0 | xargs -0 rename --subst-all 'warnings_test' 'BestAppEver'
    
  4. Find all occurrences of the string in all the files:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    $ ack --literal 'warnings_test'
    BestAppEver/AppDelegate.h
    3://  warnings_test
    
    BestAppEver/AppDelegate.m
    3://  warnings_test
    
    BestAppEver/Base.lproj/LaunchScreen.xib
    20:                <label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="warni
    ngs_test" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-
    c2-rCX">
    
    BestAppEver/main.m
    3://  warnings_test
    
    /// …skipped the rest…
    
    Look through the output to make sure all those string should be replaced. In most cases, they should.
  5. Replace all occurrences:
    1
    
    $ ack --literal --files-with-matches 'warnings_test' --print0 | xargs -0 sed -i '' 's/warnings_test/BestAppEver/g'
    
    One run is enough, there should be no output. To verify, run the command in step 3 again, you should see empty output.
  6. Open the renamed project:
    1
    
    $ open BestAppEver.xcodeproj
    
    Builds great!

Done! All your targets, schemes, files, mentions in comments, identifiers, names, etc. have been renamed. If you git add . and git status, you should see a lot of renamed: entries (just another sanity check):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ git status
## master
R  warnings_test.xcodeproj/project.pbxproj -> BestAppEver.xcodeproj/project.pbxproj
R  warnings_test.xcodeproj/project.xcworkspace/contents.xcworkspacedata -> BestAppEver.xcodeproj/project.xcworkspace/contents.xcworkspacedata
R  warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_test.xcscheme -> BestAppEver.xcodeproj/xcshareddata/xcschemes/BestAppEver.xcscheme
R  warnings_test.xcodeproj/xcshareddata/xcschemes/warnings_testTests.xcscheme -> BestAppEver.xcodeproj/xcshareddata/xcschemes/BestAppEverTests.xcscheme
R  warnings_test/AppDelegate.h -> BestAppEver/AppDelegate.h
R  warnings_test/AppDelegate.m -> BestAppEver/AppDelegate.m
R  warnings_test/Base.lproj/LaunchScreen.xib -> BestAppEver/Base.lproj/LaunchScreen.xib
R  warnings_test/Base.lproj/Main.storyboard -> BestAppEver/Base.lproj/Main.storyboard
R  warnings_test/Images.xcassets/AppIcon.appiconset/Contents.json -> BestAppEver/Images.xcassets/AppIcon.appiconset/Contents.json
R  warnings_test/Info.plist -> BestAppEver/Info.plist
R  warnings_test/ViewController.h -> BestAppEver/ViewController.h
R  warnings_test/ViewController.m -> BestAppEver/ViewController.m
R  warnings_test/main.m -> BestAppEver/main.m
R  warnings_testTests/warnings_testTests.m -> BestAppEverTests/BestAppEverTests.m
R  warnings_testTests/Info.plist -> BestAppEverTests/Info.plist

This blog post is based on my original comment is here: http://stackoverflow.com/questions/29824737/xcode-6-3-1-crashes-while-renaming-project/29830195#29830195.

Comments