This repository has been archived on 2022-03-12. You can view files and clone it, but cannot push or open issues or pull requests.
2021-04-02 02:24:13 +03:00

85 lines
3.7 KiB
Objective-C

// Copyright © 2019 650 Industries. All rights reserved.
#import <EXUpdates/EXUpdatesBareUpdate.h>
#import <EXUpdates/EXUpdatesEmbeddedAppLoader.h>
#import <EXUpdates/EXUpdatesUpdate+Private.h>
#import <EXUpdates/EXUpdatesUtils.h>
NS_ASSUME_NONNULL_BEGIN
@implementation EXUpdatesBareUpdate
+ (EXUpdatesUpdate *)updateWithBareManifest:(NSDictionary *)manifest
config:(EXUpdatesConfig *)config
database:(EXUpdatesDatabase *)database
{
EXUpdatesUpdate *update = [[EXUpdatesUpdate alloc] initWithRawManifest:manifest
config:config
database:database];
id updateId = manifest[@"id"];
id commitTime = manifest[@"commitTime"];
id metadata = manifest[@"metadata"];
id assets = manifest[@"assets"];
NSAssert([updateId isKindOfClass:[NSString class]], @"update ID should be a string");
NSAssert([commitTime isKindOfClass:[NSNumber class]], @"commitTime should be a number");
NSAssert(!metadata || [metadata isKindOfClass:[NSDictionary class]], @"metadata should be null or an object");
NSAssert(assets && [assets isKindOfClass:[NSArray class]], @"assets should be a nonnull array");
NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:(NSString *)updateId];
NSAssert(uuid, @"update ID should be a valid UUID");
NSMutableArray<EXUpdatesAsset *> *processedAssets = [NSMutableArray new];
NSString *bundleKey = [NSString stringWithFormat:@"bundle-%@", commitTime];
EXUpdatesAsset *jsBundleAsset = [[EXUpdatesAsset alloc] initWithKey:bundleKey type:EXUpdatesBareEmbeddedBundleFileType];
jsBundleAsset.isLaunchAsset = YES;
jsBundleAsset.mainBundleFilename = EXUpdatesBareEmbeddedBundleFilename;
[processedAssets addObject:jsBundleAsset];
for (NSDictionary *assetDict in (NSArray *)assets) {
NSAssert([assetDict isKindOfClass:[NSDictionary class]], @"assets must be objects");
id packagerHash = assetDict[@"packagerHash"];
id type = assetDict[@"type"];
id mainBundleDir = assetDict[@"nsBundleDir"];
id mainBundleFilename = assetDict[@"nsBundleFilename"];
NSAssert(packagerHash && [packagerHash isKindOfClass:[NSString class]], @"asset key should be a nonnull string");
NSAssert(type && [type isKindOfClass:[NSString class]], @"asset type should be a nonnull string");
NSAssert(mainBundleFilename && [mainBundleFilename isKindOfClass:[NSString class]], @"asset nsBundleFilename should be a nonnull string");
if (mainBundleDir) {
NSAssert([mainBundleDir isKindOfClass:[NSString class]], @"asset nsBundleDir should be a string");
}
NSString *key = [NSString stringWithFormat:@"%@.%@", packagerHash, type];
EXUpdatesAsset *asset = [[EXUpdatesAsset alloc] initWithKey:key type:(NSString *)type];
asset.mainBundleDir = mainBundleDir;
asset.mainBundleFilename = mainBundleFilename;
[processedAssets addObject:asset];
}
update.updateId = uuid;
update.commitTime = [NSDate dateWithTimeIntervalSince1970:[(NSNumber *)commitTime doubleValue] / 1000];
update.runtimeVersion = [EXUpdatesUtils getRuntimeVersionWithConfig:config];
if (metadata) {
update.metadata = (NSDictionary *)metadata;
}
update.status = EXUpdatesUpdateStatusEmbedded;
update.keep = YES;
update.assets = processedAssets;
if ([update.runtimeVersion containsString:@","]) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Should not be initializing EXUpdatesBareUpdate in an environment with multiple runtime versions."
userInfo:@{}];
}
return update;
}
@end
NS_ASSUME_NONNULL_END