Skip to content

Commit

Permalink
fix(maccatalyst): use the correct migrations path on the Mac Catalyst…
Browse files Browse the repository at this point in the history
… platform

Fixes prisma#10 for Mac Catalyst platform, its likely the same fix will work for macOS in the future too.

Also fixes an issue when using `NSURL.absoluteString` on a path contains spaces, the spaces would
get replaced by `%20`.
  • Loading branch information
sandyklark authored and hassankhan committed Jun 12, 2024
1 parent a2f22b1 commit ebe2cf0
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions ios/Prisma.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,54 @@ @implementation Prisma
if (cxxBridge == nil) {
return @false;
}

auto jsiRuntime = (facebook::jsi::Runtime *)cxxBridge.runtime;
if (jsiRuntime == nil) {
return @false;
}
auto &runtime = *jsiRuntime;
auto callInvoker = _bridge.jsCallInvoker;

// get migrations folder
auto bundleURL = NSBundle.mainBundle.bundleURL;
auto migrations_path_absolute = [NSString stringWithFormat:@"%@%@", bundleURL.absoluteString, @"migrations"];
NSURL *bundleURL = NSBundle.mainBundle.bundleURL;
#if TARGET_OS_MACCATALYST
NSString *migrations_path_absolute = [bundleURL.path stringByAppendingPathComponent:@"Contents/Resources/migrations"];
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];

// @TODO Add better error handling and reporting
NSURL *applicationSupport = [[NSFileManager defaultManager] URLForDirectory: NSApplicationSupportDirectory
inDomain: NSUserDomainMask
appropriateForURL: nil
create: YES
error: nil];

NSString *libraryPathAbsolute = [applicationSupport.path stringByAppendingPathComponent:identifier];
NSString *libraryPath = [libraryPathAbsolute stringByReplacingOccurrencesOfString:@"file://" withString:@""];
// @TODO Add better error handling and reporting
BOOL success = [[NSFileManager defaultManager] createDirectoryAtPath: libraryPath
withIntermediateDirectories: YES
attributes: nil
error: nil];

if (!success) {
NSLog(@"Failed to create folder at %@", libraryPath);
}
#else
NSString *migrations_path_absolute = [bundleURL.path stringByAppendingPathComponent:@"migrations"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true);
NSString *libraryPath = [paths objectAtIndex:0];
#endif
auto migrations_path = [migrations_path_absolute stringByReplacingOccurrencesOfString:@"file://" withString:@""];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, true);
NSString *libraryPath = [paths objectAtIndex:0];

#if DEBUG
std::cout << "▲ NSHomeDirectory:\n" << [NSHomeDirectory() UTF8String] << std::endl;
std::cout << "▲ Library Path:\n" << [libraryPath UTF8String] << std::endl;
std::cout << "▲ Migrations Path:\n" << [migrations_path UTF8String] << std::endl;
#endif

prisma::install_cxx(runtime, callInvoker, [libraryPath UTF8String], [migrations_path UTF8String]);
return nil;
}
Expand Down

0 comments on commit ebe2cf0

Please sign in to comment.