|
| 1 | +#!/usr/bin/env xcrun swift -F Rome |
| 2 | + |
| 3 | +import CoreData |
| 4 | +import PathKit |
| 5 | +import Stencil |
| 6 | + |
| 7 | + |
| 8 | +extension Path { |
| 9 | + static var processPath:Path { |
| 10 | + if Process.arguments[0].componentsSeparatedByString(Path.separator).count > 1 { |
| 11 | + return Path.current + Process.arguments[0] |
| 12 | + } |
| 13 | + |
| 14 | + let PATH = NSProcessInfo.processInfo().environment["PATH"]! |
| 15 | + let paths = PATH.componentsSeparatedByString(":").map { |
| 16 | + Path($0) + Process.arguments[0] |
| 17 | + }.filter { $0.exists } |
| 18 | + |
| 19 | + return paths.first! |
| 20 | + } |
| 21 | + |
| 22 | + static var defaultTemplatePath:Path { |
| 23 | + return processPath + "../../share/querykit/template.swift" |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +func compileCoreDataModel(source:Path) -> Path { |
| 29 | + let destinationExtension = source.`extension`!.hasSuffix("d") ? ".momd" : ".mom" |
| 30 | + let destination = try! Path.uniqueTemporary() + (source.lastComponentWithoutExtension + destinationExtension) |
| 31 | + system("xcrun momc \(source.absolute()) \(destination.absolute())") |
| 32 | + return destination |
| 33 | +} |
| 34 | + |
| 35 | +class AttributeDescription : NSObject { |
| 36 | + let name:String |
| 37 | + let type:String |
| 38 | + |
| 39 | + init(name:String, type:String) { |
| 40 | + self.name = name |
| 41 | + self.type = type |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +extension NSAttributeDescription { |
| 46 | + var qkAttributeDescription:AttributeDescription? { |
| 47 | + if let className = attributeValueClassName { |
| 48 | + return AttributeDescription(name: name, type: className) |
| 49 | + } |
| 50 | + |
| 51 | + return nil |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +extension NSRelationshipDescription { |
| 56 | + var qkAttributeDescription:AttributeDescription? { |
| 57 | + if let destinationEntity = destinationEntity { |
| 58 | + var type = destinationEntity.managedObjectClassName |
| 59 | + |
| 60 | + if toMany { |
| 61 | + type = "Set<\(type)>" |
| 62 | + |
| 63 | + if ordered { |
| 64 | + type = "Ordered\(type)" |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + return AttributeDescription(name: name, type: type) |
| 69 | + } |
| 70 | + |
| 71 | + return nil |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +class CommandError : ErrorType { |
| 76 | + let description:String |
| 77 | + |
| 78 | + init(description:String) { |
| 79 | + self.description = description |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func render(entity:NSEntityDescription, destination:Path, template:Template) throws { |
| 84 | + let attributes = entity.properties.flatMap { property -> AttributeDescription? in |
| 85 | + if let attribute = property as? NSAttributeDescription { |
| 86 | + return attribute.qkAttributeDescription |
| 87 | + } else if let relationship = property as? NSRelationshipDescription { |
| 88 | + return relationship.qkAttributeDescription |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | + } |
| 93 | + |
| 94 | + let context = Context(dictionary: [ |
| 95 | + "className": entity.managedObjectClassName, |
| 96 | + "attributes": attributes, |
| 97 | + "entityName": entity.name ?? "Unknown", |
| 98 | + ]) |
| 99 | + |
| 100 | + switch template.render(context) { |
| 101 | + case .Success(let string): |
| 102 | + destination.write(string) |
| 103 | + case .Error(let error): |
| 104 | + throw CommandError(description: "Failed to render '\(entity.name): \(error).'") |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func render(model:NSManagedObjectModel, destination:Path, templatePath:Path) { |
| 109 | + if !destination.exists { |
| 110 | + do { |
| 111 | + try destination.mkdir() |
| 112 | + } catch { |
| 113 | + print("Failed to create directory: '\(destination)'.") |
| 114 | + return |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + for entity in model.entities { |
| 119 | + let template = try! Template(path: templatePath)! |
| 120 | + let className = entity.managedObjectClassName |
| 121 | + |
| 122 | + if className == "NSManagedObject" { |
| 123 | + let name = entity.name ?? "Unknown" |
| 124 | + print("-> Skipping entity '\(name)', doesn't use a custom class.") |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + let destinationFile = destination + (className + "+QueryKit.swift") |
| 129 | + |
| 130 | + do { |
| 131 | + try render(entity, destination: destinationFile, template: template) |
| 132 | + print("-> Generated '\(className)' '\(destinationFile)'") |
| 133 | + } catch { |
| 134 | + print(error) |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func run() { |
| 141 | + let arguments = Process.arguments |
| 142 | + |
| 143 | + if arguments.count == 3 { |
| 144 | + let modelPath = Path(arguments[1]) |
| 145 | + let outputPath = Path(arguments[2]) |
| 146 | + let modelExtension = modelPath.`extension` |
| 147 | + let isDataModel = modelExtension == "xcdatamodel" |
| 148 | + let isDataModeld = modelExtension == "xcdatamodeld" |
| 149 | + |
| 150 | + if isDataModel || isDataModeld { |
| 151 | + if modelPath.isReadable { |
| 152 | + let templatePath = Path.defaultTemplatePath |
| 153 | + if !templatePath.isReadable { |
| 154 | + print("Template '\(templatePath)' is not readable.") |
| 155 | + } else { |
| 156 | + let compiledModel = compileCoreDataModel(modelPath) |
| 157 | + let modelURL = NSURL(fileURLWithPath: compiledModel.description) |
| 158 | + let model = NSManagedObjectModel(contentsOfURL: modelURL)! |
| 159 | + render(model, destination: outputPath, templatePath: templatePath) |
| 160 | + } |
| 161 | + } else { |
| 162 | + print("'\(modelPath)' does not exist or is not readable.") |
| 163 | + } |
| 164 | + } else { |
| 165 | + print("'\(modelPath)' is not a Core Data model.") |
| 166 | + } |
| 167 | + } else { |
| 168 | + let processName = arguments[0] |
| 169 | + print("Usage: \(processName) <model> <output-directory>") |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +run() |
| 174 | + |
0 commit comments