Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/commitPrefix.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "CommitPrefixTests"
BuildableName = "CommitPrefixTests"
BlueprintName = "CommitPrefixTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
12 changes: 7 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import PackageDescription
let package = Package(
name: "commitPrefix",
dependencies: [
// Dependencies declare other packages that this package depends on.
// 📁 John Sundell's Files Package is great for easy file reading/writing/moving/etc.
.package(url: "https://github.com/JohnSundell/Files", from: "4.0.0"),
// 🧰 SPMUtilities for CLI Argument Parsing.
.package(url: "https://github.com/apple/swift-package-manager", from: "0.5.0")
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "commitPrefix",
dependencies: ["Files", "SPMUtility"]),
dependencies: ["Files", "SPMUtility"],
// Normally don't have to specify the path, but I wan't the actual executable to be
// lowercase and SPM brings folders in Uppercased by default.
path: "Sources/CommitPrefix"),
.testTarget(
name: "commitPrefixTests",
name: "CommitPrefixTests",
dependencies: ["commitPrefix"]),
]
)
16 changes: 8 additions & 8 deletions Sources/CommitPrefix/CPFileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import Foundation
import Files

public struct CPFileHandler {
struct CPFileHandler {

private let cpInteractor: CPInteractor

public init() throws {
init() throws {
guard Folder.current.containsSubfolder(named: FolderName.git) else {
throw CPError.notAGitRepo(currentLocation: Folder.current.path)
}
Expand All @@ -40,11 +40,11 @@ public struct CPFileHandler {
try CommitMessageHook.findOrCreate(with: gitDirectory)
}

public func outputPrefixes() throws -> String {
func outputPrefixes() throws -> String {
try cpInteractor.outputPrefixes()
}

public func viewState() throws -> String {
func viewState() throws -> String {
let cpState = try cpInteractor.getCommitPrefixState()
switch cpState.mode {
case .normal:
Expand All @@ -61,19 +61,19 @@ public struct CPFileHandler {
}
}

public func deletePrefixes() throws -> String {
func deletePrefixes() throws -> String {
try cpInteractor.deletePrefixes()
}

public func writeNew(prefixes rawValue: String) throws -> String {
func writeNew(prefixes rawValue: String) throws -> String {
try cpInteractor.writeNew(prefixes: rawValue)
}

public func activateBranchMode(with validator: String) throws -> String {
func activateBranchMode(with validator: String) throws -> String {
try cpInteractor.activateBranchMode(with: validator)
}

public func activateNormalMode() throws -> String {
func activateNormalMode() throws -> String {
try cpInteractor.activateNormalMode()
}

Expand Down
26 changes: 13 additions & 13 deletions Sources/CommitPrefix/CPInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,30 @@ import Files
struct CPInteractor {

private let commitPrefixFile: File
private let commitPrefixModel: CommitPrefixModel
private let commitPrefixModel: CPModel

init (gitDirectory: Folder) throws {
let (commitPrefixFile, commitPrefixModel) = try Self.build(using: gitDirectory)
self.commitPrefixFile = commitPrefixFile
self.commitPrefixModel = commitPrefixModel
}

private static func build(using gitDirectory: Folder) throws -> (File, CommitPrefixModel) {
private static func build(using gitDirectory: Folder) throws -> (File, CPModel) {
do {
let initialModelData = try JSONEncoder().encode(CommitPrefixModel.empty())
let initialModelData = try JSONEncoder().encode(CPModel.empty())
let cpFile = try gitDirectory.createFileIfNeeded(
withName: FileName.commitPrefix,
contents: initialModelData)
let cpFileData = try cpFile.read()
let cpModel = try JSONDecoder().decode(CommitPrefixModel.self, from: cpFileData)
let cpModel = try JSONDecoder().decode(CPModel.self, from: cpFileData)
return (cpFile, cpModel)
} catch {
cpDebugPrint(error)
throw CPError.fileReadWriteError
}
}

private func saveCommitPrefix(model: CommitPrefixModel) throws {
private func saveCommitPrefix(model: CPModel) throws {
do {
let jsonEncoder = JSONEncoder()
let modelData = try jsonEncoder.encode(model)
Expand Down Expand Up @@ -99,7 +99,7 @@ struct CPInteractor {
return validator
}

public func outputPrefixes() throws -> String {
func outputPrefixes() throws -> String {
switch commitPrefixModel.prefixMode {
case .normal:
return commitPrefixModel.prefixes.joined()
Expand All @@ -111,10 +111,10 @@ struct CPInteractor {
}
}

public func getCommitPrefixState() throws -> CommitPrefixState {
func getCommitPrefixState() throws -> CPState {
switch commitPrefixModel.prefixMode {
case .normal:
return CommitPrefixState(
return CPState(
mode: .normal,
branchPrefixes: [],
normalPrefixes: commitPrefixModel.prefixes
Expand All @@ -123,35 +123,35 @@ struct CPInteractor {
let retrievedBranchPrefixes = try branchPrefixes()
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }
let normalPrefixes = commitPrefixModel.prefixes
return CommitPrefixState(
return CPState(
mode: .branchParse,
branchPrefixes: branchPrefixes,
normalPrefixes: normalPrefixes
)
}
}

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

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

public func activateBranchMode(with validator: String) throws -> String {
func activateBranchMode(with validator: String) throws -> String {
let formattedValidator = try validatorFormatter(validator)
let newModel = commitPrefixModel.updatedAsBranchMode(with: formattedValidator)
try saveCommitPrefix(model: newModel)
return "CommitPrefix MODE BRANCH_PARSE \(formattedValidator)"
}

public func activateNormalMode() throws -> String {
func activateNormalMode() throws -> String {
switch commitPrefixModel.prefixMode {
case .normal:
return "CommitPrefix already in MODE NORMAL"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// CommitPrefixModel.swift
// CPModel.swift
// commitPrefix
//
// MIT License
Expand All @@ -26,14 +26,18 @@

import Foundation

public enum PrefixMode: Int {

enum PrefixMode: Int {
case normal
case branchParse

}

public struct CommitPrefixModel: Codable {
struct CPState {
let mode: PrefixMode
let branchPrefixes: [String]
let normalPrefixes: [String]
}

struct CPModel: Codable {

let prefixMode: PrefixMode
let branchValidator: String?
Expand All @@ -58,57 +62,51 @@ public struct CommitPrefixModel: Codable {
self.prefixes = prefixes
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let prefixModeRawValue = try values.decode(Int.self, forKey: .prefixMode)
self.prefixMode = PrefixMode(rawValue: prefixModeRawValue) ?? PrefixMode.normal
self.branchValidator = try values.decodeIfPresent(String.self, forKey: .branchValidator)
self.prefixes = try values.decode([String].self, forKey: .prefixes)
}

public func encode(to encoder: Encoder) throws {
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(prefixMode.rawValue, forKey: .prefixMode)
try container.encodeIfPresent(branchValidator, forKey: .branchValidator)
try container.encode(prefixes, forKey: .prefixes)
}

static public func empty() -> CommitPrefixModel {
return CommitPrefixModel(
static func empty() -> CPModel {
return CPModel(
prefixMode: .normal,
branchValidator: nil,
prefixes: []
)
}

public func updated(with newPrefixes: [String]) -> CommitPrefixModel {
return CommitPrefixModel(
func updated(with newPrefixes: [String]) -> CPModel {
return CPModel(
prefixMode: prefixMode,
branchValidator: branchValidator,
prefixes: newPrefixes
)
}

public func updatedAsBranchMode(with newBranchValidator: String) -> CommitPrefixModel {
return CommitPrefixModel(
func updatedAsBranchMode(with newBranchValidator: String) -> CPModel {
return CPModel(
prefixMode: .branchParse,
branchValidator: newBranchValidator,
prefixes: prefixes
)
}

public func updatedAsNormalMode() -> CommitPrefixModel {
return CommitPrefixModel(
func updatedAsNormalMode() -> CPModel {
return CPModel(
prefixMode: .normal,
branchValidator: nil,
prefixes: prefixes
)
}

}

public struct CommitPrefixState {
let mode: PrefixMode
let branchPrefixes: [String]
let normalPrefixes: [String]
}
Loading