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