Mogenerator and Xcode 4

7.2k views Asked by At

I just installed mogenerator+xmo'd on my development machine and would like to start playing with it. The only instructions I could really find online were from a previous SO post, and those don't work with XCode 4 (or at least ⌘I doesn't pull up metadata any more and I don't know how).

So to get things up and running, is all that needs to happen to add xmod in the .xcdatamodeld's comments (wherever they are) and the classes will be generated/updated on save from then on?

5

There are 5 answers

0
Ryan On

I've found a Script in the "Build Phases" to be more reliable than the "Build Rules".

Under "Build Phases" for your Target, choose the button at the bottom to "Add Run Script". Drag the run script to the top so that it executes before compiling sources.

Remember that the actual data model files (.xcdatamodel) are contained within a package (.xcdatamodeld), and that you only need to compile the latest data model for your project.

Add the following to the script (replacing text in angle-brackets as appropriate)

MODELS_DIR="${PROJECT_DIR}/<path to your models without trailing slash>"
DATA_MODEL_PACKAGE="$MODELS_DIR/<your model name>.xcdatamodeld"
CURRENT_VERSION=`/usr/libexec/PlistBuddy "$DATA_MODEL_PACKAGE/.xccurrentversion" -c 'print _XCCurrentVersionName'`

# Mogenerator Location
if [ -x /usr/local/bin/mogenerator ]; then
    echo "mogenerator exists in /usr/local/bin path";
    MOGENERATOR_DIR="/usr/local/bin";
elif [ -x /usr/bin/mogenerator ]; then
    echo "mogenerator exists in /usr/bin path";
    MOGENERATOR_DIR="/usr/bin";
else
    echo "mogenerator not found"; exit 1;
fi

$MOGENERATOR_DIR/mogenerator --model "$DATA_MODEL_PACKAGE/$CURRENT_VERSION" --output-dir "$MODELS_DIR/"

Add options to mogenerator as appropriate. --base-class <your base class> and --template-var arc=true are common.

0
Neal Ehardt On

After I make changes to my model file, I just run mogenerator manually from the terminal. Using Xcode 4 and ARC, this does the trick:

cd <directory of model file>
mogenerator --model <your model>.xcdatamodeld/<current version>.xcdatamodel --template-var arc=YES

Maybe I'll use build scripts at some point, but the terminal approach is too simple to screw up.

0
Brent Priddy On

Now that Xcode 4 is released Take a look at the Issues page for mogenerator

2
jluckyiv On

While trying to find this answer myself, I found MOGenerator and Xcode 4 integration guide on esenciadev.com. This solution is not a push-button integration, but it works. The link has detailed instructions, but generally you:

  1. Copy the shell scripts into your project
  2. Add build rules to your target to run the two shell scripts

When you build your project, the script runs MOGenerator on all .xcdatamodel files in your project directory. After the build, if the script generates new class files, you must manually add them to your project. Subsequent builds will remember existing MO-Generated files.

Caveats:

  1. The example's build rule assumes you put the scripts into a /scripts/ file folder within your project directory. When I ignored this detail (creating a project folder but not a file folder) I got a build error. Make sure the build rule points to the script's file location.

  2. The script uses the --base-class argument. Unless your model classes are subclasses of a custom class (not NSManagedObject), you must delete this argument from the script. E.g.,

mogenerator --model "${INPUT_FILE_PATH}/$curVer" --output-dir "${INPUT_FILE_DIR}/" --base-class $baseClass
0
Matt Hudson On

Random tip. If you get Illegal Instruction: 4 when you run mogenerator. Install it from the command line:

$ brew update && brew upgrade mogenerator