|
1 | 1 | #include "BundleLoader.h" |
2 | 2 | #include <Foundation/Foundation.h> |
| 3 | +#include <mach-o/dyld.h> |
| 4 | +#include <stdlib.h> |
3 | 5 |
|
4 | 6 | // Check if Resources/app/ exists, then load package.json["main"] || app/index.js full file path |
5 | 7 |
|
6 | | -std::string resolveMainPath() { |
| 8 | +static NSString* resourcesPathForExecutable(NSString* executablePath) { |
| 9 | + if (executablePath == nil || [executablePath length] == 0) { |
| 10 | + return nil; |
| 11 | + } |
| 12 | + |
| 13 | + NSString* standardizedPath = [executablePath stringByStandardizingPath]; |
| 14 | + NSString* macOSPath = [standardizedPath stringByDeletingLastPathComponent]; |
| 15 | + NSString* contentsPath = [macOSPath stringByDeletingLastPathComponent]; |
| 16 | + if ([[macOSPath lastPathComponent] isEqualToString:@"MacOS"] && |
| 17 | + [[contentsPath lastPathComponent] isEqualToString:@"Contents"]) { |
| 18 | + return [contentsPath stringByAppendingPathComponent:@"Resources"]; |
| 19 | + } |
| 20 | + |
| 21 | + return nil; |
| 22 | +} |
| 23 | + |
| 24 | +static bool shouldLogBundleResolution() { |
| 25 | + return getenv("NS_BUNDLE_LOADER_DEBUG") != nullptr; |
| 26 | +} |
| 27 | + |
| 28 | +static void addCandidatePath(NSMutableArray<NSString*>* candidates, NSString* path) { |
| 29 | + if (path == nil || [path length] == 0) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + NSString* standardizedPath = [path stringByStandardizingPath]; |
| 34 | + if (![candidates containsObject:standardizedPath]) { |
| 35 | + [candidates addObject:standardizedPath]; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +static std::string resolveMainPathInResources(NSString* resourcesPath) { |
7 | 40 | NSFileManager* fileManager = [NSFileManager defaultManager]; |
8 | | - NSString* resourcesPath = [[NSBundle mainBundle] resourcePath]; |
9 | 41 | NSString* appPath = [resourcesPath stringByAppendingPathComponent:@"app"]; |
10 | 42 | BOOL isDir; |
11 | 43 |
|
12 | 44 | if ([fileManager fileExistsAtPath:appPath isDirectory:&isDir] && isDir) { |
| 45 | + if (shouldLogBundleResolution()) { |
| 46 | + NSLog(@"NativeScript BundleLoader checking app path: %@", appPath); |
| 47 | + } |
13 | 48 | NSString* packageJsonPath = [appPath stringByAppendingPathComponent:@"package.json"]; |
14 | 49 | if ([fileManager fileExistsAtPath:packageJsonPath]) { |
15 | 50 | NSData* jsonData = [NSData dataWithContentsOfFile:packageJsonPath]; |
16 | | - NSError* error; |
| 51 | + NSError* error = nil; |
17 | 52 | NSDictionary* packageDict = [NSJSONSerialization JSONObjectWithData:jsonData |
18 | 53 | options:0 |
19 | 54 | error:&error]; |
20 | 55 | if (error == nil) { |
21 | 56 | NSString* mainEntry = packageDict[@"main"]; |
| 57 | + if (shouldLogBundleResolution()) { |
| 58 | + NSLog(@"NativeScript BundleLoader package main: %@ from %@", mainEntry, packageJsonPath); |
| 59 | + } |
22 | 60 | if (mainEntry != nil) { |
23 | 61 | NSString* mainPath = [appPath stringByAppendingPathComponent:mainEntry]; |
24 | 62 | if ([fileManager fileExistsAtPath:mainPath]) { |
| 63 | + if (shouldLogBundleResolution()) { |
| 64 | + NSLog(@"NativeScript BundleLoader resolved main: %@", mainPath); |
| 65 | + } |
25 | 66 | return std::string([mainPath UTF8String]); |
26 | 67 | } |
27 | 68 |
|
28 | 69 | if ([[mainEntry pathExtension] length] == 0) { |
29 | 70 | NSString* mainPathMjs = [mainPath stringByAppendingPathExtension:@"mjs"]; |
30 | 71 | if ([fileManager fileExistsAtPath:mainPathMjs]) { |
| 72 | + if (shouldLogBundleResolution()) { |
| 73 | + NSLog(@"NativeScript BundleLoader resolved main: %@", mainPathMjs); |
| 74 | + } |
31 | 75 | return std::string([mainPathMjs UTF8String]); |
32 | 76 | } |
33 | 77 |
|
34 | 78 | NSString* mainPathJs = [mainPath stringByAppendingPathExtension:@"js"]; |
35 | 79 | if ([fileManager fileExistsAtPath:mainPathJs]) { |
| 80 | + if (shouldLogBundleResolution()) { |
| 81 | + NSLog(@"NativeScript BundleLoader resolved main: %@", mainPathJs); |
| 82 | + } |
36 | 83 | return std::string([mainPathJs UTF8String]); |
37 | 84 | } |
38 | 85 | } |
39 | 86 | } |
| 87 | + } else if (shouldLogBundleResolution()) { |
| 88 | + NSLog(@"NativeScript BundleLoader failed to parse %@: %@", packageJsonPath, error); |
40 | 89 | } |
41 | 90 | } |
42 | 91 |
|
43 | 92 | // Fallback to app/index.js |
44 | 93 | NSString* indexPath = [appPath stringByAppendingPathComponent:@"index.js"]; |
45 | 94 | if ([fileManager fileExistsAtPath:indexPath]) { |
| 95 | + if (shouldLogBundleResolution()) { |
| 96 | + NSLog(@"NativeScript BundleLoader resolved fallback main: %@", indexPath); |
| 97 | + } |
46 | 98 | return std::string([indexPath UTF8String]); |
47 | 99 | } |
| 100 | + } else if (shouldLogBundleResolution()) { |
| 101 | + NSLog(@"NativeScript BundleLoader skipped resources path: %@ appPath=%@ exists=%d isDir=%d", |
| 102 | + resourcesPath, |
| 103 | + appPath, |
| 104 | + [fileManager fileExistsAtPath:appPath], |
| 105 | + isDir); |
| 106 | + } |
| 107 | + |
| 108 | + return ""; |
| 109 | +} |
| 110 | + |
| 111 | +std::string resolveMainPath() { |
| 112 | + NSMutableArray<NSString*>* candidates = [NSMutableArray array]; |
| 113 | + addCandidatePath(candidates, [[NSBundle mainBundle] resourcePath]); |
| 114 | + addCandidatePath(candidates, resourcesPathForExecutable([[NSBundle mainBundle] executablePath])); |
| 115 | + |
| 116 | + NSArray<NSString*>* arguments = [[NSProcessInfo processInfo] arguments]; |
| 117 | + if ([arguments count] > 0) { |
| 118 | + addCandidatePath(candidates, resourcesPathForExecutable([arguments objectAtIndex:0])); |
| 119 | + } |
| 120 | + |
| 121 | + uint32_t executablePathLength = 0; |
| 122 | + _NSGetExecutablePath(nullptr, &executablePathLength); |
| 123 | + if (executablePathLength > 0) { |
| 124 | + char* executablePathBuffer = static_cast<char*>(malloc(executablePathLength)); |
| 125 | + if (executablePathBuffer != nullptr) { |
| 126 | + if (_NSGetExecutablePath(executablePathBuffer, &executablePathLength) == 0) { |
| 127 | + addCandidatePath(candidates, resourcesPathForExecutable([NSString stringWithUTF8String:executablePathBuffer])); |
| 128 | + } |
| 129 | + free(executablePathBuffer); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + NSString* currentDirectory = [[NSFileManager defaultManager] currentDirectoryPath]; |
| 134 | + addCandidatePath(candidates, currentDirectory); |
| 135 | + addCandidatePath(candidates, [currentDirectory stringByAppendingPathComponent:@"Resources"]); |
| 136 | + addCandidatePath(candidates, [[currentDirectory stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Resources"]); |
| 137 | + |
| 138 | + for (NSString* resourcesPath in candidates) { |
| 139 | + if (shouldLogBundleResolution()) { |
| 140 | + NSLog(@"NativeScript BundleLoader candidate resources: %@", resourcesPath); |
| 141 | + } |
| 142 | + std::string mainPath = resolveMainPathInResources(resourcesPath); |
| 143 | + if (!mainPath.empty()) { |
| 144 | + return mainPath; |
| 145 | + } |
48 | 146 | } |
49 | 147 |
|
50 | 148 | return ""; |
|
0 commit comments