Skip to content

Commit 8dc093f

Browse files
committed
Add support for Core Data model files
1 parent 4be6e30 commit 8dc093f

File tree

22 files changed

+382
-23
lines changed

22 files changed

+382
-23
lines changed

.bazelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# To update these lines, execute
22
# `bazel run @contrib_rules_bazel_integration_test//tools:update_deleted_packages`
3-
build --deleted_packages=examples/cc/external,examples/command_line/external,examples/ios_app,examples/ios_app/CoreUtilsObjC,examples/ios_app/Example,examples/ios_app/ExampleObjcTests,examples/ios_app/ExampleResources,examples/ios_app/ExampleTests,examples/ios_app/ExampleUITests,examples/ios_app/TestingUtils,examples/ios_app/Utils,examples/ios_app/external,examples/ios_app/test/fixtures,examples/ios_app/third_party
4-
query --deleted_packages=examples/cc/external,examples/command_line/external,examples/ios_app,examples/ios_app/CoreUtilsObjC,examples/ios_app/Example,examples/ios_app/ExampleObjcTests,examples/ios_app/ExampleResources,examples/ios_app/ExampleTests,examples/ios_app/ExampleUITests,examples/ios_app/TestingUtils,examples/ios_app/Utils,examples/ios_app/external,examples/ios_app/test/fixtures,examples/ios_app/third_party
3+
build --deleted_packages=examples/cc/external,examples/ios_app,examples/ios_app/CoreUtilsObjC,examples/ios_app/Example,examples/ios_app/ExampleObjcTests,examples/ios_app/ExampleResources,examples/ios_app/ExampleTests,examples/ios_app/ExampleUITests,examples/ios_app/TestingUtils,examples/ios_app/Utils,examples/ios_app/external,examples/ios_app/test/fixtures,examples/ios_app/test/fixtures/bwb.xcodeproj/rules_xcodeproj/links,examples/ios_app/test/fixtures/bwx.xcodeproj/rules_xcodeproj/links,examples/ios_app/third_party
4+
query --deleted_packages=examples/cc/external,examples/ios_app,examples/ios_app/CoreUtilsObjC,examples/ios_app/Example,examples/ios_app/ExampleObjcTests,examples/ios_app/ExampleResources,examples/ios_app/ExampleTests,examples/ios_app/ExampleUITests,examples/ios_app/TestingUtils,examples/ios_app/Utils,examples/ios_app/external,examples/ios_app/test/fixtures,examples/ios_app/test/fixtures/bwb.xcodeproj/rules_xcodeproj/links,examples/ios_app/test/fixtures/bwx.xcodeproj/rules_xcodeproj/links,examples/ios_app/third_party
55

66
# Import Shared settings
77
import %workspace%/shared.bazelrc

BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ filegroup(
5454
"//tools:all_files",
5555
"//tools/generator:all_files",
5656
"//tools/generator/test:all_files",
57+
"//tools/xccurrentversions_parser:all_files",
5758
"//xcodeproj:all_files",
5859
"//xcodeproj/internal:all_files",
5960
],

examples/ios_app/Example/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ apple_resource_group(
2828
resources = glob(
2929
[
3030
"Assets.xcassets/**",
31+
"Model.xcdatamodeld/**",
3132
"*.lproj/**",
3233
],
3334
exclude = ["Assets.xcassets/AppIcon.appiconset/**"],
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>_XCCurrentVersionName</key>
6+
<string>Model2.xcdatamodel</string>
7+
</dict>
8+
</plist>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21C52" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
3+
<entity name="Entity" representedClassName="Entity" syncable="YES" codeGenerationType="class">
4+
<attribute name="name" optional="YES" attributeType="String"/>
5+
</entity>
6+
<elements>
7+
<element name="Entity" positionX="-63" positionY="-18" width="128" height="44"/>
8+
</elements>
9+
</model>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21C52" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
3+
<entity name="Entity" representedClassName="Entity" syncable="YES" codeGenerationType="class">
4+
<attribute name="age" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/>
5+
<attribute name="name" optional="YES" attributeType="String"/>
6+
</entity>
7+
<entity name="Entity2" representedClassName="Entity2" syncable="YES" codeGenerationType="class">
8+
<attribute name="name" optional="YES" attributeType="String"/>
9+
<relationship name="parent" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Entity"/>
10+
</entity>
11+
<elements>
12+
<element name="Entity" positionX="-54" positionY="-9" width="128" height="59"/>
13+
<element name="Entity2" positionX="-36" positionY="27" width="128" height="59"/>
14+
</elements>
15+
</model>

examples/ios_app/test/fixtures/bwx_spec.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@
299299
"Example/PreviewContent/PreviewAssets.xcassets/rules_xcodeproj.imageset/Contents.json",
300300
"Example/PreviewContent/PreviewAssets.xcassets/rules_xcodeproj.imageset/rules_xcodeproj.png",
301301
"Example/Assets.xcassets/Contents.json",
302+
"Example/Model.xcdatamodeld/Model.xcdatamodel/contents",
303+
"Example/Model.xcdatamodeld/Model2.xcdatamodel/contents",
302304
"Example/en.lproj/Localizable.strings",
303305
"Example/es.lproj/Localizable.strings",
304306
{

test/fixtures/generator/bwx_spec.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@
459459
"tools/generator/src/Path+Extensions.swift",
460460
"tools/generator/src/SearchPaths.swift",
461461
"tools/generator/src/Sorting.swift",
462-
"tools/generator/src/TargetID.swift"
462+
"tools/generator/src/TargetID.swift",
463+
"tools/generator/src/XCCurrentVersion.swift"
463464
]
464465
},
465466
"is_swift": true,

tools/generator/src/Environment.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ struct Environment {
2222
_ pbxProj: PBXProj,
2323
_ targets: [TargetID: Target],
2424
_ extraFiles: Set<FilePath>,
25-
_ filePathResolver: FilePathResolver
25+
_ xccurrentversions: [XCCurrentVersion],
26+
_ filePathResolver: FilePathResolver,
27+
_ logger: Logger
2628
) throws -> (
2729
files: [FilePath: File],
2830
rootElements: [PBXFileElement]

tools/generator/src/Generator+CreateFilesAndGroups.swift

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import Foundation
12
import PathKit
23
import XcodeProj
34

4-
/// Wrapper for files (`PBXFileReference` and `PBXVariantGroup`), adding
5-
/// additional associated data.
5+
/// Wrapper for files (`PBXFileReference`, `PBXVariantGroup`, and
6+
/// `XCVersionGroup`), adding additional associated data.
67
enum File: Equatable {
78
case reference(PBXFileReference?, content: String)
89
case variantGroup(PBXVariantGroup)
10+
case xcVersionGroup(XCVersionGroup)
911
}
1012

1113
extension File {
@@ -25,6 +27,8 @@ extension File {
2527
return reference
2628
case .variantGroup(let group):
2729
return group
30+
case .xcVersionGroup(let group):
31+
return group
2832
}
2933
}
3034
}
@@ -53,7 +57,9 @@ extension Generator {
5357
in pbxProj: PBXProj,
5458
targets: [TargetID: Target],
5559
extraFiles: Set<FilePath>,
56-
filePathResolver: FilePathResolver
60+
xccurrentversions: [XCCurrentVersion],
61+
filePathResolver: FilePathResolver,
62+
logger: Logger
5763
) throws -> (
5864
files: [FilePath: File],
5965
rootElements: [PBXFileElement]
@@ -71,8 +77,6 @@ extension Generator {
7177
return (element, false)
7278
}
7379

74-
// TODO: Handle CoreData models
75-
7680
if filePath.path.isLocalizedContainer {
7781
// Localized container (e.g. /path/to/en.lproj)
7882
// We don't add it directly; an element will get added once the
@@ -81,6 +85,17 @@ extension Generator {
8185
} else if filePath.path.parent().isLocalizedContainer {
8286
// Localized file (e.g. /path/to/en.lproj/foo.png)
8387
return addLocalizedFile(filePath: filePath)
88+
} else if filePath.path.isCoreDataContainer {
89+
let group = XCVersionGroup(
90+
path: pathComponent,
91+
sourceTree: .group,
92+
versionGroupType: filePath.path.versionGroupType
93+
)
94+
pbxProj.add(object: group)
95+
96+
elements[filePath] = group
97+
98+
return (group, true)
8499
} else if !(isLeaf || filePath.path.isFolderTypeFileSource) {
85100
let group = createGroup(
86101
filePath: filePath,
@@ -315,6 +330,7 @@ extension Generator {
315330
lastElement = createInternalGroup()
316331
}
317332

333+
var coreDataContainer: XCVersionGroup?
318334
let components = fullFilePath.path.components
319335
for (offset, component) in components.enumerated() {
320336
filePath = filePath + component
@@ -335,6 +351,16 @@ extension Generator {
335351
} else if !isSpecialGroup(element) {
336352
rootElements.append(element)
337353
}
354+
355+
if let coreDataContainer = coreDataContainer {
356+
// When a model file is copied, we should grab
357+
// the group instead
358+
elements[filePath] = coreDataContainer
359+
}
360+
}
361+
362+
if let element = element as? XCVersionGroup {
363+
coreDataContainer = element
338364
}
339365

340366
lastElement = element
@@ -345,19 +371,31 @@ extension Generator {
345371
}
346372
}
347373

348-
if fullFilePath != filePath {
374+
if let coreDataContainer = coreDataContainer {
375+
// When a model file is copied, we should grab
376+
// the group instead
377+
elements[fullFilePath] = coreDataContainer
378+
} else if fullFilePath != filePath {
349379
// We need to add extra entries for file-like folders, to allow
350380
// easy copying of resources
351381
elements[fullFilePath] = lastElement
352382
}
353383
}
354384

385+
try setXCCurrentVersions(
386+
elements: elements,
387+
xccurrentversions: xccurrentversions,
388+
logger: logger
389+
)
390+
355391
var files: [FilePath: File] = [:]
356392
for (filePath, element) in elements {
357393
if let reference = element as? PBXFileReference {
358394
files[filePath] = .reference(reference)
359395
} else if let variantGroup = element as? PBXVariantGroup {
360396
files[filePath] = .variantGroup(variantGroup)
397+
} else if let xcVersionGroup = element as? XCVersionGroup {
398+
files[filePath] = .xcVersionGroup(xcVersionGroup)
361399
}
362400
}
363401

@@ -469,6 +507,45 @@ extension Generator {
469507

470508
return (files, rootElements)
471509
}
510+
511+
private static func setXCCurrentVersions(
512+
elements: [FilePath: PBXFileElement],
513+
xccurrentversions: [XCCurrentVersion],
514+
logger: Logger
515+
) throws {
516+
for xccurrentversion in xccurrentversions {
517+
guard let element = elements[xccurrentversion.container] else {
518+
throw PreconditionError(message: """
519+
"\(xccurrentversion.container.path)" `XCVersionGroup` not found in `elements`
520+
""")
521+
}
522+
523+
guard let container = element as? XCVersionGroup else {
524+
throw PreconditionError(message: """
525+
"\(xccurrentversion.container.path)" isn't an `XCVersionGroup`
526+
""")
527+
}
528+
529+
guard
530+
let versionChild = container.children
531+
.first(where: { $0.path == xccurrentversion.version })
532+
else {
533+
logger.logWarning("""
534+
"\(xccurrentversion.container.path)" doesn't have \
535+
"\(xccurrentversion.version)" as a child; not setting `currentVersion`
536+
""")
537+
continue
538+
}
539+
540+
guard let versionFile = versionChild as? PBXFileReference else {
541+
throw PreconditionError(message: """
542+
"\(versionChild.path!)" is not a `PBXFileReference`
543+
""")
544+
}
545+
546+
container.currentVersion = versionFile
547+
}
548+
}
472549
}
473550

474551
private extension Inputs {

0 commit comments

Comments
 (0)