-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathname-generator.swift
More file actions
executable file
·107 lines (85 loc) · 3.52 KB
/
name-generator.swift
File metadata and controls
executable file
·107 lines (85 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import Foundation
func getEnvOrFallback(_ varName: String, _ defaultValue: String) -> String {
guard let envVal = ProcessInfo.processInfo.environment[varName] else {
return defaultValue
}
if envVal.isEmpty {
return defaultValue
}
return envVal
}
func resolveFile(envVar: String, defaultFolder: String) throws -> URL {
guard let envVal = ProcessInfo.processInfo.environment[envVar] else {
// Use default folder and pick random file from there
return try pickRandomFileInFolder(defaultFolder)
}
if envVal.isEmpty {
throw NSError(domain: "Environment variable \(envVar) is empty", code: 1)
}
guard let url = URL(string: envVal) else {
throw NSError(domain: "Invalid URL from environment variable \(envVar)", code: 2)
}
if !url.isFileURL {
throw NSError(domain: "URL \(url) is not a file", code: 3)
}
return url
}
func pickRandomFileInFolder(_ folderPath: String) throws -> URL {
let folderUrl = URL(fileURLWithPath: folderPath)
let fileManager = FileManager.default
guard let contents = try? fileManager.contentsOfDirectory(at: folderUrl, includingPropertiesForKeys: nil) else {
throw NSError(domain: "Could not read directory \(folderUrl.path)", code: 4)
}
let files = contents.filter { $0.isFileURL }
if files.isEmpty {
throw NSError(domain: "No files found in directory \(folderUrl.path)", code: 5)
}
return files[Int.random(in: 0..<files.count)]
}
func readNonEmptyLines(from url: URL) throws -> [String] {
do {
let content = try String(contentsOf: url, encoding: .utf8)
return content.components(separatedBy: .newlines).filter { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty }
} catch {
throw NSError(domain: "Could not read file \(url.path)", code: 6)
}
}
func maybeDebug(_ adjective: String, _ noun: String, nounFile: URL, adjFile: URL) {
if ProcessInfo.processInfo.environment["DEBUG"] == "true" {
print("Adjective:", adjective)
print("Noun:", noun)
print("Adj File:", adjFile.path)
print("Noun File:", nounFile.path)
}
}
func main() {
let separator = getEnvOrFallback("SEPARATOR", "-")
do {
// Resolve files
guard var nounFile = try resolveFile(envVar: "NOUN_FILE", defaultFolder: "nouns") else {
throw NSError(domain: "Could not resolve NOUN_FILE", code: 7)
}
guard var adjFile = try resolveFile(envVar: "ADJ_FILE", defaultFolder: "adjectives") else {
throw NSError(domain: "Could not resolve ADJ_FILE", code: 8)
}
// Read lines
let adjectives = try readNonEmptyLines(from: adjFile)
let nouns = try readNonEmptyLines(from: nounFile)
if adjectives.isEmpty || nouns.isEmpty {
print("No valid entries in files")
return
}
// Determine count of lines to emit
let counto = Int(getEnvOrFallback("counto", (ProcessInfo.processInfo.environment["LINES"] ?? "24")))
?? 24
for _ in 0..<counto {
let randomAdjective = adjectives.randomElement()!
let randomNoun = nouns.randomElement()!.lowercased()
maybeDebug(randomAdjective, randomNoun, nounFile: nounFile, adjFile: adjFile)
print("\(randomAdjective)\(separator)\(randomNoun)")
}
} catch {
print("Error: \(error.localizedDescription)")
}
}
main()