Skip to content

Commit

Permalink
feat: minor quality of life improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonvermeulen committed Oct 18, 2024
1 parent 25b5bae commit 6644d52
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class DbtCommandExecutorService(private var project: Project) {
var output: Pair<Int, String> = Pair(-1, "")
showLoadingIndicator("Executing dbt ${command.joinToString(" ")}...") {
try {
if (!venvInitializerService.isInitialized) {
venvInitializerService.initializeEnvironment()
}
venvInitializerService.initializeEnvironment()
val process = processExecutorService.executeCommand(command)
output = outputHandler.handleOutput(process)
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class DbtToolkitSettingsService : PersistentStateComponent<DbtToolkitSettingsSer
var dbtTestPaths: List<String> = listOf(),
var dbtMacroPaths: List<String> = listOf(),
var dbtProjectName: String = "",
var dbtVersion: Pair<Int, Int>? = null,
)

private var state = State()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,29 @@ class ManifestService(project: Project) {
private var manifest: JsonObject? = null
private val manifestLock = ReentrantLock()

private fun extractMajorMinor(version: String): Pair<Int, Int>? {
val regex = """(\d+)\.(\d+)""".toRegex()

val matchResult = regex.find(version)

return matchResult?.let {
val (major, minor) = it.destructured
try {
Pair(major.toInt(), minor.toInt())
} catch (e: NumberFormatException) {
null
}
}
}

@Synchronized
private fun parseManifest() {
dbtCommandExecutorService.executeCommand(listOf("parse"))
val file = File("${settingsService.state.settingsDbtTargetDir}/manifest.json")
if (file.exists()) {
manifest = JsonParser.parseString(file.readText()).asJsonObject
val dbtVersionString = manifest!!.getAsJsonObject("metadata").get("dbt_version").asString
settingsService.state.dbtVersion = extractMajorMinor(dbtVersionString)
} else {
println("Manifest file does not exist")
println("looked in the following path: ${settingsService.state.settingsDbtTargetDir}/manifest.json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ProcessExecutorService(project: Project) {
"An error occurred, could not find dbt executable. Please install dbt globally or configure a virtual environment.\n",
ConsoleViewContentType.LOG_ERROR_OUTPUT,
)
loggingService.log(e.message!!, ConsoleViewContentType.LOG_ERROR_OUTPUT)
loggingService.log("${e.message!!}\n", ConsoleViewContentType.LOG_ERROR_OUTPUT)
} else {
throw e
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import java.nio.file.Path
class VenvInitializerService(private var project: Project) {
private val loggingService = project.service<LoggingService>()
private var venvDbtExecutablePath: String? = null
var isInitialized = false

private fun getDbtPath(venvPath: Path): Path {
return if (SystemUtils.IS_OS_WINDOWS) {
Expand All @@ -32,19 +31,16 @@ class VenvInitializerService(private var project: Project) {
"Python virtual environment not detected. Attempting to use a global dbt installation.\n\n",
ConsoleViewContentType.ERROR_OUTPUT,
)
isInitialized = true
return
}

loggingService.log("Detected Python virtual environment at: $venvExecutablePath\n", ConsoleViewContentType.NORMAL_OUTPUT)
val dbtPath = getDbtPath(venvExecutablePath.parent)

if (!Files.exists(dbtPath)) {
loggingService.log(
"dbt installation not found within the virtual environment. Please install dbt and restart your IDE.\n\n",
ConsoleViewContentType.ERROR_OUTPUT,
)
isInitialized = true
return
}

Expand All @@ -53,7 +49,6 @@ class VenvInitializerService(private var project: Project) {
"Located dbt installation within the virtual environment at: $venvDbtExecutablePath\n\n",
ConsoleViewContentType.NORMAL_OUTPUT,
)
isInitialized = true
}

private fun getPythonVenvExecutablePath(project: Project): Path? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ class CompiledSqlPanel(project: Project) : IdeaPanel, Disposable, ActiveFileList
}
}
}
dbtCommandExecutorService.executeCommand(
listOf("compile", "--no-populate-cache", "--select", activeFile!!.nameWithoutExtension),
)
val command = if (settings.state.dbtVersion != null && settings.state.dbtVersion!!.first <= 1 && settings.state.dbtVersion!!.second < 5) {
// --no-populate-cache is only available in dbt >= 1.5
listOf("compile", "--select", activeFile!!.nameWithoutExtension)
} else {
listOf("compile", "--no-populate-cache", "--select", activeFile!!.nameWithoutExtension)
}
dbtCommandExecutorService.executeCommand(command)
}
} finally {
activeFileChanged(activeFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ramonvermeulen.dbtToolkit.ui.panels
import com.github.ramonvermeulen.dbtToolkit.services.ActiveFileListener
import com.github.ramonvermeulen.dbtToolkit.services.ActiveFileService
import com.github.ramonvermeulen.dbtToolkit.services.DbtCommandExecutorService
import com.github.ramonvermeulen.dbtToolkit.services.DbtToolkitSettingsService
import com.github.ramonvermeulen.dbtToolkit.ui.IdeaPanel
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
Expand All @@ -20,6 +21,7 @@ import javax.swing.JPanel
import javax.swing.SwingUtilities

class PreviewDataPanel(project: Project) : IdeaPanel, Disposable, ActiveFileListener {
private val settings = project.service<DbtToolkitSettingsService>()
private val dbtCommandExecutorService = project.service<DbtCommandExecutorService>()
private val mainPanel = JPanel(BorderLayout())
private val previewDataButton = JButton("Preview Data")
Expand Down Expand Up @@ -82,10 +84,13 @@ class PreviewDataPanel(project: Project) : IdeaPanel, Disposable, ActiveFileList

private fun previewData() {
ApplicationManager.getApplication().executeOnPooledThread {
val output =
dbtCommandExecutorService.executeCommand(
listOf("show", "--no-populate-cache", "--select", activeFile!!.nameWithoutExtension, "--limit", "10"),
)
val command = if (settings.state.dbtVersion != null && settings.state.dbtVersion!!.first <= 1 && settings.state.dbtVersion!!.second < 5) {
// --no-populate-cache is only available in dbt >= 1.5
listOf("show", "--select", activeFile!!.nameWithoutExtension, "--limit", "10")
} else {
listOf("show", "--no-populate-cache", "--select", activeFile!!.nameWithoutExtension, "--limit", "10")
}
val output = dbtCommandExecutorService.executeCommand(command)
if (output.first == 0) {
val data = output.second.split("\n").takeLast(16).joinToString("\n").trimEnd()
SwingUtilities.invokeLater {
Expand All @@ -99,7 +104,7 @@ class PreviewDataPanel(project: Project) : IdeaPanel, Disposable, ActiveFileList
SwingUtilities.invokeLater {
ApplicationManager.getApplication().runWriteAction {
document.setText(
"Error occured during execution of \"dbt show\" command. " +
"Error occurred during execution of \"dbt show\" command. " +
"Please check the logs in the console tab.",
)
previewDataButton.isEnabled = true
Expand Down

0 comments on commit 6644d52

Please sign in to comment.