Got rid of delegation in library impl hierarchy

This commit is contained in:
Alexander Gorshenev
2019-06-05 20:02:28 +03:00
committed by Igor Chevdar
parent 65a6b08fd3
commit 5faccaa49e
@@ -96,44 +96,45 @@ class FromZipIrLibraryImpl(zipped: IrLibraryLayoutImpl, zipFileSystem: FileSyste
* This class and its children automatically extracts pieces of the library on first access. Use it if you need * This class and its children automatically extracts pieces of the library on first access. Use it if you need
* to pass extracted files to an external tool. Otherwise, stick to [FromZipBaseLibraryImpl]. * to pass extracted files to an external tool. Otherwise, stick to [FromZipBaseLibraryImpl].
*/ */
open class KotlinLibraryExtractor(private val zipped: KotlinLibraryLayoutImpl) { fun KotlinLibraryLayoutImpl.extract(file: File): File = this.klib.withZipFileSystem { zipFileSystem ->
fun extract(file: File): File = zipped.klib.withZipFileSystem { zipFileSystem -> val temporary = org.jetbrains.kotlin.konan.file.createTempFile(file.name)
val temporary = org.jetbrains.kotlin.konan.file.createTempFile(file.name) zipFileSystem.file(file).copyTo(temporary)
zipFileSystem.file(file).copyTo(temporary) temporary.deleteOnExit()
temporary.deleteOnExit() temporary
temporary }
}
fun extractDir(directory: File): File = zipped.klib.withZipFileSystem { zipFileSystem -> fun KotlinLibraryLayoutImpl.extractDir(directory: File): File = this.klib.withZipFileSystem { zipFileSystem ->
val temporary = org.jetbrains.kotlin.konan.file.createTempDir(directory.name) val temporary = org.jetbrains.kotlin.konan.file.createTempDir(directory.name)
zipFileSystem.file(directory).recursiveCopyTo(temporary) zipFileSystem.file(directory).recursiveCopyTo(temporary)
temporary.deleteOnExitRecursively() temporary.deleteOnExitRecursively()
temporary temporary
} }
open class ExtractingKotlinLibraryLayout(zipped: KotlinLibraryLayoutImpl) : KotlinLibraryLayout {
override val libDir get() = error("Extracting layout doesn't extract its own root")
override val libraryName = zipped.libraryName
} }
open class ExtractingBaseLibraryImpl(zipped: KotlinLibraryLayoutImpl) : open class ExtractingBaseLibraryImpl(zipped: KotlinLibraryLayoutImpl) :
KotlinLibraryExtractor(zipped), ExtractingKotlinLibraryLayout(zipped) {
KotlinLibraryLayout by zipped { override val manifestFile: File by lazy { zipped.extract(zipped.manifestFile) }
override val resourcesDir: File by lazy { zipped.extractDir(zipped.resourcesDir) }
override val manifestFile: File by lazy { extract(zipped.manifestFile) }
override val resourcesDir: File by lazy { extractDir(zipped.resourcesDir) }
} }
class ExtractingMetadataLibraryImpl(zipped: MetadataLibraryLayoutImpl) : class ExtractingMetadataLibraryImpl(val zipped: MetadataLibraryLayoutImpl) :
KotlinLibraryExtractor(zipped), ExtractingKotlinLibraryLayout(zipped),
MetadataKotlinLibraryLayout by zipped { MetadataKotlinLibraryLayout {
override val metadataDir: File by lazy { extractDir(zipped.metadataDir) } override val metadataDir by lazy { zipped.extractDir(zipped.metadataDir) }
} }
class ExtractingIrLibraryImpl(zipped: IrLibraryLayoutImpl) : KotlinLibraryExtractor(zipped), class ExtractingIrLibraryImpl(val zipped: IrLibraryLayoutImpl) :
IrKotlinLibraryLayout by zipped { ExtractingKotlinLibraryLayout(zipped),
IrKotlinLibraryLayout {
override val irFile: File by lazy { extract(zipped.irFile) } override val irFile: File by lazy { zipped.extract(zipped.irFile) }
} }
internal fun zippedKotlinLibraryChecks(klibFile: File) { internal fun zippedKotlinLibraryChecks(klibFile: File) {
check(klibFile.exists) { "Could not find $klibFile." } check(klibFile.exists) { "Could not find $klibFile." }
check(klibFile.isFile) { "Expected $klibFile to be a regular file." } check(klibFile.isFile) { "Expected $klibFile to be a regular file." }