I found Script in Build Phases more reliable than Build Rules.
In the "Build Phases" section, for your purpose, select the button at the bottom of the "Add Script Run" button. Drag the Script run to the beginning so that it runs before compiling the sources.
Remember that the actual data model files (.xcdatamodel) are contained in the package (.xcdatamodeld) and that you only need to collect the latest data model for your project.
Add the following to Script (replacing the text in parentheses)
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 mogenerator options if necessary. --base-class <your base class> and --template-var arc=true are common.
Ryan
source share