Skip to content

Instantly share code, notes, and snippets.

@xcodereleases
Last active June 15, 2020 04:29
Show Gist options
  • Save xcodereleases/615b1f51b4faa12feb89ff3a45c721e8 to your computer and use it in GitHub Desktop.
Save xcodereleases/615b1f51b4faa12feb89ff3a45c721e8 to your computer and use it in GitHub Desktop.
Extract beta information from a local copy of Xcode
#import <Cocoa/Cocoa.h>
@protocol FilePath <NSObject>
+ (instancetype)filePathForPathString:(NSString *)path;
@end
@protocol NamedVersion <NSObject>
- (NSString *)name;
@end
@protocol ToolsInfo <NSObject>
- (instancetype)initWithXcodeFilePath:(id<FilePath>)path error:(NSError **)error;
- (BOOL)isAppleInternal;
- (BOOL)isBeta;
- (NSUInteger)toolsBetaVersion;
- (id<NamedVersion>)toolsVersion;
- (id<NamedVersion>)toolsBuildVersion;
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSURL *xcode;
if (argc < 2) {
// no path passed in
xcode = [[NSURL fileURLWithPath:@"/var/db/xcode_select_link"] URLByResolvingSymlinksInPath];
} else {
NSURL *currentFolder = [NSURL fileURLWithPath:[[NSFileManager defaultManager] currentDirectoryPath]];
xcode = [NSURL fileURLWithPath:@(argv[1]) relativeToURL:currentFolder];
}
while ([[xcode lastPathComponent] hasSuffix:@".app"] == NO) {
xcode = [xcode URLByDeletingLastPathComponent];
}
NSURL *bundleURL = [NSURL fileURLWithPath:@"./Contents/SharedFrameworks/DVTFoundation.framework" relativeToURL:xcode];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
if (bundle == nil) { return -1; }
if ([bundle load] == NO) { return -1; }
id<FilePath> path = [NSClassFromString(@"DVTFilePath") filePathForPathString: [xcode absoluteURL].path];
id<ToolsInfo> info = [[NSClassFromString(@"DVTToolsInfo") alloc] initWithXcodeFilePath:path error:nil];
if (info == nil) { return -1; }
if (info.toolsVersion.name == nil) { return -1; }
if (info.toolsBuildVersion.name == nil) { return -1; }
NSString *final = [NSString stringWithFormat:@"{ \"beta\": %lu, \"version\": \"%@\", \"build\": \"%@\" }",
(unsigned long)info.toolsBetaVersion,
info.toolsVersion.name,
info.toolsBuildVersion.name];
printf("%s", final.UTF8String);
}
return 0;
}
@saagarjha
Copy link

saagarjha commented Nov 14, 2019

Psst…you may want to consider +[DVTToolsInfo toolsInfo].

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment