generate code data models properly

This commit is contained in:
Yonas Kolb
2018-02-26 17:11:25 +11:00
parent d88f58f20b
commit b97261db36
6 changed files with 90 additions and 14 deletions
+2 -2
View File
@@ -158,8 +158,8 @@ extension SettingsPresetFile {
if let symlink = try? bundlePath.symlinkDestination() {
possibleSettingsPaths = [
symlink + relativePath
] + possibleSettingsPaths
symlink + relativePath,
] + possibleSettingsPaths
}
guard let settingsPath = possibleSettingsPaths.first(where: { $0.exists }) else {
+39 -11
View File
@@ -96,7 +96,8 @@ class SourceGenerator {
}
func getFileReference(path: Path, inPath: Path, name: String? = nil, sourceTree: PBXSourceTree = .group, lastKnownFileType: String? = nil) -> String {
if let fileReference = fileReferencesByPath[path.string.lowercased()] {
let fileReferenceKey = path.string.lowercased()
if let fileReference = fileReferencesByPath[fileReferenceKey] {
return fileReference
} else {
let fileReferencePath = path.byRemovingBase(path: inPath)
@@ -105,17 +106,44 @@ class SourceGenerator {
fileReferenceName = nil
}
let lastKnownFileType = lastKnownFileType ?? PBXFileReference.fileType(path: path)
let fileReference = createObject(
id: path.byRemovingBase(path: spec.basePath).string,
PBXFileReference(
if path.extension == "xcdatamodeld" {
let models = (try? path.children()) ?? []
let modelFileReference = models
.filter { $0.extension == "xcdatamodel" }
.sorted()
.map { path in
createObject(
id: path.byRemovingBase(path: spec.basePath).string,
PBXFileReference(
sourceTree: .group,
lastKnownFileType: "wrapper.xcdatamodel",
path: path.lastComponent
)
)
}
let versionGroup = addObject(id: path.string, XCVersionGroup(
currentVersion: modelFileReference.first?.reference,
path: fileReferencePath.string,
sourceTree: sourceTree,
name: fileReferenceName,
lastKnownFileType: lastKnownFileType,
path: fileReferencePath.string
versionGroupType: "wrapper.xcdatamodel",
children: modelFileReference.map { $0.reference }
))
fileReferencesByPath[fileReferenceKey] = versionGroup
return versionGroup
} else {
let fileReference = createObject(
id: path.byRemovingBase(path: spec.basePath).string,
PBXFileReference(
sourceTree: sourceTree,
name: fileReferenceName,
lastKnownFileType: lastKnownFileType,
path: fileReferencePath.string
)
)
)
fileReferencesByPath[path.string.lowercased()] = fileReference.reference
return fileReference.reference
fileReferencesByPath[fileReferenceKey] = fileReference.reference
return fileReference.reference
}
}
}
@@ -125,7 +153,7 @@ class SourceGenerator {
}
if let fileExtension = path.extension {
switch fileExtension {
case "swift", "m", "mm", "cpp", "c", "S": return .sources
case "swift", "m", "mm", "cpp", "c", "S", "xcdatamodeld": return .sources
case "h", "hh", "hpp", "ipp", "tpp", "hxx", "def": return .headers
case "xcconfig", "entitlements", "gpx", "lproj", "apns": return nil
default: return .resources
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="13772" systemVersion="17D47" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Entity" representedClassName="Entity" syncable="YES" codeGenerationType="class"/>
<elements>
<element name="Entity" positionX="-63" positionY="-18" width="128" height="45"/>
</elements>
</model>
@@ -27,6 +27,7 @@
BF_303822704662 = {isa = PBXBuildFile; fileRef = FR_472296042419 /* Framework.framework */; };
BF_314868376788 /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = VG_746876637628 /* Localizable.stringsdict */; };
BF_324363145049 /* Framework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FR_472296042419 /* Framework.framework */; };
BF_329004524843 /* Model.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = VG_453922120168 /* Model.xcdatamodeld */; settings = {COMPILER_FLAGS = "-Werror"; }; };
BF_331192862207 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FR_481575785861 /* ViewController.swift */; settings = {COMPILER_FLAGS = "-Werror"; }; };
BF_334778067417 = {isa = PBXBuildFile; fileRef = FR_783122899910 /* App_iOS_Tests.xctest */; };
BF_360196406184 /* TestProjectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FR_722239415598 /* TestProjectTests.swift */; };
@@ -163,6 +164,7 @@
FR_507023492251 /* App_watchOS Extension.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = "App_watchOS Extension.appex"; sourceTree = BUILT_PRODUCTS_DIR; };
FR_525119120469 /* Framework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Framework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FR_530852296303 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
FR_570918052822 /* Model.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = Model.xcdatamodel; sourceTree = "<group>"; };
FR_587738154368 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
FR_602633703434 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.stringsdict; sourceTree = "<group>"; };
FR_609193904586 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@@ -378,6 +380,7 @@
VG_746876637628 /* Localizable.stringsdict */,
VG_118219888726 /* LocalizedStoryboard.storyboard */,
VG_609193904586 /* Main.storyboard */,
VG_453922120168 /* Model.xcdatamodeld */,
FR_481575785861 /* ViewController.swift */,
);
name = App;
@@ -928,6 +931,7 @@
buildActionMask = 2147483647;
files = (
BF_892119987440 /* AppDelegate.swift in Sources */,
BF_329004524843 /* Model.xcdatamodeld in Sources */,
BF_503484983186 /* MoreUnder.swift in Sources */,
BF_561304997165 /* Standalone.swift in Sources */,
BF_331192862207 /* ViewController.swift in Sources */,
@@ -2677,6 +2681,19 @@
defaultConfigurationName = "Production Debug";
};
/* End XCConfigurationList section */
/* Begin XCVersionGroup section */
VG_453922120168 /* Model.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
FR_570918052822 /* Model.xcdatamodel */,
);
currentVersion = FR_570918052822 /* Model.xcdatamodel */;
path = Model.xcdatamodeld;
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
/* End XCVersionGroup section */
};
rootObject = P_8448771205358 /* Project object */;
}
@@ -452,6 +452,30 @@ func projectGeneratorTests() {
try project.expectFile(paths: ["Sources", "A", "B", "b.swift"], buildPhase: .sources)
}
$0.it("generates core data models") {
let directories = """
Sources:
model.xcdatamodeld:
- model.xcdatamodel
"""
try createDirectories(directories)
let target = Target(name: "Test", type: .application, platform: .iOS, sources: ["Sources"])
let spec = ProjectSpec(basePath: directoryPath, name: "Test", targets: [target])
let project = try getPbxProj(spec)
guard let fileReference = project.objects.fileReferences.first(where: { $0.value.nameOrPath == "model.xcdatamodel" }) else {
throw failure("Couldn't find model file reference")
}
guard let versionGroup = project.objects.versionGroups.values.first else {
throw failure("Couldn't find version group")
}
try expect(versionGroup.currentVersion) == fileReference.key
try expect(versionGroup.children) == [fileReference.key]
try expect(versionGroup.path) == "model.xcdatamodeld"
try expect(fileReference.value.path) == "model.xcdatamodel"
}
$0.it("handles duplicate names") {
let directories = """
Sources:
@@ -173,7 +173,7 @@ func specLoadingTests() {
"name": "Before Build",
"settingsTarget": "Target1",
],
]
],
],
]
let scheme = try Scheme(name: "Scheme", jsonDictionary: schemeDictionary)