Skip to content

Commit

Permalink
1.2.0 (#17)
Browse files Browse the repository at this point in the history
* Swift 4.2
  • Loading branch information
alemar11 authored Sep 24, 2018
1 parent 9947835 commit 6739d00
Show file tree
Hide file tree
Showing 86 changed files with 3,128 additions and 1,060 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
4.2
45 changes: 19 additions & 26 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,75 @@ env:
- MACOS_FRAMEWORK_SCHEME="CoreDataPlus macOS"
- TVOS_FRAMEWORK_SCHEME="CoreDataPlus tvOS"
- WATCHOS_FRAMEWORK_SCHEME="CoreDataPlus watchOS"
- IOS_SDK=iphonesimulator11.4
- MACOS_SDK=macosx10.13
- TVOS_SDK=appletvsimulator11.4
- WATCHOS_SDK=watchsimulator4.3
- IOS_SDK=iphonesimulator12.0
- MACOS_SDK=macosx10.14
- TVOS_SDK=appletvsimulator12.0
- WATCHOS_SDK=watchsimulator5.0
- XCODE_RUN_TESTS="NO"

matrix:
include:

# macOS: Swift Tests, CocoaPods and SwiftLint
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
before_install:
- gem install cocoapods -no-rdoc --no-ri --no-document --quiet
- gem install cocoapods -no-rdoc --no-ri --no-document --quiet --pre
- brew outdated swiftlint || brew upgrade swiftlint
before_script:
- swiftlint --reporter "emoji"
script:
- set -o pipefail
- swift --version
- swift test -Xswiftc -target -Xswiftc x86_64-apple-macosx10.12
- pod lib lint
# - pod lib lint

# iOS 10.3.1 (32bit)
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=10.3.1,name=iPhone 5" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="ios10-32bit"

# iOS 10.3.1
- os: osx
osx_image: xcode9.4
language: objective-c
env:
- DESTINATION="OS=10.3.1,name=iPhone 7 Plus" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="ios10"

# iOS watchOS 3.2
- os: osx
osx_image: xcode9.3
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=3.2,name=Apple Watch - 42mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" XCODE_RUN_TESTS="NO" CODECOV_FLAG="watchos3"
- DESTINATION="OS=3.2,name=Apple Watch Series 2 - 38mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" XCODE_RUN_TESTS="NO" CODECOV_FLAG="watchos3"

# tvOS 10.2
- os: osx
osx_image: xcode9.3
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=10.2,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="appletvsimulator11.3" XCODE_RUN_TESTS="YES" CODECOV_FLAG="tvos10"
- DESTINATION="OS=10.2,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="tvos10"

# iOS 11.4
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=11.4,name=iPhone X" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="ios"

#watchOS 4.3
#watchOS 4.2
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=4.3,name=Apple Watch - 42mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" XCODE_RUN_TESTS="NO" CODECOV_FLAG="watchos"
- DESTINATION="OS=4.2,name=Apple Watch Series 2 - 38mm" SCHEME="$WATCHOS_FRAMEWORK_SCHEME" SDK="$WATCHOS_SDK" XCODE_RUN_TESTS="NO" CODECOV_FLAG="watchos"

# tvOS 11.4
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
env:
- DESTINATION="OS=11.4,name=Apple TV 4K (at 1080p)" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="tvos"
- DESTINATION="OS=11.4,name=Apple TV 4K (at 1080p)" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="tvos"

# macOS
- os: osx
osx_image: xcode9.4
osx_image: xcode10
language: objective-c
env:
- DESTINATION="arch=x86_64" SCHEME="$MACOS_FRAMEWORK_SCHEME" SDK="$MACOS_SDK" XCODE_RUN_TESTS="YES" CODECOV_FLAG="macos"
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog

### 1.2.0

- Mechanica is now completely migrated to **Swift 4.2**.
- CoreData migration refinements.

### 1.1.0

- Added migration between model versions.
- Refinements.

Expand Down
123 changes: 70 additions & 53 deletions CoreDataPlus.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
shouldUseLaunchSchemeArgsEnv = "NO">
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "YES"
testExecutionOrdering = "random">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "23EFDEAA1F95FEB40038BE75"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
shouldUseLaunchSchemeArgsEnv = "NO">
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "YES"
testExecutionOrdering = "random">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "23EFDE9B1F95FE990038BE75"
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
],
targets: [
.target(name: "CoreDataPlus", path: "Sources"),
.testTarget(name: "Tests", dependencies: ["CoreDataPlus"], path: "Tests")
.testTarget(name: "Tests", dependencies: ["CoreDataPlus"], path: "Tests", exclude: ["CoreDataMigrationsTests.swift"])
],
swiftLanguageVersions: [4]
)
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import CoreData

public struct Migration {
public struct CoreDataMigration {

private init() { }

Expand All @@ -39,7 +39,7 @@ public struct Migration {
/// - targetVersion: the ModelVersion to which the store is needed to migrate to.
/// - progress: a Progress instance to monitor the migration.
/// - Throws: It throws an error in cases of failure.
public static func migrateStore<Version: ModelVersion>(at sourceURL: URL, targetVersion: Version, progress: Progress? = nil) throws {
public static func migrateStore<Version: CoreDataModelVersion>(at sourceURL: URL, targetVersion: Version, progress: Progress? = nil) throws {
try migrateStore(from: sourceURL, to: sourceURL, targetVersion: targetVersion, deleteSource: false, progress: progress)
}

Expand All @@ -54,7 +54,7 @@ public struct Migration {
/// - deleteSource: if `true` the initial store will be deleted after the migration phase.
/// - progress: a Progress instance to monitor the migration.
/// - Throws: It throws an error in cases of failure.
public static func migrateStore<Version: ModelVersion>(from sourceURL: URL, to targetURL: URL, targetVersion: Version, deleteSource: Bool = false, progress: Progress? = nil) throws {
public static func migrateStore<Version: CoreDataModelVersion>(from sourceURL: URL, to targetURL: URL, targetVersion: Version, deleteSource: Bool = false, progress: Progress? = nil) throws {
guard let sourceVersion = Version(persistentStoreURL: sourceURL as URL) else {
fatalError("A ModelVersion for the store at URL \(sourceURL) could not be found.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import CoreData
/// **CoreDataPlus**
///
/// Represents a single step during the migration process.
public final class MigrationStep {
public final class CoreDataMigrationStep {
var sourceModel: NSManagedObjectModel
var destinationModel: NSManagedObjectModel
var mappings: [NSMappingModel]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,56 +23,63 @@

import CoreData

private enum ModelVersionExtension {
/// Describes a Core Data model file exention type based on the
/// [Model File Format and Versions](https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreDataVersioning/Articles/vmModelFormat.html)
/// documentation.
private enum ModelVersionFileExtension {
/// The extension for a model bundle, or a `.xcdatamodeld` file package.
static let momd = "momd"
static let omo = "omo"
/// The extension for a versioned model file, or a `.xcdatamodel` file.
static let mom = "mom"
/// The extension for an optimized version for the '.mom' file
static let omo = "omo"
/// The extension for a mapping model file, or a `.xcmappingmodel` file.
static let cdm = "cdm"
}

/// **CoreDataPlus**
///
/// Types adopting the `ModelVersion` protocol can be used to describe a Core Data Model and its versioning.
public protocol ModelVersion: Equatable, RawRepresentable {
/// Types adopting the `CoreDataModelVersion` protocol can be used to describe a Core Data Model and its versioning.
public protocol CoreDataModelVersion: Equatable, RawRepresentable {

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// List with all versions until now.
static var allVersions: [Self] { get }

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Current model version.
static var currentVersion: Self { get }

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Version name.
var versionName: String { get }

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// The next `ModelVersion` in the progressive migration.
/// The next `CoreDataModelVersion` in the progressive migration.
var successor: Self? { get }

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// NSBundle object containing the model file.
var modelBundle: Bundle { get }

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Model name.
var modelName: String { get }
Expand All @@ -81,33 +88,33 @@ public protocol ModelVersion: Equatable, RawRepresentable {
///
/// Protocol `ModelVersions`.
///
/// Return the NSManagedObjectModel for this `ModelVersion`.
/// Return the NSManagedObjectModel for this `CoreDataModelVersion`.
func managedObjectModel() -> NSManagedObjectModel

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Returns a list of mapping models needed to migrate the current version of the database to the next one.
func mappingModelsToNextModelVersion() -> [NSMappingModel]?
}

extension ModelVersion {
extension CoreDataModelVersion {

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Model file name.
var momd: String { return "\(modelName).\(ModelVersionExtension.momd)" }
var momd: String { return "\(modelName).\(ModelVersionFileExtension.momd)" }

}

extension ModelVersion {
extension CoreDataModelVersion {

/// **CoreDataPlus**
///
/// Initializes a `ModelVersion` from a `NSPersistentStore` URL.
/// Initializes a `CoreDataModelVersion` from a `NSPersistentStore` URL.
public init?(persistentStoreURL: URL) {
guard let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: persistentStoreURL, options: nil) else {
return nil
Expand All @@ -126,16 +133,16 @@ extension ModelVersion {

/// **CoreDataPlus**
///
/// Protocol `ModelVersion`.
/// Protocol `CoreDataModelVersion`.
///
/// Return the NSManagedObjectModel for this `ModelVersion`.
/// Return the NSManagedObjectModel for this `CoreDataModelVersion`.
public func managedObjectModel() -> NSManagedObjectModel {
return _managedObjectModel()
}

// swiftlint:disable:next identifier_name
internal func _managedObjectModel() -> NSManagedObjectModel {
let momURL = modelBundle.url(forResource: versionName, withExtension: "\(ModelVersionExtension.mom)", subdirectory: momd)
let momURL = modelBundle.url(forResource: versionName, withExtension: "\(ModelVersionFileExtension.mom)", subdirectory: momd)

/// As of iOS 11, Apple is advising that opening the .omo file for a managed object model is not supported, since the file format can change from release to release
// let omoURL = modelBundle.url(forResource: versionName, withExtension: "\(ModelVersionExtension.omo)", subdirectory: momd)
Expand All @@ -156,17 +163,17 @@ extension ModelVersion {

// MARK: - Migration

extension ModelVersion {
extension CoreDataModelVersion {

/// **CoreDataPlus**
///
/// Returns `true` if a migration is possible for the current store to a given `ModelVersion`.
/// Returns `true` if a migration is possible for the current store to a given `CoreDataModelVersion`.
///
/// - Parameters:
/// - storeURL: the current store URL.
/// - version: the ModelVersion to which the store is compared.
/// - Throws: It throws an error in cases of failure.
public func isMigrationPossible<Version: ModelVersion>(for storeURL: URL, to version: Version) throws -> Bool {
public func isMigrationPossible<Version: CoreDataModelVersion>(for storeURL: URL, to version: Version) throws -> Bool {
let metadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: storeURL, options: nil)
let targetModel = version.managedObjectModel()
return !targetModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
Expand All @@ -175,7 +182,7 @@ extension ModelVersion {
/// **CoreDataPlus**
///
/// Returns a list of `MigrationStep` needed to mirate to the next `version` of the store.
public func migrationSteps(to version: Self) -> [MigrationStep] {
public func migrationSteps(to version: Self) -> [CoreDataMigrationStep] {
guard self != version else {
return []
}
Expand All @@ -184,7 +191,7 @@ extension ModelVersion {
fatalError("Couldn't find any mapping models.")
}

let step = MigrationStep(source: managedObjectModel(), destination: nextVersion.managedObjectModel(), mappings: mappings)
let step = CoreDataMigrationStep(source: managedObjectModel(), destination: nextVersion.managedObjectModel(), mappings: mappings)

return [step] + nextVersion.migrationSteps(to: version)
}
Expand All @@ -197,7 +204,7 @@ extension ModelVersion {
return nil
}

guard let mappingModel = NSMappingModel(from: [modelBundle], forSourceModel: managedObjectModel(), destinationModel: nextVersion._managedObjectModel()) else {
guard let mappingModel = NSMappingModel(from: [modelBundle], forSourceModel: managedObjectModel(), destinationModel: nextVersion.managedObjectModel()) else {
fatalError("No NSMappingModel found for \(self) to \(nextVersion).")
}

Expand Down Expand Up @@ -244,13 +251,13 @@ extension ModelVersion {
}

guard
let allMappingModelsURLs = modelBundle.urls(forResourcesWithExtension: ModelVersionExtension.cdm, subdirectory: nil),
let allMappingModelsURLs = modelBundle.urls(forResourcesWithExtension: ModelVersionFileExtension.cdm, subdirectory: nil),
allMappingModelsURLs.count > 0 else {
return results
}

mappingModelNames.forEach { name in
let expectedFileName = "\(name).\(ModelVersionExtension.cdm)"
let expectedFileName = "\(name).\(ModelVersionFileExtension.cdm)"
if
let url = allMappingModelsURLs.first(where: { $0.lastPathComponent == expectedFileName }),
let mappingModel = NSMappingModel(contentsOf: url) {
Expand Down
2 changes: 1 addition & 1 deletion Support/Info-Tests.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<string>1.2.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
Expand Down
Loading

0 comments on commit 6739d00

Please sign in to comment.