<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nvie.com &#187; mogenerator</title>
	<atom:link href="http://nvie.com/archives/category/mogenerator/feed" rel="self" type="application/rss+xml" />
	<link>http://nvie.com</link>
	<description>Anything that interests me.</description>
	<lastBuildDate>Tue, 24 Aug 2010 11:21:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Auto-generate classes for your Core Data data model, revisited</title>
		<link>http://nvie.com/archives/310</link>
		<comments>http://nvie.com/archives/310#comments</comments>
		<pubDate>Mon, 19 Oct 2009 22:20:32 +0000</pubDate>
		<dc:creator>Vincent Driessen</dc:creator>
				<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[mogenerator]]></category>

		<guid isPermaLink="false">http://nvie.com/?p=310</guid>
		<description><![CDATA[A few months ago, I wrote about automatically generating classes for your Core Data entities and how to automate Xcode using users scripts, such that, when your model changed, you only needed to run your custom script again and your intermediate model files would reflect the new situation. Well, the guys from the mogenerator project [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago, I wrote about <a title="Automatically generate classes for your Core Data data model" href="/archives/263">automatically generating classes for your Core Data entities</a> and how to automate Xcode using users scripts, such that, when your model changed, you only needed to run your custom script again and your intermediate model files would reflect the new situation.</p>
<p>Well, the guys from the <a title="mogenerator project at Github" href="http://github.com/rentzsch/mogenerator">mogenerator</a> project have come up with a far superior solution in the mean time. The newest version of mogenerator comes with an Xcode plugin named Xmo&#8217;d, which monitors your *.xcdatamodel file for changes and, as soon as it changes, regenerates all of the neccessary files.</p>
<p><strong>This means that there is officially no more reason not to use mogenerator.</strong></p>
<p>To set it up, download the installer package from their (improved) <a title="mogenerator project home page" href="http://rentzsch.github.com/mogenerator/">project website</a> and install it. (Before installing, please read the important release note about the renamed method <code>+newInManagedObjectContext:</code>.)</p>
<p>When installed, all you need to do is Command-click your *.xcdatamodel file, click Get Info, switch to the Comments tab and add the string &#8220;xmod&#8221; to the comment field. This is the trigger for Xmo&#8217;d to start (re)generating your machine-classes (the underscored class files) when the data model changes. Brilliant!</p>
<p><a href="http://nvie.com/wp-content/uploads/2009/10/comment-field.png"><img class="alignnone size-full wp-image-311" title="Adding the trigger to the comment field." src="http://nvie.com/wp-content/uploads/2009/10/comment-field.png" alt="Adding the trigger to the comment field." width="381" height="545" /></a></p>
<p>Oh, the default location at which the generated files will be emitted, is in a folder named after your project, right next to where your *.xcdatamodel already sits:</p>
<p><a href="http://nvie.com/wp-content/uploads/2009/10/emission-location.png"><img class="alignnone size-medium wp-image-314" title="Location where output files are generated" src="http://nvie.com/wp-content/uploads/2009/10/emission-location-300x239.png" alt="Location where output files are generated" width="300" height="239" /></a></p>
<p>Enjoy it and spread the word!</p>
]]></content:encoded>
			<wfw:commentRss>http://nvie.com/archives/310/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically generate classes for your Core Data data model</title>
		<link>http://nvie.com/archives/263</link>
		<comments>http://nvie.com/archives/263#comments</comments>
		<pubDate>Thu, 30 Jul 2009 22:20:34 +0000</pubDate>
		<dc:creator>Vincent Driessen</dc:creator>
				<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[User scripts]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[mogenerator]]></category>

		<guid isPermaLink="false">http://nvie.com/?p=263</guid>
		<description><![CDATA[When designing a Core Data data model for your Xcode projects, you can choose to create Objective-C object wrappers for your entities, so that you can profit from type-safe code. The normal, tedious, workflow for this is that you select each entity from the model designer, select all of its attributes and relationships, Ctrl-click it [...]]]></description>
			<content:encoded><![CDATA[<p>When designing a Core Data data model for your Xcode projects, you can choose to create Objective-C object wrappers for your entities, so that you can profit from type-safe code. The normal, tedious, workflow for this is that you select each entity from the model designer, select all of its attributes and relationships, Ctrl-click it and from the contextual menu first select &#8220;Copy Obj-C 2.0 Method Declarations To Clipboard&#8221;, paste it into the appropriate class header file, then do the same thing for the method implementations in the class implementation file. Waaaaaay too much work. Not to mention the manual copy-pastes are really hard to keep in sync once you start adding functionality to these class files, since you don&#8217;t want to overwrite those additions, but you want to keep replacing everything else.</p>
<h3>Meet mogenerator</h3>
<p>Fortunately, there is a great way for automating this process, using mogenerator. The tool can be downloaded as a <a href="http://aralbalkan.com/2152">DMG installer</a> (Aral Balkan&#8217;s blog mentions a workaround for older Xcode versions, but for Xcode 3.1.3 it worked out of the box for me), or you can checkout the sources from <a href="http://github.com/rentzsch/mogenerator/">github</a> and build it yourself.</p>
<p>The mogenerator command line tool eases this generation process by reading the *.xcdatamodel file and generating both class files and intermediate class files for each entity. The intermediate classes (called <em>machine</em> classes) are continuously overwritten by subsequent regenerations, so you should never edit the contents of these files. The actual model object classes (called <em>human</em> classes) inherit from those intermediate classes with a default empty implementation, allowing for all manual extensions.</p>
<p>For example, when you design a model with two entities Foo and Bar, mogenerator can be invokes as follows:</p>
<pre>mogenerator -m MyDocument.xcdatamodel -M Entities -H Model</pre>
<p>The flag -m sets the input model file, while -M and -H specify the output directories where the machine and human classes should be generated respectively.</p>
<p>This does a few things:</p>
<ul>
<li>In the Entities subdirectory, there will be generated header and implementation files for NSManagedObject subclasses called _Foo and _Bar;</li>
<li>In the Model subdirectory, there will be generated classes called Foo and Bar—respective subclasses of _Foo and _Bar. These are only created if not available yet. Otherwise, they are left as is.</li>
</ul>
<h3>Wrapping it up</h3>
<p>The trick of how mogenerator works is that you can run the script as often as you want. After every change in your model, you&#8217;ll want to re-run the generation again to update the machine classes. You could easily leave Xcode, switch over to Terminal and issue the command above. But you&#8217;ll get quite tired of that after a few times.</p>
<p>Therefore, I&#8217;ve written a custom user script that can be added to Xcode (see figure), which does the following:</p>
<ul>
<li>You can configure the output directories in the first lines of the script. There is no per-project configuration, so choose them as you would like to use them with all your projects;</li>
<li>Mind that these generated files are not automatically included in your Xcode project. Drag them there once and ideally put the machine generated classes into a group under &#8220;Other resource&#8221;, so you never have to see them again. Whenever you add a new class to your model, new files will be generated, so again you must drag the new files to reference those, of course!</li>
<li>The script can be run with any file in the project opened. It starts out with that file and walks up the directory tree to search for your Xcode project. If found, it executes all the rest from your project directory. (Suggestions are welcome, I could not find a better implementation since a variable like %%%{PBXProjectPath}%%% does not seem to exist.)</li>
<li>It invokes mogenerator to generate all model classes for the project. It is smart enough to detect whether you are using Brian Webster&#8217;s <a href="http://www.fatcatsoftware.com/blog/2008/per-object-ordered-relationships-using-core-data">BWOrderedManagedObject</a> in your project. If so, your generated machine classes will inherit from BWOrderedManagedObject instead of NSManagedObject.</li>
</ul>
<p><a href="http://nvie.com/wp-content/uploads/2009/07/set-user-script.png"><img class="alignnone size-medium wp-image-291" title="Edit user script window" src="http://nvie.com/wp-content/uploads/2009/07/set-user-script-300x211.png" alt="Edit user script window" width="300" height="211" /></a></p>
<p>To add this script to Xcode, open the menu Scripts (the icon) &gt; Edit User Scripts&#8230; Click the &#8220;+&#8221;-button on the bottom-left and select &#8220;New shell script&#8221;. Set the values for Input, Directory, Output and Errors as in the screenshot above, then copy-paste the script below into the code window. Add a nice keyboard shortcut to this action to top it off :-) I&#8217;ve chosen ^⌥⌘G for this.</p>
<p>Please feel free to leave any comments if this helped you.</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">#!/bin/sh
#
# Automatic (re)generation of model classes for all *.xcdatamodel files.
# Written by Vincent Driessen
#
# You are free to use this script in any way.
# The original blog post is http://nvie.com/archives/263
#
&nbsp;
# Define output directories
MACHINE_DIR=&quot;Entities&quot;
MODEL_DIR=&quot;Model&quot;
&nbsp;
# Look for the Xcode project directory for this file
cd `dirname &quot;%%%{PBXFilePath}%%%&quot;`
while [ `ls -d *.xcodeproj 2&amp;gt;/dev/null | wc -l` -eq 0 ]; do
    cd ..
    if [ &quot;`pwd`&quot; = &quot;/&quot; ]; then
        echo &quot;No Xcode project found.&quot;
        exit 1
    fi
done
&nbsp;
echo &quot;Project directory is `pwd`&quot;
&nbsp;
#
# Check to see whether the base class is just a default (NSManagedObject) or maybe
# Brian Webster's excellent BWOrderedManagedObject.
# http://www.fatcatsoftware.com/blog/2008/per-object-ordered-relationships-using-core-data
#
# NOTE:
# The check really is quite arbitrary: if there exists a file called BWOrderedManagedObject.h
# somewhere below the project root directory, we assume that we want to use this as the base
# class for all generated classes.
#
EXTRA_FLAGS=
if [ `find . -name BWOrderedManagedObject.h | wc -l` -gt 0 ]; then
	EXTRA_FLAGS+=&quot;--base-class BWOrderedManagedObject&quot;
fi
&nbsp;
# Generate the model classes using mogenerator
for model in `find . -name '*.xcdatamodel'`; do
	# The output directories have to exist, so create them
    mkdir -p &quot;${MACHINE_DIR}&quot; &quot;${MODEL_DIR}&quot;
    mogenerator ${EXTRA_FLAGS} -m &quot;${model}&quot; -M &quot;${MACHINE_DIR}&quot; -H &quot;${MODEL_DIR}&quot;
done</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://nvie.com/archives/263/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
