/
CPBundle.j
215 lines (170 loc) · 4.47 KB
/
CPBundle.j
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* CPBundle.j
* Foundation
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import "CPDictionary.j"
@import "CPNotification.j"
@import "CPNotificationCenter.j"
@import "CPObject.j"
CPBundleDidLoadNotification = @"CPBundleDidLoadNotification";
/*!
@class CPBundle
@ingroup foundation
@brief Groups information about an application's code & resources.
*/
var CPBundlesForURLStrings = { };
@implementation CPBundle : CPObject
{
CFBundle _bundle;
id _delegate;
}
+ (CPBundle)bundleWithURL:(CPURL)aURL
{
return [[self alloc] initWithURL:aURL];
}
+ (CPBundle)bundleWithPath:(CPString)aPath
{
return [self bundleWithURL:aPath];
}
+ (CPBundle)bundleWithIdentifier:(CPString)anIdentifier
{
var bundle = CFBundle.bundleWithIdentifier(anIdentifier);
if (bundle)
{
var url = bundle.bundleURL(),
cpBundle = CPBundlesForURLStrings[url.absoluteString()];
if (!cpBundle)
cpBundle = [self bundleWithURL:url];
return cpBundle;
}
return nil;
}
+ (CPBundle)bundleForClass:(Class)aClass
{
return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()];
}
+ (CPBundle)mainBundle
{
return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()];
}
- (id)initWithURL:(CPURL)aURL
{
aURL = new CFURL(aURL);
var URLString = aURL.absoluteString(),
existingBundle = CPBundlesForURLStrings[URLString];
if (existingBundle)
return existingBundle;
self = [super init];
if (self)
{
_bundle = new CFBundle(aURL);
CPBundlesForURLStrings[URLString] = self;
}
return self;
}
- (id)initWithPath:(CPString)aPath
{
return [self initWithURL:aPath];
}
- (Class)classNamed:(CPString)aString
{
// ???
}
- (CPURL)bundleURL
{
return _bundle.bundleURL();
}
- (CPString)bundlePath
{
return [[self bundleURL] path];
}
- (CPString)resourcePath
{
return [[self resourceURL] path];
}
- (CPURL)resourceURL
{
return _bundle.resourcesDirectoryURL();
}
- (Class)principalClass
{
var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
//[self load];
return className ? CPClassFromString(className) : nil;
}
- (CPString)bundleIdentifier
{
return _bundle.identifier();
}
- (BOOL)isLoaded
{
return _bundle.isLoaded();
}
- (CPString)pathForResource:(CPString)aFilename
{
return _bundle.pathForResource(aFilename);
}
- (CPDictionary)infoDictionary
{
return _bundle.infoDictionary();
}
- (id)objectForInfoDictionaryKey:(CPString)aKey
{
return _bundle.valueForInfoDictionaryKey(aKey);
}
- (void)loadWithDelegate:(id)aDelegate
{
_delegate = aDelegate;
_bundle.addEventListener("load", function()
{
[_delegate bundleDidFinishLoading:self];
// userInfo should contain a list of all classes loaded from this bundle. When writing this there
// seems to be no efficient way to get it though.
[[CPNotificationCenter defaultCenter] postNotificationName:CPBundleDidLoadNotification object:self userInfo:nil];
});
_bundle.addEventListener("error", function()
{
CPLog.error("Could not find bundle: " + self);
});
_bundle.load(YES);
}
- (CPArray)staticResourceURLs
{
var staticResourceURLs = [],
staticResources = _bundle.staticResources(),
index = 0,
count = [staticResources count];
for (; index < count; ++index)
[staticResourceURLs addObject:staticResources[index].URL()];
return staticResourceURLs;
}
- (CPArray)environments
{
return _bundle.environments();
}
- (CPString)mostEligibleEnvironment
{
return _bundle.mostEligibleEnvironment();
}
- (CPString)description
{
return [super description] + "(" + [self bundlePath] + ")";
}
@end