Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi! I cleaned up your code for you! #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BConfigurationElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/*
* Configuration elements are used to instantiate objects extending from a particular extension point.
* A configuration element, with its attributes and children, directly reflects the content
* A configuration element, with its attributes and children, directly reflects the content
* and structure of the extension section within the declaring plug-in's manifest (Plugin.xml) file.
*/
@interface BConfigurationElement : NSObject {
Expand Down
34 changes: 17 additions & 17 deletions BConfigurationElement.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,41 @@ - (Class)executableExtensionClassFromAttribute:(NSString *)attributeName conform
NSArray *executableExtensionClassNameAndSelector = [[self attributeForKey:attributeName] componentsSeparatedByString:@" "];
NSString *executableExtensionClassName = [executableExtensionClassNameAndSelector objectAtIndex:0];
Class executableExtensionClass = nil;

if (!executableExtensionClassName) {
BLogError(@"Failed to find executable extension class name attribute");
} else {
@try {
@try {
executableExtensionClass = [[[self declaringExtension] plugin] classNamed:executableExtensionClassName];
} @catch (NSException *e) {
BLogErrorWithException(e, [NSString stringWithFormat:@"Exception %@ while loading executable extension class %@", e, executableExtensionClassName]);
}
}

if (!executableExtensionClass) return nil;

if (aClass) {
if (![executableExtensionClass isSubclassOfClass:aClass]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to conform to class %@", executableExtensionClass, aClass]);
return nil;
}
}

if (aProtocol) {
if (![executableExtensionClass conformsToProtocol:aProtocol]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to conform to protocol %@", executableExtensionClass, aProtocol]);
return nil;
}
}

for (NSValue *eachValue in selectors) {
SEL eachSelector = [eachValue pointerValue];
if (![executableExtensionClass instancesRespondToSelector:eachSelector]) {
BLogError([NSString stringWithFormat:@"Executable extension class %@ failed to respond to required selector %@", executableExtensionClass, NSStringFromSelector(eachSelector)]);
return nil;
}
}

return executableExtensionClass;
}

Expand All @@ -81,34 +81,34 @@ - (id)createExecutableExtensionFromAttribute:(NSString *)attributeName {

- (id)createExecutableExtensionFromAttribute:(NSString *)attributeName conformingToClass:(Class)aClass conformingToProtocol:(Protocol *)aProtocol respondingToSelectors:(NSArray *)selectors {
Class executableExtensionClass = [self executableExtensionClassFromAttribute:attributeName conformingToClass:aClass conformingToProtocol:aProtocol respondingToSelectors:selectors];

if (executableExtensionClass) {
@try {
@try {
NSArray *executableExtensionClassNameAndSelector = [[self attributeForKey:attributeName] componentsSeparatedByString:@" "];
SEL instanceSelector = nil;

if ([executableExtensionClassNameAndSelector count] > 1) {
instanceSelector = NSSelectorFromString([executableExtensionClassNameAndSelector objectAtIndex:1]);
}

if (instanceSelector != nil) {
return [executableExtensionClass performSelector:instanceSelector];
} else {
id executableExtension = [executableExtensionClass alloc];

if ([executableExtension respondsToSelector:@selector(initWithConfigurationElement:)]) {
executableExtension = [executableExtension initWithConfigurationElement:self];
} else {
executableExtension = [executableExtension init];
}

return executableExtension;
}
} @catch (NSException *e) {
BLogErrorWithException(e, [NSString stringWithFormat:@"Exception %@ while loading instance of extension %@", e, self]);
}
}

return nil;
}

Expand All @@ -123,7 +123,7 @@ - (id)createExecutableExtensionProxyFromAttribute:(NSString *)attributeName conf
return nil;
}
}

return [[BExecutableExtensionProxy alloc] initWithConfigurationElement:self attributeName:attributeName];
}

Expand Down Expand Up @@ -202,14 +202,14 @@ - (NSXMLElement *)contentAsXmlElement {

- (BOOL)assertKeysPresent:(NSArray *)keys {
NSDictionary *elementAttributes = [self attributes];

for (NSString *each in keys) {
if (![elementAttributes objectForKey:each]) {
BLogError([NSString stringWithFormat:@"Required key %@ not found in configuration element.", each]);
return NO;
}
}

return YES;
}

Expand Down
2 changes: 1 addition & 1 deletion BExtension.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
@property(readonly) BPlugin *plugin;
@property(readonly) NSString *label;
@property(readonly) NSString *extensionPointUniqueIdentifier;

#pragma mark Configuration Elements

@property(readonly) NSArray *configurationElements;
Expand Down
2 changes: 1 addition & 1 deletion BExtensionPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@class BPlugin;

/*
* An extension point declared in a plug-in. Except for the list of extensions plugged in to it,
* An extension point declared in a plug-in. Except for the list of extensions plugged in to it,
* the information available for an extension point is obtained from the declaring plug-in's manifest (Plugin.xml) file.
*/
@interface BExtensionPoint : NSObject {
Expand Down
2 changes: 1 addition & 1 deletion BExtensionRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@
@property(readonly) NSArray *extensions;
- (NSArray *)extensionsFor:(NSString *)extensionPointID;
- (NSArray *)configurationElementsFor:(NSString *)extensionPointID;

@end
56 changes: 28 additions & 28 deletions BExtensionRegistry.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ - (void)loadMainExtension {
}
}
}

for (BConfigurationElement *each in [self configurationElementsFor:@"com.blocks.Blocks.main"]) {
[each createExecutableExtensionFromAttribute:@"class"];
}
Expand All @@ -99,19 +99,19 @@ - (NSArray *)extensionsFor:(NSString *)extensionPointID {
return [extensionPointIDsToExtensions objectForKey:extensionPointID];
}

- (NSArray *)configurationElementsFor:(NSString *)extensionPointID {
- (NSArray *)configurationElementsFor:(NSString *)extensionPointID {
NSMutableArray *configurationElements = [extensionPointIDsToConfigurationElements objectForKey:extensionPointID];

if (!configurationElements) {
configurationElements = [NSMutableArray array];

for (BExtension *each in [self extensionsFor:extensionPointID]) {
[configurationElements addObjectsFromArray:[each configurationElements]];
}

[extensionPointIDsToConfigurationElements setObject:configurationElements forKey:extensionPointID];
}

return configurationElements;
}

Expand All @@ -125,26 +125,26 @@ - (void)discoverPlugins {
mainBundlePlugin = [[BPlugin alloc] initWithBundle:[NSBundle mainBundle]];
[self registerPlugin:mainBundlePlugin];
[self registerPlugin:[[BPlugin alloc] initWithBundle:[NSBundle bundleForClass:[self class]]]];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableArray *pluginSearchPaths = [[self pluginSearchPaths] mutableCopy];
NSString *eachSearchPath;

while (eachSearchPath = [pluginSearchPaths lastObject]) {
[pluginSearchPaths removeLastObject];

NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:eachSearchPath];
NSString* eachPath;

while (eachPath = [directoryEnumerator nextObject]) {
if ([[eachPath pathExtension] caseInsensitiveCompare:@"plugin"] == NSOrderedSame) {
[directoryEnumerator skipDescendents];

eachPath = [eachSearchPath stringByAppendingPathComponent:eachPath];

NSBundle *bundle = [NSBundle bundleWithPath:eachPath];
BPlugin *plugin = [[BPlugin alloc] initWithBundle:bundle];

if (!plugin) {
BLogWarning(([NSString stringWithFormat:@"failed to create plugin for path: %@", eachPath]));
} else {
Expand All @@ -162,11 +162,11 @@ - (void)discoverPlugins {
for (BPlugin *eachPlugin in [self plugins]) {
[self registerExtensionsFor:eachPlugin];
}

NSSortDescriptor *sortByProcessOrder = [[NSSortDescriptor alloc] initWithKey:@"processOrder" ascending:YES];
NSSortDescriptor *sortByPluginDiscoveryOrder = [[NSSortDescriptor alloc] initWithKey:@"plugin.discoveryOrder" ascending:YES];
NSArray *extensionsSortDescriptors = [NSArray arrayWithObjects:sortByProcessOrder, sortByPluginDiscoveryOrder, nil];

for (NSString *eachExtensionPointID in [extensionPointIDsToExtensions keyEnumerator]) {
[[extensionPointIDsToExtensions objectForKey:eachExtensionPointID] sortUsingDescriptors:extensionsSortDescriptors];
}
Expand All @@ -179,11 +179,11 @@ - (void)loadMainBundlePluginSharedApplication {
- (void)registerPlugin:(BPlugin *)plugin {
BLogAssert([[plugin identifier] isEqualToString:[[plugin bundle] bundleIdentifier]], @"plugin identifer %@ does not equal bundle identifier %@", [plugin identifier], [[plugin bundle] bundleIdentifier]);
// BLogAssert([[plugin version] isEqualToString:[[plugin bundle] version]], @"plugin version %@ does not equal bundle CFBundleVersion %@", [plugin version], [[plugin bundle] version]);

if ([pluginIDsToPlugins objectForKey:[plugin identifier]] != nil) {
BLogWarning([NSString stringWithFormat:@"plugin id %@ not unique, replacing old with new", [plugin identifier]]);
}

[plugins addObject:plugin];
[pluginIDsToPlugins setObject:plugin forKey:[plugin identifier]];

Expand All @@ -208,7 +208,7 @@ - (void)registerExtensionsFor:(BPlugin *)plugin {
if (![self extensionPointFor:eachExtensionPointUniqueIdentifier]) {
BLogWarning([NSString stringWithFormat:@"no extension point found for extension %@ declared by plugin %@, so that extension will never be loaded.", eachExtensionPointUniqueIdentifier, [plugin identifier]]);
}

NSMutableArray *pointExtensions = [extensionPointIDsToExtensions objectForKey:eachExtensionPointUniqueIdentifier];
if (!pointExtensions) {
pointExtensions = [NSMutableArray array];
Expand All @@ -222,45 +222,45 @@ - (NSArray *)pluginSearchPaths {
NSMutableArray *pluginSearchPaths = [NSMutableArray array];
NSString *applicationSupportSubpath = [NSString stringWithFormat:@"Application Support/%@/PlugIns", [[NSProcessInfo processInfo] processName]];
NSEnumerator *searchPathEnumerator = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask - NSSystemDomainMask, YES) objectEnumerator];

for (NSString *eachSearchPath in searchPathEnumerator) {
NSString *eachPluginPath = [eachSearchPath stringByAppendingPathComponent:applicationSupportSubpath];
if (![pluginSearchPaths containsObject:eachPluginPath]) {
[pluginSearchPaths addObject:eachPluginPath];
}
}

for (NSBundle *eachBundle in [NSBundle allBundles]) {
NSString *eachPluginPath = [eachBundle builtInPlugInsPath];
if (![pluginSearchPaths containsObject:eachPluginPath]) {
[pluginSearchPaths addObject:eachPluginPath];
}
}

return pluginSearchPaths;
}

- (void)handleGetSDEFEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
NSError *error = nil;
NSBundle *blocksBundle = [NSBundle bundleForClass:[self class]];
NSString *blocksSDEFPath = [blocksBundle pathForResource:@"BlocksDefault" ofType:@"sdef"];

BLogInfo(@"Will parse sdef %@", blocksSDEFPath);

NSXMLDocument *mergedOSAScriptingDefinition = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:blocksSDEFPath] options:0 error:&error];

BLogAssert(mergedOSAScriptingDefinition != nil, [NSString stringWithFormat:@"mergedOSAScriptingDefinition failed to load with error %@", error]);

NSXMLElement *mergedOSAScriptingDefinitionRoot = [mergedOSAScriptingDefinition rootElement];

[[NSProcessInfo processInfo] processName];

for (BPlugin *eachPlugin in [[BExtensionRegistry sharedInstance] plugins]) {
NSDictionary *infoDictionary = [eachPlugin.bundle infoDictionary];

if ([[infoDictionary objectForKey:@"NSAppleScriptEnabled"] isEqualToString:@"YES"]) {
NSString *osaScriptingDefinitionName = [infoDictionary objectForKey:@"OSAScriptingDefinition"];

if (eachPlugin.bundle == [NSBundle mainBundle]) {
BLogAssert([osaScriptingDefinitionName isEqualToString:@"dynamic"], @"main bundle of NSAppleScriptEnabled blocks apps should have OSAScriptingDefinition set to dynamic so that plugin sdefs will be found.");
} else {
Expand All @@ -269,7 +269,7 @@ - (void)handleGetSDEFEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSApp
BLogInfo(@"Will parse sdef %@", osaScriptingDefinitionPath);

NSXMLDocument *eachOSAScriptingDefinition = [[NSXMLDocument alloc] initWithContentsOfURL:[NSURL fileURLWithPath:osaScriptingDefinitionPath] options:0 error:&error];

if (eachOSAScriptingDefinition) {
if ([eachPlugin loadAndReturnError:&error]) {
for (NSXMLElement *eachSuiteElement in [[eachOSAScriptingDefinition rootElement] elementsForName:@"suite"]) {
Expand All @@ -285,7 +285,7 @@ - (void)handleGetSDEFEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSApp
}
}
}

[replyEvent setDescriptor:[NSAppleEventDescriptor descriptorWithDescriptorType:typeUTF8Text data:[mergedOSAScriptingDefinition XMLData]] forKeyword:keyDirectObject];
}

Expand Down
2 changes: 1 addition & 1 deletion BLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <Cocoa/Cocoa.h>
#import <syslog.h>

/*
/*
* Logging levels from syslog.h
*
* priorities/facilities are encoded into a single 32-bit quantity, where the
Expand Down
16 changes: 8 additions & 8 deletions BLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ + (void)logErrorWithException:(NSException *)exception lineNumber:(NSInteger)lin
va_list args;
va_start(args, message);
[self logWithLevel:LOG_ERR lineNumber:lineNumber fileName:fileName function:functionName format:message arguments:(va_list)args];
va_end(args);
va_end(args);
}

+ (void)assert:(BOOL)assertion lineNumber:(NSInteger)lineNumber fileName:(char *)fileName function:(char *)functionName message:(NSString *)message, ... {
Expand All @@ -71,21 +71,21 @@ + (void)assert:(BOOL)assertion lineNumber:(NSInteger)lineNumber fileName:(char *
// From http://code.google.com/p/ilcrashreporter-ng/
+ (NSString*)gatherConsoleLogFromDate:(NSDate*)date {
aslmsg query = asl_new(ASL_TYPE_QUERY);

if(query == NULL) return nil;

const uint32_t senderQueryOptions = ASL_QUERY_OP_EQUAL|ASL_QUERY_OP_CASEFOLD|ASL_QUERY_OP_SUBSTRING;
const int aslSetSenderQueryReturnCode = asl_set_query(query, ASL_KEY_SENDER, [[[NSProcessInfo processInfo] processName] UTF8String], senderQueryOptions);
if(aslSetSenderQueryReturnCode != 0) return nil;

static const size_t timeBufferLength = 64;
char oneHourAgo[timeBufferLength];
snprintf(oneHourAgo, timeBufferLength, "%0lf", [date timeIntervalSince1970]);
const int aslSetTimeQueryReturnCode = asl_set_query(query, ASL_KEY_TIME, oneHourAgo, ASL_QUERY_OP_GREATER_EQUAL);
if(aslSetTimeQueryReturnCode != 0) return nil;

aslresponse response = asl_search(NULL, query);

NSMutableString* searchResults = [NSMutableString string];

for(;;) {
Expand All @@ -100,9 +100,9 @@ + (NSString*)gatherConsoleLogFromDate:(NSDate*)date {
NSCalendarDate* date = [NSCalendarDate dateWithTimeIntervalSince1970:atof(time)];
[searchResults appendFormat:@"%@[%s]: %s\n", [date description], level, messageText];
}

aslresponse_free(response);

return searchResults;
}

Expand Down
Loading