Building the Build System – Part 2 – Project Templates
2011-10-18 16:42
399 查看
In Part 1 of our series, you learned how to use basic xcconfig files to manage build configuration in Xcode rather than using the Build Panel. This is useful, but a bit tedious to set
up every time you make a new project. What we need is a way to automatically create new projects that have our setup in place already. Wouldn’t it be nice if you could create new Project Templates just like the ones that come with Xcode? You can, and since
the release of the iPhone version of Xcode, it’s easier than ever. Let’s make one.
The easiest way to create a new template is to base it off of an existing template. First, let’s get a sense of what a project template is, and how Xcode finds and uses them. Open the following directories to see the default templates:
As with much of the move in Xcode to
In these directories, you’ll see several templates that should look familiar under File Templates, Project Templates and Target Templates. Let’s take a look at the iPhone – Application – Window-Based Application template. You should see the following structure:
Basically, it looks very similar to an empty project called
The one thing special about the template is a little bit of meta-data stored inside the xcodeproj. Open this in Finder (Ctrl-Click and select Show Package Contents). You’ll see the standard project.pbxproj, but you’ll also see TemplateIcon.icns and TemplateInfo.plist.
The first is the icon shown in Xcode, and the latter includes the description of the project (again for the Xcode template chooser screen).
You’ll note the use of
removed in the identifier version. This is important in making class names for instance. So if your project name is “My Application”, the xcodeproj will be “My Application.xcodeproj”, but the AppDelegate class header will be “MyApplication_AppDelegate.h”.
These macros (
for a Cocoa project, you can fix the main menu to read “About PROJECTNAME” and it’ll be substituted for you. With NIBs, you couldn’t do this, which is why “NewApplication” is found all over the place in the Mac MainMenu.xib. It’s
unfortunate that Apple didn’t fix this when they converted the Cocoa templates to XIBs.
There are several other macros available in project templates. These are not documented as far as I know. I’ve worked them out by looking at the strings in the Xcode framework that provides them, and then testing which ones actually work in project templates
(file templates and target templates have slightly different ones).
Note that while there are
last time.
Open
Under
Open
Copy (don’t move),
Open the
Go back to
“Abandoning the Build Panel” and apply the changes there. Or apply whatever changes you want to be in every project. In your
Quit Xcode
Delete the build/ directory if one was created
Right-click the xcodeproj and select “Show Package Contents”
Delete any .mode1v3 and .pbxuser files that may have been created
Open TemplateInfo.plist in Property Editor and set the description as you like
If you like, replace TemplateIcon.icns with an icon file you like
You can now launch XCode, select File>New Project, and you should see a “User Templates” section with your template.
If you’d like an example of the project template I use, along with all my xcconfig settings, I’ve attached it. We’ll be discussing some of these settings in the future.
up every time you make a new project. What we need is a way to automatically create new projects that have our setup in place already. Wouldn’t it be nice if you could create new Project Templates just like the ones that come with Xcode? You can, and since
the release of the iPhone version of Xcode, it’s easier than ever. Let’s make one.
The easiest way to create a new template is to base it off of an existing template. First, let’s get a sense of what a project template is, and how Xcode finds and uses them. Open the following directories to see the default templates:
/Developer/Library/Xcode– Mac
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode– iPhone
As with much of the move in Xcode to
/Developer/Platforms, the existing Mac files have been inconsistently transfered, and some are still found in
/Developer/Library. Hopefully Apple will get more consistent around this over time.
In these directories, you’ll see several templates that should look familiar under File Templates, Project Templates and Target Templates. Let’s take a look at the iPhone – Application – Window-Based Application template. You should see the following structure:
PROJECTNAME.xcodeproj PROJECTNAMEASIDENTIFIER_Prefix.pch Classes/ _PROJECTNAMEASIDENTIFIER_AppDelegate.h _PROJECTNAMEASIDENTIFIER_AppDelegate.m Info.plist main.m MainWindow.xib
Basically, it looks very similar to an empty project called
_PROJECTNAME_because that’s what it is. You could open this project in Xcode, build it and run it. Be a little mindful of doing this; it will create a
builddirectory that you’ll want to delete later, but this is basically how we’re going to modify our templates.
The one thing special about the template is a little bit of meta-data stored inside the xcodeproj. Open this in Finder (Ctrl-Click and select Show Package Contents). You’ll see the standard project.pbxproj, but you’ll also see TemplateIcon.icns and TemplateInfo.plist.
The first is the icon shown in Xcode, and the latter includes the description of the project (again for the Xcode template chooser screen).
You’ll note the use of
_PROJECTNAME_versus
_PROJECTNAMEASIDENTIFIER_. For the majority of projects these are the same. But if your project has characters that would be illegal in an Objective-C identifier but legal in a file name (spaces, numbers, symbols), then those will be
removed in the identifier version. This is important in making class names for instance. So if your project name is “My Application”, the xcodeproj will be “My Application.xcodeproj”, but the AppDelegate class header will be “MyApplication_AppDelegate.h”.
These macros (
_PROJECTNAME_and
_PROJECTNAMEASIDENTIFIER_) will be substituted anywhere they are found in a filename or within the files. That includes inside of XIB files. So in the MainMenu.xib
for a Cocoa project, you can fix the main menu to read “About PROJECTNAME” and it’ll be substituted for you. With NIBs, you couldn’t do this, which is why “NewApplication” is found all over the place in the Mac MainMenu.xib. It’s
unfortunate that Apple didn’t fix this when they converted the Cocoa templates to XIBs.
There are several other macros available in project templates. These are not documented as far as I know. I’ve worked them out by looking at the strings in the Xcode framework that provides them, and then testing which ones actually work in project templates
(file templates and target templates have slightly different ones).
___UUIDASIDENTIFIER___ ___UUID___ ___YEAR___ ___ORGANIZATIONNAME___ ___FULLUSERNAME___ ___USERNAME___ ___TIME___ ___DATE___ ___PROJECTNAMEASXML___ ___PROJECTNAMEASIDENTIFIER___ ___PROJECTNAME___
Note that while there are
DATEand
YEARmacros, there are no
DAYor
MONTHmacros.
ORGANIZATIONNAMEis set by default to
__MyCompanyName__. You can fix this by setting it in defaults:
defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ORGANIZATIONNAME = "My Company"; }'
Walking through it all
Now that we’ve talked it to death, let’s walk through actually doing it based on our worklast time.
Open
~/Library/Application Support/Developer/Shared/Xcodein Finder and make a directory called
Project Templates
Under
Project Templates, make a directory called
iPhone OS.
Open
/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Applicationin Finder.
Copy (don’t move),
Window-Based Applicationfrom the
/Developerwindow to
~/Librarywindow.
Open the
Window-Based Applicationfolder and open
___PROJECTNAME___.xcodeprojin Xcode.
Go back to
“Abandoning the Build Panel” and apply the changes there. Or apply whatever changes you want to be in every project. In your
Application.xcconfig, you’ll want this:
PRODUCT_NAME = ___PROJECTNAME___ GCC_PREFIX_HEADER = ___PROJECTNAMEASIDENTIFIER____Prefix.pch
Quit Xcode
Delete the build/ directory if one was created
Right-click the xcodeproj and select “Show Package Contents”
Delete any .mode1v3 and .pbxuser files that may have been created
Open TemplateInfo.plist in Property Editor and set the description as you like
If you like, replace TemplateIcon.icns with an icon file you like
You can now launch XCode, select File>New Project, and you should see a “User Templates” section with your template.
If you’d like an example of the project template I use, along with all my xcconfig settings, I’ve attached it. We’ll be discussing some of these settings in the future.
相关文章推荐
- Building the Build System – Part 1 – Abandoning the Build Panel
- A closer look at the Android project build system part I
- A closer look at the Android project build system part II
- myeclipse报错:Building workspace' has encountered a problem. Errors occurred during the build
- Intel-x86-System-Programming-Guide, Part 1,Chapter 2.1 OVERVIEW OF THE SYSTEM-LEVEL ARCHITECTURE
- ZOJ-2966-Build The Electric System【5th浙江省赛】【最小生成树】
- The Google File System : part8 RELATED WORK and CONCLUSIONS and ACKNOWLEDGMENTS
- [原] XAF How to see and edit the time part in the DatePropertyEditor for the System.DateTime property
- Cannot find the class file for java.lang.Object. Fix the build path then try building this project
- BNU 16485 Build The Electric System
- 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
- Cannot find the class file for java.lang.Object. Fix the build path then try building this project
- 错误:The project cannot be built until build path errors are resolved 和 Unbound classpath container: 'JRE System Library [JavaSE-1.7]' in project 'MyJavaCode'的解决方法
- Building System之 get_abs_build_var() && get_build_var()
- Call SOAP Web services with Ajax, Part 1: Build the Web services client
- Eclipse:Cannot find the class file for org.apache.http.Header. Fix the build path then try building
- c# 调用MD5CryptoServiceProvider出现 System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
- ZOJ 2966 Build The Electric System
- Building Forms with PowerShell – Part 1 (The Form)
- 2014ACM集训13级PK赛5-Build The Electric System