forked from ivucica/buildtool
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.m
117 lines (98 loc) · 2.92 KB
/
main.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
/*
Project: buildtool
Author: Gregory John Casamento,,,
Created: 2011-08-20 11:42:51 -0400 by heron
*/
#import <Foundation/Foundation.h>
#import <XCode/PBXCoder.h>
#import <XCode/PBXContainer.h>
#import <XCode/NSString+PBXAdditions.h>
NSString *
findProjectFilename(NSArray *projectDirEntries)
{
NSEnumerator *e = [projectDirEntries objectEnumerator];
NSString *fileName;
while ((fileName = [e nextObject]))
{
NSRange range = [fileName rangeOfString:@"._"];
if ([[fileName pathExtension] isEqual: @"xcodeproj"] && range.location == NSNotFound)
{
return [fileName stringByAppendingPathComponent: @"project.pbxproj"];
}
}
return nil;
}
int
main(int argc, const char *argv[])
{
if(argc == 0)
{
return 0;
}
setlocale(LC_ALL, "en_US.utf8");
id pool = [[NSAutoreleasePool alloc] init];
NSString *fileName = nil;
NSString *function = nil;
PBXCoder *coder = nil;
PBXContainer *container = nil;
NSString *projectDir;
NSArray *projectDirEntries;
NSFileManager *fileManager = [NSFileManager defaultManager];
projectDir = [fileManager currentDirectoryPath];
projectDirEntries = [fileManager directoryContentsAtPath: projectDir];
// Get the project...
if(argv[1] != NULL)
{
NSString *xcodeFilePath = [NSString stringWithCString: argv[1]];
fileName = [xcodeFilePath
stringByAppendingPathComponent:
@"project.pbxproj"];
if([[xcodeFilePath pathExtension] isEqualToString:@"xcodeproj"] == NO)
{
fileName = findProjectFilename(projectDirEntries);
function = [NSString stringWithCString: argv[1]];
}
else
{
// If there is a project, add the build operation...
if(argv[2] != NULL && argc > 1)
{
function = [NSString stringWithCString: argv[2]];
}
}
}
else
{
fileName = findProjectFilename(projectDirEntries);
}
if([function isEqualToString: @""] || function == nil)
{
function = @"build"; // default action...
}
NSString *display = [function stringByCapitalizingFirstCharacter];
// Unarchive...
coder = [[PBXCoder alloc] initWithContentsOfFile: fileName];
container = [coder unarchive];
// Build...
SEL operation = NSSelectorFromString(function);
if ([container respondsToSelector: operation])
{
// build...
puts([[NSString stringWithFormat: @"\033[1;32m**\033[0m Start operation %@", display] cString]);
if ([container performSelector: operation])
{
puts([[NSString stringWithFormat: @"\033[1;32m**\033[0m %@ Succeeded", display] cString]);
}
else
{
puts([[NSString stringWithFormat: @"\033[1;31m**\033[0m %@ Failed", display] cString]);
}
}
else
{
puts([[NSString stringWithFormat: @"Unknown build operation \"%@",display] cString]);
}
// The end...
[pool release];
return 0;
}