-
Notifications
You must be signed in to change notification settings - Fork 1
/
cartclone.swift
executable file
·200 lines (136 loc) · 4.19 KB
/
cartclone.swift
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#! /usr/bin/env xcrun swift
import Foundation
/// *Standard Stream Protocol*
/// Defines the file handle used and simple print string and output data functions
protocol StandardStream {
var stream: FileHandle { get }
func print(_ str: String)
func output(_ data: Data)
}
/// Default implementations of print(...) and output(...)
extension StandardStream {
func print(_ str: String) {
guard let data = (str + "\n").data(using: .utf8) else { return }
stream.write(data)
}
func output(_ data: Data) {
stream.write(data)
}
}
struct Repo: CustomStringConvertible {
let type: String
let uri: String
let version: String
var directory: String {
return (self.uri as NSString).lastPathComponent
}
var githubURL: String {
return "http://github.com/\(self.uri)"
}
var description: String {
return "\(type) -> \(uri), version: \(version)"
}
init(withString: String) {
let tokens = withString.components(separatedBy: .whitespacesAndNewlines) //static var punctuationCharacters
//console.print("TOKENS: \(tokens)")
type = tokens[0].trimmingCharacters(in: CharacterSet(charactersIn: "\""))
uri = tokens[1].trimmingCharacters(in: CharacterSet(charactersIn: "\""))
version = tokens[2].trimmingCharacters(in: CharacterSet(charactersIn: "\""))
}
}
struct LinkCommand {
private let task = Process()
@discardableResult
init(from: String, to: String, complete: (() -> ())? = nil) {
self.task.launchPath = "/bin/ln"
self.task.arguments = ["-s", from, to]
task.launch()
task.waitUntilExit()
complete?()
}
}
struct MakeDirCommand {
private let task = Process()
@discardableResult
init(_ path: String, complete: (() -> ())? = nil) {
self.task.launchPath = "/bin/mkdir"
self.task.arguments = ["-p", path]
task.launch()
task.waitUntilExit()
complete?()
}
}
struct RemoveDirCommand {
private let task = Process()
@discardableResult
init(_ path: String, complete: (() -> ())? = nil) {
self.task.launchPath = "/bin/rm"
self.task.arguments = ["-fR", path]
task.launch()
task.waitUntilExit()
complete?()
}
}
struct GitCommand {
private let task = Process()
@discardableResult
init(withArgumants args: [String], complete: (() -> ())?) {
self.task.launchPath = "/usr/bin/git"
self.task.arguments = args
task.launch()
task.waitUntilExit()
complete?()
}
}
// Standard Out - writes to `stdout`
struct StandardOut: StandardStream {
var stream: FileHandle { return FileHandle.standardOutput }
}
// Standard Error - writes to `stderr`
struct StandardErr: StandardStream {
var stream: FileHandle { return FileHandle.standardError }
}
let console = StandardOut()
let stderr = StandardErr()
let proc = ProcessInfo()
let switchOffset = proc.arguments.index(where: { return $0 == "--" }) ?? 0
let reposRequired = Array(proc.arguments[(switchOffset + 1)...])
let currentPath = FileManager.default.currentDirectoryPath
let reletiveCloneDir = currentPath + "/Carthage/Cartclone"
MakeDirCommand(reletiveCloneDir)
// Open Cartfile
guard let cartfile = FileManager.default.contents(atPath: currentPath + "/Cartfile.resolved") else {
stderr.print("Error: Could not read Cartfile")
exit(1)
}
guard let text = String(data: cartfile, encoding: .utf8) else {
stderr.print("Error: Could not read Cartfile")
exit(1)
}
let repoInfo: [String] = text.split(separator: "\n").compactMap {
// remove comments
guard !$0.hasPrefix("#") else {
return nil
}
return String($0)
}
let repos = repoInfo.map { Repo(withString: $0) }
repos.filter { reposRequired.contains($0.uri) }.forEach {
let cloneDir = reletiveCloneDir + "/" + $0.directory
let carthageCloneDir = currentPath + "/Carthage/Checkouts/" + $0.directory
let version = $0.version
console.print("Clone \($0.uri) into \(cloneDir)")
GitCommand(withArgumants: ["clone", $0.githubURL, cloneDir]) {
// Decend into cloned repo folder
FileManager.default.changeCurrentDirectoryPath(cloneDir)
// Check out the required version
GitCommand(withArgumants: ["checkout", version]) {
console.print("...done")
}
RemoveDirCommand(carthageCloneDir) {
//delete carthage version of the repo, then sym-link
LinkCommand(from: cloneDir, to: carthageCloneDir)
}
}
}
exit(0)