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
9 changes: 9 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ let package = Package(
// 📁 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")
.package(url: "https://github.com/apple/swift-package-manager", from: "0.5.0"),
// Consler for Styled outputs to the Console
.package(url: "https://github.com/enuance/consler", from: "0.2.0")
],
targets: [
.target(
name: "commitPrefix",
dependencies: ["Files", "SPMUtility"],
dependencies: ["Files", "SPMUtility", "Consler"],
// 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"),
Expand Down
31 changes: 16 additions & 15 deletions Sources/CommitPrefix/CPFileHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Consler
import Foundation
import Files

Expand All @@ -40,40 +41,40 @@ struct CPFileHandler {
try CommitMessageHook.findOrCreate(with: gitDirectory)
}

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

func viewState() throws -> String {
func viewState() throws -> ConslerOutput {
let cpState = try cpInteractor.getCommitPrefixState()
switch cpState.mode {
case .normal:
return """
CommitPrefix MODE NORMAL
- prefixes: \(cpState.normalPrefixes.joined())
"""
return ConslerOutput(values: [
"CommitPrefix ", "MODE NORMAL",
"- prefixes: ", cpState.normalPrefixes.joined()])
.describedBy(.normal, .cyanEndsLine, .normal, .cyan)
case .branchParse:
return """
CommitPrefix MODE BRANCH_PARSE
- branch prefixes: \(cpState.branchPrefixes.joined())
- stored prefixes: \(cpState.normalPrefixes.joined())
"""
return ConslerOutput(values: [
"CommitPrefix ", "MODE BRANCH_PARSE",
"- branch prefixes: ", cpState.branchPrefixes.joined(),
"- stored prefixes: ", cpState.normalPrefixes.joined()])
.describedBy(.normal, .cyanEndsLine, .normal, .cyanEndsLine, .normal, .cyan)
}
}

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

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

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

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

Expand Down
42 changes: 23 additions & 19 deletions Sources/CommitPrefix/CPInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation
import Consler
import Files
import Foundation

struct CPInteractor {

Expand Down Expand Up @@ -53,7 +54,7 @@ struct CPInteractor {
return (cpFile, cpModel, headFile)
} catch {
cpDebugPrint(error)
throw CPError.fileReadWriteError
throw CPError.cpFileIOError
}
}

Expand All @@ -64,24 +65,24 @@ struct CPInteractor {
try commitPrefixFile.write(modelData)
} catch {
cpDebugPrint(error)
throw CPError.fileReadWriteError
throw CPError.cpFileIOError
}
}

private func branchPrefixes() throws -> [String] {
guard let regexValue = commitPrefixModel.regexValue else {
throw CPTermination.branchValidatorNotPresent
throw CPError.branchValidatorNotFound
}

guard let branch = try? gitHEADFile.readAsString(encodedAs: .utf8) else {
throw CPTermination.unableToReadHEAD
throw CPError.headFileIOError
}

let matches = branch.occurances(ofRegex: regexValue)

guard matches.count > 0 else {
let validator = commitPrefixModel.branchValidator ?? "Validator Not Present"
throw CPTermination.invalidBranchPrefix(validator: validator)
throw CPError.invalidBranchPrefix(validator: validator)
}

let uniqueMatches = Set(matches)
Expand All @@ -101,20 +102,20 @@ struct CPInteractor {
let containsNoNumbers = validator.occurances(ofRegex: #"(\d+)"#).isEmpty
let atLeastTwoCharacters = validator.count > 1
guard containsNoNumbers && atLeastTwoCharacters else {
throw CPError.branchValidatorFormatError
throw CPError.invalidBranchValidatorFormat
}
return validator
}

func outputPrefixes() throws -> String {
func outputPrefixes() throws -> ConslerOutput {
switch commitPrefixModel.prefixMode {
case .normal:
return commitPrefixModel.prefixes.joined()
return ConslerOutput(commitPrefixModel.prefixes.joined())
case .branchParse:
let retrievedBranchPrefixes = try branchPrefixes()
let branchPrefixes = retrievedBranchPrefixes.map { "[\($0)]" }.joined()
let normalPrefixes = commitPrefixModel.prefixes.joined()
return branchPrefixes + normalPrefixes
return ConslerOutput(branchPrefixes, normalPrefixes)
}
}

Expand All @@ -138,34 +139,37 @@ struct CPInteractor {
}
}

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

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

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

func activateNormalMode() throws -> String {
func activateNormalMode() throws -> ConslerOutput {
switch commitPrefixModel.prefixMode {
case .normal:
return "CommitPrefix already in MODE NORMAL"
return ConslerOutput("CommitPrefix ", "already in ", "MODE NORMAL")
.describedBy(.normal, .yellow, .cyan)
case .branchParse:
let newModel = commitPrefixModel.updatedAsNormalMode()
try saveCommitPrefix(model: newModel)
return "CommitPrefix MODE NORMAL"
return ConslerOutput("CommitPrefix ", "MODE NORMAL").describedBy(.normal, .cyan)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CommitPrefix/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Foundation

struct CPInfo {

static let version = "1.3.2"
static let version = "1.4.0"

}

Expand Down
Loading