diff --git a/.gitignore b/.gitignore index 57a0276..d307381 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ ## Build generated build/ DerivedData/ +.DS_Store ## Various settings *.pbxuser diff --git a/Package.swift b/Package.swift index d3af0a6..a31656e 100644 --- a/Package.swift +++ b/Package.swift @@ -30,14 +30,15 @@ let package = Package( ], dependencies: [ // Dependencies declare other packages that this package depends on. - .package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMajor(from: "1.0.0")) + .package(url: "https://github.com/IBM-Swift/LoggerAPI.git", .upToNextMajor(from: "1.0.0")), + .package(url: "https://github.com/IBM-Swift/FileKit.git", .upToNextMajor(from: "0.0.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: "Configuration", - dependencies: ["LoggerAPI"] + dependencies: ["LoggerAPI", "FileKit"] ), .testTarget( name: "ConfigurationTests", diff --git a/Sources/Configuration/ConfigurationManager.swift b/Sources/Configuration/ConfigurationManager.swift index dae172a..ff16423 100644 --- a/Sources/Configuration/ConfigurationManager.swift +++ b/Sources/Configuration/ConfigurationManager.swift @@ -16,6 +16,7 @@ import Foundation import LoggerAPI +import FileKit /// ConfigurationManager class /// @@ -79,11 +80,11 @@ public class ConfigurationManager { public var path: String { switch self { case .executable: - return executableFolder + return FileKit.executableFolder case .pwd: - return presentWorkingDirectory + return FileKit.workingDirectory case .project: - return projectFolder + return FileKit.projectFolder case .customPath(let path): return path } diff --git a/Sources/Configuration/PathUtilities.swift b/Sources/Configuration/PathUtilities.swift deleted file mode 100644 index dc5b87e..0000000 --- a/Sources/Configuration/PathUtilities.swift +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 Foundation -import LoggerAPI - -// This URL is pointing to the executable, always -private let executableURL = { () -> URL in - #if os(Linux) - // Bundle is not available on Linux yet - // Get path to executable via /proc/self/exe - // https://unix.stackexchange.com/questions/333225/which-process-is-proc-self-for - return URL(fileURLWithPath: "/proc/self/exe") - #else - // Bundle is available on Darwin - return (Bundle.main.executableURL ?? URL(fileURLWithPath: CommandLine.arguments[0])) - #endif - }().resolvingSymlinksInPath() - -// This URL points to this source file -private let sourceFileURL = URL(fileURLWithPath: #file) - -// When program is ran from inside Xcode, the executable's path contains /DerivedData -private let isRanInsideXcode = executableURL.path.range(of: "/DerivedData/") != nil - -// When code is executed from within XCTestCase in Xcode, the executable is xctest -private let isRanFromXCTest = executableURL.path.hasSuffix("/xctest") - -/// Directory containing the executable of the project, or, if run from inside Xcode, -/// the /.build/debug folder in the project's root folder. -private let executableFolderURL = { () -> URL in - if isRanInsideXcode || isRanFromXCTest { - // Get URL to /debug manually - let sourceFile = sourceFileURL.path - - if let range = sourceFile.range(of: "/checkouts/") { - // In Swift 3.1, package source code is downloaded to //checkouts - return URL(fileURLWithPath: sourceFile[.., assume /.build instead - return URL(fileURLWithPath: sourceFile[.. URL? in - let fileManager = FileManager() - var startingDir = startingDir.appendingPathComponent("dummy") - - repeat { - startingDir.appendPathComponent("..") - startingDir.standardize() - let packageFilePath = startingDir.appendingPathComponent("Package.swift").path - - if fileManager.fileExists(atPath: packageFilePath) { - return startingDir - } - } while startingDir.path != "/" - - return nil -} -/// Directory containing the Package.swift of the project (as determined by traversing -/// up the directory structure starting at the directory containing the executable), or -/// if no Package.swift is found then the directory containing the executable -private let projectFolderURL = { () -> URL in - guard let url = projectHeadIterator(executableFolderURL) else { - Log.warning("No Package.swift found. Using executable folder as project folder.") - return executableFolderURL - } - - return url - -}().standardized - -/// Directory containing the Package.swift of the project when run through XCode or XCTest -/// Otherwise, returns the current working directory -let presentWorkingDirectoryURL = { () -> URL in - guard isRanInsideXcode || isRanFromXCTest, let url = projectHeadIterator(executableFolderURL) else { - return URL(fileURLWithPath: "") - } - - Log.warning("Running from Xcode or XcTest. Using project folder as pwd folder.") - - return url - -}().standardized - -/// Absolute path to the executable's folder -let executableFolder = executableFolderURL.path - -/// Absolute path to the project's root folder -let projectFolder = projectFolderURL.path - -/// Absolute path to the present working directory (PWD) -let presentWorkingDirectory = presentWorkingDirectoryURL.path diff --git a/Tests/ConfigurationTests/ConfigurationManagerTest.swift b/Tests/ConfigurationTests/ConfigurationManagerTest.swift index 6a60945..de1bc35 100644 --- a/Tests/ConfigurationTests/ConfigurationManagerTest.swift +++ b/Tests/ConfigurationTests/ConfigurationManagerTest.swift @@ -16,6 +16,7 @@ import XCTest import Foundation +import FileKit @testable import Configuration class ConfigurationManagerTest: XCTestCase { @@ -32,9 +33,12 @@ class ConfigurationManagerTest: XCTestCase { static let testJSONURL = URL(fileURLWithPath: #file).appendingPathComponent("../test.json").standardized - static let symlinkInPWD = URL(fileURLWithPath: "test.json") + static let symlinkInPWD = { () -> URL in + var pwd = FileKit.workingDirectoryURL.appendingPathComponent("test.json") + return FileKit.executableURL.path.hasSuffix("/xctest") ? pwd : URL(fileURLWithPath: "test.json") + }() - static let symlinkInExecutableFolder = URL(fileURLWithPath: executableFolder).appendingPathComponent("test.json").standardized + static let symlinkInExecutableFolder = URL(fileURLWithPath: FileKit.executableFolder).appendingPathComponent("test.json").standardized let jsonString = "{\n \"env\": \"\",\n \"OAuth\": {\n \"name\": \"facebook\",\n \"configuration\": {\n \"clientID\": \"\",\n \"clientSecret\": \"\",\n \"profileFields\": [\"displayName\", \"emails\", \"id\", \"name\"],\n \"profileURL\": \"https://graph.facebook.com/v2.6/me\",\n \"scope\": [\"email\"],\n \"state\": true\n }\n },\n \"port\": \"\"\n}" @@ -172,6 +176,10 @@ class ConfigurationManagerTest: XCTestCase { manager = ConfigurationManager().load(file: "test.json", relativeFrom: .pwd) XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true) + // Project Folder + manager = ConfigurationManager().load(file: "test.json", relativeFrom: .project) + XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true) + // Executable manager = ConfigurationManager().load(file: "test.json", relativeFrom: .executable) XCTAssertEqual(manager["OAuth:configuration:state"] as? Bool, true)