Browse files

Makes some changes to the ObjCPluginManager and moves all the PluginM…

…anager files to the src folder.
  • Loading branch information...
1 parent 6297453 commit a0221783710e04de30c745f6010a62b7054f4378 @Grayson committed Feb 5, 2009
View
102 ObjCPluginManager.m
@@ -1,102 +0,0 @@
-//
-// ObjCPluginManager.m
-// PluginManager
-//
-// Created by Grayson Hansard
-// Copyright 2009 From Concentrate Software. All rights reserved.
-//
-
-#import "ObjCPluginManager.h"
-
-
-@implementation ObjCPluginManager
-
-+(void)load {
- NSAutoreleasePool *pool = [NSAutoreleasePool new];
- [PluginManager registerManager:[[self new] autorelease]];
- [pool release];
-}
-
--(NSString *)name { return @"Objective-C"; }
--(NSArray *)extensions { return [NSArray arrayWithObject:@"bundle"]; }
-
-- (id)init
-{
- self = [super init];
- if (!self) return nil;
-
- return self;
-}
-
-- (void)build
-{
- if (_plugins) [_plugins release];
- _plugins = [NSMutableDictionary new];
- NSString *pluginsPath = [PluginManager pathToPluginsFolder];
- NSFileManager *fm = [NSFileManager defaultManager];
- BOOL isFolder;
- if (![fm fileExistsAtPath:pluginsPath isDirectory:&isFolder] || !isFolder) return;
-
- NSArray *plugins = [fm directoryContentsAtPath:pluginsPath];
- plugins = [plugins arrayByAddingObjectsFromArray:[fm directoryContentsAtPath:[[NSBundle mainBundle] pathForResource:@"Plugins" ofType:nil]]];
- NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
- NSString *path;
- NSArray *extensions = [self extensions];
- while (path = [pluginEnumerator nextObject])
- {
- if (![extensions containsObject:[path pathExtension]]) goto next;
-
- NSBundle *b = [NSBundle bundleWithPath:path];
- if (!b) goto next;
-
- Class c = [b principalClass];
- if (![c conformsToProtocol:@protocol(ObjCPlugin)]) goto next;
-
- id<ObjCPlugin> plugin = [c new];
- NSString *property = [plugin actionProperty];
- NSMutableArray *arr = [_plugins objectForKey:property];
- if (!arr) arr = [NSMutableArray array];
- [arr addObject:plugin];
- [_plugins setObject:arr forKey:property];
-
- next:;
- }
-}
-
--(NSArray *)pluginsForProperty:(NSString *)property forValue:(id)forValue withValue:(id)withValue
-{
- if (!_plugins) [self build];
- NSArray *plugins = [_plugins objectForKey:property];
- if (!plugins || ![plugins count]) return nil;
-
- NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
- id plugin;
- NSMutableArray *ret = [NSMutableArray array];
- while (plugin = [pluginEnumerator nextObject])
- {
- if ([plugin actionEnableForValue:forValue withValue:withValue])
- [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
- [plugin actionTitleForValue:forValue withValue:withValue], @"title",
- plugin, @"plugin",
- forValue, @"forValue",
- withValue, @"value",
- nil]];
- }
-
- return ret;
-}
-
--(void)runPlugin:(NSDictionary *)plugin forValue:(id)forValue withValue:(id)withValue
-{
- id p = [plugin objectForKey:@"plugin"];
- [p actionPerformForValue:forValue withValue:withValue];
-}
-
--(id)runScriptAtPath:(NSString *)path
-{
- return NSLocalizedString(@"Objective-C plugins cannot be done run. They must be compiled using Xcode.", @"Error string");
-}
-
--(BOOL)canRunAsScript { return NO; }
-
-@end
View
0 ApplescriptPluginManager.h → src/ApplescriptPluginManager.h
File renamed without changes.
View
0 ApplescriptPluginManager.m → src/ApplescriptPluginManager.m
File renamed without changes.
View
0 FScriptPlugInManager.h → src/FScriptPlugInManager.h
File renamed without changes.
View
0 FScriptPlugInManager.m → src/FScriptPlugInManager.m
File renamed without changes.
View
0 JavascriptPluginManager.h → src/JavascriptPluginManager.h
File renamed without changes.
View
0 JavascriptPluginManager.m → src/JavascriptPluginManager.m
File renamed without changes.
View
0 LuaPluginManager.h → src/LuaPluginManager.h
File renamed without changes.
View
0 LuaPluginManager.m → src/LuaPluginManager.m
File renamed without changes.
View
0 NuPluginManager.h → src/NuPluginManager.h
File renamed without changes.
View
0 NuPluginManager.m → src/NuPluginManager.m
File renamed without changes.
View
8 ObjCPluginManager.h → src/ObjCPluginManager.h
@@ -10,6 +10,10 @@
#import "PluginManager.h"
#import "PluginManagerProtocol.h"
+enum {
+ ObjCPMBundleLoadError = 1,
+ ObjCPMClassLoadError = 2
+}
@interface ObjCPluginManager : NSObject <PluginManagerProtocol> {
NSMutableDictionary *_plugins;
@@ -23,6 +27,9 @@
-(BOOL)canRunAsScript;
+- (NSMutableDictionary *)plugins;
+- (void)setPlugins:(NSMutableDictionary *)aValue;
+
@end
@protocol ObjCPlugin
@@ -31,5 +38,6 @@
-(BOOL)actionEnableForValue:(id)forValue withValue:(id)withValue;
-(NSString *)actionTitleForValue:(id)forValue withValue:(id)withValue;
-(void)actionPerformForValue:(id)forValue withValue:(id)withValue;
+-(id)run;
@end
View
156 src/ObjCPluginManager.m
@@ -0,0 +1,156 @@
+//
+// ObjCPluginManager.m
+// PluginManager
+//
+// Created by Grayson Hansard
+// Copyright 2009 From Concentrate Software. All rights reserved.
+//
+
+#import "ObjCPluginManager.h"
+
+
+@implementation ObjCPluginManager
+
++(void)load {
+ NSAutoreleasePool *pool = [NSAutoreleasePool new];
+ [PluginManager registerManager:[[self new] autorelease]];
+ [pool release];
+}
+
+-(NSString *)name { return @"Objective-C"; }
+-(NSArray *)extensions { return [NSArray arrayWithObject:@"bundle"]; }
+
+- (id)init
+{
+ self = [super init];
+ if (!self) return nil;
+
+ return self;
+}
+
+- (void)dealloc
+{
+ [self setPlugins:nil];
+}
+
+- (void)build
+{
+ NSMutableDictionary *plugins = [self plugins];
+ if (!plugins) {
+ plugins = [NSMutableDictionary dictionary];
+ [self setPlugins:plugins];
+ }
+
+ NSString *pluginsPath = [PluginManager pathToPluginsFolder];
+ NSFileManager *fm = [NSFileManager defaultManager];
+ BOOL isFolder;
+ if (![fm fileExistsAtPath:pluginsPath isDirectory:&isFolder] || !isFolder) return;
+
+ // Add all of the plugins available in either the plugins folder in Application Support or in the bundle resources
+ NSArray *plugins = [fm directoryContentsAtPath:pluginsPath];
+ plugins = [plugins arrayByAddingObjectsFromArray:[fm directoryContentsAtPath:[[NSBundle mainBundle] pathForResource:@"Plug-Ins" ofType:nil]]];
+ NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
+ NSString *path;
+ NSArray *extensions = [self extensions];
+ while (path = [pluginEnumerator nextObject])
+ {
+ if (![extensions containsObject:[path pathExtension]]) continue;
+
+ NSBundle *b = [NSBundle bundleWithPath:path];
+ if (!b) goto next;
+
+ Class c = [b principalClass];
+ if (![c conformsToProtocol:@protocol(ObjCPlugin)]) continue;
+
+ id<ObjCPlugin> plugin = [c new];
+ NSString *property = [plugin actionProperty];
+ NSMutableArray *arr = [plugins objectForKey:property];
+ if (!arr) arr = [NSMutableArray array];
+ [arr addObject:plugin];
+ [plugins setObject:arr forKey:property];
+ }
+}
+
+-(NSArray *)pluginsForProperty:(NSString *)property forValue:(id)forValue withValue:(id)withValue
+{
+ if (![self plugins]) [self build];
+ NSArray *plugins = [[self plugins] objectForKey:property];
+ if (!plugins || ![plugins count]) return nil;
+
+ NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
+ id plugin;
+ NSMutableArray *ret = [NSMutableArray array];
+ while (plugin = [pluginEnumerator nextObject])
+ {
+ if ([plugin actionEnableForValue:forValue withValue:withValue])
+ [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
+ [plugin actionTitleForValue:forValue withValue:withValue], @"title",
+ plugin, @"plugin",
+ forValue, @"forValue",
+ withValue, @"value",
+ nil]];
+ }
+
+ return ret;
+}
+
+-(void)runPlugin:(NSDictionary *)plugin forValue:(id)forValue withValue:(id)withValue
+{
+ id p = [plugin objectForKey:@"plugin"];
+ [p actionPerformForValue:forValue withValue:withValue];
+}
+
+-(id)runScriptAtPath:(NSString *)path
+{
+ if (!path) return nil;
+ NSMutableDictionary *plugins = [self plugins];
+ if (!plugins) plugins = [NSMutableDictionary dictionary];
+ id plugin = [plugins objectForKey:path];
+ unsigned int errorCode = 0;
+ NSString *errorString = nil;
+ if (!plugin) {
+ NSBundle *b = [NSBundle bundleWithPath:path];
+ if (![b load]) {
+ errorString = [NSString stringWithFormat:NSLocalizedString(@"File at path '%@' is not a valid bundle.", @"error message"), path];
+ errorCode = ObjCPMBundleLoadError;
+ goto error;
+ }
+ plugin = [[[b class] new] autorelease];
+ if (!plugin) {
+ errorString = [NSString stringWithFormat:NSLocalizedString(@"Could not load class from Cocoa bundle at path: %@", @"error message"), path];
+ errorCode = ObjCPMClassLoadError;
+ goto error;
+ }
+ if (![plugin respondsToSelector:@selector(run)]) {
+ errorString = [NSString stringWithFormat:NSLocalizedString(@"Loaded bundle at path '%@' does not respond to -(id)run.", @"error message"), path];
+ errorCode = 3;
+ goto error;
+ }
+ [plugins setObject:plugin forKey:path];
+ }
+
+ return [plugin run];
+
+ // If a plugin can't be loaded and used, create an NSError and return it. I dislike using exceptions here
+ // since plugins aren't really show stoppers and shouldn't stop an application to deal with them.
+ error:;
+ if (!errorString) return nil;
+ NSDictionary *errorDict = [NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
+ return [NSError errorWithDomain:@"com.fcs.objcpluginmanager" code:errorCode userInfo:errorDict];
+}
+
+-(BOOL)canRunAsScript { return YES; }
+
+- (NSMutableDictionary *)plugins
+{
+ return _plugins;
+}
+
+- (void)setPlugins:(NSMutableDictionary *)aValue
+{
+ NSMutableDictionary *oldPlugins = _plugins;
+ _plugins = [aValue retain];
+ [oldPlugins release];
+}
+
+@end
View
0 PluginManager.h → src/PluginManager.h
File renamed without changes.
View
0 PluginManager.m → src/PluginManager.m
File renamed without changes.
View
0 PluginManagerApplescriptIncludes.h → src/PluginManagerApplescriptIncludes.h
File renamed without changes.
View
0 PluginManagerProtocol.h → src/PluginManagerProtocol.h
File renamed without changes.
View
0 PythonPluginManager.h → src/PythonPluginManager.h
File renamed without changes.
View
0 PythonPluginManager.m → src/PythonPluginManager.m
File renamed without changes.
View
0 RubyPluginManager.h → src/RubyPluginManager.h
File renamed without changes.
View
0 RubyPluginManager.m → src/RubyPluginManager.m
File renamed without changes.

0 comments on commit a022178

Please sign in to comment.