/
Settings+Appledoc.m
150 lines (116 loc) · 4.35 KB
/
Settings+Appledoc.m
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
//
// Settings+Appledoc.m
// appledoc
//
// Created by Tomaž Kragelj on 3/13/12.
// Copyright (c) 2012 Tomaz Kragelj. All rights reserved.
//
#import "DDCliUtil.h"
#import "CommandLineArgumentsParser.h"
#import "Settings+Appledoc.h"
@implementation Settings (Appledoc)
#pragma mark - Initialization & disposal
+ (id)appledocSettingsWithName:(NSString *)name parent:(Settings *)parent {
id result = [self settingsWithName:name parent:parent];
if (result) {
[result registerArrayForKey:GBSettingsKeys.inputPaths];
}
return result;
}
#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";
}
- (void)applyGlobalSettingsFromCmdLineSettings:(Settings *)settings {
}
- (void)applyProjectSettingsFromCmdLineSettings:(Settings *)settings {
}
- (void)registerOptionsToCommandLineParser:(CommandLineArgumentsParser *)parser {
[parser registerOption:GBSettingsKeys.projectName shortcut:'p' requirement:GBCommandLineValueRequired];
[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:'?'];
}
@end
#pragma mark -
@implementation Settings (Diagnostics)
- (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 -
const struct GBSettingsKeys GBSettingsKeys = {
.projectName = @"project-name",
.projectVersion = @"project-version",
.companyName = @"company-name",
.companyIdentifier = @"company-id",
.inputPaths = @"input",
.printSettings = @"print-settings",
};