How do I use Mogenerator? - xcode

I installed Mogenerator. Now what do I do? How do I use it?
The first problem I have is that I have no idea where it was installed to. During the install process, it only let me select the hard drive to install it on, not the directory. The most natural location would be the Applications folder, but it isn't there.
Next, the readme (which I found online) states:
Xmo'd works by noticing when your
*.xcdatamodel is saved. If the model file's Xcode project item comment
contains xmod, an AppleScript is fired
that creates a folder based on your
model's file name and populates it
with derived source code files from
your model. It then adds the new
folder to your project as a Group
Reference and adds all the source
files to your project.
There are several issues with the above statement that aren't clear:
What does "the model file's Xcode project item comment" refer to? How can I make it contain "xmod"?
Is adding this comment and having mogenerator monitor the .xcdatamodel file the only way to use mogenerator? Is there any way I can manually run mogenerator so that it recreates the generated files?

One more caveat to be aware of: You have to already set the Class properties of your entities to something different than NSManagedObject. Otherwise Xmo'd won't do anything.
Note: Xmo'd currently doesn't work with Xcode 4/5, afaik.
What I do is just add a "MOGenerator" target in Xcode:
Go to your project and click on "Add Target..." in the "Targets" section.
Choose "iOS -> Other -> Aggregate"
Go to "Build Phases"
Select from the Menu "Editor -> Add Build Phase -> Add Run Script Build Phase"
Paste your MOGenerator command into the Run Script section, for example:
PATH=${PATH}:/usr/local/bin
cd "${PROJECT_DIR}/MyApp"
mogenerator --human-dir Classes --machine-dir MOGenerated --model MyApp.xcdatamodeld/MyApp.xcdatamodel --template-var arc=true
Now you can update your MOGenerator-generated by simply running this target.

mogenerator is a script that is installed into your developer directory as I recall. However it might be installed into the Xcode scripts directory under your ~/Library.
What do you mean by manually triggering the application? You can trigger a build by "touching" the data model. Any save on the data model will trigger the build
In Xcode if you select the model file and hit ⌘I you will get its metadata. Click on the comments tab and add xmod there. mogenerator looks for that comment to know if it should generate files.
Update
You can run mogenerator from the command line as well as have it monitor your files. Type mogenerator --help in the Terminal to see the options.

I searched my hard drive and found the following files:
The application is installed to: /usr/bin/mogenerator.
The /Library/Application Support/mogenerator/ directory contains some .motemplate files.

⌘I doesn't work in Xcode 4 any more. please check out the command line tool. Here is the doc

Studying line 22 of make_installer.command, I found that /Developer/Library/Xcode/Plug-ins/Xmod.pbplugin is also installed.
And then, searching mogenerator GitHub Issues for "uninstall," I found official instructions on how to uninstall mogenerator from the creator himself.

using mogenerator:
download mogenerator
run and build the mogenerator project
locate the built file in the product group
copy the built file in to /usr/bin directory
in the terminal copy this code and hit enter:
mogenerator -m /Users/hashem/Desktop/Projects/myApp/myAppModel.xcdatamodel -O /Users/hashem/Desktop/Projects/myApp/managedObjects --template-var arc=true
NOTE: here first I have entered myApp.xcdatamodel file path, and next path is the location of generated files. if the file path contains space character be sure to add \ character before space in the file path. like /desktop/xcode\ projects/myApp/....
enjoy!

Related

Copy build to a different directory after finishing building

When I build my project with Xcode 8, it saves the final build in ~/Library/Developer/Xcode/DerivedData/MyProject-[add-lots-of-random-chars-here]/Build/Products/Release-iphoneos. Is there any way to make Xcode copy the app bundle to a user-specified path after building it? e.g. how can I make Xcode copy the built app bundle to /MyBuilds after building it?
I know that I can change the path for storing derived data in my project's settings in Xcode but doing so will of course make Xcode store all data (including intermediate stuff like object code etc) in this location which I don't want. I really only want Xcode to copy the final, ready-for-distribution app bundle to a user-specified location without any intermediate files used in the build process.
How can I do that?
The solution using a script in "Build Phases" does not work properly since Xcode is not finished building the app when running the script. Here is a solution with a script that runs after all build tasks are finished:
Go to "Edit Scheme"
Click on the triangle next to "Build"
Select "Post-action"
Press the + button and select "New Run Script Option"
Select your app name in "Provide build settings from"
Add the following shell script:
Script:
PRODUCT="${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app"
cp -R "${PRODUCT}" ~/Desktop
Add a shell script to your build phases to copy the product:
Script:
PRODUCT="${BUILT_PRODUCTS_DIR}/${TARGET_NAME}.app"
cp -R "${PRODUCT}" ~/Desktop
Certainly replace ~/Desktop with a target directory of your choice.

how to use appledoc to generate an Apple-like documentations

I am reading on the article how to generate an Apple-like HTML documentation at here. My questions are what the command lines are used for. How can we combine command lines and appledoc xcode project to generate a HTML.
I'm the author of appledoc tool. The tool is intended to be used as answered by Caleb above. Recommended installation method is to clone repository from GitHub, open project in Xcode, build and optionally copy binary to your path as suggested above. Additionally, you must also copy template files required for running the tool to one of predefined paths. All the steps required are described in the readme file on appledoc page on GitHub, see Quick Install section.
There are also other methods for installing - all contribution from users:
You can use install-appledoc.sh script included in the git repository.
There's also homebrew recipe available, although it doesn't install template files to my understanding (see this link).
For any additional questions go to appledoc Google group. I just created it few days ago, so there's no content at the moment of this writing, but feel free to ask questions there.
I haven't used 'appledoc', but from a quick look at the page you linked it appears that it's an open-source command-line utility. So the first step will be to compile the 'appledoc' program itself, and then stick it in a directory that's in your path, such as /usr/local/bin or ~/bin/.
The next step is to try to use it to generate some documentation (assuming that you've already created the markdown files that it consumes). Open a terminal window, navigate to the directory where your files are, and then use the command given at the top of the page that you linked:
appledoc --project-name appledoc --project-company "Gentle Bytes" --company-id com.gentlebytes --output ~/help .
If you want to use 'appledoc' to regenerate your documentation every time you build some project, you can add a "Run Script" build phase to an existing target in your project, or create a new target just for documentation that has nothing but a "Run Script" build phase. Either way, the script in that phase should be shell script containing a command similar to the one above (though you'll probably want to specify the source directory explicitly rather than just using the 'current' directory, i.e. .).
As I found on this post, you can generate a complete HTML documentation of your code with this command line:
appledoc --project-name BabelCPP --project-company "My Name"
--company-id com.mycompany --no-create-docset --output ./doc/ .
The last "." is the path to your code.

How to add a new project to source control in Xcode 4?

How do I add a new project to source control (SVN) using Xcode 4?
Open Organizer. Click on the root of your SVN repository in the tree on the left. Click on Import on the bottom right and choose the directory from finder that you want to add and click Import.
This will add the project to SVN
The XCode 4 documentation recommend using command-line tools or a utility program to set up a Git or Subversion repository.
To set up a Subversion repository using the command line
1/ Open the Terminal utility and use the svnadmin create command to create a Subversion repository.
For example, if you want a repository named Sketch_svn in the existing location /Users/myUserName/Repositories, you would enter the command:
svnadmin create /Users/myUserName/Repositories/Sketch_svn
Tip: The easiest way to get the full path to a folder into Terminal without risking typing errors is to first type the command (cd in this case), enter a space, and then drag the folder from the Finder and drop it at the end of the Terminal command line.
Note that the directory /Users/myUserName/Repositories/ must already exist before you execute this command. You can use the Finder or a mkdir command to create the directory.
2/ In another location—not in the repository you just created—create a folder to hold a temporary copy of the project. In that folder, create three additional folders named branches, tags, and trunk.
3/ Create a new Xcode project in the trunk folder, using Xcode, or put your existing project in the trunk folder, using the command line or the Finder.
4/ Use the svn import function to import your project into the repository you created and place it under Subversion source control.
For example, if your temporary copy is in /Users/myUserName/Projects/Sketch_tmp, you would enter the following command in Terminal:
svn import /Users/myUserName/Projects/Sketch_tmp \
file:///Users/myUserName/Repositories/Sketch_svn -m "Initial import"
Notes
The backslash at the end of the first line indicates that the command is continued on the next line. You can omit the backslash and type the entire command on one line. If you do use the backslash, be sure there are no spaces following it before you press Return.
There are three forward slashes in the string file:///.
If you type the entire command on one line, be sure there is a space before file:///.
You can include any comment you want in the quotation marks, but be sure your comment will be meaningful to anyone using the repository.
5/ In the repositories organizer in Xcode, click the Add (+) button at the bottom of the navigator pane, and choose Checkout Repository to create a working copy.
Use Terminal. At the command line, you can use the svn import command to add the project to your repository.
From the way you worded your question, I'm assuming that your subversion repository already exists. You'll follow the same basic steps outlined in Add Git or Subversion Version Control to an Existing Project in the Xcode 4 Users Guide, except that instead of creating a whole new repository you'll just add a new directory to your existing repository.
It's handy that Xcode has support for version control, but it's not a full-featured GUI version control client. You should still know how to manage your repository using the svn command line program. Alternately, you can get a GUI front end like Versions. Either way, you should have (and have read) the Subversion Book. There's a lot more to using version control than just checking files in and out, and you need to know how it works and how best to use it if it's going to help you.

How to bundle an openframeworks application in xcode (relative resource linking?)

An trying to get openframeworks to build me my application so that i can open it from anywhere and it will load the needed images from within the apps Resources folder.
I believe this is relative linking?
I have done it before, on an older xcode and oF 61.
To do this i dragged the needed images into the project file on the left and added it to the executable target, with in a 'build phase' -> 'copy files'.
Been trying all sorts of methods, ofSetDataPathRoot() which solved the problem last time isnt working for me this time.
Any ideas/help would be appreciated!
Thanks
First you need to tell xCode to copy your /bin/data directory into your application bundle by adding a build phase:
1. Click on your project in the Project Navigator
2. Select "Build Phases"
3. Toggle open the "Run Script" section and paste in the following:
cp -r bin/data "$TARGET_BUILD_DIR/$PRODUCT_NAME.app/Contents/Resources";
Then tell your app where to find the data folder relative to itself within the bundle.
Inside the setup method of your oF app:
ofSetDataPathRoot("../Resources/data/");
ofSetDataPathRoot() should solve this problem. Perhaps you are setting the replacement root path incorrectly?
Try calling ofToDataPath() yourself on a string path and print out the result, then use Terminal and cd inside the .app bundle to check if the path sounds correct. Paths are expressed relative to the location of the actual executable inside the .app bundle, so if the executable is at myApp.app/Contents/MacOS/myApp and the data files are at myApp.app/Contents/Resources then ofToDataPath( "texture.png" ) should return something like ../Resources/texture.png.
You can double-check the current working directory (myApp.app/Contents/MacOS in my example) by calling getcwd(), open up a terminal and type man getcwd for more info on that.
oF now sets data path root and does internal calls to ofToDataPath() by default. What version are you using?
Have you looked inside the product's package contents to make sure your resources are getting copies in the proper build phase?

Xcode: Running a script before every build that modifies source code directly

What I did:
I have a script that
Read some configuration files to generate source code snippets
Find relevant Objective-C source files and
Replace some portions of the source code with the generated code in step 1.
and a Makefile that has a special timestamp file as a make target and the configuration files as target sources:
SRC = $(shell find ../config -iname "*.txt")
STAMP = $(PROJECT_TEMP_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME).stamp
$(STAMP): $(SRC)
python inject.py
touch $(STAMP)
I added this Makefile as a "Run Script Build Phase" on top of the stack of build phases for the project target.
What happened:
The script build phase was run before compiling the source.
However, since the script modifies source code during its execution, I needed to build twice to get the most recent version of the build product. Here is what I imagine to be happening:
1st run: Xcode collects dependency information ---> no changes
1st run: Xcode runs "Run Script Build Phase" ---> source is changed behind Xcode's back
1st run: Xcode finishes build, thinking nothing needs to be updated
2nd run: Xcode collects dependency information ---> source has changed, needs rebuild!
2nd run: Xcode runs Run Script Build Phase" ---> everything is up-to-date
2nd run: Xcode proceeds to compilation
After reading Xcode documentation on Build Phases, I tried adding a source file which is known to be updated every time the script is run as the output of "Run Script Build Phases", but nothing changed. Since the number of configuration files may vary in my project, I don't want to specify every input and output file.
Question:
How do I make Xcode aware of source file changes made during "Run Script Build Phase"?
Edit:
Added that I placed the script build phase before the other build phases
Every technique mentioned so far is an overkill. Reproducing steve kim's comment for visibility:
In the build phases tab, simply drag the "Run Script" step to a higher location (e.g. before "Compile Sources").
Tested on Xcode 6
This solution is probably outdated. See the higher voted answer instead; I no longer actively use Xcode and am not qualified to vet a solution.
Use "External Target":
Select "Project" > "New Target..." from the menu
Select "Mac OS X" > "Other" > "External Target" and add it to your project
Open its settings and fill in your script setup
Open the "General" tab of the main target's settings and add the new target as it's direct dependency
Now the new "External Target" runs before the main target even starts gathering dependency information, so that any changes made during the script execution should be included in the build.
There is another, slightly simpler option that doesn't require a separate target, but it's only viable if your script tends to modify the same source files every time.
First, here's a brief explanation for anyone who's confused about why Xcode sometimes requires you to build twice (or do a clean build) to see certain changes reflected in your target app. Xcode compiles a source file if the object file it produces is missing, or if the object file's last-modified date is earlier than the source file's last-modified date was at the beginning of the first build phase. If your project runs a script that modifies a source file in a pre-compilation build phase, Xcode won't notice that the source file's last-modified date has changed, so it won't bother to recompile it. It's only when you build the project a second time that Xcode will notice the date change and recompile the file.
Here's a simple solution if your script happens to modify the same source files every time. Just add a Run Script build phase at the end of your build process like this:
touch Classes/FirstModifiedFile.m Classes/SecondModifiedFile.m
exit $?
Running touch on these source files at the end of your build process guarantees that they will always have a later last-modified date than their object files, so Xcode will recompile them every time.
As of Xcode 4, it looks like if you add the generated files to the output section of the build phase, it will respect that setting, and not generate the ... has been modified since the precompiled header was built error messages.
This is a good option if your script is only generating a handful of files each time.
I as well struggled with this for a long time. The answer is to use ento's "External Target" solution. He is WHY this problem occurs and how we use it in practice...
Xcode 4 build steps do not execute until AFTER the plist has been compiled. This is silly, of course, because it means that any pre-build steps that modify the plist won't take effect. But if you think about it, they actually DO take effect...on the NEXT build. That's why some people have talked about "caching" of plist values or "I have to do 2 builds to make it work." What happens is the plist is built, then your script runs. Next time you build, the plist builds using your modified files, hence the second build.
ento's solution is the one way I've found to actually do a true pre-build step. Unfortunately I also found that it does not cause the plist to update without a clean build and I fixed that. Here is how we have data-driven user values in the plist:
Add an External Build System project that points to a python script and passes some arguments
Add user-defined build settings to the build. These are the arguments that you pass to python (more on why we do this later)
The python script reads some input JSON files and builds a plist preprocessor header file AND touches the main app plist
The main project has "preprocess plist files" turned on and points to this preprocessor file
Using touch on the main app plist file causes the main target to generate the plist every time. The reason we pass in build settings as parameters is so our command-line build can override settings:
Add a user-defined variable "foo" to the prebuild project.
In your prebuild you can use $(foo) to pass the value into the python script.
On the command-line you can add foo=test to pass in a new value.
The python script uses base settings files and allows for user-defined settings files to override the defaults. You make a change and immediately it ends up in the plist. We only use this for settings that MUST be in the plist. For anything else it's a waste of effort....generate a json file or something similar instead and load it at run-time :)
I hope this helps...it's been a couple rough days figuring this out.
The External Target solution from #ento no longer works as of Xcode 11.5. The solution is to add all files that will be changed under Output Files in the Run Script.
Another option is to create a subproject framework with your scripts and just add it as a dependency to all targets. The phase scripts of this subproject should now be executed before all targets.

Resources