From 4b4ace850072aa1b5a5fcaf46190477077ca6d53 Mon Sep 17 00:00:00 2001 From: Roman Gardukevich Date: Mon, 28 Jun 2021 18:03:17 +0300 Subject: [PATCH 1/2] 1.0.0-beta.6 release --- Cartfile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Cartfile b/Cartfile index adc0d1ef7..3ae943e1f 100644 --- a/Cartfile +++ b/Cartfile @@ -1,4 +1,4 @@ -binary "https://api.mapbox.com/downloads/v2/carthage/search-sdk/MapboxSearch.json" == 1.0.0-beta.5 -binary "https://api.mapbox.com/downloads/v2/carthage/search-sdk/MapboxSearchUI.json" == 1.0.0-beta.5 -binary "https://api.mapbox.com/downloads/v2/carthage/mapbox-common/MapboxCommon.json" ~> 11.0.0 -github "mapbox/mapbox-events-ios" ~> 0.10.0 +binary "https://api.mapbox.com/downloads/v2/carthage/search-sdk/MapboxSearch.json" == 1.0.0-beta.6 +binary "https://api.mapbox.com/downloads/v2/carthage/search-sdk/MapboxSearchUI.json" == 1.0.0-beta.6 +binary "https://api.mapbox.com/downloads/v2/carthage/mapbox-common/MapboxCommon.json" ~> 13.0.0 +github "mapbox/mapbox-events-ios" ~> 1.0.0 From 22a701493b5de9a0c9b6e17d5860690eafb22b36 Mon Sep 17 00:00:00 2001 From: Roman Gardukevich Date: Wed, 28 Oct 2020 13:46:12 +0300 Subject: [PATCH 2/2] Include SPM support --- .gitignore | 2 + Package.resolved | 25 +++ Package.swift | 181 +++++++++++++++++++ Sources/MapboxSearchDependencies/Dummy.swift | 1 + 4 files changed, 209 insertions(+) create mode 100644 .gitignore create mode 100644 Package.resolved create mode 100644 Package.swift create mode 100644 Sources/MapboxSearchDependencies/Dummy.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..b0ccdfcad --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +.swiftpm diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 000000000..c739a83f2 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,25 @@ +{ + "object": { + "pins": [ + { + "package": "MapboxCommon", + "repositoryURL": "git@github.com:mapbox/mapbox-common-ios.git", + "state": { + "branch": null, + "revision": "16e7bb71288b7835e420ba8213c1c45276338839", + "version": "13.0.0" + } + }, + { + "package": "MapboxMobileEvents", + "repositoryURL": "https://github.com/mapbox/mapbox-events-ios", + "state": { + "branch": null, + "revision": "8e0c725271dee0a0c957b9b9738aef81b601f12b", + "version": "1.0.2" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift new file mode 100644 index 000000000..8d0f2dc6a --- /dev/null +++ b/Package.swift @@ -0,0 +1,181 @@ +// swift-tools-version:5.3 +// The swift-tools-version declares the minimum version of Swift required to build this package. + + +import PackageDescription +import Foundation + +let registry = SDKRegistry() + +let (searchVersion, searchVersionHash) = ("1.0.0-beta.6", "27b630300a2647fc33da3208b63321b4c1cef7478ab531aabaea605998ce536c") +let (searchUIVersion, searchUIVersionHash) = ("1.0.0-beta.6", "a273c17b9c778d5f5617933bf93575719c2c914c99209a73bf3c110826f54fdc") + + +let package = Package( + name: "MapboxSearch", + platforms: [ + .iOS(.v11) + ], + products: [ + .library( + name: "MapboxSearch", + targets: [ + "MapboxSearchDependencies", + "MapboxSearch" + ] + ), + .library( + name: "MapboxSearchUI", + targets: [ + "MapboxSearchUI" + ] + ), + ], + dependencies: [ + .package(name: "MapboxMobileEvents", + url: "https://github.com/mapbox/mapbox-events-ios", + from: "1.0.0"), + .package(name: "MapboxCommon", + url: "git@github.com:mapbox/mapbox-common-ios.git", + from: "13.0.0"), + ], + targets: [ + .target(name: "MapboxSearchDependencies", + dependencies: [ + "MapboxMobileEvents", + "MapboxCommon" + ]), + registry.mapboxSearchTarget(version: searchVersion, + checksum: searchVersionHash), + registry.mapboxSearchUITarget(version: searchUIVersion, + checksum: searchUIVersionHash) + ], + cxxLanguageStandard: .cxx14 +) + + +struct SDKRegistry { + let host = "api.mapbox.com" + + func binaryTarget(name: String, version: String, path: String, filename: String, checksum: String) -> Target { + var url = "https://\(host)/downloads/v2/\(path)/releases/ios/packages/\(version)/\(filename)" + + if let token = netrcToken { + url += "?access_token=\(token)" + } else { + debugPrint("Mapbox token wasn't founded in ~/.netrc. Fix this issue to integrate Mapbox SDK. Otherwise, you will see 'invalid status code 401' or 'no XCFramework found. To clean issue in Xcode, remove ~/Library/Developer/Xcode/DerivedData folder") + } + + return .binaryTarget(name: name, url: url, checksum: checksum) + } + + var netrcToken: String? { + var mapboxToken: String? + do { + let netrc = try Netrc.load().get() + mapboxToken = netrc.machines.first(where: { $0.name == host })?.password + } catch { + // Do nothing on client machines + } + + return mapboxToken + } +} + +extension SDKRegistry { + func mapboxSearchTarget(version: String, checksum: String) -> Target { + return binaryTarget(name: "MapboxSearch", + version: version, + path: "search-sdk", + filename: "MapboxSearch.zip", + checksum: checksum) + } + func mapboxSearchUITarget(version: String, checksum: String) -> Target { + return binaryTarget(name: "MapboxSearchUI", + version: version, + path: "search-sdk", + filename: "MapboxSearchUI.zip", + checksum: checksum) + } +} + +// Reference: https://github.com/apple/swift-tools-support-core/pull/88 +// Sub-reference: https://github.com/Carthage/Carthage/pull/2774 +struct NetrcMachine { + let name: String + let login: String + let password: String +} + +struct Netrc { + + enum NetrcError: Error { + case fileNotFound(URL) + case unreadableFile(URL) + case machineNotFound + case missingToken(String) + case missingValueForToken(String) + } + + public let machines: [NetrcMachine] + + init(machines: [NetrcMachine]) { + self.machines = machines + } + + static func load(from fileURL: URL = URL(fileURLWithPath: "\(NSHomeDirectory())/.netrc")) -> Result { + guard FileManager.default.fileExists(atPath: fileURL.path) else { return .failure(NetrcError.fileNotFound(fileURL)) } + guard FileManager.default.isReadableFile(atPath: fileURL.path) else { return .failure(NetrcError.unreadableFile(fileURL)) } + + return Result(catching: { try String(contentsOf: fileURL, encoding: .utf8) }) + .flatMap { Netrc.from($0) } + } + + static func from(_ content: String) -> Result { + let trimmedCommentsContent = trimComments(from: content) + let tokens = trimmedCommentsContent + .trimmingCharacters(in: .whitespacesAndNewlines) + .components(separatedBy: .whitespacesAndNewlines) + .filter({ $0 != "" }) + + var machines: [NetrcMachine] = [] + + let machineTokens = tokens.split { $0 == "machine" } + guard tokens.contains("machine"), machineTokens.count > 0 else { return .failure(NetrcError.machineNotFound) } + + for machine in machineTokens { + let values = Array(machine) + guard let name = values.first else { continue } + guard let login = values["login"] else { return .failure(NetrcError.missingValueForToken("login")) } + guard let password = values["password"] else { return .failure(NetrcError.missingValueForToken("password")) } + machines.append(NetrcMachine(name: name, login: login, password: password)) + } + + guard machines.count > 0 else { return .failure(NetrcError.machineNotFound) } + return .success(Netrc(machines: machines)) + } + + private static func trimComments(from text: String) -> String { + let regex = try! NSRegularExpression(pattern: "\\#[\\s\\S]*?.*$", options: .anchorsMatchLines) + let nsString = text as NSString + let range = NSRange(location: 0, length: nsString.length) + let matches = regex.matches(in: text, range: range) + var trimmedCommentsText = text + matches.forEach { + trimmedCommentsText = trimmedCommentsText + .replacingOccurrences(of: nsString.substring(with: $0.range), with: "") + } + return trimmedCommentsText + } +} + +fileprivate extension Array where Element == String { + subscript(_ token: String) -> String? { + guard let tokenIndex = firstIndex(of: token), + count > tokenIndex, + !["machine", "login", "password"].contains(self[tokenIndex + 1]) else { + return nil + } + return self[tokenIndex + 1] + } +} diff --git a/Sources/MapboxSearchDependencies/Dummy.swift b/Sources/MapboxSearchDependencies/Dummy.swift new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Sources/MapboxSearchDependencies/Dummy.swift @@ -0,0 +1 @@ +