Brian Ray's Blog : MacDev/CustomizeXcodeTemplates.html

Sat, 05 Nov 2005

Customize Xcode Templates

When I first added a new file to my XCode 2.1 project I ended up with something like this:

/*
 *  untitled.cpp
 *  FooProject
 *
 *  Created by Brian H Ray on 11/5/05.
 *  Copyright __MyCompanyName__. All rights reserved.
 *
 */

#include "untitled.h"

The MyCompanyName macro was easily accessible:

$ defaults read com.apple.XCode PBXCustomTemplateMacroDefinitions
{ORGANIZATIONNAME = "__MyCompanyName__"; }

Changing this value with write does the trick. See 'man defaults'. However, I still do not like this header on every file. I want to customize these templates so they are more friendly to doxygen, subversion, and some common class definitions I use with most new files. Here I will use C++ as the example file, although these customization tip works just as well with Java, Cocoa, or any other languages already defined or you define (I added Python). The files I need to customize are located under "/Library/Application Support/Apple/Developer Tools/File Templates". The C++ header looks like:

/*
 *  «FILENAME»
 *  «PROJECTNAME»
 *
 *  Created by «FULLUSERNAME» on «DATE».
 *  Copyright «YEAR» «ORGANIZATIONNAME». All rights reserved.
 *
 */

#include <Carbon/Carbon.h>

So here is the complete list (I think) of the macro available for expansion. WARNING: when opening these templates open the as Unicode, otherwise you will see junk for the start and stop character.

Expanded Example Macro Description
MyCompany ORGANIZATIONNAME Name set from above macro
11/5/05 DATE current date (using NSCalendarDate format "%x")
/tmp/Foo & Test Project DIRECTORY full path of directory in which the new file is being created
test.h FILENAME full file name, exactly as typed by the user
test FILEBASENAME file name without extension
test FILEBASENAMEASIDENTIFIER same as FILEBASENAME, but mangled to a legal C-style identifier
h FILEEXTENSION extension of file name
Brian H Ray FULLUSERNAME full name of the logged-in user
Foo & Test Project PROJECTNAME name of project in which file is created, "" if no project
Foo___Test_Project PROJECTNAMEASIDENTIFIER same as PROJECTNAME, but mangled to a legal C-style identifier
Foo &amp; Test Project PROJECTNAMEASXML XML entities replaced
10:08:20 AM TIME current time (using NSCalendarDate format "%X")
2005 YEAR four int date
bray USERNAME account name of the logged-in use
test.app PRODUCTNAME only works in Taget Templates
DEBUG TARGETNAMEASIDENTIFIER only works in Target Templates
95C97E10-9F0E-4C68-8... UUID unique User ID

My result template looks like this:

/*!  
   \file «FILENAME» 

   $Id$
   $URL$
   
   \version   $Rev$
   \author    «FULLUSERNAME»
   \date      «DATE»
   \namespace «PROJECTNAMEASIDENTIFIER»
*/
#ifndef «PROJECTNAMEASIDENTIFIER»«FILEBASENAMEASIDENTIFIER»
#define «PROJECTNAMEASIDENTIFIER»«FILEBASENAMEASIDENTIFIER»


namespace «PROJECTNAMEASIDENTIFIER»
{

/*! \class «FILEBASENAME».h "$URL$"
 *  \brief «FILEBASENAME» class from «PROJECTNAME»
 *
 *  
 */
class «FILEBASENAMEASIDENTIFIER»
{


  public:

    //! «FILEBASENAMEASIDENTIFIER» constructor.
    /*!
      
    */
    «FILEBASENAMEASIDENTIFIER»();

    //! «FILEBASENAMEASIDENTIFIER» destructor.
    /*!
      
    */
   ~«FILEBASENAMEASIDENTIFIER»();

};

}  // end namespace «PROJECTNAMEASIDENTIFIER»

#endif // «PROJECTNAMEASIDENTIFIER»«FILEBASENAMEASIDENTIFIER»

When creating a new file from XCode the result looks like:

/*!  
   \file Test.h 

   $Id$
   $URL$
   
   \version   $Rev$
   \author    Brian H Ray
   \date      11/5/05
   \namespace Foo___Test_Project
*/
#ifndef Foo___Test_ProjectTest
#define Foo___Test_ProjectTest

namespace Foo___Test_Project
{

/*! \class Test.h "$URL$"
 *  \brief Test class from Foo & Test Project
 *
 *  
 */
class Test
{


  public:

    //! Test constructor.
    /*!
      
    */
    Test();

    //! Test destructor.
    /*!
      
    */
   ~Test();

};

}  // end namespace Foo___Test_Project

#endif // Foo___Test_ProjectTest

For further expansion of the Subversion Keywords, I added the meta data for these in subversion:

$ svn proplist --verbose test.h
$ svn propset svn:keywords "Id Rev URL" test.h 

The file of course needs to be added/committed to subversion before these are expanded. XCode supports subversion, btw so I do not need to leave the IDE.

posted at: 13:38 | path: /MacDev | permanent link to this entry

Made with PyBlosxom