-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYVersionTriplet.m
executable file
·218 lines (174 loc) · 6.74 KB
/
SSYVersionTriplet.m
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
216
217
218
#import "SSYVersionTriplet.h"
@implementation SSYVersionTriplet
+ (NSString*)rawVersionStringFromBundle:(NSBundle*)bundle {
NSString* versionNumberString = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"] ;
// Shiira does not have a CFBundleShortVersionString, but it has CFBundleVersion
if (versionNumberString == nil) {
versionNumberString = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"] ;
}
return versionNumberString;
}
+ (NSString*)rawVersionStringFromBundleIdentifier:(NSString*)bundleIdentifier {
NSString* bundlePath = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:bundleIdentifier] ;
// Note: The above is more reliable than -fullPathForApplication,
// but still not reliable enough. The problem is that its behavior
// when more than one version of an app is installed is not defined.
// BookMacster Beta Tester says that the behavior matches that of
// LSFindApplicationForInfo, which, according to documentation, is
// the proper way to locate bundles. Moreover, the bundle methods are
// governed by some preference rules:
// http://developer.apple.com/mac/library/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCConcepts/LSCConcepts.html#//apple_ref/doc/uid/TP30000999-CH202-BABBJJEF
// According to the above, bundles in the main disk are preferred, and,
// I am so sorry that I forgot to mention that, I am using FileVault, so
// the bundle in my '~/Applications' is not preferred even if it is a
// later version, due to 'late mounted' (another mount) effect of FileVault.
// Nevertheless, I noticed that the Adobe bundled Opera is located inside
// 'Device Central.app' and probably it got registered by the application or
// its installer with LSRegisterFSRef, so I rebuilt the Launch Services database
// using lsregister (/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister)
// with the option -r, so not to recurse in packages, and
// now the test detection system and the BookMacster application work perfectly.
// Concluding, there are problems with
// (a) App installed in FileVault
// (b) App installed in ~/Applications
NSBundle* bundle = [NSBundle bundleWithPath:bundlePath] ;
NSString* versionNumberString = [self rawVersionStringFromBundle:bundle] ;
return versionNumberString ;
}
+ (SSYVersionTriplet*)versionTripletFromString:(NSString*)versionString {
SSYVersionTriplet* versionTriplet = nil ;
BOOL hasADecimalDigit = [versionString rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound ;
if ((versionString != nil) && hasADecimalDigit) {
NSArray* parts = [versionString componentsSeparatedByString:@"."] ;
NSInteger major = 0 ;
NSInteger minor = 0 ;
NSInteger bugFix = 0 ;
NSInteger nParts = [parts count] ;
NSArray* majorWords = [[parts objectAtIndex:0] componentsSeparatedByString:@" "] ;
major = [[majorWords lastObject] integerValue] ;
if (nParts>1)
{
minor = [[parts objectAtIndex:1] integerValue] ;
if (nParts>2)
bugFix = [[parts objectAtIndex:2] integerValue] ;
}
versionTriplet = [SSYVersionTriplet versionTripletWithMajor:major
minor:minor
bugFix:bugFix] ;
}
return versionTriplet ;
}
+ (SSYVersionTriplet*)versionTripletFromBundle:(NSBundle*)bundle {
NSString* versionString = [self rawVersionStringFromBundle:bundle] ;
return [self versionTripletFromString:versionString] ;
}
+ (SSYVersionTriplet*)versionTripletFromBundleIdentifier:(NSString*)bundleIdentifier {
NSString* versionString = [self rawVersionStringFromBundleIdentifier:bundleIdentifier] ;
return [self versionTripletFromString:versionString] ;
}
- (void)setMajor:(NSInteger)value {
versionStruct_ivar.major = value ;
}
- (void)setMinor:(NSInteger)value {
versionStruct_ivar.minor = value ;
}
- (void)setBugFix:(NSInteger)value {
versionStruct_ivar.bugFix = value ;
}
- (NSInteger)major {
return versionStruct_ivar.major ;
}
- (NSInteger)minor {
return versionStruct_ivar.minor ;
}
- (NSInteger)bugFix {
return versionStruct_ivar.bugFix ;
}
- (id)initWithStruct:(SSYVersionStruct const)versionStruct {
self = [super init] ;
if (self != nil) {
versionStruct_ivar = versionStruct ;
}
return self ;
}
+ (SSYVersionTriplet*)versionTripletWithStruct:(SSYVersionStruct const)versionStruct {
SSYVersionTriplet* instance = [[SSYVersionTriplet alloc] initWithStruct:versionStruct] ;
return [instance autorelease] ;
}
+ (SSYVersionTriplet*)versionTripletWithMajor:(NSInteger)major
minor:(NSInteger)minor
bugFix:(NSInteger)bugFix {
SSYVersionStruct versionStruct ;
versionStruct.major = major ;
versionStruct.minor = minor ;
versionStruct.bugFix = bugFix ;
SSYVersionTriplet* instance = [[SSYVersionTriplet alloc] initWithStruct:versionStruct] ;
return [instance autorelease] ;
}
+ (SSYVersionTriplet*)zeroVersionTriplet {
return [self versionTripletWithMajor:0 minor:0 bugFix:0] ;
}
- (enum SSYVersionTripletComparisonResult)compare:(SSYVersionTriplet*)otherTriplet {
enum SSYVersionTripletComparisonResult result ;
if (versionStruct_ivar.major == [otherTriplet major]) {
// Majors are equal, could be a minor or a bug fix
if (versionStruct_ivar.minor == [otherTriplet minor]) {
// Major and minor are equal, could be a bug fix
if (versionStruct_ivar.bugFix > [otherTriplet bugFix]) {
result = SSYDescendingBugFix ;
}
else if (versionStruct_ivar.bugFix < [otherTriplet bugFix]) {
result = SSYAscendingBugFix ;
}
else {
result = SSYEqual ;
}
}
else {
// Majors are equal but minors are not
if (versionStruct_ivar.minor < [otherTriplet minor]) {
result = SSYAscendingMinor ;
}
else {
result = SSYDescendingMinor ;
}
}
}
else {
// Majors are unequal
if (versionStruct_ivar.major < [otherTriplet major]) {
result = SSYAscendingMajor ;
}
else {
result = SSYDescendingMajor ;
}
}
return result ;
}
- (BOOL)isEarlierThan:(SSYVersionTriplet*)otherTriplet {
return ([self compare:otherTriplet] > SSYEqual) ;
}
- (BOOL)isLaterThan:(SSYVersionTriplet*)otherTriplet {
return ([self compare:otherTriplet] < SSYEqual) ;
}
- (BOOL)isEqual:(id)otherTriplet {
return ([self compare:otherTriplet] == SSYEqual) ;
}
// Documentation says to override -hash if you override -isEqual:
- (NSUInteger)hash {
return [[self string] hash] ;
}
- (NSString*)string {
return [NSString stringWithFormat:@"%ld.%ld.%ld",
(long)[self major],
(long)[self minor],
(long)[self bugFix]] ;
}
- (NSString*)description {
return [self string] ;
}
+ (NSString*)cleanVersionStringFromBundleIdentifier:(NSString*)bundleIdentifier {
SSYVersionTriplet* vt = [self versionTripletFromBundleIdentifier:bundleIdentifier] ;
return [vt string] ;
}
@end