-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#147: Implement connection auto-completion from
appsettings.json
, u…
…ser secrets and SQLite data sources
- Loading branch information
Showing
20 changed files
with
392 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...lin/me/seclerp/rider/plugins/efcore/features/connections/AppSettingsConnectionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.util.io.isFile | ||
import com.jetbrains.rider.model.RdCustomLocation | ||
import com.jetbrains.rider.model.RdProjectDescriptor | ||
import kotlin.io.path.Path | ||
import kotlin.io.path.listDirectoryEntries | ||
import kotlin.io.path.name | ||
|
||
@Service | ||
class AppSettingsConnectionProvider : DbConnectionProvider { | ||
companion object { | ||
private val json = jacksonObjectMapper() | ||
fun getInstance(intellijProject: Project) = intellijProject.service<AppSettingsConnectionProvider>() | ||
} | ||
|
||
override fun getAvailableConnections(project: RdProjectDescriptor) = | ||
buildList { | ||
val directory = (project.location as RdCustomLocation?)?.customLocation?.let(::Path)?.parent ?: return@buildList | ||
val connectionStrings = directory.listDirectoryEntries("appsettings*.json") | ||
.filter { it.isFile() } | ||
.map { it.name to json.readTree(it.toFile()) } | ||
.mapNotNull { (fileName, json) -> (json.get("ConnectionStrings") as ObjectNode?)?.let { fileName to it } } | ||
.flatMap { (fileName, obj) -> | ||
obj.fieldNames().asSequence().map { connName -> | ||
(obj[connName] as TextNode?)?.let { node -> Triple(fileName, connName, node.textValue()) } | ||
} | ||
} | ||
.filterNotNull() | ||
.map { (fileName, connName, connString) -> DbConnectionInfo(connName, connString, fileName, null) } | ||
|
||
addAll(connectionStrings) | ||
}.toList() | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...kotlin/me/seclerp/rider/plugins/efcore/features/connections/DataGripConnectionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.intellij.database.Dbms | ||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.database.dataSource.LocalDataSourceManager | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import com.jetbrains.rider.model.RdProjectDescriptor | ||
|
||
@Service | ||
class DataGripConnectionProvider(private val intellijProject: Project) : DbConnectionProvider { | ||
companion object { | ||
fun getInstance(intellijProject: Project) = intellijProject.service<DataGripConnectionProvider>() | ||
} | ||
override fun getAvailableConnections(project: RdProjectDescriptor) = buildList { | ||
LocalDataSourceManager.getInstance(intellijProject).dataSources.forEach { | ||
val connString = generateConnectionString(it) | ||
if (connString != null) | ||
add(DbConnectionInfo(it.name, connString, "Data sources", it.dbms)) | ||
} | ||
} | ||
|
||
private fun generateConnectionString(source: LocalDataSource) = | ||
when (source.dbms) { | ||
Dbms.SQLITE -> "Data Source=${source.url?.removePrefix("jdbc:sqlite:")}" | ||
else -> null | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ider/main/kotlin/me/seclerp/rider/plugins/efcore/features/connections/DbConnectionInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.intellij.database.Dbms | ||
|
||
data class DbConnectionInfo( | ||
val name: String, | ||
val connectionString: String, | ||
val sourceName: String, | ||
val dbms: Dbms? | ||
) | ||
|
7 changes: 7 additions & 0 deletions
7
.../main/kotlin/me/seclerp/rider/plugins/efcore/features/connections/DbConnectionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.jetbrains.rider.model.RdProjectDescriptor | ||
|
||
interface DbConnectionProvider { | ||
fun getAvailableConnections(project: RdProjectDescriptor): List<DbConnectionInfo> | ||
} |
34 changes: 34 additions & 0 deletions
34
...ain/kotlin/me/seclerp/rider/plugins/efcore/features/connections/DbConnectionsCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.workspaceModel.ide.WorkspaceModel | ||
import com.jetbrains.rider.model.RdProjectDescriptor | ||
import com.jetbrains.rider.projectView.workspace.findProjects | ||
import java.util.* | ||
|
||
@Service | ||
@Suppress("UnstableApiUsage") | ||
class DbConnectionsCollector(private val intellijProject: Project) { | ||
companion object { | ||
fun getInstance(intellijProject: Project) = intellijProject.service<DbConnectionsCollector>() | ||
} | ||
|
||
private val providers = listOf( | ||
AppSettingsConnectionProvider.getInstance(intellijProject), | ||
UserSecretsConnectionProvider.getInstance(intellijProject), | ||
DataGripConnectionProvider.getInstance(intellijProject) | ||
) | ||
|
||
fun collect(projectId: UUID): List<DbConnectionInfo> { | ||
val project = WorkspaceModel.getInstance(intellijProject) | ||
.findProjects() | ||
.filter { it.descriptor is RdProjectDescriptor } | ||
.map { it.descriptor as RdProjectDescriptor } | ||
.firstOrNull { it.originalGuid == projectId } | ||
?: return emptyList() | ||
|
||
return providers.flatMap { it.getAvailableConnections(project) } | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...lin/me/seclerp/rider/plugins/efcore/features/connections/UserSecretsConnectionProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package me.seclerp.rider.plugins.efcore.features.connections | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.fasterxml.jackson.databind.node.TextNode | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.components.service | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.openapi.util.SystemInfo | ||
import com.jetbrains.rider.ideaInterop.welcomeWizard.transferSettingsRider.utilities.WindowsEnvVariables | ||
import com.jetbrains.rider.model.RdProjectDescriptor | ||
import com.jetbrains.rider.projectView.nodes.getUserData | ||
import kotlin.io.path.Path | ||
|
||
@Service | ||
class UserSecretsConnectionProvider : DbConnectionProvider { | ||
companion object { | ||
private val json = jacksonObjectMapper() | ||
private val userSecretsFolder = if (SystemInfo.isWindows) | ||
Path(WindowsEnvVariables.applicationData, "Microsoft", "UserSecrets") | ||
else | ||
Path(System.getenv("HOME"), ".microsoft", "usersecrets") | ||
fun getInstance(intellijProject: Project) = intellijProject.service<UserSecretsConnectionProvider>() | ||
} | ||
|
||
override fun getAvailableConnections(project: RdProjectDescriptor) = | ||
buildList { | ||
val userSecretsId = project.getUserData("UserSecretsId") ?: return@buildList | ||
val userSecretsFile = userSecretsFolder.resolve(userSecretsId).resolve("secrets.json").toFile() | ||
if (!userSecretsFile.exists() || !userSecretsFile.isFile) | ||
return@buildList | ||
val obj = json.readTree(userSecretsFile).get("ConnectionStrings") as ObjectNode? ?: return@buildList | ||
obj.fieldNames().forEach { connName -> | ||
val connString = (obj[connName] as TextNode?)?.textValue() | ||
if (connString != null) | ||
add(DbConnectionInfo(connName, connString, "User secrets", null)) | ||
} | ||
}.toList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.