Просмотр исходного кода

Run CI on both Swift 4.2 and 5, and fix Swift 5 warning (#426)

Fixes a warning when compiling with Swift 5:

> 'withUnsafeBytes' is deprecated: use `withUnsafeBytes<R>(…)

Also updates CI to run tests on both Swift 4.2 and Swift 5.0.
Michael Rebello 6 лет назад
Родитель
Сommit
0f2af51ff7
5 измененных файлов с 49 добавлено и 28 удалено
  1. 6 6
      .travis-install.sh
  2. 28 13
      .travis.yml
  3. 2 1
      Package.swift
  4. 2 2
      README.md
  5. 11 6
      Sources/SwiftGRPC/Core/ByteBuffer.swift

+ 6 - 6
.travis-install.sh

@@ -18,23 +18,23 @@
 #
 # Install dependencies that aren't available as Ubuntu packages (or already present on macOS).
 #
-# Everything goes into $HOME/local. 
+# Everything goes into $HOME/local.
 #
-# Scripts should add 
-# - $HOME/local/bin to PATH 
+# Scripts should add
+# - $HOME/local/bin to PATH
 # - $HOME/local/lib to LD_LIBRARY_PATH
 #
 
 cd
 mkdir -p local
 
-if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
+if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then
   PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-osx-x86_64.zip
 else
   # Install swift
-  SWIFT_URL=https://swift.org/builds/swift-4.1.1-release/ubuntu1404/swift-4.1.1-RELEASE/swift-4.1.1-RELEASE-ubuntu14.04.tar.gz
+  SWIFT_URL=https://swift.org/builds/swift-${SWIFT_VERSION}-release/ubuntu1404/swift-${SWIFT_VERSION}-RELEASE/swift-${SWIFT_VERSION}-RELEASE-ubuntu14.04.tar.gz
   echo $SWIFT_URL
-  curl -fSsL $SWIFT_URL -o swift.tar.gz 
+  curl -fSsL $SWIFT_URL -o swift.tar.gz
   tar -xzf swift.tar.gz --strip-components=2 --directory=local
 
   PROTOC_URL=https://github.com/google/protobuf/releases/download/v3.5.1/protoc-3.5.1-linux-x86_64.zip

+ 28 - 13
.travis.yml

@@ -15,9 +15,26 @@
 #
 # Travis CI build file for Swift gRPC.
 
-os:
-  - linux
-  - osx
+matrix:
+  include:
+    - os: linux
+      env:
+        - SWIFT_VERSION=4.2
+        - MAJOR_VERSION=4.2
+    - os: osx
+      osx_image: xcode10.2
+      env:
+        - SWIFT_VERSION=4.2
+        - MAJOR_VERSION=4.2
+    - os: linux
+      env:
+        - SWIFT_VERSION=5.0
+        - MAJOR_VERSION=5
+    - os: osx
+      osx_image: xcode10.2
+      env:
+        - SWIFT_VERSION=5.0
+        - MAJOR_VERSION=5
 
 cache:
   apt: true
@@ -28,8 +45,6 @@ cache:
 # Use Ubuntu 14.04
 dist: trusty
 
-osx_image: xcode9.3
-
 sudo: false
 
 addons:
@@ -37,14 +52,14 @@ addons:
     sources:
       - sourceline: 'ppa:ondrej/apache2'  # for libnghttp2-dev
     packages:
-      - clang-3.8 
-      - lldb-3.8 
-      - libicu-dev 
-      - libtool 
-      - libcurl4-openssl-dev 
-      - libbsd-dev 
-      - build-essential 
-      - libssl-dev 
+      - clang-3.8
+      - lldb-3.8
+      - libicu-dev
+      - libtool
+      - libcurl4-openssl-dev
+      - libbsd-dev
+      - build-essential
+      - libssl-dev
       - uuid-dev
       - curl
       - unzip

+ 2 - 1
Package.swift

@@ -1,4 +1,4 @@
-// swift-tools-version:4.0
+// swift-tools-version:4.2
 /*
  * Copyright 2017, gRPC Authors All rights reserved.
  *
@@ -95,5 +95,6 @@ let package = Package(
     .testTarget(name: "SwiftGRPCTests", dependencies: ["SwiftGRPC"]),
     .testTarget(name: "SwiftGRPCNIOTests", dependencies: ["SwiftGRPC", "SwiftGRPCNIO"])
   ],
+  swiftLanguageVersions: [.v4, .v4_2, .version("5")],
   cLanguageStandard: .gnu11,
   cxxLanguageStandard: .cxx11)

+ 2 - 2
README.md

@@ -167,8 +167,8 @@ Original SwiftGRPC issue: https://github.com/grpc/grpc-swift/issues/337.
 grpc-swift depends on Swift, Xcode, and swift-protobuf. We are currently
 testing with the following versions:
 
-- Xcode 9.1
-- Swift 4.0
+- Xcode 10.0 / 10.2
+- Swift 4.2 / 5.0
 - swift-protobuf 1.3.1
 
 ## `SwiftGRPCNIO` package

+ 11 - 6
Sources/SwiftGRPC/Core/ByteBuffer.swift

@@ -14,9 +14,9 @@
  * limitations under the License.
  */
 #if SWIFT_PACKAGE
-  import CgRPC
+import CgRPC
 #endif
-import Foundation // for String.Encoding
+import Foundation
 
 /// Representation of raw data that may be sent and received using gRPC
 public class ByteBuffer {
@@ -35,11 +35,16 @@ public class ByteBuffer {
   ///
   /// - Parameter data: the data to store in the buffer
   public init(data: Data) {
-    var underlyingByteBuffer: UnsafeMutableRawPointer?
-    data.withUnsafeBytes { bytes in
-      underlyingByteBuffer = cgrpc_byte_buffer_create_by_copying_data(bytes, data.count)
+#if swift(>=5.0)
+    self.underlyingByteBuffer = data.withUnsafeBytes { bytes in
+      let buffer = bytes.bindMemory(to: UInt8.self).baseAddress
+      return cgrpc_byte_buffer_create_by_copying_data(buffer, data.count)
     }
-    self.underlyingByteBuffer = underlyingByteBuffer!
+#else
+    self.underlyingByteBuffer = data.withUnsafeBytes { bytes in
+      return cgrpc_byte_buffer_create_by_copying_data(bytes, data.count)
+    }
+#endif
   }
 
   deinit {