Skip to content

Commit 1865fb0

Browse files
authored
Merge pull request #10 from enuance/CPFeature-008-CommitPrefixInterface
CPFeature-008 Commit Prefix Interface
2 parents 2e32ee2 + 1ee5453 commit 1865fb0

File tree

8 files changed

+179
-109
lines changed

8 files changed

+179
-109
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// CPInterface.swift
3+
// commitPrefix
4+
//
5+
// MIT License
6+
//
7+
// Copyright (c) 2020 STEPHEN L. MARTINEZ
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in all
17+
// copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
// SOFTWARE.
26+
27+
import Consler
28+
import Foundation
29+
30+
protocol CPInterface {
31+
32+
func outputVersion() -> ConslerOutput
33+
34+
func outputPrefixes() throws -> ConslerOutput
35+
36+
func viewState() throws -> ConslerOutput
37+
38+
func deletePrefixes() throws -> ConslerOutput
39+
40+
func writeNew(prefixes rawValue: String) throws -> ConslerOutput
41+
42+
func activateBranchMode(with validator: String) throws -> ConslerOutput
43+
44+
func activateNormalMode() throws -> ConslerOutput
45+
46+
}

Sources/CommitPrefix/CPFileHandler.swift renamed to Sources/CommitPrefix/CPInterfaceImpl.swift

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CPFileHandler.swift
2+
// CPInterfaceImpl.swift
33
// commitPrefix
44
//
55
// MIT License
@@ -25,27 +25,43 @@
2525
// SOFTWARE.
2626

2727
import Consler
28-
import Foundation
2928
import Files
29+
import Foundation
3030

31-
struct CPFileHandler {
31+
struct CommitPrefix {
3232

33-
private let cpInteractor: CPInteractor
33+
private init() {}
3434

35-
init() throws {
35+
static func interface() -> CPInterface { CommitPrefix() }
36+
37+
private func getInteractor() throws -> CPInteractor {
3638
guard Folder.current.containsSubfolder(named: FolderName.git) else {
3739
throw CPError.notAGitRepo(currentLocation: Folder.current.path)
3840
}
3941
let gitDirectory = try Folder.current.subfolder(named: FolderName.git)
40-
self.cpInteractor = try CPInteractor(gitDirectory: gitDirectory)
4142
try CommitMessageHook.findOrCreate(with: gitDirectory)
43+
let cpInteractor = try CPInteractor(gitDirectory: gitDirectory)
44+
return cpInteractor
4245
}
4346

47+
}
48+
49+
// MARK: - CPInterface Conformances
50+
extension CommitPrefix: CPInterface {
51+
4452
func outputPrefixes() throws -> ConslerOutput {
45-
try cpInteractor.outputPrefixes()
53+
let cpInteractor = try getInteractor()
54+
return try cpInteractor.outputPrefixes()
55+
}
56+
57+
func outputVersion() -> ConslerOutput {
58+
return ConslerOutput(
59+
"CommitPrefix ", "version ", CPInfo.version)
60+
.describedBy(.normal, .cyan, .cyan)
4661
}
4762

4863
func viewState() throws -> ConslerOutput {
64+
let cpInteractor = try getInteractor()
4965
let cpState = try cpInteractor.getCommitPrefixState()
5066
switch cpState.mode {
5167
case .normal:
@@ -63,19 +79,23 @@ struct CPFileHandler {
6379
}
6480

6581
func deletePrefixes() throws -> ConslerOutput {
66-
try cpInteractor.deletePrefixes()
82+
let cpInteractor = try getInteractor()
83+
return try cpInteractor.deletePrefixes()
6784
}
6885

6986
func writeNew(prefixes rawValue: String) throws -> ConslerOutput {
70-
try cpInteractor.writeNew(prefixes: rawValue)
87+
let cpInteractor = try getInteractor()
88+
return try cpInteractor.writeNew(prefixes: rawValue)
7189
}
7290

7391
func activateBranchMode(with validator: String) throws -> ConslerOutput {
74-
try cpInteractor.activateBranchMode(with: validator)
92+
let cpInteractor = try getInteractor()
93+
return try cpInteractor.activateBranchMode(with: validator)
7594
}
7695

7796
func activateNormalMode() throws -> ConslerOutput {
78-
try cpInteractor.activateNormalMode()
97+
let cpInteractor = try getInteractor()
98+
return try cpInteractor.activateNormalMode()
7999
}
80100

81101
}

Sources/CommitPrefix/Constants.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import Foundation
2828

2929
struct CPInfo {
3030

31-
static let version = "1.4.0"
31+
static let version = "1.4.2"
3232

3333
}
3434

Sources/CommitPrefix/Error+Debug/CPDebugPrint.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,15 @@ private let isDebugMode = false
3535
/// A Debug Printer that only prints in debug mode
3636
func cpDebugPrint(_ value: Any, file: String = #file, line: Int = #line, function: String = #function) {
3737
guard isDebugMode else { return }
38-
print("********** Commit Prefix Debug **********")
39-
print("File: \(file)")
40-
print("Line: \(line)")
41-
print("Function: \(function)")
42-
print("value: ", value)
43-
print("*****************************************")
38+
let debugOutput = """
39+
40+
********** Commit Prefix Debug **********
41+
File: \(file)
42+
Line: \(line)
43+
Function: \(function)
44+
value: \(value)
45+
*****************************************
46+
47+
"""
48+
print(debugOutput)
4449
}

Sources/CommitPrefix/Hook/CommitMessageHookContents.swift

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ struct CommitMessageHookContents {
3030

3131
let fileIdentifier = "Created by CommitPrefix \(CPInfo.version)"
3232

33+
private let tab = "<tab>"
34+
3335
private var currentDate: String {
3436
let formatter = DateFormatter()
3537
formatter.dateFormat = "MM/dd/yyyy"
3638
return formatter.string(from: Date())
3739
}
3840

39-
func renderScript() -> String { """
41+
func renderScript() -> String {
42+
let script = """
4043
#!/usr/bin/env swift
4144
//
4245
// Commit-msg
@@ -45,36 +48,38 @@ struct CommitMessageHookContents {
4548
//
4649
4750
import Foundation
48-
51+
4952
\(renderEnumIOError())
5053
5154
\(renderStructIOCommitPrefix())
5255
5356
\(renderMainDoTryCatch())
5457
5558
"""
59+
60+
return script.replacingOccurrences(of: tab, with: " ")
5661
}
5762

5863
private func renderEnumIOError() -> String { """
5964
enum IOError: Error {
6065
61-
case invalidArgument
62-
case overwriteError
63-
case commitPrefixError
64-
65-
var message: String {
66-
switch self {
67-
case .invalidArgument:
68-
return "Intended to recieve .git/COMMIT_EDITMSG arg"
69-
case .overwriteError:
70-
return "There was an error writting to the commit message"
71-
case .commitPrefixError:
72-
return \"\"\"
66+
\(tab)case invalidArgument
67+
\(tab)case overwriteError
68+
\(tab)case commitPrefixError
69+
70+
\(tab)var message: String {
71+
\(tab + tab)switch self {
72+
\(tab + tab)case .invalidArgument:
73+
\(tab + tab + tab)return "Intended to recieve .git/COMMIT_EDITMSG arg"
74+
\(tab + tab)case .overwriteError:
75+
\(tab + tab + tab)return "There was an error writting to the commit message"
76+
\(tab + tab)case .commitPrefixError:
77+
\(tab + tab + tab)return \"\"\"
7378
74-
- CommitPrefix Error
75-
\"\"\"
76-
}
77-
}
79+
\(tab + tab + tab)- CommitPrefix Error
80+
\(tab + tab + tab)\"\"\"
81+
\(tab + tab)}
82+
\(tab)}
7883
7984
}
8085
"""
@@ -83,102 +88,102 @@ struct CommitMessageHookContents {
8388
private func renderStructIOCommitPrefix() -> String { """
8489
struct IOCommitPrefix {
8590
86-
let commitMsgPath: String
91+
\(tab)let commitMsgPath: String
8792
88-
init(filePath: [String] = Array(CommandLine.arguments.dropFirst())) throws {
89-
guard let firstArg = filePath.first else { throw IOError.invalidArgument }
90-
self.commitMsgPath = firstArg
91-
}
93+
\(tab)init(filePath: [String] = Array(CommandLine.arguments.dropFirst())) throws {
94+
\(tab + tab)guard let firstArg = filePath.first else { throw IOError.invalidArgument }
95+
\(tab + tab)self.commitMsgPath = firstArg
96+
\(tab)}
9297
93-
\(renderIOCPMethodGetPrefixes())
98+
\(renderIOCPMethodGetPrefixes())
9499
95-
\(renderIOCPMethodGetCommitMessage())
100+
\(renderIOCPMethodGetCommitMessage())
101+
102+
\(renderIOCPMethodOverwriteContents())
96103
97-
\(renderIOCPMethodOverwriteContents())
98-
99104
}
100105
"""
101106
}
102107

103108
private func renderIOCPMethodGetPrefixes() -> String { """
104-
func getPrefixes() throws -> String {
105-
let readProcess = Process()
106-
readProcess.launchPath = "/usr/bin/env"
109+
\(tab)func getPrefixes() throws -> String {
110+
\(tab + tab)let readProcess = Process()
111+
\(tab + tab)readProcess.launchPath = "/usr/bin/env"
107112
108-
var readProcessEnv = ProcessInfo.processInfo.environment
109-
let paths = readProcessEnv["PATH"]
110-
paths.map { readProcessEnv["PATH"] = "/usr/local/bin:\\($0)" }
113+
\(tab + tab)var readProcessEnv = ProcessInfo.processInfo.environment
114+
\(tab + tab)let paths = readProcessEnv["PATH"]
115+
\(tab + tab)paths.map { readProcessEnv["PATH"] = "/usr/local/bin:\\($0)" }
111116
112-
readProcess.environment = readProcessEnv
113-
readProcess.arguments = ["commitPrefix", "-o"]
117+
\(tab + tab)readProcess.environment = readProcessEnv
118+
\(tab + tab)readProcess.arguments = ["commitPrefix", "-o"]
114119
115-
let pipe = Pipe()
116-
readProcess.standardOutput = pipe
117-
readProcess.launch()
120+
\(tab + tab)let pipe = Pipe()
121+
\(tab + tab)readProcess.standardOutput = pipe
122+
\(tab + tab)readProcess.launch()
118123
119-
readProcess.waitUntilExit()
124+
\(tab + tab)readProcess.waitUntilExit()
120125
121-
if readProcess.terminationStatus != 0 {
122-
throw IOError.commitPrefixError
123-
}
126+
\(tab + tab)if readProcess.terminationStatus != 0 {
127+
\(tab + tab + tab)throw IOError.commitPrefixError
128+
\(tab + tab)}
124129
125-
let data = pipe.fileHandleForReading.readDataToEndOfFile()
126-
let contents = String(data: data, encoding: .utf8)
130+
\(tab + tab)let data = pipe.fileHandleForReading.readDataToEndOfFile()
131+
\(tab + tab)let contents = String(data: data, encoding: .utf8)
127132
128-
return contents ?? ""
129-
}
133+
\(tab + tab)return contents ?? ""
134+
\(tab)}
130135
"""
131136
}
132137

133138
private func renderIOCPMethodGetCommitMessage() -> String { """
134-
func getCommitMessage() -> String {
135-
let readProcess = Process()
136-
readProcess.launchPath = "/usr/bin/env"
137-
readProcess.arguments = ["cat", commitMsgPath]
139+
\(tab)func getCommitMessage() -> String {
140+
\(tab + tab)let readProcess = Process()
141+
\(tab + tab)readProcess.launchPath = "/usr/bin/env"
142+
\(tab + tab)readProcess.arguments = ["cat", commitMsgPath]
138143
139-
let pipe = Pipe()
140-
readProcess.standardOutput = pipe
141-
readProcess.launch()
144+
\(tab + tab)let pipe = Pipe()
145+
\(tab + tab)readProcess.standardOutput = pipe
146+
\(tab + tab)readProcess.launch()
142147
143-
readProcess.waitUntilExit()
148+
\(tab + tab)readProcess.waitUntilExit()
144149
145-
let data = pipe.fileHandleForReading.readDataToEndOfFile()
146-
let contents = String(data: data, encoding: .utf8)
150+
\(tab + tab)let data = pipe.fileHandleForReading.readDataToEndOfFile()
151+
\(tab + tab)let contents = String(data: data, encoding: .utf8)
147152
148-
return contents ?? ""
149-
}
153+
\(tab + tab)return contents ?? ""
154+
\(tab)}
150155
"""
151156
}
152157

153158
private func renderIOCPMethodOverwriteContents() -> String { """
154-
func overwriteContents(with contents: String) throws {
155-
do {
156-
try contents.write(toFile: commitMsgPath, atomically: true, encoding: .utf8)
157-
} catch {
158-
throw IOError.overwriteError
159-
}
160-
}
159+
\(tab)func overwriteContents(with contents: String) throws {
160+
\(tab + tab)do {
161+
\(tab + tab + tab)try contents.write(toFile: commitMsgPath, atomically: true, encoding: .utf8)
162+
\(tab + tab)} catch {
163+
\(tab + tab + tab)throw IOError.overwriteError
164+
\(tab + tab)}
165+
\(tab)}
161166
"""
162167
}
163168

164169
private func renderMainDoTryCatch() -> String { """
165170
do {
166171
167-
let ioCommitPrefix = try IOCommitPrefix()
172+
\(tab)let ioCommitPrefix = try IOCommitPrefix()
168173
169-
let prefixes = try ioCommitPrefix.getPrefixes()
170-
.trimmingCharacters(in: .newlines)
174+
\(tab)let prefixes = try ioCommitPrefix.getPrefixes()
175+
\(tab + tab).trimmingCharacters(in: .newlines)
171176
172-
let commitMessage = ioCommitPrefix.getCommitMessage()
173-
.trimmingCharacters(in: .newlines)
177+
\(tab)let commitMessage = ioCommitPrefix.getCommitMessage()
178+
\(tab + tab).trimmingCharacters(in: .newlines)
174179
175-
let newCommitMessage = [prefixes, commitMessage].joined(separator: " ")
176-
try ioCommitPrefix.overwriteContents(with: newCommitMessage)
180+
\(tab)let newCommitMessage = [prefixes, commitMessage].joined(separator: " ")
181+
\(tab)try ioCommitPrefix.overwriteContents(with: newCommitMessage)
177182
178183
} catch let ioError as IOError {
179184
180-
print(ioError.message)
181-
exit(1)
185+
\(tab)print(ioError.message)
186+
\(tab)exit(1)
182187
183188
}
184189
"""

0 commit comments

Comments
 (0)