JS: KotlinJavascriptSerializationUtil, KotlinJavascriptSerializedResourcePaths: add some useful functions
This commit is contained in:
@@ -31,6 +31,7 @@ public object KotlinJavascriptMetadataUtils {
|
|||||||
|
|
||||||
public val JS_EXT: String = ".js"
|
public val JS_EXT: String = ".js"
|
||||||
public val META_JS_SUFFIX: String = ".meta.js"
|
public val META_JS_SUFFIX: String = ".meta.js"
|
||||||
|
public val VFS_PROTOCOL: String = "kotlin-js-meta"
|
||||||
private val KOTLIN_JAVASCRIPT_METHOD_NAME = "kotlin_module_metadata"
|
private val KOTLIN_JAVASCRIPT_METHOD_NAME = "kotlin_module_metadata"
|
||||||
private val KOTLIN_JAVASCRIPT_METHOD_NAME_PATTERN = "\\.kotlin_module_metadata\\(".toRegex()
|
private val KOTLIN_JAVASCRIPT_METHOD_NAME_PATTERN = "\\.kotlin_module_metadata\\(".toRegex()
|
||||||
/**
|
/**
|
||||||
|
|||||||
+18
-14
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.serialization.SerializationUtil
|
|||||||
import org.jetbrains.kotlin.serialization.StringTable
|
import org.jetbrains.kotlin.serialization.StringTable
|
||||||
import org.jetbrains.kotlin.serialization.deserialization.FlexibleTypeCapabilitiesDeserializer
|
import org.jetbrains.kotlin.serialization.deserialization.FlexibleTypeCapabilitiesDeserializer
|
||||||
import org.jetbrains.kotlin.storage.StorageManager
|
import org.jetbrains.kotlin.storage.StorageManager
|
||||||
|
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadata
|
||||||
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
|
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
@@ -70,11 +71,11 @@ public object KotlinJavascriptSerializationUtil {
|
|||||||
path ->
|
path ->
|
||||||
if (!contentMap.containsKey(path)) {
|
if (!contentMap.containsKey(path)) {
|
||||||
when {
|
when {
|
||||||
KotlinJavascriptSerializedResourcePaths.isPackageMetadataFile(path) ->
|
isPackageMetadataFile(path) ->
|
||||||
ByteArrayInputStream(PACKAGE_DEFAULT_BYTES)
|
ByteArrayInputStream(PACKAGE_DEFAULT_BYTES)
|
||||||
KotlinJavascriptSerializedResourcePaths.isStringTableFile(path) ->
|
isStringTableFile(path) ->
|
||||||
ByteArrayInputStream(STRING_TABLE_DEFAULT_BYTES)
|
ByteArrayInputStream(STRING_TABLE_DEFAULT_BYTES)
|
||||||
KotlinJavascriptSerializedResourcePaths.isClassesInPackageFile(path) ->
|
isClassesInPackageFile(path) ->
|
||||||
ByteArrayInputStream(CLASSES_IN_PACKAGE_DEFAULT_BYTES)
|
ByteArrayInputStream(CLASSES_IN_PACKAGE_DEFAULT_BYTES)
|
||||||
else ->
|
else ->
|
||||||
null
|
null
|
||||||
@@ -171,17 +172,6 @@ public object KotlinJavascriptSerializationUtil {
|
|||||||
return KotlinJavascriptSerializedResourcePaths.getClassMetadataPath(classDescriptor.classId)
|
return KotlinJavascriptSerializedResourcePaths.getClassMetadataPath(classDescriptor.classId)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ByteArray.toContentMap(): Map<String, ByteArray> {
|
|
||||||
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(this))
|
|
||||||
val content = JsProtoBuf.Library.parseFrom(gzipInputStream)
|
|
||||||
gzipInputStream.close()
|
|
||||||
|
|
||||||
val contentMap: MutableMap<String, ByteArray> = hashMapOf()
|
|
||||||
content.getEntryList().forEach { entry -> contentMap[entry.getPath()] = entry.getContent().toByteArray() }
|
|
||||||
|
|
||||||
return contentMap
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun ModuleDescriptor.toContentMap(): Map<String, ByteArray> {
|
private fun ModuleDescriptor.toContentMap(): Map<String, ByteArray> {
|
||||||
val contentMap = hashMapOf<String, ByteArray>()
|
val contentMap = hashMapOf<String, ByteArray>()
|
||||||
|
|
||||||
@@ -214,3 +204,17 @@ public object KotlinJavascriptSerializationUtil {
|
|||||||
private fun ModuleDescriptor.toBinaryMetadata(): ByteArray =
|
private fun ModuleDescriptor.toBinaryMetadata(): ByteArray =
|
||||||
KotlinJavascriptSerializationUtil.contentMapToByteArray(this.toContentMap())
|
KotlinJavascriptSerializationUtil.contentMapToByteArray(this.toContentMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun KotlinJavascriptMetadata.forEachFile(operation: (filePath: String, fileContent: ByteArray) -> Unit): Unit =
|
||||||
|
this.body.toContentMap().forEach { operation(it.getKey(), it.getValue()) }
|
||||||
|
|
||||||
|
private fun ByteArray.toContentMap(): Map<String, ByteArray> {
|
||||||
|
val gzipInputStream = GZIPInputStream(ByteArrayInputStream(this))
|
||||||
|
val content = JsProtoBuf.Library.parseFrom(gzipInputStream)
|
||||||
|
gzipInputStream.close()
|
||||||
|
|
||||||
|
val contentMap: MutableMap<String, ByteArray> = hashMapOf()
|
||||||
|
content.getEntryList().forEach { entry -> contentMap[entry.getPath()] = entry.getContent().toByteArray() }
|
||||||
|
|
||||||
|
return contentMap
|
||||||
|
}
|
||||||
+38
-32
@@ -23,48 +23,30 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||||
|
|
||||||
public object KotlinJavascriptSerializedResourcePaths : SerializedResourcePaths() {
|
public object KotlinJavascriptSerializedResourcePaths : SerializedResourcePaths() {
|
||||||
public override val EXTENSION_REGISTRY: ExtensionRegistryLite
|
public override val EXTENSION_REGISTRY: ExtensionRegistryLite = ExtensionRegistryLite.newInstance()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
EXTENSION_REGISTRY = ExtensionRegistryLite.newInstance()
|
|
||||||
JsProtoBuf.registerAllExtensions(EXTENSION_REGISTRY)
|
JsProtoBuf.registerAllExtensions(EXTENSION_REGISTRY)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val CLASSES_FILE_EXTENSION = "kotlin_classes"
|
private val CLASSES_FILE_EXTENSION = "kotlin_classes"
|
||||||
private val STRING_TABLE_FILE_EXTENSION = "kotlin_string_table"
|
private val STRING_TABLE_FILE_EXTENSION = "kotlin_string_table"
|
||||||
|
|
||||||
private val PACKAGE_CLASS_NAME_SUFFIX: String = "Package"
|
|
||||||
private val DEFAULT_PACKAGE_CLASS_NAME: String = "_Default" + PACKAGE_CLASS_NAME_SUFFIX
|
|
||||||
|
|
||||||
public fun getClassesInPackageFilePath(fqName: FqName): String =
|
public fun getClassesInPackageFilePath(fqName: FqName): String =
|
||||||
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + CLASSES_FILE_EXTENSION
|
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + CLASSES_FILE_EXTENSION
|
||||||
|
|
||||||
|
|
||||||
public override fun getClassMetadataPath(classId: ClassId): String {
|
override fun getClassMetadataPath(classId: ClassId): String {
|
||||||
return classId.getPackageFqName().toPath().withSepIfNotEmpty() + classId.getRelativeClassName().asString() +
|
return classId.getPackageFqName().toPath().withSepIfNotEmpty() + classId.getRelativeClassName().asString() +
|
||||||
"." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
|
"." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun getPackageFilePath(fqName: FqName): String =
|
override fun getPackageFilePath(fqName: FqName): String =
|
||||||
getPackageClassFqName(fqName).toPath() + "." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
|
getPackageClassFqName(fqName).toPath() + "." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
|
||||||
|
|
||||||
public override fun getStringTableFilePath(fqName: FqName): String =
|
override fun getStringTableFilePath(fqName: FqName): String =
|
||||||
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + STRING_TABLE_FILE_EXTENSION
|
fqName.toPath().withSepIfNotEmpty() + shortName(fqName) + "." + STRING_TABLE_FILE_EXTENSION
|
||||||
|
|
||||||
public fun isPackageMetadataFile(fileName: String): Boolean =
|
|
||||||
KotlinJavascriptSerializedResourcePaths.getPackageFilePath(getPackageFqName(fileName)) == fileName
|
|
||||||
|
|
||||||
public fun isStringTableFile(fileName: String): Boolean =
|
|
||||||
KotlinJavascriptSerializedResourcePaths.getStringTableFilePath(getPackageFqName(fileName)) == fileName
|
|
||||||
|
|
||||||
public fun isClassesInPackageFile(fileName: String): Boolean =
|
|
||||||
KotlinJavascriptSerializedResourcePaths.getClassesInPackageFilePath(getPackageFqName(fileName)) == fileName
|
|
||||||
|
|
||||||
private fun getPackageFqName(fileName: String): FqName = FqName(getPackageName(fileName))
|
|
||||||
|
|
||||||
private fun getPackageName(filePath: String): String =
|
|
||||||
if (filePath.indexOf('/') >= 0) filePath.substringBeforeLast('/').replace('/', '.') else ""
|
|
||||||
|
|
||||||
private fun FqName.toPath() = this.asString().replace('.', '/')
|
private fun FqName.toPath() = this.asString().replace('.', '/')
|
||||||
|
|
||||||
private fun String.withSepIfNotEmpty() = if (this.isEmpty()) this else this + "/"
|
private fun String.withSepIfNotEmpty() = if (this.isEmpty()) this else this + "/"
|
||||||
@@ -72,15 +54,39 @@ public object KotlinJavascriptSerializedResourcePaths : SerializedResourcePaths(
|
|||||||
private fun shortName(fqName: FqName): String =
|
private fun shortName(fqName: FqName): String =
|
||||||
if (fqName.isRoot()) "default-package" else fqName.shortName().asString()
|
if (fqName.isRoot()) "default-package" else fqName.shortName().asString()
|
||||||
|
|
||||||
private fun getPackageClassFqName(packageFQN: FqName): FqName {
|
}
|
||||||
return packageFQN.child(Name.identifier(getPackageClassName(packageFQN)))
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getPackageClassName(packageFQN: FqName): String {
|
private val PACKAGE_CLASS_NAME_SUFFIX: String = "Package"
|
||||||
return if (packageFQN.isRoot()) DEFAULT_PACKAGE_CLASS_NAME else capitalizeNonEmptyString(packageFQN.shortName().asString()) + PACKAGE_CLASS_NAME_SUFFIX
|
private val DEFAULT_PACKAGE_CLASS_NAME: String = "_Default" + PACKAGE_CLASS_NAME_SUFFIX
|
||||||
}
|
private val DEFAULT_PACKAGE_METAFILE_NAME: String = DEFAULT_PACKAGE_CLASS_NAME + "." + KotlinJavascriptSerializationUtil.CLASS_METADATA_FILE_EXTENSION
|
||||||
|
|
||||||
|
public fun FqName.isPackageClassFqName(): Boolean = !this.isRoot() && getPackageClassFqName(this.parent()) == this
|
||||||
|
|
||||||
|
public fun isDefaultPackageMetafile(fileName: String): Boolean = fileName == DEFAULT_PACKAGE_METAFILE_NAME
|
||||||
|
|
||||||
|
public fun isPackageMetadataFile(fileName: String): Boolean =
|
||||||
|
KotlinJavascriptSerializedResourcePaths.getPackageFilePath(getPackageFqName(fileName)) == fileName
|
||||||
|
|
||||||
|
public fun isStringTableFile(fileName: String): Boolean =
|
||||||
|
KotlinJavascriptSerializedResourcePaths.getStringTableFilePath(getPackageFqName(fileName)) == fileName
|
||||||
|
|
||||||
|
public fun isClassesInPackageFile(fileName: String): Boolean =
|
||||||
|
KotlinJavascriptSerializedResourcePaths.getClassesInPackageFilePath(getPackageFqName(fileName)) == fileName
|
||||||
|
|
||||||
|
private fun getPackageFqName(fileName: String): FqName = FqName(getPackageName(fileName))
|
||||||
|
|
||||||
|
private fun getPackageName(filePath: String): String =
|
||||||
|
if (filePath.indexOf('/') >= 0) filePath.substringBeforeLast('/').replace('/', '.') else ""
|
||||||
|
|
||||||
|
private fun getPackageClassFqName(packageFQN: FqName): FqName {
|
||||||
|
return packageFQN.child(Name.identifier(getPackageClassName(packageFQN)))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getPackageClassName(packageFQN: FqName): String {
|
||||||
|
return if (packageFQN.isRoot()) DEFAULT_PACKAGE_CLASS_NAME else capitalizeNonEmptyString(packageFQN.shortName().asString()) + PACKAGE_CLASS_NAME_SUFFIX
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun capitalizeNonEmptyString(s: String): String {
|
||||||
|
return if (Character.isUpperCase(s.charAt(0))) s else Character.toUpperCase(s.charAt(0)) + s.substring(1)
|
||||||
|
}
|
||||||
|
|
||||||
private fun capitalizeNonEmptyString(s: String): String {
|
|
||||||
return if (Character.isUpperCase(s.charAt(0))) s else Character.toUpperCase(s.charAt(0)) + s.substring(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user