28 lines
1.3 KiB
Swift
28 lines
1.3 KiB
Swift
import Foundation
|
|
|
|
public enum ProjectTitle {
|
|
/// `APP_NAME` from a project's `.env` text, unquoted; nil when absent or empty. The last assignment wins.
|
|
public static func appName(fromEnv contents: String) -> String? {
|
|
var raw: String?
|
|
for line in contents.split(omittingEmptySubsequences: false, whereSeparator: \.isNewline) {
|
|
let trimmed = line.drop(while: { $0 == " " || $0 == "\t" })
|
|
if trimmed.hasPrefix("#") { continue }
|
|
guard trimmed.hasPrefix("APP_NAME") else { continue }
|
|
let rest = trimmed.dropFirst("APP_NAME".count).drop(while: { $0 == " " || $0 == "\t" })
|
|
guard rest.first == "=" else { continue }
|
|
raw = String(rest.dropFirst())
|
|
}
|
|
guard var value = raw?.trimmingCharacters(in: .whitespaces) else { return nil }
|
|
if value.count >= 2, let first = value.first, first == "\"" || first == "'", value.last == first {
|
|
value = String(value.dropFirst().dropLast())
|
|
}
|
|
value = value.trimmingCharacters(in: .whitespaces)
|
|
return value.isEmpty ? nil : value
|
|
}
|
|
|
|
/// The title a window shows: `APP_NAME` when the project sets one, else the folder name.
|
|
public static func title(folderName: String, envContents: String?) -> String {
|
|
envContents.flatMap(appName(fromEnv:)) ?? folderName
|
|
}
|
|
}
|