2017-05-08 15:21:30 +00:00
|
|
|
/*
|
|
|
|
* Copyright @ 2017-present Atlassian Pty Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-06-06 22:43:51 +00:00
|
|
|
#include "RCTBridgeWrapper.h"
|
2017-05-08 15:21:30 +00:00
|
|
|
|
2017-06-10 00:17:01 +00:00
|
|
|
/**
|
2017-05-08 15:21:30 +00:00
|
|
|
* Wrapper around RCTBridge which also implements the RCTBridgeDelegate methods,
|
|
|
|
* allowing us to specify where the bundles are loaded from.
|
|
|
|
*/
|
2017-06-06 22:43:51 +00:00
|
|
|
@implementation RCTBridgeWrapper
|
2017-05-08 15:21:30 +00:00
|
|
|
|
2019-01-23 12:48:34 +00:00
|
|
|
- (instancetype)init {
|
2017-05-08 15:21:30 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
2017-07-03 09:37:37 +00:00
|
|
|
_bridge
|
|
|
|
= [[RCTBridge alloc] initWithDelegate:self
|
2019-01-23 12:48:34 +00:00
|
|
|
launchOptions:nil];
|
2017-05-08 15:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2021-10-13 15:44:29 +00:00
|
|
|
- (void)invalidate {
|
|
|
|
[_bridge invalidate];
|
|
|
|
}
|
|
|
|
|
2017-05-08 15:21:30 +00:00
|
|
|
#pragma mark helper methods for getting the packager URL
|
|
|
|
|
|
|
|
#if DEBUG
|
2017-06-06 22:43:51 +00:00
|
|
|
static NSURL *serverRootWithHost(NSString *host) {
|
|
|
|
return
|
|
|
|
[NSURL URLWithString:
|
|
|
|
[NSString stringWithFormat:@"http://%@:8081/", host]];
|
2017-05-08 15:21:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:43:51 +00:00
|
|
|
- (BOOL)isPackagerRunning:(NSString *)host {
|
2019-03-20 12:29:45 +00:00
|
|
|
NSURL *url = [serverRootWithHost(host) URLByAppendingPathComponent:@"status"];
|
2017-06-06 22:43:51 +00:00
|
|
|
|
2019-03-20 12:29:45 +00:00
|
|
|
NSURLSession *session = [NSURLSession sharedSession];
|
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:url];
|
|
|
|
__block NSURLResponse *response;
|
|
|
|
__block NSData *data;
|
|
|
|
|
|
|
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
|
|
|
[[session dataTaskWithRequest:request
|
|
|
|
completionHandler:^(NSData *d,
|
|
|
|
NSURLResponse *res,
|
|
|
|
__unused NSError *err) {
|
|
|
|
data = d;
|
|
|
|
response = res;
|
|
|
|
dispatch_semaphore_signal(semaphore);
|
|
|
|
}] resume];
|
|
|
|
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
|
|
|
|
|
|
|
NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
2017-05-08 15:21:30 +00:00
|
|
|
return [status isEqualToString:@"packager-status:running"];
|
|
|
|
}
|
|
|
|
|
2017-06-06 22:43:51 +00:00
|
|
|
- (NSString *)guessPackagerHost {
|
2017-05-08 15:21:30 +00:00
|
|
|
static NSString *ipGuess;
|
2017-06-10 00:17:01 +00:00
|
|
|
static dispatch_once_t dispatchOncePredicate;
|
2017-06-06 22:43:51 +00:00
|
|
|
|
2017-06-10 00:17:01 +00:00
|
|
|
dispatch_once(&dispatchOncePredicate, ^{
|
2017-06-06 22:43:51 +00:00
|
|
|
NSString *ipPath
|
|
|
|
= [[NSBundle bundleForClass:self.class] pathForResource:@"ip"
|
|
|
|
ofType:@"txt"];
|
|
|
|
|
|
|
|
ipGuess
|
|
|
|
= [[NSString stringWithContentsOfFile:ipPath
|
|
|
|
encoding:NSUTF8StringEncoding
|
|
|
|
error:nil]
|
|
|
|
stringByTrimmingCharactersInSet:
|
|
|
|
[NSCharacterSet newlineCharacterSet]];
|
2017-05-08 15:21:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
NSString *host = ipGuess ?: @"localhost";
|
2017-06-06 22:43:51 +00:00
|
|
|
|
2017-05-08 15:21:30 +00:00
|
|
|
if ([self isPackagerRunning:host]) {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#pragma mark RCTBridgeDelegate methods
|
|
|
|
|
2017-06-06 22:43:51 +00:00
|
|
|
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge {
|
2017-05-08 15:21:30 +00:00
|
|
|
#if DEBUG
|
2017-06-06 22:43:51 +00:00
|
|
|
// In debug mode, try to fetch the bundle from the packager, or fallback to
|
|
|
|
// the one inside the framework. The IP address for the packager host is
|
|
|
|
// fetched from the ip.txt file inside the framework.
|
|
|
|
//
|
|
|
|
// This duplicates some functionality present in RCTBundleURLProvider, but
|
|
|
|
// that mode is not designed to work inside a framework, because all
|
|
|
|
// resources are loaded from the main bundle.
|
2017-05-08 15:21:30 +00:00
|
|
|
NSString *host = [self guessPackagerHost];
|
2017-06-06 22:43:51 +00:00
|
|
|
|
2017-05-08 15:21:30 +00:00
|
|
|
if (host != nil) {
|
2019-03-14 13:30:30 +00:00
|
|
|
NSString *path = @"/index.bundle";
|
2017-05-08 15:21:30 +00:00
|
|
|
NSString *query = @"platform=ios&dev=true&minify=false";
|
|
|
|
NSURLComponents *components
|
2017-06-06 22:43:51 +00:00
|
|
|
= [NSURLComponents componentsWithURL:serverRootWithHost(host)
|
|
|
|
resolvingAgainstBaseURL:NO];
|
|
|
|
|
2017-05-08 15:21:30 +00:00
|
|
|
components.path = path;
|
|
|
|
components.query = query;
|
|
|
|
|
|
|
|
return components.URL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return [[NSBundle bundleForClass:self.class] URLForResource:@"main"
|
|
|
|
withExtension:@"jsbundle"];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|