Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Swift Package Manager #362

Merged
merged 25 commits into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ step-library:
command: bash <(curl -s https://codecov.io/bash)

jobs:
spm-job:
parameters:
xcode:
type: string
macos:
xcode: << parameters.xcode >>
environment:
HOMEBREW_NO_AUTO_UPDATE: 1
steps:
- checkout
- run: swift build

static-pod-test:
parameters:
xcode:
type: string
macos:
xcode: << parameters.xcode >>
environment:
HOMEBREW_NO_AUTO_UPDATE: 1
steps:
- checkout
- run:
name: CP static obj-c
command: |
cd Tests/PodTests/static
brew install xcodegen
xcodegen generate
pod install
xcodebuild -sdk iphonesimulator -workspace StaticPod.xcworkspace -scheme 'app' clean build | xcpretty

build-job:
parameters:
xcode:
Expand Down Expand Up @@ -104,10 +135,9 @@ workflows:
iOS: "12.0"
tvOS: "12.0"
codecoverage: true
- build-job:
name: "Xcode_9.4"
xcode: "9.4.1"
iOS: "9.3"
tvOS: "11.4"
watchOS: "4.3"
test: false
- spm-job:
name: "SPM_build"
xcode: "10.2.0"
- static-pod-test:
name: "CP_static"
xcode: "10.2.0"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Carthage/Build
Carthage/Checkouts
/build
/documentation
.build
5 changes: 3 additions & 2 deletions MapboxDirections.swift.podspec → MapboxDirections.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Pod::Spec.new do |s|

# ――― Spec Metadata ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.name = "MapboxDirections.swift"
s.name = "MapboxDirections"
s.version = "0.28.0"
s.summary = "Mapbox Directions API wrapper for Swift and Objective-C."

Expand Down Expand Up @@ -37,7 +37,8 @@ Pod::Spec.new do |s|

# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

s.source_files = ["MapboxDirections", "MapboxDirections/*/*"]
s.source_files = ["Sources/MapboxDirections", "Sources/MapboxDirections/*/*", "Sources/objc"]
s.exclude_files = ["Sources/MapboxDirections/SPM"]

# ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #

Expand Down
194 changes: 125 additions & 69 deletions MapboxDirections.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "Polyline",
"repositoryURL": "https://github.com/raphaelmor/Polyline.git",
"state": {
"branch": null,
"revision": "011fbc1b0f27a3398bd0fb7c5e2cf5c62ae82717",
"version": "4.2.1"
}
}
]
},
"version": 1
}
28 changes: 28 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "MapboxDirections",
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "MapboxDirections",
targets: ["MapboxDirections"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/raphaelmor/Polyline.git", from: "4.2.1")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "MapboxDirections",
dependencies: ["Polyline"]),
.testTarget(
name: "MapboxDirectionsTests",
dependencies: ["MapboxDirections"]),
]
)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import Foundation


typealias JSONDictionary = [String: Any]

/// Indicates that an error occurred in MapboxDirections.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation
import Polyline
import CoreLocation


/**
Maximum length of an HTTP request URL for the purposes of switching from GET to
Expand Down Expand Up @@ -219,6 +221,7 @@ public enum InstructionFormat: UInt, CustomStringConvertible {

You do not create instances of this class directly. Instead, create instances of `MatchOptions` or `RouteOptions`.
*/
@objcMembers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, didn’t know about this annotation. Good find. There are still some bare @objc keywords in the class; are they redundant now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@objc is still needed for refinements. It's also useful for compile-time errors of unbridgeable types.
Only using @objcMembers would fail silently if a type all of a sudden doesn't bridge to Objective-C. Haven't figured out a best practice yet, but as long as we have Obj-C bridging tests, a missing @objc annotation is fine since it would get caught by the test at compile-time.

@objc(MBDirectionsOptions)
open class DirectionsOptions: NSObject, NSSecureCoding, NSCopying {

Expand All @@ -230,7 +233,7 @@ open class DirectionsOptions: NSObject, NSSecureCoding, NSCopying {
- parameter waypoints: An array of `Waypoint` objects representing locations that the route should visit in chronological order. The array should contain at least two waypoints (the source and destination) and at most 25 waypoints. (Some profiles, such as `MBDirectionsProfileIdentifierAutomobileAvoidingTraffic`, [may have lower limits](https://docs.mapbox.com/api/navigation/#directions).)
- parameter profileIdentifier: A string specifying the primary mode of transportation for the routes. This parameter, if set, should be set to `MBDirectionsProfileIdentifierAutomobile`, `MBDirectionsProfileIdentifierAutomobileAvoidingTraffic`, `MBDirectionsProfileIdentifierCycling`, or `MBDirectionsProfileIdentifierWalking`. `MBDirectionsProfileIdentifierAutomobile` is used by default.
*/
@objc required public init(waypoints: [Waypoint], profileIdentifier: MBDirectionsProfileIdentifier? = nil) {
required public init(waypoints: [Waypoint], profileIdentifier: MBDirectionsProfileIdentifier? = nil) {
self.waypoints = waypoints
self.profileIdentifier = profileIdentifier ?? .automobile
}
Expand Down Expand Up @@ -298,7 +301,12 @@ open class DirectionsOptions: NSObject, NSSecureCoding, NSCopying {
guard let profileIdentifier = decoder.decodeObject(of: NSString.self, forKey: "profileIdentifier") as String? else {
return nil
}

#if SWIFT_PACKAGE
self.profileIdentifier = MBDirectionsProfileIdentifier(rawValue: profileIdentifier) ?? MBDirectionsProfileIdentifier.automobileAvoidingTraffic
#else
self.profileIdentifier = MBDirectionsProfileIdentifier(rawValue: profileIdentifier)
#endif

includesSteps = decoder.decodeBool(forKey: "includesSteps")

Expand Down Expand Up @@ -382,7 +390,7 @@ open class DirectionsOptions: NSObject, NSSecureCoding, NSCopying {

This property should be set to `MBDirectionsProfileIdentifierAutomobile`, `MBDirectionsProfileIdentifierAutomobileAvoidingTraffic`, `MBDirectionsProfileIdentifierCycling`, or `MBDirectionsProfileIdentifierWalking`. The default value of this property is `MBDirectionsProfileIdentifierAutomobile`, which specifies driving directions.
*/
@objc open var profileIdentifier: MBDirectionsProfileIdentifier
open var profileIdentifier: MBDirectionsProfileIdentifier

/**
A Boolean value indicating whether `MBRouteStep` objects should be included in the response.
Expand Down Expand Up @@ -418,7 +426,11 @@ open class DirectionsOptions: NSObject, NSSecureCoding, NSCopying {

By default, no attribute options are specified. It is recommended that `routeShapeResolution` be set to `.full`.
*/
@objc open var attributeOptions: AttributeOptions = []
#if SWIFT_PACKAGE
open var attributeOptions: AttributeOptions = []
#else
@objc open var attributeOptions: MBAttributeOptions = []
#endif

/**
The locale in which the route’s instructions are written.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
import Polyline
import CoreLocation


/**
A `DirectionsResult` represents a result returned from either the Mapbox Directions service.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Foundation
import CoreLocation


/**
A single cross street along a step.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,30 @@ import Foundation
/**
A lane on the road approaching an intersection.
*/
@objcMembers
@objc(MBLane)
public class Lane: NSObject, NSSecureCoding {
/**
The lane indications specifying the maneuvers that may be executed from the lane.
*/
@objc public let indications: LaneIndication
#if SWIFT_PACKAGE
public let indications: LaneIndication
#else
@objc public let indications: MBLaneIndication
#endif

/**
Initializes a new `Lane` using the given lane indications.
*/
#if SWIFT_PACKAGE
Copy link
Contributor Author

@frederoni frederoni Apr 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely satisfied with this workaround, having to provide duplicated initializers.
What I would like to see is a pre-processor macro:

#if SWIFT_PACKAGE
#define OBJC_NO_SPM
#define OBJC_NO_SPM(x)
#else
#define OBJC_NO_SPM @objc
#define OBJC_NO_SPM(x) @objc(x)
#endif

but an Obj-C macro would not work with SPM and SPM doesn't support these macros.

@objcMembers solves roughly half of the issues, since it ignores the @objc-flag if it's not able to bridge properly to Obj-C, but it only works for some methods and properties.

public init(indications: LaneIndication) {
self.indications = indications
}
#else
@objc public init(indications: LaneIndication) {
self.indications = indications
}
#endif

internal convenience init(json: JSONDictionary) {
let indications = LaneIndication(descriptions: json["indications"] as! [String])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Foundation


public typealias LaneIndication = MBLaneIndication


extension LaneIndication: CustomStringConvertible {
/**
Creates a lane indication from the given description strings.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import Foundation


/**
A component that represents a lane representation of an instruction.
*/
@objcMembers
@objc(MBLaneIndicationComponent)

This comment was marked as resolved.

open class LaneIndicationComponent: NSObject, ComponentRepresentable {

Expand All @@ -9,14 +13,14 @@ open class LaneIndicationComponent: NSObject, ComponentRepresentable {

If the value is `[LaneIndication.left", LaneIndication.straightAhead]`, the driver can go left or straight ahead from that lane. This is only set when the `component` is a `lane`.
*/
@objc public var indications: LaneIndication
public var indications: LaneIndication

/**
The boolean that indicates whether the lane can be used to complete the maneuver.

If multiple lanes are active, then they can all be used to complete the upcoming maneuver. This value is set to `false` by default.
*/
@objc public var isUsable: Bool = false
public var isUsable: Bool = false

public static var supportsSecureCoding: Bool = true

Expand All @@ -26,12 +30,12 @@ open class LaneIndicationComponent: NSObject, ComponentRepresentable {
- parameter indications: The directions to go from a lane component.
- parameter isUsable: The flag to indicate that the upcoming maneuver can be completed with a lane component.
*/
@objc public init(indications: LaneIndication, isUsable: Bool) {
public init(indications: LaneIndication, isUsable: Bool) {
self.indications = indications
self.isUsable = isUsable
}

@objc public required init?(coder decoder: NSCoder) {
public required init?(coder decoder: NSCoder) {
guard let directions = decoder.decodeObject(of: [NSArray.self, NSString.self], forKey: "indications") as? [String], let indications = LaneIndication(descriptions: directions) else {
return nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Foundation
import CoreLocation
import Polyline


/**
A `Route` object defines a single route that the user can follow to visit a series of waypoints in order. The route object includes information about the route, such as its distance and expected travel time. Depending on the criteria used to calculate the route, the route object may also include detailed turn-by-turn instructions.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import Foundation
import CoreLocation
import Polyline


/**
A `RouteLeg` object defines a single leg of a route between two waypoints. If the overall route has only two waypoints, it has a single `RouteLeg` object that covers the entire route. The route leg object includes information about the leg, such as its name, distance, and expected travel time. Depending on the criteria used to calculate the route, the route leg object may also include detailed turn-by-turn instructions.

You do not create instances of this class directly. Instead, you receive route leg objects as part of route objects when you request directions using the `Directions.calculate(_:completionHandler:)` method.
*/
@objcMembers
@objc(MBRouteLeg)
open class RouteLeg: NSObject, NSSecureCoding {

Expand Down Expand Up @@ -87,7 +91,11 @@ open class RouteLeg: NSObject, NSSecureCoding {
guard let decodedProfileIdentifier = decoder.decodeObject(of: NSString.self, forKey: "profileIdentifier") as String? else {
return nil
}
#if SWIFT_PACKAGE
profileIdentifier = MBDirectionsProfileIdentifier(rawValue: decodedProfileIdentifier) ?? MBDirectionsProfileIdentifier.automobileAvoidingTraffic
#else
profileIdentifier = MBDirectionsProfileIdentifier(rawValue: decodedProfileIdentifier)
#endif

segmentDistances = decoder.decodeObject(of: [NSArray.self, NSNumber.self], forKey: "segmentDistances") as? [CLLocationDistance]
expectedSegmentTravelTimes = decoder.decodeObject(of: [NSArray.self, NSNumber.self], forKey: "expectedSegmentTravelTimes") as? [TimeInterval]
Expand Down Expand Up @@ -209,7 +217,11 @@ open class RouteLeg: NSObject, NSSecureCoding {

The value of this property is `MBDirectionsProfileIdentifierAutomobile`, `MBDirectionsProfileIdentifierAutomobileAvoidingTraffic`, `MBDirectionsProfileIdentifierCycling`, or `MBDirectionsProfileIdentifierWalking`, depending on the `profileIdentifier` property of the original `RouteOptions` object. This property reflects the primary mode of transportation used for the route leg. Individual steps along the route leg might use different modes of transportation as necessary.
*/
#if SWIFT_PACKAGE
public let profileIdentifier: MBDirectionsProfileIdentifier
#else
@objc public let profileIdentifier: MBDirectionsProfileIdentifier
#endif

func debugQuickLookObject() -> Any? {
let coordinates = steps.reduce([], { $0 + ($1.coordinates ?? []) })
Expand Down
Loading