Skip to content

Commit

Permalink
Swift 4 and Backward Compatibility (#38)
Browse files Browse the repository at this point in the history
* Revert "Update README.md"

This reverts commit a0c797e.

* Revert "Migration to Swift 4 (#36)"

This reverts commit e76d9a1.

* Enables swift 4

* updates travis

* Uses updated packages

* Update .swift-version

* Updates Package.swift

* Update Package.swift

* Update README.md

* Update .gitignore
  • Loading branch information
Aaron Liberatore authored Oct 2, 2017
1 parent a0c797e commit 1100268
Show file tree
Hide file tree
Showing 26 changed files with 146 additions and 135 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
.DS_Store
/.build
/.build-linux/
/*.xcodeproj
/build
Package.resolved
.build/*
*.xcodeproj/*

2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
3.1.1
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ matrix:
- os: linux
dist: trusty
sudo: required
- os: linux
dist: trusty
sudo: required
env: SWIFT_SNAPSHOT=4.0
- os: osx
osx_image: xcode8.3
sudo: required
- os: osx
osx_image: xcode9
sudo: required
env: SWIFT_SNAPSHOT=4.0

before_install:
- git clone https://github.com/IBM-Swift/Package-Builder.git
Expand Down
5 changes: 5 additions & 0 deletions Package.pins
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"autoPin": false,
"pins": [],
"version": 1
}
62 changes: 23 additions & 39 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

/**
* Copyright IBM Corporation 2016, 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/*
* Copyright IBM Corporation 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import PackageDescription

let package = Package(
name: "CloudEnvironment",
products: [
.library(
name: "CloudEnvironment",
targets: ["CloudEnvironment"]
)
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/Swift-cfenv.git", .upToNextMajor(from: "5.0.0")),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMajor(from: "2.0.0"))
],
targets: [
.target(
name: "CloudEnvironment",
dependencies: ["CloudFoundryEnv", "LoggerAPI"]
),
.testTarget(
name: "CloudEnvironmentTests",
dependencies: ["CloudEnvironment"]
)
]
name: "CloudEnvironment",
targets: [
Target(name: "CloudEnvironment", dependencies: [])
],
dependencies: [
.Package(url: "https://github.com/IBM-Swift/LoggerAPI.git", majorVersion: 1),
.Package(url: "https://github.com/IBM-Swift/Swift-cfenv.git", majorVersion: 4)
]
)
43 changes: 43 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// swift-tools-version:4.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
/**
* Copyright IBM Corporation 2016, 2017
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

import PackageDescription

let package = Package(
name: "CloudEnvironment",
products: [
.library(
name: "CloudEnvironment",
targets: ["CloudEnvironment"]
)
],
dependencies: [
.package(url: "https://github.com/IBM-Swift/Swift-cfenv.git", .upToNextMajor(from: "4.0.0")),
.package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMajor(from: "1.0.0"))
],
targets: [
.target(
name: "CloudEnvironment",
dependencies: ["CloudFoundryEnv", "LoggerAPI"]
),
.testTarget(
name: "CloudEnvironmentTests",
dependencies: ["CloudEnvironment"]
)
]
)
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
CloudEnvironment (formerly known as CloudConfiguration) is a convenience Swift package for accessing environment variables mapped to JSON objects from various Cloud computing environments, such as, but not limited to, Cloud Foundry and Kubernetes. For example, to obtain the credentials for accessing a Cloudant database, you need to parse the `VCAP_SERVICES` environment variable when running in Cloud Foundry, while to obtain the same credentials when running in Kubernetes, you may need to parse an environment variable named `CLOUDANT_CREDENTIALS`. In other words, the path for obtaining certain environment values may differ from one cloud environment to another. By leveraging this package, you can make your Swift application environment-agnostic when it comes to obtaining such values. Using CloudEnvironment allows you to abstract these low-level details from your application's source code.

## Swift version
The latest version of CloudEnvironment works with the `4.0` version of the Swift binaries. You can download this version of the Swift binaries by following this [link](https://swift.org/download/#snapshots).
The latest version of CloudEnvironment works with the `3.1.1` and newer versions of the Swift binaries. You can download any of these versions of the Swift binaries by following this [link](https://swift.org/download/#releases).

## Abstraction and supported search pattern types
This package allows you to define a lookup key that your Swift application can leverage for searching its corresponding value. This abstraction decouples your application from the actual name used for the environment variable you are looking for. For example, if you created a Cloudant service named `my-awesome-cloudant-db`, you don't have to use this name as the key in your Swift code to obtain its credentials. Instead, you can define a lookup key, say `cloudant-credentials` and mapped it to the actual service name, `my-awesome-cloudant-db`.
Expand Down Expand Up @@ -53,7 +53,11 @@ let package = Package(
...

dependencies: [
.Package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", majorVersion: 5),
// Swift 3.1.1
.Package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", majorVersion: 4),

//Swift 4.0
.package(url: "https://github.com/IBM-Swift/CloudEnvironment.git", .upToNextMajor(from: "4.0.0")),

...

Expand Down
31 changes: 21 additions & 10 deletions Sources/CloudEnvironment/CloudEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,27 @@ public class CloudEnv {

/// Constructor
///
/// - Parameter mappingsFilePath: Optional. The absolute path to a `mappings.json` file.
/// - Parameter cloudFoundryFile: Optional. The absolute path to a JSON file that contains values for Cloud Foundry environment variables.
/// (mainly used for testing).
public init(mappingsFileFolder: String? = nil, cloudFoundryFile: String? = nil) {
/// - Parameter mappingsFilePath: Optional. The path to the `mappings.json` file; this path should be relative to the root folder of the Swift application.
/// - Parameter cloudFoundryFile: Optional. The path to a JSON file that contains values for Cloud Foundry environment variables (mainly used for testing);
/// this path should be relative to the current working directory (which in most cases it is the folder of the Swift application).
public init(mappingsFilePath: String? = nil, cloudFoundryFile: String? = nil) {

// Set instance properties
self.cloudFoundryFile = cloudFoundryFile

// Compute path to mappings.json
let mappingsFilePath: String = (mappingsFileFolder != nil) ? mappingsFileFolder! : "config"

let filePath: String
if let mappingsFilePath = mappingsFilePath {
let sanitizedPath = sanitize(path: mappingsFilePath)
filePath = sanitizedPath
} else {
filePath = "config"
}

// Load mappings file (Cloud Foundry & local execution)
// If running locally, make sure the app is started from the root folder of the project
// In other words, the current working directory should be the project folder.
mapManager.load(file: "\(mappingsFilePath)/\(CloudEnv.mappingsFile)", relativeFrom: .pwd)
// If running locally, make sure the app is starter from the project folder
// In other words, the current working directory should be the project folder
mapManager.load(file: "\(filePath)/\(CloudEnv.mappingsFile)", relativeFrom: .pwd)
}

/// Returns the corresponding JSON dictionary value in a string.
Expand Down Expand Up @@ -164,7 +170,12 @@ public class CloudEnv {
guard let range = path.range(of: "/") else {
return path
}
return String(path[range.upperBound...])
#if swift(>=3.2)
return String(path[range.upperBound...])
#else
return path.substring(from: range.upperBound)
#endif

}
return path
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/CloudEnvironmentTests/AlertNotificationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class AlertNotificationTests: BaseTest {
class AlertNotificationTests: XCTestCase {

static var allTests : [(String, (AlertNotificationTests) -> () throws -> Void)] {
return [
Expand All @@ -29,7 +29,7 @@ class AlertNotificationTests: BaseTest {
func testGetCredentials() {

// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFileFolder: resourcesFolder, cloudFoundryFile: cloudFoundryFile)
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getAlertNotificationCredentials(name: "AlertNotificationKey") else {
XCTFail("Could not load Alert Notification service credentials.")
Expand Down
5 changes: 3 additions & 2 deletions Tests/CloudEnvironmentTests/AppIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class AppIDTests: BaseTest {
class AppIDTests: XCTestCase {

static var allTests : [(String, (AppIDTests) -> () throws -> Void)] {
return [
Expand All @@ -27,8 +27,9 @@ class AppIDTests: BaseTest {
}

func testGetCredentials() {

// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFileFolder: resourcesFolder, cloudFoundryFile: cloudFoundryFile)
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getAppIDCredentials(name: "AppIDKey") else {
XCTFail("Could not load AppID credentials.")
Expand Down
4 changes: 2 additions & 2 deletions Tests/CloudEnvironmentTests/AutoScalingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class AutoScalingTests: BaseTest {
class AutoScalingTests: XCTestCase {

static var allTests : [(String, (AutoScalingTests) -> () throws -> Void)] {
return [
Expand All @@ -29,7 +29,7 @@ class AutoScalingTests: BaseTest {
func testGetCredentials() {

// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFileFolder: resourcesFolder, cloudFoundryFile: cloudFoundryFile)
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getAutoScalingCredentials(name: "AutoScalingKey") else {
XCTFail("Could not load AutoScaling credentials.")
Expand Down
43 changes: 0 additions & 43 deletions Tests/CloudEnvironmentTests/BaseTest.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Tests/CloudEnvironmentTests/CloudantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class CloudantTests: BaseTest {
class CloudantTests: XCTestCase {

static var allTests : [(String, (CloudantTests) -> () throws -> Void)] {
return [
Expand All @@ -29,7 +29,7 @@ class CloudantTests: BaseTest {
func testGetCredentials() {

// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFileFolder: resourcesFolder, cloudFoundryFile: cloudFoundryFile)
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getCloudantCredentials(name: "CloudantKey") else {
XCTFail("Could not load Cloudant credentials.")
Expand Down
4 changes: 2 additions & 2 deletions Tests/CloudEnvironmentTests/DB2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class DB2Tests: BaseTest {
class DB2Tests: XCTestCase {

static var allTests : [(String, (DB2Tests) -> () throws -> Void)] {
return [
Expand All @@ -29,7 +29,7 @@ class DB2Tests: BaseTest {
func testGetCredentials() {

// Load test mappings.json file and Cloud Foundry test credentials-- VCAP_SERVICES and VCAP_APPLICATION
let cloudEnv = CloudEnv(mappingsFileFolder: resourcesFolder, cloudFoundryFile: cloudFoundryFile)
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources", cloudFoundryFile: "Tests/CloudEnvironmentTests/resources/config_cf_example.json")

guard let credentials = cloudEnv.getDB2Credentials(name: "DB2Key") else {
XCTFail("Could not load DB2 service credentials.")
Expand Down
4 changes: 2 additions & 2 deletions Tests/CloudEnvironmentTests/EnvironmentVariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import Configuration
@testable import CloudEnvironment

class EnvironmentVariablesTests: BaseTest {
class EnvironmentVariablesTests: XCTestCase {

static var allTests : [(String, (EnvironmentVariablesTests) -> () throws -> Void)] {
return [
Expand All @@ -29,7 +29,7 @@ class EnvironmentVariablesTests: BaseTest {
func testGetCredentials() {

// Load test mappings.json file
let cloudEnv = CloudEnv(mappingsFileFolder: "Tests/CloudEnvironmentTests/resources")
let cloudEnv = CloudEnv(mappingsFilePath: "Tests/CloudEnvironmentTests/resources")

let jsonString = "{\"name\":\"21a084f4-4eb3-4de4-9834-33bdc7be5df9/d2a85740-da7a-4615-aabf-5bdc35c63618\",\"password\":\"alertnotification-pwd\",\"url\":\"https://ibmnotifybm.mybluemix.net/api/alerts/v1\",\"swaggerui\":\"https://ibmnotifybm.mybluemix.net/docs/alerts/v1\"}"

Expand Down
Loading

0 comments on commit 1100268

Please sign in to comment.