Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

…anager files to the src folder.
  • Loading branch information...
commit a0221783710e04de30c745f6010a62b7054f4378 1 parent 6297453
Grayson Hansard authored
102 ObjCPluginManager.m
... ... @@ -1,102 +0,0 @@
1   -//
2   -// ObjCPluginManager.m
3   -// PluginManager
4   -//
5   -// Created by Grayson Hansard
6   -// Copyright 2009 From Concentrate Software. All rights reserved.
7   -//
8   -
9   -#import "ObjCPluginManager.h"
10   -
11   -
12   -@implementation ObjCPluginManager
13   -
14   -+(void)load {
15   - NSAutoreleasePool *pool = [NSAutoreleasePool new];
16   - [PluginManager registerManager:[[self new] autorelease]];
17   - [pool release];
18   -}
19   -
20   --(NSString *)name { return @"Objective-C"; }
21   --(NSArray *)extensions { return [NSArray arrayWithObject:@"bundle"]; }
22   -
23   -- (id)init
24   -{
25   - self = [super init];
26   - if (!self) return nil;
27   -
28   - return self;
29   -}
30   -
31   -- (void)build
32   -{
33   - if (_plugins) [_plugins release];
34   - _plugins = [NSMutableDictionary new];
35   - NSString *pluginsPath = [PluginManager pathToPluginsFolder];
36   - NSFileManager *fm = [NSFileManager defaultManager];
37   - BOOL isFolder;
38   - if (![fm fileExistsAtPath:pluginsPath isDirectory:&isFolder] || !isFolder) return;
39   -
40   - NSArray *plugins = [fm directoryContentsAtPath:pluginsPath];
41   - plugins = [plugins arrayByAddingObjectsFromArray:[fm directoryContentsAtPath:[[NSBundle mainBundle] pathForResource:@"Plugins" ofType:nil]]];
42   - NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
43   - NSString *path;
44   - NSArray *extensions = [self extensions];
45   - while (path = [pluginEnumerator nextObject])
46   - {
47   - if (![extensions containsObject:[path pathExtension]]) goto next;
48   -
49   - NSBundle *b = [NSBundle bundleWithPath:path];
50   - if (!b) goto next;
51   -
52   - Class c = [b principalClass];
53   - if (![c conformsToProtocol:@protocol(ObjCPlugin)]) goto next;
54   -
55   - id<ObjCPlugin> plugin = [c new];
56   - NSString *property = [plugin actionProperty];
57   - NSMutableArray *arr = [_plugins objectForKey:property];
58   - if (!arr) arr = [NSMutableArray array];
59   - [arr addObject:plugin];
60   - [_plugins setObject:arr forKey:property];
61   -
62   - next:;
63   - }
64   -}
65   -
66   --(NSArray *)pluginsForProperty:(NSString *)property forValue:(id)forValue withValue:(id)withValue
67   -{
68   - if (!_plugins) [self build];
69   - NSArray *plugins = [_plugins objectForKey:property];
70   - if (!plugins || ![plugins count]) return nil;
71   -
72   - NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
73   - id plugin;
74   - NSMutableArray *ret = [NSMutableArray array];
75   - while (plugin = [pluginEnumerator nextObject])
76   - {
77   - if ([plugin actionEnableForValue:forValue withValue:withValue])
78   - [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
79   - [plugin actionTitleForValue:forValue withValue:withValue], @"title",
80   - plugin, @"plugin",
81   - forValue, @"forValue",
82   - withValue, @"value",
83   - nil]];
84   - }
85   -
86   - return ret;
87   -}
88   -
89   --(void)runPlugin:(NSDictionary *)plugin forValue:(id)forValue withValue:(id)withValue
90   -{
91   - id p = [plugin objectForKey:@"plugin"];
92   - [p actionPerformForValue:forValue withValue:withValue];
93   -}
94   -
95   --(id)runScriptAtPath:(NSString *)path
96   -{
97   - return NSLocalizedString(@"Objective-C plugins cannot be done run. They must be compiled using Xcode.", @"Error string");
98   -}
99   -
100   --(BOOL)canRunAsScript { return NO; }
101   -
102   -@end
0  ApplescriptPluginManager.h → src/ApplescriptPluginManager.h
File renamed without changes
0  ApplescriptPluginManager.m → src/ApplescriptPluginManager.m
File renamed without changes
0  FScriptPlugInManager.h → src/FScriptPlugInManager.h
File renamed without changes
0  FScriptPlugInManager.m → src/FScriptPlugInManager.m
File renamed without changes
0  JavascriptPluginManager.h → src/JavascriptPluginManager.h
File renamed without changes
0  JavascriptPluginManager.m → src/JavascriptPluginManager.m
File renamed without changes
0  LuaPluginManager.h → src/LuaPluginManager.h
File renamed without changes
0  LuaPluginManager.m → src/LuaPluginManager.m
File renamed without changes
0  NuPluginManager.h → src/NuPluginManager.h
File renamed without changes
0  NuPluginManager.m → src/NuPluginManager.m
File renamed without changes
8 ObjCPluginManager.h → src/ObjCPluginManager.h
@@ -10,6 +10,10 @@
10 10 #import "PluginManager.h"
11 11 #import "PluginManagerProtocol.h"
12 12
  13 +enum {
  14 + ObjCPMBundleLoadError = 1,
  15 + ObjCPMClassLoadError = 2
  16 +}
13 17
14 18 @interface ObjCPluginManager : NSObject <PluginManagerProtocol> {
15 19 NSMutableDictionary *_plugins;
@@ -23,6 +27,9 @@
23 27
24 28 -(BOOL)canRunAsScript;
25 29
  30 +- (NSMutableDictionary *)plugins;
  31 +- (void)setPlugins:(NSMutableDictionary *)aValue;
  32 +
26 33 @end
27 34
28 35 @protocol ObjCPlugin
@@ -31,5 +38,6 @@
31 38 -(BOOL)actionEnableForValue:(id)forValue withValue:(id)withValue;
32 39 -(NSString *)actionTitleForValue:(id)forValue withValue:(id)withValue;
33 40 -(void)actionPerformForValue:(id)forValue withValue:(id)withValue;
  41 +-(id)run;
34 42
35 43 @end
156 src/ObjCPluginManager.m
... ... @@ -0,0 +1,156 @@
  1 +//
  2 +// ObjCPluginManager.m
  3 +// PluginManager
  4 +//
  5 +// Created by Grayson Hansard
  6 +// Copyright 2009 From Concentrate Software. All rights reserved.
  7 +//
  8 +
  9 +#import "ObjCPluginManager.h"
  10 +
  11 +
  12 +@implementation ObjCPluginManager
  13 +
  14 ++(void)load {
  15 + NSAutoreleasePool *pool = [NSAutoreleasePool new];
  16 + [PluginManager registerManager:[[self new] autorelease]];
  17 + [pool release];
  18 +}
  19 +
  20 +-(NSString *)name { return @"Objective-C"; }
  21 +-(NSArray *)extensions { return [NSArray arrayWithObject:@"bundle"]; }
  22 +
  23 +- (id)init
  24 +{
  25 + self = [super init];
  26 + if (!self) return nil;
  27 +
  28 + return self;
  29 +}
  30 +
  31 +- (void)dealloc
  32 +{
  33 + [self setPlugins:nil];
  34 +}
  35 +
  36 +- (void)build
  37 +{
  38 + NSMutableDictionary *plugins = [self plugins];
  39 + if (!plugins) {
  40 + plugins = [NSMutableDictionary dictionary];
  41 + [self setPlugins:plugins];
  42 + }
  43 +
  44 + NSString *pluginsPath = [PluginManager pathToPluginsFolder];
  45 + NSFileManager *fm = [NSFileManager defaultManager];
  46 + BOOL isFolder;
  47 + if (![fm fileExistsAtPath:pluginsPath isDirectory:&isFolder] || !isFolder) return;
  48 +
  49 + // Add all of the plugins available in either the plugins folder in Application Support or in the bundle resources
  50 + NSArray *plugins = [fm directoryContentsAtPath:pluginsPath];
  51 + plugins = [plugins arrayByAddingObjectsFromArray:[fm directoryContentsAtPath:[[NSBundle mainBundle] pathForResource:@"Plug-Ins" ofType:nil]]];
  52 + NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
  53 + NSString *path;
  54 + NSArray *extensions = [self extensions];
  55 + while (path = [pluginEnumerator nextObject])
  56 + {
  57 + if (![extensions containsObject:[path pathExtension]]) continue;
  58 +
  59 + NSBundle *b = [NSBundle bundleWithPath:path];
  60 + if (!b) goto next;
  61 +
  62 + Class c = [b principalClass];
  63 + if (![c conformsToProtocol:@protocol(ObjCPlugin)]) continue;
  64 +
  65 + id<ObjCPlugin> plugin = [c new];
  66 + NSString *property = [plugin actionProperty];
  67 + NSMutableArray *arr = [plugins objectForKey:property];
  68 + if (!arr) arr = [NSMutableArray array];
  69 + [arr addObject:plugin];
  70 + [plugins setObject:arr forKey:property];
  71 + }
  72 +}
  73 +
  74 +-(NSArray *)pluginsForProperty:(NSString *)property forValue:(id)forValue withValue:(id)withValue
  75 +{
  76 + if (![self plugins]) [self build];
  77 + NSArray *plugins = [[self plugins] objectForKey:property];
  78 + if (!plugins || ![plugins count]) return nil;
  79 +
  80 + NSEnumerator *pluginEnumerator = [plugins objectEnumerator];
  81 + id plugin;
  82 + NSMutableArray *ret = [NSMutableArray array];
  83 + while (plugin = [pluginEnumerator nextObject])
  84 + {
  85 + if ([plugin actionEnableForValue:forValue withValue:withValue])
  86 + [ret addObject:[NSDictionary dictionaryWithObjectsAndKeys:
  87 + [plugin actionTitleForValue:forValue withValue:withValue], @"title",
  88 + plugin, @"plugin",
  89 + forValue, @"forValue",
  90 + withValue, @"value",
  91 + nil]];
  92 + }
  93 +
  94 + return ret;
  95 +}
  96 +
  97 +-(void)runPlugin:(NSDictionary *)plugin forValue:(id)forValue withValue:(id)withValue
  98 +{
  99 + id p = [plugin objectForKey:@"plugin"];
  100 + [p actionPerformForValue:forValue withValue:withValue];
  101 +}
  102 +
  103 +-(id)runScriptAtPath:(NSString *)path
  104 +{
  105 + if (!path) return nil;
  106 + NSMutableDictionary *plugins = [self plugins];
  107 + if (!plugins) plugins = [NSMutableDictionary dictionary];
  108 + id plugin = [plugins objectForKey:path];
  109 + unsigned int errorCode = 0;
  110 + NSString *errorString = nil;
  111 + if (!plugin) {
  112 + NSBundle *b = [NSBundle bundleWithPath:path];
  113 + if (![b load]) {
  114 + errorString = [NSString stringWithFormat:NSLocalizedString(@"File at path '%@' is not a valid bundle.", @"error message"), path];
  115 + errorCode = ObjCPMBundleLoadError;
  116 + goto error;
  117 + }
  118 + plugin = [[[b class] new] autorelease];
  119 + if (!plugin) {
  120 + errorString = [NSString stringWithFormat:NSLocalizedString(@"Could not load class from Cocoa bundle at path: %@", @"error message"), path];
  121 + errorCode = ObjCPMClassLoadError;
  122 + goto error;
  123 + }
  124 + if (![plugin respondsToSelector:@selector(run)]) {
  125 + errorString = [NSString stringWithFormat:NSLocalizedString(@"Loaded bundle at path '%@' does not respond to -(id)run.", @"error message"), path];
  126 + errorCode = 3;
  127 + goto error;
  128 + }
  129 + [plugins setObject:plugin forKey:path];
  130 + }
  131 +
  132 + return [plugin run];
  133 +
  134 + // If a plugin can't be loaded and used, create an NSError and return it. I dislike using exceptions here
  135 + // since plugins aren't really show stoppers and shouldn't stop an application to deal with them.
  136 + error:;
  137 + if (!errorString) return nil;
  138 + NSDictionary *errorDict = [NSDictionary dictionaryWithObject:errorString forKey:NSLocalizedDescriptionKey];
  139 + return [NSError errorWithDomain:@"com.fcs.objcpluginmanager" code:errorCode userInfo:errorDict];
  140 +}
  141 +
  142 +-(BOOL)canRunAsScript { return YES; }
  143 +
  144 +- (NSMutableDictionary *)plugins
  145 +{
  146 + return _plugins;
  147 +}
  148 +
  149 +- (void)setPlugins:(NSMutableDictionary *)aValue
  150 +{
  151 + NSMutableDictionary *oldPlugins = _plugins;
  152 + _plugins = [aValue retain];
  153 + [oldPlugins release];
  154 +}
  155 +
  156 +@end
0  PluginManager.h → src/PluginManager.h
File renamed without changes
0  PluginManager.m → src/PluginManager.m
File renamed without changes
0  PluginManagerApplescriptIncludes.h → src/PluginManagerApplescriptIncludes.h
File renamed without changes
0  PluginManagerProtocol.h → src/PluginManagerProtocol.h
File renamed without changes
0  PythonPluginManager.h → src/PythonPluginManager.h
File renamed without changes
0  PythonPluginManager.m → src/PythonPluginManager.m
File renamed without changes
0  RubyPluginManager.h → src/RubyPluginManager.h
File renamed without changes
0  RubyPluginManager.m → src/RubyPluginManager.m
File renamed without changes

0 comments on commit a022178

Please sign in to comment.
Something went wrong with that request. Please try again.