70 lines
2.1 KiB
Swift
Executable File
70 lines
2.1 KiB
Swift
Executable File
#!/usr/bin/env swift
|
|
|
|
import Foundation
|
|
|
|
final class ReleaseNotesCreator {
|
|
|
|
func gitLogs(count: Int) -> [String] {
|
|
|
|
let gitProcess = Process()
|
|
gitProcess.executableURL = URL(fileURLWithPath: "/usr/bin/git")
|
|
gitProcess.arguments = ["log", "-\(count)",
|
|
"--pretty=format:\"%h (%cn) %s\""]
|
|
|
|
let gitPipe = Pipe()
|
|
gitProcess.standardOutput = gitPipe
|
|
|
|
do {
|
|
try gitProcess.run()
|
|
gitProcess.waitUntilExit()
|
|
|
|
let data = gitPipe.fileHandleForReading.readDataToEndOfFile()
|
|
if let output = String(data: data, encoding: String.Encoding.utf8) { return output.components(separatedBy: "\n") }
|
|
} catch { }
|
|
|
|
return []
|
|
}
|
|
|
|
func write(notes: [String], to filePath: String, anchor: String) {
|
|
|
|
let data = try! Data(contentsOf: URL(string: "file://\(filePath)")!)
|
|
let content = String(decoding: data, as: UTF8.self)
|
|
var lines = content.components(separatedBy: "\n")
|
|
|
|
guard let index = lines.firstIndex(of: anchor) else { return }
|
|
|
|
var result = [String]()
|
|
result.append("<h3>GitLab updates</h3>")
|
|
result.append("<h2>Commits</h2>")
|
|
notes.forEach { result.append("<li>\($0.replacingOccurrences(of: "\"", with: ""))</li>") }
|
|
result.append("<ul>")
|
|
result.append("</ul>")
|
|
|
|
lines.insert(contentsOf: result, at: index + 1)
|
|
|
|
let resultString = lines.joined(separator: "\n")
|
|
let resultData = resultString.data(using: .utf8)!
|
|
|
|
try? resultData.write(to: URL(string: "file://\(filePath)")!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func showHelp() {
|
|
print("How to use:")
|
|
print("--path=[PATH TO RELEASE NOTES]")
|
|
}
|
|
|
|
guard let argument = CommandLine.arguments.first(where: { $0.contains("path") })
|
|
, let path = argument.split(separator: "=").last else {
|
|
showHelp()
|
|
exit(1)
|
|
}
|
|
|
|
let rc = ReleaseNotesCreator()
|
|
let logs = rc.gitLogs(count: 10)
|
|
rc.write(notes: logs, to: String(path), anchor: "<div id=\"release\">")
|
|
|
|
exit(0)
|