Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 154 lines (128 sloc) 4.368 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
//
// GBParser.m
// appledoc
//
// Created by Tomaz Kragelj on 23.7.10.
// Copyright (C) 2010, Gentle Bytes. All rights reserved.
//

#import "GBStore.h"
#import "GBApplicationSettingsProvider.h"
#import "GBObjectiveCParser.h"
#import "GBParser.h"

@interface GBParser ()

- (void)parseDirectory:(NSString *)path;
- (void)parseFile:(NSString *)path;
- (BOOL)isPathIgnored:(NSString *)path;
- (BOOL)isFileIgnored:(NSString *)filename;
- (BOOL)isDirectoryIgnored:(NSString *)filename;
- (BOOL)isSourceCodeFile:(NSString *)path;
@property (assign) NSUInteger numberOfParsedFiles;
@property (retain) GBObjectiveCParser *objectiveCParser;
@property (retain) GBStore *store;
@property (retain) GBApplicationSettingsProvider *settings;

@end

#pragma mark -

@implementation GBParser

#pragma mark Initialization & disposal

+ (id)parserWithSettingsProvider:(id)settingsProvider {
return [[[self alloc] initWithSettingsProvider:settingsProvider] autorelease];
}

- (id)initWithSettingsProvider:(id)settingsProvider {
NSParameterAssert(settingsProvider != nil);
GBLogDebug(@"Initializing parser with settings provider %@...", settingsProvider);
self = [super init];
if (self) {
self.settings = settingsProvider;
self.objectiveCParser = [GBObjectiveCParser parserWithSettingsProvider:self.settings];
}
return self;
}

#pragma mark File system parsing handling

- (void)parseObjectsFromPaths:(NSArray *)paths toStore:(id)store {
NSParameterAssert(paths != nil);
NSParameterAssert(store != nil);
GBLogVerbose(@"Parsing objects from %u paths...", [paths count]);
self.store = store;
self.numberOfParsedFiles = 0;
for (NSString *path in paths) {
GBLogVerbose(@"Parsing '%@'...", path);
if ([self.fileManager isPathDirectory:path]) {
[self parseDirectory:path];
} else {
[self parseFile:path];
}
}
}

- (void)parseDirectory:(NSString *)path {
GBLogDebug(@"Parsing path '%@'...", path);
if ([self isPathIgnored:path]) {
GBLogNormal(@"Ignoring path '%@'...", path);
return;
}

NSError *error = nil;
NSArray *contents = [self.fileManager contentsOfDirectoryAtPath:path error:&error];
if (error) {
GBLogNSError(error, @"Failed fetching contents of '%@'!", path);
return;
}

// First process files. Skip ignored files.
for (NSString *subpath in contents) {
NSString *fullPath = [path stringByAppendingPathComponent:subpath];
if ([self.fileManager isPathDirectory:fullPath]) continue;
if ([self isFileIgnored:subpath]) continue;
[self parseFile:fullPath];
}

// Now process all subdirectories. Skip ignored directories.
for (NSString *subpath in contents) {
NSString *fullPath = [path stringByAppendingPathComponent:subpath];
if (![self.fileManager isPathDirectory:fullPath]) continue;
if ([self isDirectoryIgnored:subpath]) continue;
[self parseDirectory:fullPath];
}
}

- (void)parseFile:(NSString *)path {
GBLogDebug(@"Parsing file '%@'...", path);
if ([self isPathIgnored:path]) {
GBLogNormal(@"Ignoring file '%@'...", path);
return;
}
if (![self isSourceCodeFile:path]) return;

GBLogInfo(@"Parsing source code from '%@'...", path);
NSError *error = nil;
NSString *input = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error) {
GBLogNSError(error, @"Failed reading contents of file '%@'!", path);
return;
}

[self.objectiveCParser parseObjectsFromString:input sourceFile:[path lastPathComponent] toStore:self.store];
self.numberOfParsedFiles++;
}

- (BOOL)isPathIgnored:(NSString *)path {
for (NSString *ignored in self.settings.ignoredPaths) {
if ([path hasSuffix:ignored]) return YES;
}
return NO;
}

- (BOOL)isFileIgnored:(NSString *)filename {
if ([filename isEqualToString:@".DS_Store"]) return YES;
return NO;
}

- (BOOL)isDirectoryIgnored:(NSString *)filename {
if ([filename isEqualToString:@".git"]) return YES;
if ([filename isEqualToString:@".svn"]) return YES;
return NO;
}

- (BOOL)isSourceCodeFile:(NSString *)path {
NSString *extension = [path pathExtension];
if ([extension isEqualToString:@"h"]) return YES;
if ([extension isEqualToString:@"hh"]) return YES;
if ([extension isEqualToString:@"m"]) return YES;
if ([extension isEqualToString:@"mm"]) return YES;
return NO;
}

#pragma mark Properties

@synthesize numberOfParsedFiles;
@synthesize objectiveCParser;
@synthesize settings;
@synthesize store;

@end
Something went wrong with that request. Please try again.