io.micronaut.annotation.processing.test.support.SourceFile.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-inject-kotlin-test Show documentation
Show all versions of micronaut-inject-kotlin-test Show documentation
Core components supporting the Micronaut Framework
The newest version!
/*
* Copyright 2017-2023 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.annotation.processing.test.support
import okio.buffer
import okio.sink
import org.intellij.lang.annotations.Language
import java.io.File
/**
* A source file for the [KotlinCompilation]
*/
abstract class SourceFile {
internal abstract fun writeIfNeeded(dir: File): File
companion object {
/**
* Create a new Java source file for the compilation when the compilation is run
*/
fun java(name: String, @Language("java") contents: String, trimIndent: Boolean = true): SourceFile {
require(File(name).hasJavaFileExtension())
val finalContents = if (trimIndent) contents.trimIndent() else contents
return new(name, finalContents)
}
/**
* Create a new Kotlin source file for the compilation when the compilation is run
*/
fun kotlin(name: String, @Language("kotlin") contents: String, trimIndent: Boolean = true): SourceFile {
require(File(name).hasKotlinFileExtension())
val finalContents = if (trimIndent) contents.trimIndent() else contents
return new(name, finalContents)
}
/**
* Create a new source file for the compilation when the compilation is run
*/
fun new(name: String, contents: String) = object : SourceFile() {
override fun writeIfNeeded(dir: File): File {
val file = dir.resolve(name)
file.createNewFile()
file.sink().buffer().use {
it.writeUtf8(contents)
}
return file
}
}
/**
* Compile an existing source file
*/
fun fromPath(path: File) = object : SourceFile() {
init {
require(path.isFile)
}
override fun writeIfNeeded(dir: File): File = path
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy