This repository was archived by the owner on Mar 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbeak.swift
More file actions
69 lines (53 loc) · 2.89 KB
/
beak.swift
File metadata and controls
69 lines (53 loc) · 2.89 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
#!/usr/bin/env beak run
// MARK: - Script Dependencies
// beak: kareman/SwiftShell @ .upToNextMajor(from: "4.1.2")
// beak: onevcat/Rainbow @ .upToNextMajor(from: "3.1.2")
import Foundation
import Rainbow
import SwiftShell
// MARK: - Runnable Tasks
/// Makes sure all Swift files in the Scripts folder can be run as executables without the `.swift` extension or `beak run` prefix.
public func link() throws {
let scriptsDirUrl = URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent("Scripts")
let symlinksDirUrl = scriptsDirUrl.appendingPathComponent("SymLinks")
let gitignoreFileUrl = URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent(".gitignore")
if !FileManager.default.fileExists(atPath: symlinksDirUrl.path) {
try execute(bash: "mkdir \(symlinksDirUrl.path)")
}
var gitignoreContents = try String(contentsOfFile: gitignoreFileUrl.path)
if !gitignoreContents.contains("\nScripts/SymLinks/\n") {
if !gitignoreContents.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
gitignoreContents += "\n\n"
}
gitignoreContents += "# Beak Scripts' user-specific symbolic links\nScripts/SymLinks/\n"
try gitignoreContents.write(toFile: gitignoreFileUrl.path, atomically: true, encoding: .utf8)
}
for filePath in try FileManager.default.contentsOfDirectory(atPath: scriptsDirUrl.path) {
guard filePath.hasSuffix(".swift") else { continue } // skip if not a Swift file
let fileUrl = scriptsDirUrl.appendingPathComponent(filePath)
let toolName = fileUrl.lastPathComponent.replacingOccurrences(of: ".swift", with: "")
let symlinkUrl = symlinksDirUrl.appendingPathComponent(toolName)
try execute(bash: "chmod +x \(fileUrl.path)")
if !FileManager.default.fileExists(atPath: symlinkUrl.path) {
try execute(bash: "ln -s \(fileUrl.path) \(symlinkUrl.path)")
}
}
for bashConfigFile in [".bash_profile", ".bashrc", ".profile"] {
let bashConfigUrl = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent(bashConfigFile)
let direnvLine = "eval \"$(direnv hook bash)\""
if !FileManager.default.fileExists(atPath: bashConfigUrl.path) {
FileManager.default.createFile(atPath: bashConfigUrl.path, contents: nil)
}
if FileManager.default.fileExists(atPath: bashConfigUrl.path) && run(bash: "grep '\(direnvLine)' \(bashConfigUrl.path)").stdout.isEmpty {
try execute(bash: "echo '\(direnvLine)' >> \(bashConfigUrl.path)")
try execute(bash: "source \(bashConfigUrl.path)")
}
}
try execute(bash: "direnv allow .")
try execute(bash: "direnv reload")
}
// MARK: - Helpers
private func execute(bash command: String) throws {
print("⏳ Executing '\(command.italic.lightYellow)'".bold)
try runAndPrint(bash: command)
}