Skip to content

Commit

Permalink
Merge pull request #103 from overpas/#101/icons-from-jar
Browse files Browse the repository at this point in the history
Allow to view icons from jar files
  • Loading branch information
overpas authored May 25, 2024
2 parents 321b09f + 09722c8 commit 101940e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package by.overpass.svgtocomposeintellij.preview

import by.overpass.svgtocomposeintellij.Bundle
import by.overpass.svgtocomposeintellij.preview.data.KotlinFileIconDataParser
import by.overpass.svgtocomposeintellij.preview.data.asInputStream
import by.overpass.svgtocomposeintellij.preview.data.imageVectorDeclarationPattern
import by.overpass.svgtocomposeintellij.preview.presentation.ComposeImageVectorPreviewViewModelImpl
import by.overpass.svgtocomposeintellij.preview.ui.ComposeImageVectorPreviewEditor
Expand Down Expand Up @@ -33,8 +34,7 @@ class ComposeImageVectorPreviewEditorProvider : FileEditorProvider, DumbAware {
viewModel = ComposeImageVectorPreviewViewModelImpl(
coroutineScope = coroutineScope,
iconDataParser = KotlinFileIconDataParser(
file.toNioPath()
.toFile(),
file.asInputStream(),
),
),
coroutineScope = coroutineScope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import androidx.compose.ui.graphics.vector.DefaultStrokeLineWidth
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.PathBuilder
import androidx.compose.ui.unit.dp
import java.io.File
import java.io.InputStream
import java.lang.reflect.Modifier
import java.lang.reflect.Proxy
import kotlinx.coroutines.Dispatchers
Expand All @@ -35,7 +35,7 @@ interface IconDataParser {

@Suppress("TooManyFunctions")
class KotlinFileIconDataParser(
private val file: File,
private val inputStream: InputStream,
) : IconDataParser {

private val builderPattern =
Expand Down Expand Up @@ -78,7 +78,7 @@ class KotlinFileIconDataParser(

override suspend fun parse(): Result<IconData> = withContext(Dispatchers.Default) {
runCatching<IconData> {
val text = file.readText()
val text = inputStream.reader().readText()
val name = getIconName(text)
val builderDefinition = getBuilderDefinition(text) ?: throw IllegalStateException("Failed to parse icon")
val builderParams = getNamedParams(builderDefinition.builderParams)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package by.overpass.svgtocomposeintellij.preview.data

import com.intellij.openapi.vfs.VirtualFile
import java.io.InputStream
import java.net.JarURLConnection
import java.net.URL
import kotlin.io.path.inputStream

fun VirtualFile.asInputStream(): InputStream {
return if (fileSystem.protocol == "jar") {
val url = URL("jar:file:$path")
val jarUrlConnection = url.openConnection() as JarURLConnection
jarUrlConnection.inputStream
} else {
toNioPath().inputStream()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class KotlinFileIconDataParserTest {

@Test
fun `Ab-testing icon is parsed`() = runTest {
val file = File("src/test/resources/kotlin/Ab-testing.kt")
val parser = KotlinFileIconDataParser(file)
val inputStream = File("src/test/resources/kotlin/Ab-testing.kt").inputStream()
val parser = KotlinFileIconDataParser(inputStream)

val actual = parser.parse()

Expand All @@ -18,8 +18,8 @@ class KotlinFileIconDataParserTest {

@Test
fun `Note icon is parsed`() = runTest {
val file = File("src/test/resources/kotlin/Note.kt")
val parser = KotlinFileIconDataParser(file)
val inputStream = File("src/test/resources/kotlin/Note.kt").inputStream()
val parser = KotlinFileIconDataParser(inputStream)

val actual = parser.parse()

Expand All @@ -28,8 +28,8 @@ class KotlinFileIconDataParserTest {

@Test
fun `Clear icon is NOT parsed`() = runTest {
val file = File("src/test/resources/kotlin/Clear.kt")
val parser = KotlinFileIconDataParser(file)
val inputStream = File("src/test/resources/kotlin/Clear.kt").inputStream()
val parser = KotlinFileIconDataParser(inputStream)

val actual = parser.parse()

Expand All @@ -38,8 +38,8 @@ class KotlinFileIconDataParserTest {

@Test
fun `__MyIconPack file is NOT parsed`() = runTest {
val file = File("src/test/resources/kotlin/Clear.kt")
val parser = KotlinFileIconDataParser(file)
val inputStream = File("src/test/resources/kotlin/Clear.kt").inputStream()
val parser = KotlinFileIconDataParser(inputStream)

val actual = parser.parse()

Expand Down

0 comments on commit 101940e

Please sign in to comment.