/* * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.generators.util import com.intellij.openapi.util.SystemInfo import com.intellij.openapi.util.text.StringUtil import java.io.File import java.io.IOException import kotlin.io.path.* object GeneratorsFileUtil { val isTeamCityBuild: Boolean = System.getProperty("TEAMCITY_VERSION") != null @OptIn(ExperimentalPathApi::class) @JvmStatic @JvmOverloads @Throws(IOException::class) fun writeFileIfContentChanged(file: File, newText: String, logNotChanged: Boolean = true) { val parentFile = file.parentFile if (!parentFile.exists()) { if (isTeamCityBuild) assertTeamCityMode() if (parentFile.mkdirs()) { println("Directory created: " + parentFile.absolutePath) } else { throw IllegalStateException("Cannot create directory: $parentFile") } } if (!isFileContentChangedIgnoringLineSeparators(file, newText)) { if (logNotChanged) { println("Not changed: " + file.absolutePath) } return } if (isTeamCityBuild) assertTeamCityMode() val useTempFile = !SystemInfo.isWindows val targetFile = file.toPath() val tempFile = if (useTempFile) createTempDirectory(targetFile.name) / "${targetFile.name}.tmp" else targetFile tempFile.writeText(newText, Charsets.UTF_8) println("File written: ${tempFile.toAbsolutePath()}") if (useTempFile) { tempFile.moveTo(targetFile, overwrite = true) println("Renamed $tempFile to $targetFile") } println() } fun assertTeamCityMode(): Nothing { throw IllegalStateException("You should commit all newly generated files before pushing them to TeamCity") } fun isFileContentChangedIgnoringLineSeparators(file: File, content: String): Boolean { val currentContent: String = try { StringUtil.convertLineSeparators(file.readText(Charsets.UTF_8)) } catch (ignored: Throwable) { return true } return StringUtil.convertLineSeparators(content) != currentContent } }