32 lines
1.5 KiB
Swift
32 lines
1.5 KiB
Swift
import Foundation
|
|
import HelderCore
|
|
|
|
public enum ProjectLocator {
|
|
/// The folder a fresh window opens: `$HELDER_PROJECT`, else the first directory argument; nil shows the launcher.
|
|
/// A bare "." is ignored, so a development launch lands on the launcher too.
|
|
public static func initialRoot(environment: [String: String] = ProcessInfo.processInfo.environment,
|
|
arguments: [String] = CommandLine.arguments) -> URL? {
|
|
if let env = environment["HELDER_PROJECT"], isDirectory(env) {
|
|
return URL(fileURLWithPath: env).standardizedFileURL
|
|
}
|
|
let candidate = arguments.dropFirst().first { $0 != "." && !$0.hasPrefix("-") && isDirectory($0) }
|
|
return candidate.map { URL(fileURLWithPath: $0).standardizedFileURL }
|
|
}
|
|
|
|
/// The folder name, used for recents and the Explorer header.
|
|
public static func name(of root: URL) -> String {
|
|
root.lastPathComponent.isEmpty ? root.path : root.lastPathComponent
|
|
}
|
|
|
|
/// The window title: `APP_NAME` from the project's `.env`, else the folder name.
|
|
public static func title(of root: URL) -> String {
|
|
let env = try? String(contentsOf: root.appendingPathComponent(".env"), encoding: .utf8)
|
|
return ProjectTitle.title(folderName: name(of: root), envContents: env)
|
|
}
|
|
|
|
private static func isDirectory(_ path: String) -> Bool {
|
|
var isDir: ObjCBool = false
|
|
return FileManager.default.fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue
|
|
}
|
|
}
|