Skip to content

Commit 683d9d0

Browse files
committed
[CPBugFix-002][UpdatedDocs] Fixed the bug, updated some docs, and re-organized a bit.
1 parent b1969df commit 683d9d0

File tree

15 files changed

+490
-383
lines changed

15 files changed

+490
-383
lines changed

.swiftpm/xcode/xcshareddata/xcschemes/commitPrefix.xcscheme

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@
5252
ReferencedContainer = "container:">
5353
</BuildableReference>
5454
</TestableReference>
55+
<TestableReference
56+
skipped = "NO">
57+
<BuildableReference
58+
BuildableIdentifier = "primary"
59+
BlueprintIdentifier = "CommitPrefixTests"
60+
BuildableName = "CommitPrefixTests"
61+
BlueprintName = "CommitPrefixTests"
62+
ReferencedContainer = "container:">
63+
</BuildableReference>
64+
</TestableReference>
5565
</Testables>
5666
</TestAction>
5767
<LaunchAction

Package.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import PackageDescription
66
let package = Package(
77
name: "commitPrefix",
88
dependencies: [
9-
// Dependencies declare other packages that this package depends on.
9+
// 📁 John Sundell's Files Package is great for easy file reading/writing/moving/etc.
1010
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0"),
11+
// 🧰 SPMUtilities for CLI Argument Parsing.
1112
.package(url: "https://github.com/apple/swift-package-manager", from: "0.5.0")
1213
],
1314
targets: [
14-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
15-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
1615
.target(
1716
name: "commitPrefix",
18-
dependencies: ["Files", "SPMUtility"]),
17+
dependencies: ["Files", "SPMUtility"],
18+
// Normally don't have to specify the path, but I wan't the actual executable to be
19+
// lowercase and SPM brings folders in Uppercased by default.
20+
path: "Sources/CommitPrefix"),
1921
.testTarget(
20-
name: "commitPrefixTests",
22+
name: "CommitPrefixTests",
2123
dependencies: ["commitPrefix"]),
2224
]
2325
)

Sources/CommitPrefix/CPFileHandler.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import Foundation
2828
import Files
2929

30-
public struct CPFileHandler {
30+
struct CPFileHandler {
3131

3232
private let cpInteractor: CPInteractor
3333

34-
public init() throws {
34+
init() throws {
3535
guard Folder.current.containsSubfolder(named: FolderName.git) else {
3636
throw CPError.notAGitRepo(currentLocation: Folder.current.path)
3737
}
@@ -40,11 +40,11 @@ public struct CPFileHandler {
4040
try CommitMessageHook.findOrCreate(with: gitDirectory)
4141
}
4242

43-
public func outputPrefixes() throws -> String {
43+
func outputPrefixes() throws -> String {
4444
try cpInteractor.outputPrefixes()
4545
}
4646

47-
public func viewState() throws -> String {
47+
func viewState() throws -> String {
4848
let cpState = try cpInteractor.getCommitPrefixState()
4949
switch cpState.mode {
5050
case .normal:
@@ -61,19 +61,19 @@ public struct CPFileHandler {
6161
}
6262
}
6363

64-
public func deletePrefixes() throws -> String {
64+
func deletePrefixes() throws -> String {
6565
try cpInteractor.deletePrefixes()
6666
}
6767

68-
public func writeNew(prefixes rawValue: String) throws -> String {
68+
func writeNew(prefixes rawValue: String) throws -> String {
6969
try cpInteractor.writeNew(prefixes: rawValue)
7070
}
7171

72-
public func activateBranchMode(with validator: String) throws -> String {
72+
func activateBranchMode(with validator: String) throws -> String {
7373
try cpInteractor.activateBranchMode(with: validator)
7474
}
7575

76-
public func activateNormalMode() throws -> String {
76+
func activateNormalMode() throws -> String {
7777
try cpInteractor.activateNormalMode()
7878
}
7979

Sources/CommitPrefix/CPInteractor.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,30 @@ import Files
3030
struct CPInteractor {
3131

3232
private let commitPrefixFile: File
33-
private let commitPrefixModel: CommitPrefixModel
33+
private let commitPrefixModel: CPModel
3434

3535
init (gitDirectory: Folder) throws {
3636
let (commitPrefixFile, commitPrefixModel) = try Self.build(using: gitDirectory)
3737
self.commitPrefixFile = commitPrefixFile
3838
self.commitPrefixModel = commitPrefixModel
3939
}
4040

41-
private static func build(using gitDirectory: Folder) throws -> (File, CommitPrefixModel) {
41+
private static func build(using gitDirectory: Folder) throws -> (File, CPModel) {
4242
do {
43-
let initialModelData = try JSONEncoder().encode(CommitPrefixModel.empty())
43+
let initialModelData = try JSONEncoder().encode(CPModel.empty())
4444
let cpFile = try gitDirectory.createFileIfNeeded(
4545
withName: FileName.commitPrefix,
4646
contents: initialModelData)
4747
let cpFileData = try cpFile.read()
48-
let cpModel = try JSONDecoder().decode(CommitPrefixModel.self, from: cpFileData)
48+
let cpModel = try JSONDecoder().decode(CPModel.self, from: cpFileData)
4949
return (cpFile, cpModel)
5050
} catch {
5151
cpDebugPrint(error)
5252
throw CPError.fileReadWriteError
5353
}
5454
}
5555

56-
private func saveCommitPrefix(model: CommitPrefixModel) throws {
56+
private func saveCommitPrefix(model: CPModel) throws {
5757
do {
5858
let jsonEncoder = JSONEncoder()
5959
let modelData = try jsonEncoder.encode(model)
@@ -99,7 +99,7 @@ struct CPInteractor {
9999
return validator
100100
}
101101

102-
public func outputPrefixes() throws -> String {
102+
func outputPrefixes() throws -> String {
103103
switch commitPrefixModel.prefixMode {
104104
case .normal:
105105
return commitPrefixModel.prefixes.joined()
@@ -111,10 +111,10 @@ struct CPInteractor {
111111
}
112112
}
113113

114-
public func getCommitPrefixState() throws -> CommitPrefixState {
114+
func getCommitPrefixState() throws -> CPState {
115115
switch commitPrefixModel.prefixMode {
116116
case .normal:
117-
return CommitPrefixState(
117+
return CPState(
118118
mode: .normal,
119119
branchPrefixes: [],
120120
normalPrefixes: commitPrefixModel.prefixes
@@ -123,35 +123,35 @@ struct CPInteractor {
123123
let retrievedBranchPrefixes = try branchPrefixes()
124124
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }
125125
let normalPrefixes = commitPrefixModel.prefixes
126-
return CommitPrefixState(
126+
return CPState(
127127
mode: .branchParse,
128128
branchPrefixes: branchPrefixes,
129129
normalPrefixes: normalPrefixes
130130
)
131131
}
132132
}
133133

134-
public func deletePrefixes() throws -> String {
134+
func deletePrefixes() throws -> String {
135135
let newModel = commitPrefixModel.updated(with: [])
136136
try saveCommitPrefix(model: newModel)
137137
return "CommitPrefix DELETED"
138138
}
139139

140-
public func writeNew(prefixes rawValue: String) throws -> String {
140+
func writeNew(prefixes rawValue: String) throws -> String {
141141
let newPrefixes = prefixFormatter(rawValue)
142142
let newModel = commitPrefixModel.updated(with: newPrefixes)
143143
try saveCommitPrefix(model: newModel)
144144
return "CommitPrefix STORED \(newPrefixes.joined())"
145145
}
146146

147-
public func activateBranchMode(with validator: String) throws -> String {
147+
func activateBranchMode(with validator: String) throws -> String {
148148
let formattedValidator = try validatorFormatter(validator)
149149
let newModel = commitPrefixModel.updatedAsBranchMode(with: formattedValidator)
150150
try saveCommitPrefix(model: newModel)
151151
return "CommitPrefix MODE BRANCH_PARSE \(formattedValidator)"
152152
}
153153

154-
public func activateNormalMode() throws -> String {
154+
func activateNormalMode() throws -> String {
155155
switch commitPrefixModel.prefixMode {
156156
case .normal:
157157
return "CommitPrefix already in MODE NORMAL"

Sources/CommitPrefix/CommitPrefixModel.swift renamed to Sources/CommitPrefix/CPModel.swift

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// CommitPrefixModel.swift
2+
// CPModel.swift
33
// commitPrefix
44
//
55
// MIT License
@@ -26,14 +26,18 @@
2626

2727
import Foundation
2828

29-
public enum PrefixMode: Int {
30-
29+
enum PrefixMode: Int {
3130
case normal
3231
case branchParse
33-
3432
}
3533

36-
public struct CommitPrefixModel: Codable {
34+
struct CPState {
35+
let mode: PrefixMode
36+
let branchPrefixes: [String]
37+
let normalPrefixes: [String]
38+
}
39+
40+
struct CPModel: Codable {
3741

3842
let prefixMode: PrefixMode
3943
let branchValidator: String?
@@ -58,57 +62,51 @@ public struct CommitPrefixModel: Codable {
5862
self.prefixes = prefixes
5963
}
6064

61-
public init(from decoder: Decoder) throws {
65+
init(from decoder: Decoder) throws {
6266
let values = try decoder.container(keyedBy: CodingKeys.self)
6367
let prefixModeRawValue = try values.decode(Int.self, forKey: .prefixMode)
6468
self.prefixMode = PrefixMode(rawValue: prefixModeRawValue) ?? PrefixMode.normal
6569
self.branchValidator = try values.decodeIfPresent(String.self, forKey: .branchValidator)
6670
self.prefixes = try values.decode([String].self, forKey: .prefixes)
6771
}
6872

69-
public func encode(to encoder: Encoder) throws {
73+
func encode(to encoder: Encoder) throws {
7074
var container = encoder.container(keyedBy: CodingKeys.self)
7175
try container.encode(prefixMode.rawValue, forKey: .prefixMode)
7276
try container.encodeIfPresent(branchValidator, forKey: .branchValidator)
7377
try container.encode(prefixes, forKey: .prefixes)
7478
}
7579

76-
static public func empty() -> CommitPrefixModel {
77-
return CommitPrefixModel(
80+
static func empty() -> CPModel {
81+
return CPModel(
7882
prefixMode: .normal,
7983
branchValidator: nil,
8084
prefixes: []
8185
)
8286
}
8387

84-
public func updated(with newPrefixes: [String]) -> CommitPrefixModel {
85-
return CommitPrefixModel(
88+
func updated(with newPrefixes: [String]) -> CPModel {
89+
return CPModel(
8690
prefixMode: prefixMode,
8791
branchValidator: branchValidator,
8892
prefixes: newPrefixes
8993
)
9094
}
9195

92-
public func updatedAsBranchMode(with newBranchValidator: String) -> CommitPrefixModel {
93-
return CommitPrefixModel(
96+
func updatedAsBranchMode(with newBranchValidator: String) -> CPModel {
97+
return CPModel(
9498
prefixMode: .branchParse,
9599
branchValidator: newBranchValidator,
96100
prefixes: prefixes
97101
)
98102
}
99103

100-
public func updatedAsNormalMode() -> CommitPrefixModel {
101-
return CommitPrefixModel(
104+
func updatedAsNormalMode() -> CPModel {
105+
return CPModel(
102106
prefixMode: .normal,
103107
branchValidator: nil,
104108
prefixes: prefixes
105109
)
106110
}
107111

108112
}
109-
110-
public struct CommitPrefixState {
111-
let mode: PrefixMode
112-
let branchPrefixes: [String]
113-
let normalPrefixes: [String]
114-
}

0 commit comments

Comments
 (0)