Skip to content

Commit

Permalink
fix validation when cli not installed
Browse files Browse the repository at this point in the history
  • Loading branch information
ellizio committed Mar 24, 2024
1 parent a7fd4a0 commit 4c89d2f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 30 deletions.
8 changes: 6 additions & 2 deletions protocol/src/main/kotlin/model/rider/ProtocolModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import com.jetbrains.rd.generator.nova.*
import com.jetbrains.rider.model.nova.ide.SolutionModel

object ProtocolModel : Ext(SolutionModel.Solution) {
private val CliToolDefinition = structdef {
field("installed", PredefinedType.bool)
field("version", PredefinedType.string.nullable)
}

init {
property("cliVersion", PredefinedType.string)
call("getCliVersion", PredefinedType.void, PredefinedType.string)
call("getCliDefinition", PredefinedType.void, CliToolDefinition)
}
}
15 changes: 8 additions & 7 deletions src/dotnet/ReSharperPlugin.ODataCliUi/PluginHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,39 @@ namespace ReSharperPlugin.ODataCliUi;
[SolutionComponent]
public sealed class PluginHost : IDisposable
{
private readonly ProtocolModel _protocolModel;
private readonly Tracker _tracker;

private CliToolDefinition _cliToolDefinition;

public PluginHost(ISolution solution, Tracker tracker)
{
_protocolModel = solution.GetProtocolSolution().GetProtocolModel();
_protocolModel.GetCliVersion.SetSync(GetCliVersion);
var protocolModel = solution.GetProtocolSolution().GetProtocolModel();
protocolModel.GetCliDefinition.SetSync(GetCliDefinition);

_tracker = tracker;
tracker.DotNetToolCacheChanged += OnDotNetToolCacheChanged;
tracker.Start();
}

private string GetCliVersion(Lifetime lifetime, Unit unit) => _protocolModel.CliVersion.Value;
private CliToolDefinition GetCliDefinition(Lifetime lifetime, Unit unit) => _cliToolDefinition;

private void OnDotNetToolCacheChanged(DotNetToolCache cache)
{
var localTool = cache.ToolLocalCache.GetAllLocalTools().FirstOrDefault(t => t.PackageId == Constants.ODataCliPackageId);
if (localTool is not null)
{
_protocolModel.CliVersion.Value = $"Local, {localTool.Version}";
_cliToolDefinition = new CliToolDefinition(true, $"Local, {localTool.Version}");
return;
}

var tool = cache.ToolGlobalCache.GetGlobalTool(Constants.ODataCliPackageId)?.FirstOrDefault();
if (tool is not null)
{
_protocolModel.CliVersion.Value = $"Global, {tool.Version}";
_cliToolDefinition = new CliToolDefinition(true, $"Global, {tool.Version}");
return;
}

_protocolModel.CliVersion.Value = "Not installed";
_cliToolDefinition = new CliToolDefinition(false, null);
}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ import com.jetbrains.rider.projectView.workspace.isProject
import com.jetbrains.rider.projectView.workspace.isWebReferenceFolder

class OpenCliDialogAction : AnAction() {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val actionMetadata = e.toMetadata() ?: return
override fun actionPerformed(event: AnActionEvent) {
val project = event.project ?: return
val actionMetadata = event.toMetadata() ?: return

val dialogModel = CliDialogModel(project, actionMetadata)

// launchOnUi is available since 233.11799.241
// RD-2023.3 has build number 233.11799.261
@Suppress("MissingRecentApi")
project.lifetime.launchOnUi {
val dialog = CliDialog(dialogModel)
if (dialog.showAndGet()) {
Expand All @@ -35,14 +39,14 @@ class OpenCliDialogAction : AnAction() {

override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

override fun update(e: AnActionEvent) {
val entity = e.entityForAction
override fun update(event: AnActionEvent) {
val entity = event.entityForAction
if (entity == null) {
e.presentation.isVisible = false
event.presentation.isVisible = false
return
}
e.presentation.isEnabled = e.project?.isProjectModelReady() ?: false
e.presentation.isVisible = entity.isWebReferenceFolder() || entity.isProject()
event.presentation.isEnabled = event.project?.isProjectModelReady() ?: false
event.presentation.isVisible = entity.isWebReferenceFolder() || entity.isProject()
}

private suspend fun executeCommand(project: Project, model: CliDialogModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ import com.jetbrains.rider.plugins.odatacliui.extensions.emptyText
import com.jetbrains.rider.plugins.odatacliui.models.CliDialogModel
import javax.swing.JComponent

@Suppress("UnstableApiUsage")
class CliDialog(private val model: CliDialogModel) : DialogWrapper(false) {
init {
title = Constants.PLUGIN_NAME
setOKActionEnabled(true)
init()
}

override fun setOKActionEnabled(isEnabled: Boolean) {
if (!model.cliDefinition.installed) {
setOKButtonTooltip(UiBundle.text("cli.ok-action-button.tooltip.not-installed"))
super.setOKActionEnabled(false)
return
}
else {
setOKButtonTooltip(null)
}

super.setOKActionEnabled(isEnabled)
}

override fun createCenterPanel(): JComponent {
val tabbedPane = JBTabbedPane()
val generationTab = buildGenerationArgumentsTab()
Expand All @@ -27,7 +40,7 @@ class CliDialog(private val model: CliDialogModel) : DialogWrapper(false) {

return panel {
row {
label(model.cliVersion)
label(model.cliDefinition.version ?: UiBundle.text("cli.cli-version.label-value.not-installed"))
.label(UiBundle.text("cli.cli-version.label"))
.comment(UiBundle.text("cli.cli-version.comment"))
}.bottomGap(BottomGap.SMALL)
Expand All @@ -49,9 +62,6 @@ class CliDialog(private val model: CliDialogModel) : DialogWrapper(false) {
row {
cell(tabbedPane)
}.resizableRow()
}.apply {
registerIntegratedPanel(generationTab)
registerIntegratedPanel(requestTab)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun AnActionEvent.toMetadata(): ActionMetadata? {
descriptor = entity.parentEntity!!.descriptor as RdProjectDescriptor
}
else {
throw IllegalStateException()
throw IllegalStateException("Selected item not supported")
}

val projectPath = (descriptor.location as RdCustomLocation).customLocation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jetbrains.rider.plugins.odatacliui.models

import com.intellij.openapi.project.Project
import com.jetbrains.rd.ide.model.CliToolDefinition
import com.jetbrains.rd.ide.model.protocolModel
import com.jetbrains.rider.plugins.odatacliui.Constants
import com.jetbrains.rider.plugins.odatacliui.extensions.dotnetAddPackageCommand
Expand All @@ -15,10 +16,10 @@ private const val CONNECTED_SERVICES = "Connected Services"
class CliDialogModel(project: Project, private val actionMetadata: ActionMetadata) {
val validator = CliDialogModelValidator()

val cliVersion: String
val cliDefinition: CliToolDefinition

init {
cliVersion = getCliVersion(project)
cliDefinition = project.solution.protocolModel.getCliDefinition.sync(Unit)
}

val serviceName = MutableProperty("")
Expand All @@ -37,11 +38,6 @@ class CliDialogModel(project: Project, private val actionMetadata: ActionMetadat
val customHeaders = MutableProperty("")
val proxy = MutableProperty("")

private fun getCliVersion(project: Project): String {
return project.solution.protocolModel.cliVersion.valueOrNull
?: project.solution.protocolModel.getCliVersion.sync(Unit)
}

private fun getOutputDirectory(): String = Path(Path(actionMetadata.projectPath).parent.toString(), CONNECTED_SERVICES, serviceName.get()).toString()

fun buildCommand(): BatchCommandLine = BatchCommandLineBuilder()
Expand Down
2 changes: 2 additions & 0 deletions src/rider/main/resources/UiBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ action.odatacliui.OpenCliDialogAction.text=OData Reference\u2026
cli.tab.generation=Generation Arguments
cli.tab.request=Generation Arguments
cli.cli-version.label=OData CLI Version:
cli.cli-version.label-value.not-installed=Not installed
cli.cli-version.comment=Not installed? Follow <a href='https://learn.microsoft.com/en-us/odata/odatacli/getting-started#install'>instruction</a>
cli.service-name.row=Service name:
cli.metadata-source.row=Metadata source:
Expand All @@ -26,3 +27,4 @@ cli.custom-headers.empty-text=Example: Header1:HeaderValue, Header2:HeaderValue
cli.custom-headers.comment=Headers that will get sent along with the request when fetching the metadata document from the service
cli.proxy.empty-text=Example: domain\\user:password@SERVER:PORT
cli.proxy.comment=Proxy settings
cli.ok-action-button.tooltip.not-installed=OData CLI not installed

0 comments on commit 4c89d2f

Please sign in to comment.