JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Folder.current.path showing build path, not project directory #84

Closed mrBaraam closed 4 years ago

mrBaraam commented 5 years ago

Trying to locate the actual path where my project is located. for example, Users/userName/Desktop/porjectName.

however, when I run: Folder.current.path I get the following: /Users/userName/Library/Developer/Xcode/DerivedData/projectName-ajpbrmlcmenwspeipndfoqpvyzce/Build/Products/Debug/

perhaps this is not how I get the actual directory of the project, if not, can someone please help to figure out how I can get the project directory as in "Users/userName/Desktop/porjectName."

I am using macOS commandLine tool btw,

thank you

Amzd commented 4 years ago

You can get the project folder from the current file: File(path: #file).parent.

I have an extension for it but I can't decide which syntax I should add in a pull request:

public extension File {
    /// Initialize an instance of an existing location at a given path.
    /// - parameter path: The absolute path of the location. Defaults to current file.
    /// - throws: `LocationError` if the item couldn't be found.
    init(path: StaticString = #file) throws {
        try self.init(path: path.withUTF8Buffer {
            String(decoding: $0, as: UTF8.self)
        })
    }

    /// Initialize an instance of File for the location this is called from.
    /// - parameter file: The absolute path of the location. Defaults to current file.
    /// - throws: `LocationError` if the item couldn't be found.
    static func current(_ file: StaticString = #file) throws -> File {
        try self.init(path: file.withUTF8Buffer {
            String(decoding: $0, as: UTF8.self)
        })
    }
}
let current = File()
// vs
let current = File.current()
JohnSundell commented 4 years ago

Like @Amzd said, using #file you can get access to the path of the current source file, from which you can resolve your project folder. Folder.current refers to the current execution folder, which will be Xcode's build folder when the program is run through Xcode, and the current command line location when run through a terminal.