From 5fba1fe0466e28cef30cee1d0b65078886c8f107 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 26 Jun 2021 23:18:22 -0700 Subject: [PATCH] Add channels support to Sparkle This allows developers to post updates to specific channels like for beta updates. Updaters only get notifications from channels they're allowed to be on. --- Sparkle.xcodeproj/project.pbxproj | 4 ++-- Sparkle/SPUUpdaterDelegate.h | 29 +++++++++++++++++++++++++++++ Sparkle/SUAppcast.m | 8 ++++---- Sparkle/SUAppcastDriver.m | 23 +++++++++++++++++++++-- Sparkle/SUAppcastItem.h | 1 + Sparkle/SUAppcastItem.m | 8 ++++++++ Sparkle/SUConstants.h | 2 ++ Sparkle/SUConstants.m | 2 ++ Sparkle/Sparkle.h | 1 + 9 files changed, 70 insertions(+), 8 deletions(-) diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 91729ac5..34fae351 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -81,7 +81,7 @@ 5D1AF5900FD767AD0065DB48 /* libxml2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D1AF58F0FD767AD0065DB48 /* libxml2.dylib */; }; 5D1AF59A0FD767E50065DB48 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D1AF5990FD767E50065DB48 /* libz.dylib */; }; 5F1510A21C96E591006E1629 /* testnamespaces.xml in Resources */ = {isa = PBXBuildFile; fileRef = 5F1510A11C96E591006E1629 /* testnamespaces.xml */; }; - 61299A5C09CA6D4500B7442F /* SUConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 61299A5B09CA6D4500B7442F /* SUConstants.h */; settings = {ATTRIBUTES = (); }; }; + 61299A5C09CA6D4500B7442F /* SUConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 61299A5B09CA6D4500B7442F /* SUConstants.h */; settings = {ATTRIBUTES = (Public, ); }; }; 61299A6009CA6EB100B7442F /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; 61299B3609CB04E000B7442F /* Sparkle.h in Headers */ = {isa = PBXBuildFile; fileRef = 61299B3509CB04E000B7442F /* Sparkle.h */; settings = {ATTRIBUTES = (Public, ); }; }; 612DCBB00D488BC60015DBEA /* SUUpdatePermissionPrompt.m in Sources */ = {isa = PBXBuildFile; fileRef = 612DCBAE0D488BC60015DBEA /* SUUpdatePermissionPrompt.m */; }; @@ -2313,6 +2313,7 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 61299A5C09CA6D4500B7442F /* SUConstants.h in Headers */, 61299B3609CB04E000B7442F /* Sparkle.h in Headers */, 72B767E61C9CFD7200A07552 /* SPUAutomaticUpdateDriver.h in Headers */, 72B767D61C9C8B5C00A07552 /* SPUBasicUpdateDriver.h in Headers */, @@ -2357,7 +2358,6 @@ 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */, 7214B8811D456A8500CB5CED /* SUBundleIcon.h in Headers */, 720767D31E2EE9C200F9A850 /* SUTouchBarForwardDeclarations.h in Headers */, - 61299A5C09CA6D4500B7442F /* SUConstants.h in Headers */, 55E6F33319EC9F6C00005E76 /* SUErrors.h in Headers */, 14652F8419A978C200959E44 /* SUExport.h in Headers */, 7267E5E51D3D90AA00D1BF90 /* SUFileManager.h in Headers */, diff --git a/Sparkle/SPUUpdaterDelegate.h b/Sparkle/SPUUpdaterDelegate.h index 18bad993..798da22f 100644 --- a/Sparkle/SPUUpdaterDelegate.h +++ b/Sparkle/SPUUpdaterDelegate.h @@ -160,6 +160,35 @@ typedef NS_ENUM(NSInteger, SPUUpdateCheck) */ - (void)updater:(SPUUpdater *)updater didFinishLoadingAppcast:(SUAppcast *)appcast; +/*! + Returns the set of Sparkle channels the updater is allowed to find new updates from. + + An appcast item can specify a set of channels its update is posted to. Without specifying any channels, appcast items are posted to the default channel. + For instance: + + 2.0 Beta 1 + + + + + + This example posts an update to the sparkle:beta channel, so only updaters that are allowed to use the sparkle:beta channel can find this update. + + If the is empty or not present, the update item is posted to the default channel and can be found by any complying updater. + + Sparkle allows you to use the 'sparkle:beta' channel name (SUAppcastElementBetaChannel) for distributing beta updates to your own applications. + + Otherwise you can use any other name you'd like; however, for other names you will need to use and define your own XML namespace tailored for your app. + To define your own namespace, add an attribute to rss element like xmlns:myapp="https://myappwebsite.net" + + Note to use this feature, all app versions that your users may update from in your feed must use a version of Sparkle that supports this feature. + This feature was added in Sparkle 2. + + \return The set of channel names the updater is allowed to find new updates in. An empty set is the default behavior, + which means the updater will only look for updates in the default channel. + */ +- (NSSet *)allowedChannelsForUpdater:(SPUUpdater *)updater; + /*! Returns the item in the appcast corresponding to the update that should be installed. diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index c3d927cc..ef2ee5a5 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -144,16 +144,16 @@ } [dict setObject:deltas forKey:name]; } - else if ([name isEqualToString:SUAppcastElementTags]) { - NSMutableArray *tags = [NSMutableArray array]; + else if ([name isEqualToString:SUAppcastElementTags] || [name isEqualToString:SUAppcastElementChannels]) { + NSMutableArray *names = [NSMutableArray array]; NSEnumerator *childEnum = [[node children] objectEnumerator]; for (NSXMLNode *child in childEnum) { NSString *childName = child.name; if (childName) { - [tags addObject:childName]; + [names addObject:childName]; } } - [dict setObject:tags forKey:name]; + [dict setObject:names forKey:name]; } else if ([name isEqualToString:SUAppcastElementInformationalUpdate]) { NSMutableSet *informationalUpdateVersions = [NSMutableSet set]; diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 82834df5..4406e813 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -166,9 +166,28 @@ NSDictionary *userInfo = @{ SUUpdaterAppcastNotificationKey: loadedAppcast }; [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterDidFinishLoadingAppCastNotification object:self.updater userInfo:userInfo]; - // We will never care about other OS's + NSSet *allowedChannels; + if ([self.updaterDelegate respondsToSelector:@selector(allowedChannelsForUpdater:)]) { + allowedChannels = [self.updaterDelegate allowedChannelsForUpdater:self.updater]; + } else { + allowedChannels = [NSSet set]; + } + SUAppcast *macOSAppcast = [loadedAppcast copyByFilteringItems:^(SUAppcastItem *item) { - return (BOOL)[item isMacOsUpdate]; + // We will never care about other OS's + BOOL macOSUpdate = [item isMacOsUpdate]; + if (!macOSUpdate) { + return NO; + } + + NSSet *channels = item.channels; + if (channels.count == 0) { + // Item is on the default channel + return YES; + } + + // Item channels and allowed channels must have one channel in common + return [channels intersectsSet:allowedChannels]; }]; id applicationVersionComparator = [self versionComparator]; diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 4d3efba1..4fcaa58f 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -52,6 +52,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly, nullable) NSNumber* phasedRolloutInterval; @property (nonatomic, copy, readonly) NSString *installationType; @property (copy, readonly, nullable) NSString *minimumAutoupdateVersion; +@property (nonatomic, readonly) NSSet *channels; @property (getter=isDeltaUpdate, readonly) BOOL deltaUpdate; @property (getter=isCriticalUpdate, readonly) BOOL criticalUpdate; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index b7aaee47..80900171 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -73,6 +73,7 @@ static NSString *SUAppcastItemStateKey = @"SUAppcastItemState"; @synthesize state = _state; @synthesize hasCriticalInformation = _hasCriticalInformation; @synthesize informationalUpdateVersions = _informationalUpdateVersions; +@synthesize channels = _channels; + (BOOL)supportsSecureCoding { @@ -344,6 +345,13 @@ static NSString *SUAppcastItemStateKey = @"SUAppcastItemState"; } return nil; } + + NSArray *channels = [dict objectForKey:SUAppcastElementChannels]; + if (channels == nil) { + _channels = [NSSet set]; + } else { + _channels = [NSSet setWithArray:channels]; + } _propertiesDictionary = [[NSDictionary alloc] initWithDictionary:dict]; _title = [(NSString *)[dict objectForKey:SURSSElementTitle] copy]; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index a1e65b66..f147b6f1 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -86,6 +86,8 @@ extern NSString *const SUAppcastElementReleaseNotesLink; extern NSString *const SUAppcastElementTags; extern NSString *const SUAppcastElementPhasedRolloutInterval; extern NSString *const SUAppcastElementInformationalUpdate; +extern NSString *const SUAppcastElementChannels; +extern NSString *const SUAppcastElementBetaChannel; extern NSString *const SURSSAttributeURL; extern NSString *const SURSSAttributeLength; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 0a60324e..f72b648a 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -80,6 +80,8 @@ NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink"; NSString *const SUAppcastElementTags = @"sparkle:tags"; NSString *const SUAppcastElementPhasedRolloutInterval = @"sparkle:phasedRolloutInterval"; NSString *const SUAppcastElementInformationalUpdate = @"sparkle:informationalUpdate"; +NSString *const SUAppcastElementChannels = @"sparkle:channels"; +NSString *const SUAppcastElementBetaChannel = @"sparkle:beta"; NSString *const SURSSAttributeURL = @"url"; NSString *const SURSSAttributeLength = @"length"; diff --git a/Sparkle/Sparkle.h b/Sparkle/Sparkle.h index a048d267..b9bd8afa 100644 --- a/Sparkle/Sparkle.h +++ b/Sparkle/Sparkle.h @@ -22,6 +22,7 @@ #import #import #import +#import #import #import #import