Skip to content

Commit

Permalink
Use instrumentsPath launch argument if available, else try to load …
Browse files Browse the repository at this point in the history
…hardcoded profiler path from /Applications

Delay loading to a fraction later to allow logging facility to load.
  • Loading branch information
LeoNatan authored and noomorph committed Mar 12, 2019
1 parent 0fe1893 commit 095a652
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
95 changes: 57 additions & 38 deletions detox/ios/Detox/DetoxInstrumentsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,66 @@ @implementation DetoxInstrumentsManager
id _recorderInstance;
}

+ (void)load
+ (void)_loadProfiler
{
__DTXProfiler = NSClassFromString(@"DTXProfiler");

if(__DTXProfiler == NULL)
{
//The user has not linked the Profiler framework. Load it manually.
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__DTXProfiler = NSClassFromString(@"DTXProfiler");

//TODO: Use launch argument (if it exists) and only then fallback to hardcoded path.
NSBundle* profilerBundle = [NSBundle bundleWithURL:[NSURL fileURLWithPath:@"/Applications/Detox Instruments.app/Contents/SharedSupport/ProfilerFramework/DTXProfiler.framework"]];
NSError* error = nil;
[profilerBundle loadAndReturnError:&error];
if(__DTXProfiler == NULL)
{
//The user has not linked the Profiler framework. Load it manually.

NSString* instrumentsPath = [NSUserDefaults.standardUserDefaults stringForKey:@"instrumentsPath"];
if(instrumentsPath == nil)
{
instrumentsPath = @"/Applications/Detox Instruments.app";
}

NSURL* bundleURL = [[NSURL fileURLWithPath:instrumentsPath] URLByAppendingPathComponent:@"/Contents/SharedSupport/ProfilerFramework/DTXProfiler.framework"];
NSBundle* profilerBundle = [NSBundle bundleWithURL:bundleURL];

if(profilerBundle == nil)
{
dtx_log_error(@"Error loading Profiler framework bundle. Bundle not found at %@", bundleURL.path);
return;
}

NSError* error = nil;
[profilerBundle loadAndReturnError:&error];

if(error != nil)
{
dtx_log_error(@"Error loading Profiler framework bundle: %@", error);
return;
}
}

if(error != nil)
__DTXProfiler = NSClassFromString(@"DTXProfiler");
if(__DTXProfiler == NULL)
{
dtx_log_error(@"Error loading Profiler framework bundle: %@", error);
dtx_log_error(@"DTXProfiler class not found—this should not have happened!");
return;
}
}

__DTXProfiler = NSClassFromString(@"DTXProfiler");
if(__DTXProfiler == NULL)
{
dtx_log_error(@"DTXProfiler class not found—this should not have happened!");
return;
}

__DTXMutableProfilingConfiguration = NSClassFromString(@"DTXMutableProfilingConfiguration");
if(__DTXMutableProfilingConfiguration == NULL)
{
dtx_log_error(@"DTXMutableProfilingConfiguration class not found—this should not have happened!");
return;
}

__DTXProfilerAddTag = dlsym(RTLD_DEFAULT, "DTXProfilerAddTag");
__DTXProfilerMarkEventIntervalBegin = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalBegin");
__DTXProfilerMarkEventIntervalEnd = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalEnd");
__DTXProfilerMarkEvent = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEvent");

if(__DTXProfilerAddTag == NULL || __DTXProfilerMarkEventIntervalBegin == NULL || __DTXProfilerMarkEventIntervalEnd == NULL || __DTXProfilerMarkEvent == NULL)
{
dtx_log_error(@"One or more DTXProfilerAPI functions are NULL—this should not have happened!");
return;
}

__DTXMutableProfilingConfiguration = NSClassFromString(@"DTXMutableProfilingConfiguration");
if(__DTXMutableProfilingConfiguration == NULL)
{
dtx_log_error(@"DTXMutableProfilingConfiguration class not found—this should not have happened!");
return;
}

__DTXProfilerAddTag = dlsym(RTLD_DEFAULT, "DTXProfilerAddTag");
__DTXProfilerMarkEventIntervalBegin = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalBegin");
__DTXProfilerMarkEventIntervalEnd = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEventIntervalEnd");
__DTXProfilerMarkEvent = dlsym(RTLD_DEFAULT, "DTXProfilerMarkEvent");

if(__DTXProfilerAddTag == NULL || __DTXProfilerMarkEventIntervalBegin == NULL || __DTXProfilerMarkEventIntervalEnd == NULL || __DTXProfilerMarkEvent == NULL)
{
dtx_log_error(@"One or more DTXProfilerAPI functions are NULL—this should not have happened!");
return;
}
});
}

+ (NSString *)_sanitizeFileNameString:(NSString *)fileName
Expand All @@ -111,6 +128,8 @@ + (NSURL*)defaultURLForTestName:(NSString*)testName

- (instancetype)init
{
[DetoxInstrumentsManager _loadProfiler];

self = [super init];

if(self)
Expand Down
4 changes: 3 additions & 1 deletion detox/ios/Detox/DetoxManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ - (instancetype)init
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appDidLaunch:) name:UIApplicationDidFinishLaunchingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_appDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

_recordingManager = [DetoxInstrumentsManager new];
dispatch_async(dispatch_get_main_queue(), ^{
_recordingManager = [DetoxInstrumentsManager new];
});

if([NSUserDefaults.standardUserDefaults objectForKey:@"currentTestSummaryDataURL"])
{
Expand Down

0 comments on commit 095a652

Please sign in to comment.