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) { for (file in previouslyGeneratedFiles) {
if (file.absolutePath !in generatedFilesPath) { if (file.absolutePath !in generatedFilesPath) {
if (GeneratorsFileUtil.isTeamCityBuild) { if (GeneratorsFileUtil.failOnTeamCity("File delete `${file.absolutePath}`")) continue
GeneratorsFileUtil.assertTeamCityMode()
}
println("Deleted: ${file.absolutePath}") println("Deleted: ${file.absolutePath}")
file.delete() file.delete()
} }
+26 -4
View File
@@ -20,7 +20,7 @@ object GeneratorsFileUtil {
fun writeFileIfContentChanged(file: File, newText: String, logNotChanged: Boolean = true) { fun writeFileIfContentChanged(file: File, newText: String, logNotChanged: Boolean = true) {
val parentFile = file.parentFile val parentFile = file.parentFile
if (!parentFile.exists()) { if (!parentFile.exists()) {
if (isTeamCityBuild) assertTeamCityMode() if (failOnTeamCity("Create dir `${parentFile.path}`")) return
if (parentFile.mkdirs()) { if (parentFile.mkdirs()) {
println("Directory created: " + parentFile.absolutePath) println("Directory created: " + parentFile.absolutePath)
} else { } else {
@@ -33,7 +33,7 @@ object GeneratorsFileUtil {
} }
return return
} }
if (isTeamCityBuild) assertTeamCityMode() if (failOnTeamCity("Write file `${file.toPath()}`")) return
val useTempFile = !SystemInfo.isWindows val useTempFile = !SystemInfo.isWindows
val targetFile = file.toPath() val targetFile = file.toPath()
val tempFile = val tempFile =
@@ -47,8 +47,30 @@ object GeneratorsFileUtil {
println() println()
} }
fun assertTeamCityMode(): Nothing { fun failOnTeamCity(message: String): Boolean {
throw IllegalStateException("You should commit all newly generated files before pushing them to TeamCity") 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 { fun isFileContentChangedIgnoringLineSeparators(file: File, content: String): Boolean {