@@ -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