-
Notifications
You must be signed in to change notification settings - Fork 4
/
AppDuplicator.m
88 lines (74 loc) · 3.17 KB
/
AppDuplicator.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
#import "AppDuplicator.h"
#import "thirdparty/SSZipArchive/SSZipArchive.h"
#import "thirdparty/TDUtils.h"
@implementation AppDuplicator
- (NSString *)duplicateAppWithBundleID:(NSString *)appDirectory
bundleID:(NSString *)bundleID
name:(NSString *)name {
uint32_t num = arc4random_uniform(99999) + 1;
bundleID = [NSString stringWithFormat:@"%@.%u", bundleID, num];
NSString *tmpPath = [docPath() stringByAppendingPathComponent:bundleID];
NSString *zipDir = [NSString stringWithFormat:@"%@/ipa", tmpPath];
NSString *dstAppPath =
[NSString stringWithFormat:@"%@/Payload/%@", zipDir, [appDirectory lastPathComponent]];
if (![self copyDirectoryFromPath:appDirectory toPath:dstAppPath]) {
NSLog(@"Failed to copy Application directory.");
return nil;
}
NSString *infoPlistPath = [dstAppPath stringByAppendingPathComponent:@"Info.plist"];
NSMutableDictionary *infoPlist =
[NSMutableDictionary dictionaryWithContentsOfFile:infoPlistPath];
if (!infoPlist) {
NSLog(@"Failed to read Info.plist file.");
return nil;
}
infoPlist[@"CFBundleDisplayName"] = name;
infoPlist[@"CFBundleIdentifier"] = bundleID;
BOOL success = [infoPlist writeToFile:infoPlistPath atomically:YES];
if (!success) {
NSLog(@"Failed to write modified Info.plist file.");
return nil;
}
NSString *IPAFile =
[NSString stringWithFormat:@"%@/%@_%@_duplicated.ipa", docPath(), bundleID, name];
@try {
BOOL success = [SSZipArchive createZipFileAtPath:IPAFile
withContentsOfDirectory:zipDir
keepParentDirectory:NO
compressionLevel:1
password:nil
AES:NO
progressHandler:nil];
return success ? IPAFile : nil;
} @catch (NSException *e) {
NSLog(@"[appduplicator] BAAAAAAAARF during ZIP operation!!! , %@", e);
return nil;
}
}
- (BOOL)copyDirectoryFromPath:(NSString *)srcPath toPath:(NSString *)dstPath {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDirectory;
if (![fileManager fileExistsAtPath:srcPath isDirectory:&isDirectory] || !isDirectory) {
return NO;
}
[fileManager removeItemAtPath:dstPath error:nil];
if (![fileManager createDirectoryAtPath:dstPath
withIntermediateDirectories:YES
attributes:nil
error:nil]) {
return NO;
}
NSArray *contents = [fileManager contentsOfDirectoryAtPath:srcPath error:nil];
if (!contents) {
return NO;
}
for (NSString *item in contents) {
NSString *sourceItemPath = [srcPath stringByAppendingPathComponent:item];
NSString *destinationItemPath = [dstPath stringByAppendingPathComponent:item];
if (![fileManager copyItemAtPath:sourceItemPath toPath:destinationItemPath error:nil]) {
return NO;
}
}
return YES;
}
@end