Skip to content

Commit

Permalink
Don't load if no shadcn, error notif on config parse
Browse files Browse the repository at this point in the history
  • Loading branch information
WarningImHack3r committed Jan 8, 2024
1 parent d498c59 commit 119cb79
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ abstract class Source<C : Config>(val project: Project, private val serializer:
// Utility methods
protected fun getLocalConfig(): C {
return FileManager(project).getFileContentsAtPath("components.json")?.let {
Json.decodeFromString(serializer, it)
try {
Json.decodeFromString(serializer, it)
} catch (e: Exception) {
throw UnparseableConfigException(project, "Unable to parse components.json", e)
}
} ?: throw NoSuchFileException("components.json not found")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.warningimhack3r.intellijshadcnplugin.backend.sources

import com.github.warningimhack3r.intellijshadcnplugin.notifications.NotificationManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.project.Project

class UnparseableConfigException(
project: Project,
message: String? = null,
cause: Throwable? = null
) : Exception(message, cause) {
init {
NotificationManager(project).sendNotification(
"Unparseable configuration file",
"Your <code>components.json</code> file could not be parsed.<br />Please check that it is a valid JSON and that it contains the correct fields.",
NotificationType.ERROR
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.warningimhack3r.intellijshadcnplugin.ui

import com.github.warningimhack3r.intellijshadcnplugin.backend.SourceScanner
import com.github.warningimhack3r.intellijshadcnplugin.backend.helpers.FileManager
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runReadAction
Expand All @@ -15,7 +16,7 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.future.asCompletableFuture
import javax.swing.SwingConstants

class ISPToolWindow: ToolWindowFactory {
class ISPToolWindow : ToolWindowFactory {
override fun init(toolWindow: ToolWindow) {
ApplicationManager.getApplication().invokeLater {
toolWindow.setIcon(ISPIcons.logo)
Expand All @@ -27,11 +28,15 @@ class ISPToolWindow: ToolWindowFactory {
with(toolWindow.contentManager) {
addContent(factory.createContent(SimpleToolWindowPanel(true).apply {
GlobalScope.async {
return@async runReadAction {
FileManager(project).getVirtualFilesByName("package.json").size
}
}.asCompletableFuture().thenApplyAsync { count ->
if (count > 1) {
return@async Pair(runReadAction {
SourceScanner.findShadcnImplementation(project)
} != null, runReadAction {
FileManager(project).getVirtualFilesByName("package.json")
}.size)
}.asCompletableFuture().thenApplyAsync { (hasShadcn, count) ->
if (!hasShadcn) {
add(JBLabel("No shadcn/ui implementation detected.", SwingConstants.CENTER))
} else if (count > 1) {
add(JBLabel("Multiple projects detected, not supported yet.", SwingConstants.CENTER))
} else {
add(ISPWindowContents(project).panel())
Expand Down

0 comments on commit 119cb79

Please sign in to comment.