Skip to content

Commit 4d4be64

Browse files
committed
Fix file loading pathing from the Swift Package Manager bundles. Previous bundle logic will not work the same from Cocoapods.
1 parent 2f8785e commit 4d4be64

19 files changed

Lines changed: 99 additions & 52 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Pods
55
xcuserdata/
66
*.swp
77
Carthage/
8+
.build/

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import PackageDescription
44

5+
/// Swift Package Conversion Notes:
6+
/// * Added all the same resources and updated multiple call sites to load from NSBundle to SWIFTPM_MODULE_BUNDLE
7+
/// * Tests require UIKit and take a long time to run
58
let package = Package(
69
name: "GeoPackage",
710
defaultLocalization: "en",
@@ -83,7 +86,6 @@ let package = Package(
8386
.copy("tractor.png"),
8487
.copy("webMercator.png"),
8588
.copy("point.png"),
86-
8789
],
8890
cSettings: [
8991
.headerSearchPath(""),

geopackage-ios/io/GPKGIOUtils.m

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,10 @@ +(NSString *) propertyListPathWithName: (NSString *) name{
2323
}
2424

2525
+(NSString *) resourcePathWithName: (NSString *) name andType: (NSString *) type{
26-
27-
NSString *resource = [NSString stringWithFormat:@"%@/%@", GPKG_BUNDLE_NAME, name];
28-
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:resource ofType:type];
29-
30-
// FIXME: Paul cleanup if SPM is only way to load
31-
if (resourcePath == nil) {
32-
resourcePath = [SWIFTPM_MODULE_BUNDLE pathForResource:name ofType:type];
33-
}
34-
35-
if(resourcePath == nil){
36-
resourcePath = [[NSBundle bundleForClass:[self class]] pathForResource:resource ofType:type];
37-
if(resourcePath == nil){
38-
resourcePath = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:type];
39-
if(resourcePath == nil){
40-
resourcePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
41-
}
42-
}
43-
}
26+
NSString *resourcePath = [SWIFTPM_MODULE_BUNDLE pathForResource:name ofType:type];
4427
if(resourcePath == nil){
4528
[NSException raise:@"Resource Not Found" format:@"Failed to find resource '%@' of type '%@'", name, type];
4629
}
47-
4830
return resourcePath;
4931
}
5032

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// GPKGBundleHelper.h
3+
// GeoPackage
4+
//
5+
// Created by Paul Solt on 5/9/25.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
/// Helper for loading resources from Bundles to make resource loading compatible with SPM
13+
/// and previous bundle loading logic. Checks SPM bundle, then main bundle, then bundle for class.
14+
@interface GPKGBundleHelper : NSObject
15+
16+
/// Loads a resource from a bundle (i.e., myFile.txt)
17+
+ (NSString *)pathForResource:(NSString *)resource;
18+
19+
+ (NSString *)resourcePath;
20+
21+
@end
22+
23+
NS_ASSUME_NONNULL_END
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// GPKGBundleHelper.m
3+
// GeoPackage
4+
//
5+
// Created by Paul Solt on 5/9/25.
6+
//
7+
8+
#import "GPKGBundleHelper.h"
9+
10+
@implementation GPKGBundleHelper
11+
12+
+ (NSString *)pathForResource:(NSString *)resource {
13+
NSString *filename = [resource stringByDeletingPathExtension];
14+
NSString *extension = [resource pathExtension];
15+
NSString *path = [SWIFTPM_MODULE_BUNDLE pathForResource:filename ofType:extension];
16+
17+
if (!path) {
18+
NSLog(@"Error: Unable to find file in bundle: %@", resource);
19+
}
20+
return path;
21+
}
22+
23+
+ (NSString *)resourcePath {
24+
return [SWIFTPM_MODULE_BUNDLE resourcePath];
25+
}
26+
27+
@end

geopackage-iosTests/GPKGGeoPackageExample.m

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
#import "GPKGGeoPackageExample.h"
1010
#import "GPKGTestUtils.h"
11-
1211
#import "GPKGRelatedTablesUtils.h"
13-
12+
#import "GPKGBundleHelper.h"
1413

1514
@import Projections;
1615
@import GeoPackage;
@@ -700,7 +699,7 @@ +(void) createTilesWithGeoPackage: (GPKGGeoPackage *) geoPackage andName: (NSStr
700699

701700
GPKGTileMatrixDao *tileMatrixDao = [geoPackage tileMatrixDao];
702701

703-
NSString *resourcePath = [[NSBundle bundleForClass:[GPKGGeoPackageExample class]] resourcePath];
702+
NSString *resourcePath = [GPKGBundleHelper resourcePath];
704703

705704
GPKGTileGrid *tileGrid = totalTileGrid;
706705

@@ -1428,7 +1427,7 @@ +(void) insertRelatedTablesMediaExtensionRowsWithGeoPackage: (GPKGGeoPackage *)
14281427
GPKGUserMappingDao *userMappingDao = [relatedTables mappingDaoForRelation:relation];
14291428

14301429
GPKGMediaRow *mediaRow = [mediaDao newRow];
1431-
NSString *mediaPath = [[[NSBundle bundleForClass:[GPKGGeoPackageExample class]] resourcePath] stringByAppendingPathComponent:file];
1430+
NSString *mediaPath = [GPKGBundleHelper pathForResource:file];
14321431
NSData *mediaData = [[NSFileManager defaultManager] contentsAtPath:mediaPath];
14331432
[mediaRow setData:mediaData];
14341433
[mediaRow setContentType:contentType];
@@ -1738,7 +1737,7 @@ +(void) createFeatureStyleExtensionWithGeoPackage: (GPKGGeoPackage *) geoPackage
17381737

17391738
NSMutableArray<GPKGIconRow *> *icons = [NSMutableArray array];
17401739

1741-
NSString *buildingPath = [[[NSBundle bundleForClass:[GPKGGeoPackageExample class]] resourcePath] stringByAppendingPathComponent:@"building.png"];
1740+
NSString *buildingPath = [GPKGBundleHelper pathForResource:@"building.png"];
17421741
GPKGIconRow *icon1 = [[GPKGIconRow alloc] init];
17431742
[icon1 setName:@"Building"];
17441743
[icon1 setDescription:@"Building Icon"];
@@ -1749,7 +1748,7 @@ +(void) createFeatureStyleExtensionWithGeoPackage: (GPKGGeoPackage *) geoPackage
17491748
[icon1 setAnchorVValue:1.0];
17501749
[icons addObject:icon1];
17511750

1752-
NSString *collegePath = [[[NSBundle bundleForClass:[GPKGGeoPackageExample class]] resourcePath] stringByAppendingPathComponent:@"college.png"];
1751+
NSString *collegePath = [GPKGBundleHelper pathForResource:@"college.png"];
17531752
GPKGIconRow *icon2 = [[GPKGIconRow alloc] init];
17541753
[icon2 setName:@"College"];
17551754
[icon2 setDescription:@"College Icon"];
@@ -1759,7 +1758,7 @@ +(void) createFeatureStyleExtensionWithGeoPackage: (GPKGGeoPackage *) geoPackage
17591758
[icon2 setHeightValue:44.0];
17601759
[icons addObject:icon2];
17611760

1762-
NSString *tractorPath = [[[NSBundle bundleForClass:[GPKGGeoPackageExample class]] resourcePath] stringByAppendingPathComponent:@"tractor.png"];
1761+
NSString *tractorPath = [GPKGBundleHelper pathForResource:@"tractor.png"];
17631762
GPKGIconRow *icon3 = [[GPKGIconRow alloc] init];
17641763
[icon3 setName:@"Tractor"];
17651764
[icon3 setDescription:@"Tractor Icon"];

geopackage-iosTests/GPKGImportCoverageDataGeoPackageTestCase.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "GPKGImportCoverageDataGeoPackageTestCase.h"
1010
#import "GPKGTestConstants.h"
11+
#import "GPKGBundleHelper.h"
1112

1213
@import GeoPackage;
1314

@@ -20,7 +21,7 @@ -(GPKGGeoPackage *) createGeoPackage{
2021
// Delete
2122
[manager delete:GPKG_TEST_IMPORT_COVERAGE_DATA_DB_NAME];
2223

23-
NSString *filePath = [[[NSBundle bundleForClass:[GPKGImportCoverageDataGeoPackageTestCase class]] resourcePath] stringByAppendingPathComponent:GPKG_TEST_IMPORT_COVERAGE_DATA_DB_FILE_NAME];
24+
NSString *filePath = [GPKGBundleHelper pathForResource:GPKG_TEST_IMPORT_COVERAGE_DATA_DB_FILE_NAME];
2425

2526
// Import
2627
[manager importGeoPackageFromPath:filePath withName:GPKG_TEST_IMPORT_COVERAGE_DATA_DB_NAME];

geopackage-iosTests/GPKGImportCoverageDataTiffGeoPackageTestCase.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#import "GPKGImportCoverageDataTiffGeoPackageTestCase.h"
1010
#import "GPKGTestConstants.h"
11+
#import "GPKGBundleHelper.h"
1112

1213
@import GeoPackage;
1314

@@ -20,7 +21,7 @@ -(GPKGGeoPackage *) createGeoPackage{
2021
// Delete
2122
[manager delete:GPKG_TEST_IMPORT_COVERAGE_DATA_TIFF_DB_NAME];
2223

23-
NSString *filePath = [[[NSBundle bundleForClass:[GPKGImportCoverageDataTiffGeoPackageTestCase class]] resourcePath] stringByAppendingPathComponent:GPKG_TEST_IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME];
24+
NSString *filePath = [GPKGBundleHelper pathForResource:GPKG_TEST_IMPORT_COVERAGE_DATA_TIFF_DB_FILE_NAME];
2425

2526
// Import
2627
[manager importGeoPackageFromPath:filePath withName:GPKG_TEST_IMPORT_COVERAGE_DATA_TIFF_DB_NAME];

geopackage-iosTests/GPKGLoadGeoPackageTestCase.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "GPKGLoadGeoPackageTestCase.h"
10+
#import "GPKGBundleHelper.h"
1011

1112
@import GeoPackage;
1213

@@ -27,7 +28,7 @@ -(GPKGGeoPackage *) createGeoPackage{
2728
// Delete
2829
[manager delete:self.dbName];
2930

30-
NSString *filePath = [[[NSBundle bundleForClass:[GPKGLoadGeoPackageTestCase class]] resourcePath] stringByAppendingPathComponent:self.file];
31+
NSString *filePath = [GPKGBundleHelper pathForResource:self.file];
3132

3233
// Import
3334
[manager importGeoPackageFromPath:filePath withName:self.dbName];

0 commit comments

Comments
 (0)