Permalink
Browse files

Added print settings option showing level where each setting was defi…

…ned.
  • Loading branch information...
1 parent 6efd2f9 commit b84a2630a0b74050f78d563486091cd8b29780f4 @tomaz committed Mar 14, 2012
@@ -21,6 +21,7 @@
73280F28150FE48700D1E6E8 /* Settings+Appledoc.m in Sources */ = {isa = PBXBuildFile; fileRef = 73280F27150FE48700D1E6E8 /* Settings+Appledoc.m */; };
73280F29150FE48700D1E6E8 /* Settings+Appledoc.m in Sources */ = {isa = PBXBuildFile; fileRef = 73280F27150FE48700D1E6E8 /* Settings+Appledoc.m */; };
73280F321510989600D1E6E8 /* AppledocTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 73280F311510989600D1E6E8 /* AppledocTests.m */; };
+ 739704641510CEFD00486507 /* DDCliUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D274D4150E08FE004D27F4 /* DDCliUtil.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
73D274B2150E05DA004D27F4 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73D274B1150E05DA004D27F4 /* Foundation.framework */; };
73D274B5150E05DA004D27F4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D274B4150E05DA004D27F4 /* main.m */; };
73D274DA150E08FE004D27F4 /* DDCliUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 73D274D4150E08FE004D27F4 /* DDCliUtil.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
@@ -330,6 +331,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 739704641510CEFD00486507 /* DDCliUtil.m in Sources */,
7317FAC3150F556800B39CFD /* TestCaseBase.m in Sources */,
7317FAC4150F56A500B39CFD /* Logging.m in Sources */,
7317FAC5150F56A800B39CFD /* Appledoc.m in Sources */,
@@ -33,16 +33,28 @@
@property (nonatomic, strong) NSArray *inputPaths;
-#pragma mark - Helper methods
+#pragma mark - Debugging aid
+@property (nonatomic, assign) BOOL printSettings;
+
+@end
+
+#pragma mark -
+
+@interface Settings (Helpers)
- (void)applyFactoryDefaults;
- (void)applyGlobalSettingsFromCmdLineSettings:(Settings *)settings;
- (void)applyProjectSettingsFromCmdLineSettings:(Settings *)settings;
- (void)registerOptionsToCommandLineParser:(CommandLineArgumentsParser *)parser;
+@end
+@interface Settings (Diagnostics)
+- (void)printSettingValuesIfNeeded;
++ (void)printAppledocVersion;
++ (void)printAppledocHelp;
@end
-#pragma mark - Settings keys
+#pragma mark -
/** All the keys used for settings defined in a convenient namespaced struct.
@@ -55,4 +67,6 @@ extern const struct GBSettingsKeys {
__unsafe_unretained NSString *companyIdentifier;
__unsafe_unretained NSString *inputPaths;
+
+ __unsafe_unretained NSString *printSettings;
} GBSettingsKeys;
@@ -6,6 +6,7 @@
// Copyright (c) 2012 Tomaz Kragelj. All rights reserved.
//
+#import "DDCliUtil.h"
#import "CommandLineArgumentsParser.h"
#import "Settings+Appledoc.h"
@@ -21,7 +22,26 @@ + (id)appledocSettingsWithName:(NSString *)name parent:(Settings *)parent {
return result;
}
-#pragma mark - Helper methods
+#pragma mark - Project information
+
+GB_SYNTHESIZE_COPY(NSString *, projectVersion, setProjectVersion)
+GB_SYNTHESIZE_COPY(NSString *, projectName, setProjectName) // Required!
+GB_SYNTHESIZE_COPY(NSString *, companyName, setCompanyName) // Required!
+GB_SYNTHESIZE_COPY(NSString *, companyIdentifier, setCompanyIdentifier) // Required!
+
+#pragma mark - Paths
+
+GB_SYNTHESIZE_OBJECT(NSArray *, inputPaths, setInputPaths)
+
+#pragma mark - Debugging aid
+
+GB_SYNTHESIZE_BOOL(printSettings, setPrintSettings)
+
+@end
+
+#pragma mark -
+
+@implementation Settings (Helpers)
- (void)applyFactoryDefaults {
self.projectVersion = @"1.0";
@@ -38,22 +58,85 @@ - (void)registerOptionsToCommandLineParser:(CommandLineArgumentsParser *)parser
[parser registerOption:GBSettingsKeys.projectVersion shortcut:'v' requirement:GBCommandLineValueRequired];
[parser registerOption:GBSettingsKeys.companyName shortcut:'c' requirement:GBCommandLineValueRequired];
[parser registerOption:GBSettingsKeys.companyIdentifier requirement:GBCommandLineValueRequired];
+
+ [parser registerSwitch:GBSettingsKeys.printSettings];
+
+ [parser registerSwitch:@"version"];
+ [parser registerSwitch:@"help" shortcut:'?'];
}
-#pragma mark - Project information
+@end
-GB_SYNTHESIZE_COPY(NSString *, projectVersion, setProjectVersion)
-GB_SYNTHESIZE_COPY(NSString *, projectName, setProjectName) // Required!
-GB_SYNTHESIZE_COPY(NSString *, companyName, setCompanyName) // Required!
-GB_SYNTHESIZE_COPY(NSString *, companyIdentifier, setCompanyIdentifier) // Required!
+#pragma mark -
-#pragma mark - Paths
+@implementation Settings (Diagnostics)
-GB_SYNTHESIZE_OBJECT(NSArray *, inputPaths, setInputPaths)
+- (void)printSettingValuesIfNeeded {
+ if (!self.printSettings) return;
+
+ #define GB_ADD_SETTING(key) { \
+ NSMutableArray *columns = [NSMutableArray array]; \
+ Settings *settings = self; \
+ while (settings) { \
+ if (columns.count == 0) { \
+ lengths[0] = MAX(key.length, lengths[0]); \
+ [columns addObject:key]; \
+ } \
+ if (rows.count == 1) { \
+ lengths[columns.count] = settings.name.length; \
+ [headers addObject:settings.name]; \
+ } \
+ NSString *value = [settings isKeyPresentAtThisLevel:key] ? [settings objectForKey:key] : @""; \
+ lengths[columns.count] = MAX(value.length, lengths[columns.count]); \
+ [columns addObject:value]; \
+ settings = settings.parent; \
+ } \
+ [rows addObject:columns]; \
+ }
+
+ ddprintf(@"Input files and paths:\n");
+ for (NSString *path in self.inputPaths) ddprintf(@"- %@\n", path);
+ ddprintf(@"\n");
+
+ // Prepare the data.
+ NSUInteger lengths[] = { 0, 0, 0, 0, 0, 0 }; // Just use plain C array to simplify calculations with risk of overflowing if adding more levels...
+ NSMutableArray *rows = [NSMutableArray array];
+ NSMutableArray *headers = [NSMutableArray arrayWithObject:@"Setting"];
+ [rows addObject:headers];
+ GB_ADD_SETTING(GBSettingsKeys.projectName)
+ GB_ADD_SETTING(GBSettingsKeys.projectVersion)
+ GB_ADD_SETTING(GBSettingsKeys.companyName)
+ GB_ADD_SETTING(GBSettingsKeys.companyIdentifier)
+
+ // Draw all rows. Note that we need to use pointer to C array otherwise clang complains...
+ NSUInteger *columnLengths = lengths;
+ [rows enumerateObjectsUsingBlock:^(NSArray *columns, NSUInteger rowIdx, BOOL *stopRow) {
+ [columns enumerateObjectsUsingBlock:^(NSString *value, NSUInteger colIdx, BOOL *stopCol) {
+ NSUInteger columnSize = columnLengths[colIdx];
+ NSUInteger valueSize = value.length;
+ ddprintf(@"%@", value);
+ while (valueSize <= columnSize) {
+ ddprintf(@" ");
+ valueSize++;
+ }
+ }];
+ if (rowIdx == 0) ddprintf(@"\n");
+ ddprintf(@"\n");
+ }];
+}
+
++ (void)printAppledocVersion {
+ ddprintf(@"appledoc: version 3.0a1 (build 100)\n");
+ ddprintf(@"\n");
+}
+
++ (void)printAppledocHelp {
+ ddprintf(@"Usage: appledoc [OPTIONS] <paths to files or dirs>\n");
+}
@end
-#pragma mark - Settings keys
+#pragma mark -
const struct GBSettingsKeys GBSettingsKeys = {
.projectName = @"project-name",
@@ -62,4 +145,6 @@ - (void)registerOptionsToCommandLineParser:(CommandLineArgumentsParser *)parser
.companyIdentifier = @"company-id",
.inputPaths = @"input",
+
+ .printSettings = @"print-settings",
};
@@ -69,8 +69,13 @@
#pragma mark - Introspection
-- (NSString *)nameOfSettingsForKey:(NSString *)key;
- (Settings *)settingsForKey:(NSString *)key;
+- (BOOL)isKeyPresentAtThisLevel:(NSString *)key;
+
+#pragma mark - Properties
+
+@property (nonatomic, readonly, copy) NSString *name;
+@property (nonatomic, readonly, strong) Settings *parent;
@end
View
@@ -10,8 +10,8 @@
@interface Settings ()
- (BOOL)isKeyArray:(NSString *)key;
-@property (nonatomic, copy) NSString *name;
-@property (nonatomic, strong) Settings *parent;
+@property (nonatomic, readwrite, copy) NSString *name;
+@property (nonatomic, readwrite, strong) Settings *parent;
@property (nonatomic, strong) NSMutableSet *arrayKeys;
@property (nonatomic, strong) NSMutableDictionary *storage;
@end
@@ -115,11 +115,6 @@ - (void)setFloat:(CGFloat)value forKey:(NSString *)key {
#pragma mark - Introspection
-- (NSString *)nameOfSettingsForKey:(NSString *)key {
- Settings *level = [self settingsForKey:key];
- return level.name;
-}
-
- (Settings *)settingsForKey:(NSString *)key {
Settings *level = self;
while (level) {
@@ -129,6 +124,11 @@ - (Settings *)settingsForKey:(NSString *)key {
return level;
}
+- (BOOL)isKeyPresentAtThisLevel:(NSString *)key {
+ if ([self.storage objectForKey:key]) return YES;
+ return NO;
+}
+
- (BOOL)isKeyArray:(NSString *)key {
return [self.arrayKeys containsObject:key];
}
View
@@ -23,26 +23,37 @@ int main(int argc, char *argv[]) {
CommandLineArgumentsParser *parser = [[CommandLineArgumentsParser alloc] init];
[settings registerOptionsToCommandLineParser:parser];
__block BOOL commandLineValid = YES;
+ __block BOOL finished = NO;
[parser parseOptionsWithArguments:argv count:argc block:^(NSString *argument, id value, BOOL *stop) {
if (value == GBCommandLineArgumentResults.unknownArgument) {
ddprintf(@"Unknown command line argument %@!\n", argument);
commandLineValid = NO;
} else if (value == GBCommandLineArgumentResults.missingValue) {
ddprintf(@"Missing value for command line argument %@!\n", argument);
commandLineValid = NO;
+ } else if ([argument isEqualToString:@"version"]) {
+ [Settings printAppledocVersion];
+ finished = YES;
+ *stop = YES;
+ } else if ([argument isEqualToString:@"help"]) {
+ [Settings printAppledocHelp];
+ finished = YES;
+ *stop = YES;
} else {
[settings setObject:value forKey:argument];
}
}];
+ if (finished) return 0;
if (!commandLineValid) return 1;
// Prepare command line settings with additional data.
settings.inputPaths = parser.arguments;
- // Apply factory defaults, global and project settings.
+ // Apply factory defaults, global and project settings, then print settings if necessary.
[factoryDefaults applyFactoryDefaults];
[globalSettings applyGlobalSettingsFromCmdLineSettings:settings];
[projectSettings applyProjectSettingsFromCmdLineSettings:settings];
+ [settings printSettingValuesIfNeeded];
// Initialize and run the application.
Appledoc *appledoc = [[Appledoc alloc] init];

0 comments on commit b84a263

Please sign in to comment.