-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSCM.swift
204 lines (167 loc) · 5.72 KB
/
SCM.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
201
202
203
//
// SCM.swift
// ModuloKit
//
// Created by Brandon Sneed on 6/17/16.
// Copyright © 2016 TheHolyGrail. All rights reserved.
//
import Foundation
#if NOFRAMEWORKS
#else
import ELCLI
import ELFoundation
#endif
public typealias SCMCommandParser = (_ status: Int32, _ output: String?) -> Void
public let SCMDefaultError: Int32 = 9999
public enum SCMResult {
case success
case error(code: Int32, message: String)
func errorMessage() -> String {
switch self {
case .error(let code, let message) :
return "\(message), (code \(code))"
default:
return ""
}
}
func errorCode() -> Int32 {
switch self {
case .error(let code, _) :
return code
default:
return 0
}
}
}
extension SCMResult: Equatable {}
public func == (left: SCMResult, right: SCMResult) -> Bool {
switch left {
case .success:
switch right {
case .success:
return true
default:
return false
}
case .error(let leftCode, let leftMessage):
switch right {
case .error(let rightCode, let rightMessage):
return leftMessage == rightMessage && leftCode == rightCode
default:
return false
}
}
}
public protocol SCM {
var verbose: Bool { get set }
var isInstalled: Bool { get }
var isInitialized: Bool { get }
var defaultCheckout: String { get }
func remoteURL() -> String?
func nameFromRemoteURL(_ url: String) -> String
func branchName(_ path: String) -> String?
func remoteTrackingBranch(_ path: String) -> String?
func clone(_ url: String, path: String) -> SCMResult
func fetch(_ path: String) -> SCMResult
func pull(_ path: String, remoteData: String?) -> SCMResult
func checkout(version: SemverRange, path: String) -> SCMResult
/// Check out an arbitrary point or the HEAD of a branch (in git)
/// or the equivalent in other SCM solutions
func checkout(branchOrHash: String, path: String) -> SCMResult
func remove(_ path: String) -> SCMResult
func adjustIgnoreFile(pattern: String, removing: Bool) -> SCMResult
func checkStatus(_ path: String) -> SCMResult
func branches(_ path: String) -> [String]
func tags(_ path: String) -> [Semver]
func remotes(_ path: String) -> [String]
//func verifyCheckout(_ path: String, checkout: SCMCheckoutType) -> Bool
}
extension SCM {
/*fileprivate func shell(_ command: String) -> (status: Int32, output: String?, error: String?) {
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", command]
let outputPipe = Pipe()
let errorPipe = Pipe()
task.standardOutput = outputPipe
task.standardError = errorPipe
task.launch()
task.waitUntilExit()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: outputData, encoding: String.Encoding.utf8.rawValue) as? String
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let error = NSString(data: errorData, encoding: String.Encoding.utf8.rawValue) as? String
let status = task.terminationStatus
return (status, output, error)
}
@discardableResult
public func runCommand(_ command: String, completion: SCMCommandParser? = nil) -> Int32 {
if verbose {
writeln(.stdout, "Running command: \(command)")
}
let result = shell(command)
if let completion = completion {
let output = result.output
let error = result.error
let status = result.status
completion(status, output, error)
if status != 0 {
if let output = output {
writeln(.stderr, output)
}
if let error = error {
writeln(.stderr, error)
}
}
}
return result.status
}*/
@discardableResult
public func runCommand(_ command: String, silence: Bool = false, completion: SCMCommandParser? = nil) -> Int32 {
var execute = command
if verbose {
writeln(.stdout, "Running command: \(command)")
}
let tempFile = FileManager.temporaryFile()
if completion != nil {
execute = "\(execute) &> \(tempFile)"
}
let status = Int32(modulo_system(execute))
if let completion = completion {
let output = try? String(contentsOfFile: tempFile)
completion(status, output)
if status != 0 && silence == false, let output = output {
writeln(.stderr, output)
}
}
return status
}
@discardableResult
public func remove(_ path: String) -> SCMResult {
let result = runCommand("rm -rf \(path)")
if result == 0 {
return SCMResult.success
} else {
return SCMResult.error(code: result, message: "Unable to remove \(path). Check your permissions.")
}
}
}
public func currentSCM() -> SCM {
let scmList = [Git()/*, Mercurial()*/]
var result: SCM? = nil
for scm in scmList {
let installed = scm.isInstalled
let initialized = scm.isInitialized
if !installed {
exit(.scmNotFound)
}
if !initialized {
exit(.scmNotInitialized)
}
if installed && initialized {
result = scm
break
}
}
return result!
}