I have a tool that writes package-style documents. It's implemented using NSDocument and overrides the following NSDocument methods:
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
error:(NSError **)outError;
- (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
ofType:(NSString *)typeName
error:(NSError **)outError;
This is all lovely, except when I save a document that's under version control. (The .svn directories aren't preserved, etc.)
Is there a good recipe somewhere for making my documents play well with svn?
Here is my solution based on Mike's answer!
My document packages are bundles, with the usual hierarchical structure… So there are four directories that I mutate during saves:
The recipe starts with adding a mutable dictionary slot to your document class to preserve the contents of each of these directories.
When creating a new document, these start out as empty dictionaries.
When reading in an existing document, replace these with mutable copies of the relevant fileWrapper contents.
And finally, when saving a document use the dictionaries so painstakingly prepared.
Now when a package document is edited, the application preserves any files it didn't add to the bundle, including SCM artifacts and "other" localizations!
I'm guessing your code works by creating a fresh file wrapper each time
-fileWrapperOfType:error:
is called. This is convenient, but disregards any additional files that may exist on disk inside the package.So instead, what if you start by creating/using a file wrapper that refers to the existing contents on disk. Modify that wrapper to match the document's current state, and return the result. When that wrapper is written out to disk, svn files should be properly maintained.