代码之家  ›  专栏  ›  技术社区  ›  Kaelin Colclasure

如何使文档包与svn配合良好?

  •  0
  • Kaelin Colclasure  · 技术社区  · 14 年前

    我有一个工具,写包风格的文件。它使用NSDocument实现,并覆盖以下NSDocument方法:

    - (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                                  error:(NSError **)outError;
    
    - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
                        ofType:(NSString *)typeName
                         error:(NSError **)outError;
    

    这是所有可爱的,除了当我保存一个文件,在版本控制。(不保留.svn目录等)

    有没有一个好的方法可以让我的文档在svn中很好地发挥作用?

    2 回复  |  直到 11 年前
        1
  •  3
  •   Mike Abdullah    14 年前

    我猜你的代码每次都会创建一个新的文件包装器 -fileWrapperOfType:error: 被称为。这很方便,但不考虑包内磁盘上可能存在的任何其他文件。

    因此,如果您首先创建/使用引用磁盘上现有内容的文件包装器,会怎么样呢。修改包装器以匹配文档的当前状态,并返回结果。当包装器被写到磁盘上时,应该正确地维护svn文件。

        2
  •  1
  •   Kaelin Colclasure    14 年前

    我的文档包是捆绑包,具有通常的层次结构,因此在保存过程中有四个目录发生了变化:

    1. 目录已更改(My.bundle/目录)
    2. 已更新本地化资源(My.bundle/Contents/Resources/en(lproj先生)

    这个方法首先向文档类添加一个可变的字典槽,以保留每个目录的内容。

    @interface LMDocument : NSDocument {
    @private
        // All this is for preserving SCM artifacts across saves…
        NSMutableDictionary * bundleWrappers;
        NSMutableDictionary * contentsWrappers;
        NSMutableDictionary * resourcesWrappers;
        NSMutableDictionary * localizedWrappers;
    }
    

    当创建一个新文档时,这些文档以空字典开始。

    - (id)init;
    {
        if ((self = [super init]) != nil) {
            bundleWrappers = [[NSMutableDictionary alloc] initWithCapacity:0];
            contentsWrappers = [[NSMutableDictionary alloc] initWithCapacity:0];
            resourcesWrappers = [[NSMutableDictionary alloc] initWithCapacity:0];
            localizedWrappers = [[NSMutableDictionary alloc] initWithCapacity:0];
        }
        return self;
    }
    

    - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper
                         ofType:(NSString *)typeName
                          error:(NSError **)outError;
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        bundleWrappers = [[fileWrapper fileWrappers] mutableCopy];
        contentsWrappers = [[[bundleWrappers objectForKey:@"Contents"] fileWrappers] mutableCopy];
        resourcesWrappers = [[[contentsWrappers objectForKey:@"Resources"] fileWrappers] mutableCopy];
        localizedWrappers = [[[resourcesWrappers objectForKey:@"en.lproj"] fileWrappers] mutableCopy];
        NSFileWrapper * infoPlistWrapper = [contentsWrappers objectForKey:@"Info.plist"];
        [contentsWrappers removeObjectForKey:@"Info.plist"]; // Replaced during save…
        // …
        NSMutableDictionary * localizedWrappersCopy = [localizedWrappers mutableCopy];
        [localizedWrappers enumerateKeysAndObjectsUsingBlock:^(id key,
                                                               id obj,
                                                               BOOL * stop)
         {
             if (mumble) { // If it's a file that will be replaced during save…
                 [localizedWrappersCopy removeObjectForKey:key]; // Replaced during save…
                 // …
             }
         }];
        localizedWrappers = localizedWrappersCopy;
        [pool drain];
        return YES;
    }
    

    最后 ,保存文档时请使用精心准备的词典。

    - (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName
                                   error:(NSError **)outError;
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        NSFileWrapper * localizedWrapper =
        [[NSFileWrapper alloc] initDirectoryWithFileWrappers:localizedWrappers];
        [resourcesWrappers setObject:localizedWrapper
                              forKey:@"en.lproj"];
        NSFileWrapper * resourcesWrapper =
        [[NSFileWrapper alloc] initDirectoryWithFileWrappers:resourcesWrappers];
        [contentsWrappers setObject:resourcesWrapper
                             forKey:@"Resources"];
        NSFileWrapper * contentsWrapper =
        [[NSFileWrapper alloc] initDirectoryWithFileWrappers:contentsWrappers];
        // …
        for (id item in mumble) {
            NSString * filename = [item filename];
            NSData * data = [item data];
            [localizedWrapper addRegularFileWithContents:data
                                       preferredFilename:filename];
        }
        [contentsWrapper addRegularFileWithContents:[self infoPlistData]
                                  preferredFilename:@"Info.plist"];
        [pool drain];
        [bundleWrappers setObject:contentsWrapper
                           forKey:@"Contents"];
        NSFileWrapper * bundleWrapper =
        [[[NSFileWrapper alloc] initDirectoryWithFileWrappers:bundleWrappers] autorelease];
        return bundleWrapper;
    }