Add basic file content transformations.
^KT-45744 In Progress
This commit is contained in:
committed by
TeamCityServer
parent
f5dfe7ce95
commit
3dcac00aec
+45
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 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.gradle.testbase
|
||||||
|
|
||||||
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modify file content under [Path].
|
||||||
|
*
|
||||||
|
* @param transform function receiving current file content and outputting new file content
|
||||||
|
*/
|
||||||
|
fun Path.modify(transform: (currentContent: String) -> String) {
|
||||||
|
assert(Files.isRegularFile(this)) { "$this is not a regular file!" }
|
||||||
|
|
||||||
|
val file = toFile()
|
||||||
|
file.writeText(transform(file.readText()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append [textToAppend] to the file content under [Path].
|
||||||
|
*/
|
||||||
|
fun Path.append(
|
||||||
|
textToAppend: String
|
||||||
|
) {
|
||||||
|
modify {
|
||||||
|
"""
|
||||||
|
$it
|
||||||
|
|
||||||
|
$textToAppend
|
||||||
|
""".trimIndent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append [textToAppend] to the current [TestProject] 'build.gradle' file.
|
||||||
|
*/
|
||||||
|
fun TestProject.appendToBuildFile(
|
||||||
|
textToAppend: String
|
||||||
|
) {
|
||||||
|
projectPath.resolve("build.gradle").append(textToAppend)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user