Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb601eaf

Browse files
author
Jim Puls
committed
Add a new tool, colordump
1 parent59c2bc1 commitb601eaf

File tree

12 files changed

+491
-137
lines changed

12 files changed

+491
-137
lines changed

‎Shared/CGUCodeGenTool.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// CGCodeGenTool.h
3+
// codegenutils
4+
//
5+
// Created by Jim Puls on 9/6/13.
6+
// Copyright (c) 2013 Square, Inc. All rights reserved.
7+
//
8+
9+
#import<Foundation/Foundation.h>
10+
11+
12+
@interfaceCGUCodeGenTool :NSObject
13+
14+
+ (int)startWithArgc:(int)argcargv:(constchar **)argv;
15+
16+
+ (NSString *)inputFileExtension;
17+
18+
@property (copy)NSURL *inputURL;
19+
@property (copy)NSString *classPrefix;
20+
21+
@property (copy)NSString *className;
22+
@property (strong)NSMutableArray *interfaceContents;
23+
@property (strong)NSMutableArray *implementationContents;
24+
25+
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
26+
27+
- (void)writeOutputFiles;
28+
29+
- (NSString *)methodNameForKey:(NSString *)key;
30+
31+
@end

‎Shared/CGUCodeGenTool.m

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
//
2+
// CGCodeGenTool.m
3+
// codegenutils
4+
//
5+
// Created by Jim Puls on 9/6/13.
6+
// Copyright (c) 2013 Square, Inc. All rights reserved.
7+
//
8+
9+
#import"CGUCodeGenTool.h"
10+
11+
#import<libgen.h>
12+
13+
@interfaceCGUCodeGenTool ()
14+
15+
@property (copy)NSString *toolName;
16+
17+
@end
18+
19+
20+
@implementationCGUCodeGenTool
21+
22+
+ (NSString *)inputFileExtension;
23+
{
24+
NSAssert(NO,@"Unimplemented abstract method:%@",NSStringFromSelector(_cmd));
25+
returnnil;
26+
}
27+
28+
+ (int)startWithArgc:(int)argcargv:(constchar **)argv;
29+
{
30+
char opt = -1;
31+
NSURL *searchURL =nil;
32+
NSString *classPrefix =@"";
33+
NSMutableArray *inputURLs = [NSMutableArrayarray];
34+
35+
while ((opt =getopt(argc, (char *const*)argv,"o:f:p:h")) != -1) {
36+
switch (opt) {
37+
case'h': {
38+
printf("Usage:%s [-o <path>] [-f <path>] [-p <prefix>] [<paths>]\n",basename((char *)argv[0]));
39+
printf("%s -h\n\n",basename((char *)argv[0]));
40+
printf("Options:\n");
41+
printf(" -o <path> Output files at <path>\n");
42+
printf(" -f <path> Search for *.%s folders starting from <path>\n", [[selfinputFileExtension]UTF8String]);
43+
printf(" -p <prefix> Use <prefix> as the class prefix in the generated code\n");
44+
printf(" -h Print this help and exit\n");
45+
printf(" <paths> Input files; this and/or -f are required.\n");
46+
return0;
47+
}
48+
49+
case'o': {
50+
NSString *outputPath = [[NSStringalloc]initWithUTF8String:optarg];
51+
outputPath = [outputPathstringByExpandingTildeInPath];
52+
[[NSFileManagerdefaultManager]changeCurrentDirectoryPath:outputPath];
53+
break;
54+
}
55+
56+
case'f': {
57+
NSString *searchPath = [[NSStringalloc]initWithUTF8String:optarg];
58+
searchPath = [searchPathstringByExpandingTildeInPath];
59+
searchURL = [NSURLfileURLWithPath:searchPath];
60+
break;
61+
}
62+
63+
case'p': {
64+
classPrefix = [[NSStringalloc]initWithUTF8String:optarg];
65+
break;
66+
}
67+
68+
default:
69+
break;
70+
}
71+
}
72+
73+
for (int index = optind; index < argc; index++) {
74+
NSString *inputPath = [[NSStringalloc]initWithUTF8String:argv[index]];
75+
inputPath = [inputPathstringByExpandingTildeInPath];
76+
[inputURLsaddObject:[NSURLfileURLWithPath:inputPath]];
77+
}
78+
79+
if (searchURL) {
80+
NSDirectoryEnumerator *enumerator = [[NSFileManagerdefaultManager]enumeratorAtURL:searchURLincludingPropertiesForKeys:@[NSURLNameKey]options:0errorHandler:NULL];
81+
for (NSURL *url in enumerator) {
82+
if ([url.pathExtensionisEqualToString:@"xcassets"]) {
83+
[inputURLsaddObject:url];
84+
}
85+
}
86+
}
87+
88+
dispatch_group_t group =dispatch_group_create();
89+
90+
for (NSURL *url in inputURLs) {
91+
dispatch_group_enter(group);
92+
93+
CGUCodeGenTool *target = [selfnew];
94+
target.inputURL = url;
95+
target.classPrefix = classPrefix;
96+
target.toolName = [[NSStringstringWithUTF8String:argv[0]]lastPathComponent];
97+
[targetstartWithCompletionHandler:^{
98+
dispatch_group_leave(group);
99+
}];
100+
}
101+
102+
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
103+
return0;
104+
}
105+
106+
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
107+
{
108+
NSAssert(NO,@"Unimplemented abstract method:%@",NSStringFromSelector(_cmd));
109+
}
110+
111+
- (void)writeOutputFiles;
112+
{
113+
NSAssert(self.className,@"Class name isn't set");
114+
115+
NSString *classNameH = [self.classNamestringByAppendingPathExtension:@"h"];
116+
NSString *classNameM = [self.classNamestringByAppendingPathExtension:@"m"];
117+
118+
NSURL *currentDirectory = [NSURLfileURLWithPath:[[NSFileManagernew]currentDirectoryPath]];
119+
NSURL *interfaceURL = [currentDirectoryURLByAppendingPathComponent:classNameH];
120+
NSURL *implementationURL = [currentDirectoryURLByAppendingPathComponent:classNameM];
121+
122+
[self.interfaceContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) {
123+
return [obj1compare:obj2];
124+
}];
125+
[self.implementationContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) {
126+
return [obj1compare:obj2];
127+
}];
128+
129+
NSString *interface = [NSStringstringWithFormat:@"//\n// This file is generated from%@ by%@.\n// Please do not edit.\n//\n\n#import <UIKit/UIKit.h>\n\n\n@interface%@ : NSObject\n\n%@\n@end\n",self.inputURL.lastPathComponent,self.toolName,self.className, [self.interfaceContentscomponentsJoinedByString:@""]];
130+
if (![interfaceisEqualToString:[NSStringstringWithContentsOfURL:interfaceURLencoding:NSUTF8StringEncodingerror:NULL]]) {
131+
[interfacewriteToURL:interfaceURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL];
132+
}
133+
134+
NSString *implementation = [NSStringstringWithFormat:@"//\n// This file is generated from%@ by%@.\n// Please do not edit.\n//\n\n#import\"%@\"\n\n\n@implementation%@\n\n%@\n\n@end\n",self.inputURL.lastPathComponent,self.toolName, classNameH,self.className, [self.implementationContentscomponentsJoinedByString:@"\n"]];
135+
if (![implementationisEqualToString:[NSStringstringWithContentsOfURL:implementationURLencoding:NSUTF8StringEncodingerror:NULL]]) {
136+
[implementationwriteToURL:implementationURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL];
137+
}
138+
139+
NSLog(@"Wrote%@ to%@", self.className, currentDirectory);
140+
}
141+
142+
- (NSString *)methodNameForKey:(NSString *)key;
143+
{
144+
NSMutableString *mutableKey = [keymutableCopy];
145+
[mutableKeyreplaceCharactersInRange:NSMakeRange(0,1)withString:[[keysubstringToIndex:1]lowercaseString]];
146+
[mutableKeyreplaceOccurrencesOfString:@""withString:@""options:0range:NSMakeRange(0, mutableKey.length)];
147+
[mutableKeyreplaceOccurrencesOfString:@"~"withString:@""options:0range:NSMakeRange(0, mutableKey.length)];
148+
return [mutableKeycopy];
149+
}
150+
151+
@end

‎assetgen/AGCatalogParser.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,8 @@
66
// Copyright (c) 2013 Square, Inc. All rights reserved.
77
//
88

9-
#import<Foundation/Foundation.h>
9+
#import"CGUCodeGenTool.h"
1010

11-
@interfaceAGCatalogParser :NSObject
12-
13-
+ (instancetype)assetCatalogAtURL:(NSURL *)url;
14-
15-
@property (copy)NSString *classPrefix;
16-
17-
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
1811

12+
@interfaceAGCatalogParser :CGUCodeGenTool
1913
@end

‎assetgen/AGCatalogParser.m

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,13 @@
1010

1111
@interfaceAGCatalogParser ()
1212

13-
@property (copy)NSURL *rootURL;
1413
@property (strong)NSArray *imageSetURLs;
15-
@property (copy)NSString *catalogName;
16-
17-
@property (strong)NSMutableArray *interfaceContents;
18-
@property (strong)NSMutableArray *implementationContents;
1914

2015
@end
2116

2217

2318
@implementationAGCatalogParser
2419

25-
+ (instancetype)assetCatalogAtURL:(NSURL *)url;
26-
{
27-
AGCatalogParser *parser = [selfnew];
28-
29-
NSLog(@"Starting catalog for url%@", url);
30-
31-
parser.rootURL = url;
32-
parser.catalogName = [url.lastPathComponentstringByDeletingPathExtension];
33-
parser.classPrefix =@"";
34-
35-
return parser;
36-
}
37-
3820
- (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
3921
{
4022
dispatch_group_t dispatchGroup =dispatch_group_create();
@@ -45,6 +27,8 @@ - (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
4527
self.interfaceContents = [NSMutableArrayarray];
4628
self.implementationContents = [NSMutableArrayarray];
4729

30+
self.className = [NSStringstringWithFormat:@"%@%@Catalog",self.classPrefix, [[self.inputURLlastPathComponent]stringByDeletingPathExtension]];
31+
4832
for (NSURL *imageSetURL in self.imageSetURLs) {
4933
dispatch_group_async(dispatchGroup, dispatchQueue, ^{
5034
[selfparseImageSetAtURL:imageSetURL];
@@ -53,7 +37,7 @@ - (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
5337

5438
dispatch_group_wait(dispatchGroup, DISPATCH_TIME_FOREVER);
5539

56-
[selfoutputCode];
40+
[selfwriteOutputFiles];
5741

5842
completionBlock();
5943
});
@@ -62,7 +46,7 @@ - (void)startWithCompletionHandler:(dispatch_block_t)completionBlock;
6246
- (void)findImageSetURLs;
6347
{
6448
NSMutableArray *imageSetURLs = [NSMutableArrayarray];
65-
NSDirectoryEnumerator *enumerator = [[NSFileManagernew]enumeratorAtURL:self.rootURLincludingPropertiesForKeys:@[NSURLNameKey]options:0errorHandler:NULL];
49+
NSDirectoryEnumerator *enumerator = [[NSFileManagernew]enumeratorAtURL:self.inputURLincludingPropertiesForKeys:@[NSURLNameKey]options:0errorHandler:NULL];
6650
for (NSURL *url in enumerator) {
6751
if ([url.pathExtensionisEqualToString:@"imageset"]) {
6852
[imageSetURLsaddObject:url];
@@ -73,7 +57,7 @@ - (void)findImageSetURLs;
7357

7458
- (void)parseImageSetAtURL:(NSURL *)url;
7559
{
76-
NSString *name = [[[urllastPathComponent]stringByDeletingPathExtension]stringByReplacingOccurrencesOfString:@"~"withString:@"_"];
60+
NSString *methodName = [selfmethodNameForKey:[[urllastPathComponent]stringByDeletingPathExtension]];
7761
NSURL *contentsURL = [urlURLByAppendingPathComponent:@"Contents.json"];
7862
NSData *contentsData = [NSDatadataWithContentsOfURL:contentsURLoptions:NSDataReadingMappedIfSafeerror:NULL];
7963
if (!contentsData) {
@@ -109,7 +93,7 @@ - (void)parseImageSetAtURL:(NSURL *)url;
10993
return -[obj1[@"scale"]compare:obj2[@"scale"]];
11094
}];
11195

112-
NSString *interface = [NSStringstringWithFormat:@"+ (UIImage *)imageFor%@;\n",name];
96+
NSString *interface = [NSStringstringWithFormat:@"+ (UIImage *)%@Image;\n",methodName];
11397
@synchronized(self.interfaceContents) {
11498
[self.interfaceContentsaddObject:interface];
11599
}
@@ -180,36 +164,4 @@ - (void)parseImageSetAtURL:(NSURL *)url;
180164
}
181165
}
182166

183-
- (void)outputCode;
184-
{
185-
NSURL *currentDirectory = [NSURLfileURLWithPath:[[NSFileManagernew]currentDirectoryPath]];
186-
NSString *className = [NSStringstringWithFormat:@"%@%@Catalog",self.classPrefix,self.catalogName];
187-
NSString *classNameH = [classNamestringByAppendingPathExtension:@"h"];
188-
NSString *classNameM = [classNamestringByAppendingPathExtension:@"m"];
189-
190-
NSURL *interfaceURL = [currentDirectoryURLByAppendingPathComponent:classNameH];
191-
NSURL *implementationURL = [currentDirectoryURLByAppendingPathComponent:classNameM];
192-
193-
[self.interfaceContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) {
194-
return [obj1compare:obj2];
195-
}];
196-
[self.implementationContentssortUsingComparator:^NSComparisonResult(id obj1,id obj2) {
197-
return [obj1compare:obj2];
198-
}];
199-
200-
NSString *interface = [NSStringstringWithFormat:@"//\n// This file is generated from%@.xcassets by objc-assetgen.\n// Please do not edit.\n//\n\n#import <UIKit/UIKit.h>\n\n@interface%@ : NSObject\n\n%@\n@end\n",self.catalogName, className, [self.interfaceContentscomponentsJoinedByString:@""]];
201-
202-
if (![interfaceisEqualToString:[NSStringstringWithContentsOfURL:interfaceURLencoding:NSUTF8StringEncodingerror:NULL]]) {
203-
[interfacewriteToURL:interfaceURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL];
204-
}
205-
206-
NSString *implementation = [NSStringstringWithFormat:@"//\n// This file is generated from%@.xcassets by objc-assetgen.\n// Please do not edit.\n//\n\n#import\"%@\"\n\n@implementation%@\n\n%@\n\n@end\n",self.catalogName, classNameH, className, [self.implementationContentscomponentsJoinedByString:@"\n"]];
207-
208-
if (![implementationisEqualToString:[NSStringstringWithContentsOfURL:implementationURLencoding:NSUTF8StringEncodingerror:NULL]]) {
209-
[implementationwriteToURL:implementationURLatomically:YESencoding:NSUTF8StringEncodingerror:NULL];
210-
}
211-
212-
NSLog(@"Wrote%@ to%@", className, currentDirectory);
213-
}
214-
215167
@end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp