Add teamcity problems when generating files instead of exception

This commit is contained in:
Nikolay Krasko
2021-04-05 19:43:04 +03:00
committed by TeamCityServer
parent 3757bd0c1f
commit 14ead33db6
2 changed files with 27 additions and 7 deletions
@@ -20,9 +20,7 @@ fun removeExtraFilesFromPreviousGeneration(previouslyGeneratedFiles: List<File>,
for (file in previouslyGeneratedFiles) {
if (file.absolutePath !in generatedFilesPath) {
if (GeneratorsFileUtil.isTeamCityBuild) {
GeneratorsFileUtil.assertTeamCityMode()
}
if (GeneratorsFileUtil.failOnTeamCity("File delete `${file.absolutePath}`")) continue
println("Deleted: ${file.absolutePath}")
file.delete()
}
+26 -4
View File
@@ -20,7 +20,7 @@ object GeneratorsFileUtil {
fun writeFileIfContentChanged(file: File, newText: String, logNotChanged: Boolean = true) {
val parentFile = file.parentFile
if (!parentFile.exists()) {
if (isTeamCityBuild) assertTeamCityMode()
if (failOnTeamCity("Create dir `${parentFile.path}`")) return
if (parentFile.mkdirs()) {
println("Directory created: " + parentFile.absolutePath)
} else {
@@ -33,7 +33,7 @@ object GeneratorsFileUtil {
}
return
}
if (isTeamCityBuild) assertTeamCityMode()
if (failOnTeamCity("Write file `${file.toPath()}`")) return
val useTempFile = !SystemInfo.isWindows
val targetFile = file.toPath()
val tempFile =
@@ -47,8 +47,30 @@ object GeneratorsFileUtil {
println()
}
fun assertTeamCityMode(): Nothing {
throw IllegalStateException("You should commit all newly generated files before pushing them to TeamCity")
fun failOnTeamCity(message: String): Boolean {
if (!isTeamCityBuild) return false
fun String.escapeForTC(): String = StringBuilder(length).apply {
for (char in this@escapeForTC) {
append(
when (char) {
'|' -> "||"
'\'' -> "|'"
'\n' -> "|n"
'\r' -> "|r"
'[' -> "|["
']' -> "|]"
else -> char
}
)
}
}.toString()
val fullMessage = "[Re-generation needed!] $message\n" +
"Run correspondent (check the log above) Gradle task locally and commit changes."
println("##teamcity[buildProblem description='${fullMessage.escapeForTC()}']")
return true
}
fun isFileContentChangedIgnoringLineSeparators(file: File, content: String): Boolean {