Skip to content

Commit

Permalink
fix dotnet tools execution on unix
Browse files Browse the repository at this point in the history
  • Loading branch information
ellizio committed Apr 5, 2024
1 parent 1e0f64f commit c0b40b8
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 63 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Rider plugin to generate OData Reference using OData CLI

## Installation

1. Install plugin
2. Install latest version of [OData CLI](https://learn.microsoft.com/en-us/odata/odatacli/getting-started#install) tool
1. Install plugin
2. Install the latest version of the OData CLI tool with the command `dotnet tool install -g Microsoft.OData.Cli`

## Usage

Expand All @@ -20,3 +20,11 @@ Rider plugin to generate OData Reference using OData CLI
![](/img/dialog.png)
4. Click `OK` and wait until generation finishes
![](/img/terminal.png)

## ⚠️ Known Restrictions

TODO

## Additional References

- [OData CLI](https://learn.microsoft.com/en-us/odata/odatacli/getting-started)
1 change: 0 additions & 1 deletion src/dotnet/ReSharperPlugin.ODataCliUi/IODataCliUiZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ReSharperPlugin.ODataCliUi
{
[ZoneDefinition]
// [ZoneDefinitionConfigurableFeature("Title", "Description", IsInProductSection: false)]
public interface IODataCliUiZone : IZone,
IRequire<IProjectModelZone>,
IRequire<INuGetZone>;
Expand Down
19 changes: 4 additions & 15 deletions src/dotnet/ReSharperPlugin.ODataCliUi/PluginHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,10 @@ private Task AddEmbeddedResourceAsync(Lifetime lifetime, EmbeddedResourceDefinit

private void OnDotNetToolCacheChanged(DotNetToolCache cache)
{
var localTool = cache.ToolLocalCache.GetAllLocalTools().FirstOrDefault(t => t.PackageId == Constants.MicrosoftODataCliPackageId);
if (localTool is not null)
{
_odataCliTool = new CliTool(true, $"Local, {localTool.Version}");
return;
}

var globalTool = cache.ToolGlobalCache.GetGlobalTool(Constants.MicrosoftODataCliPackageId)?.FirstOrDefault();
if (globalTool is not null)
{
_odataCliTool = new CliTool(true, $"Global, {globalTool.Version}");
return;
}

_odataCliTool = new CliTool(false, null);
var tool = cache.ToolGlobalCache.GetGlobalTool(Constants.MicrosoftODataCliPackageId)?.FirstOrDefault();
_odataCliTool = tool is null
? _odataCliTool = new CliTool(false, null)
: _odataCliTool = new CliTool(true, $"Global, {tool.Version}");
}

public void Dispose()
Expand Down
5 changes: 1 addition & 4 deletions src/dotnet/ReSharperPlugin.ODataCliUi/ProjectModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public ProjectModifier(IProject project)

public void AddEmbeddedResource(string include)
{
_host.EditProject(_mark, session =>
{
session.AddItem(_mark, EmbeddedResource, include);
});
_host.EditProject(_mark, session => session.AddItem(_mark, EmbeddedResource, include));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package ru.ellizio.odatacliui.extensions
import ru.ellizio.odatacliui.Constants
import ru.ellizio.odatacliui.terminal.builders.BatchCommandLineBuilder

fun BatchCommandLineBuilder.dotnetAddPackageCommand(csprojPath: String, packageId: String): BatchCommandLineBuilder {
return addCommand("dotnet", "add")
fun BatchCommandLineBuilder.dotnetAddPackageCommand(dotnetExePath: String?, csprojPath: String, packageId: String): BatchCommandLineBuilder {
return addCommand(dotnetExePath ?: "dotnet", "add")
.withParameter(csprojPath)
.withParameter("package")
.withParameter(packageId)
.withNotBlankParameter("-s", Constants.NUGET_SOURCE)
.withParameter("-s", Constants.NUGET_SOURCE)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import com.intellij.execution.configurations.GeneralCommandLine
import com.intellij.openapi.project.Project
import com.jetbrains.rd.ide.model.CliTool
import com.jetbrains.rd.ide.model.protocolModel
import com.jetbrains.rider.model.dotNetActiveRuntimeModel
import ru.ellizio.odatacliui.Constants
import ru.ellizio.odatacliui.extensions.dotnetAddPackageCommand
import ru.ellizio.odatacliui.models.validators.CliDialogModelValidator
import ru.ellizio.odatacliui.terminal.BatchCommandLine
import ru.ellizio.odatacliui.terminal.builders.BatchCommandLineBuilder
import com.jetbrains.rider.projectView.solution
import ru.ellizio.odatacliui.terminal.builders.CommandLineBuilder
import ru.ellizio.odatacliui.utils.DotnetToolsUtils
import kotlin.io.path.Path

private const val CONNECTED_SERVICES = "Connected Services"
Expand All @@ -20,9 +22,11 @@ class CliDialogModel(project: Project, private val actionMetadata: ActionMetadat
val validator = CliDialogModelValidator()

val odataCliTool: CliTool
val dotnetCliPath: String?

init {
odataCliTool = project.solution.protocolModel.getODataCliTool.sync(Unit)
dotnetCliPath = project.solution.dotNetActiveRuntimeModel.activeRuntime.valueOrNull?.dotNetCliExePath
}

val serviceName = MutableProperty("")
Expand All @@ -45,26 +49,26 @@ class CliDialogModel(project: Project, private val actionMetadata: ActionMetadat

fun getCsdlPath(): String = Path(CONNECTED_SERVICES, serviceName.get(), CSDL_NAME).toString()

fun buildODataCliCommand(): GeneralCommandLine = CommandLineBuilder("odata-cli", "generate")
.withNotBlankParameter("--metadata-uri", metadataUri.get())
.withNotBlankParameter("--file-name", fileName.get())
.withNotBlankParameter("--custom-headers", customHeaders.get())
.withNotBlankParameter("--proxy", proxy.get())
.withNotBlankParameter("--namespace-prefix", namespacePrefix.get())
.withNotBlankParameter("--excluded-operation-imports", excludedOperationImports.get())
.withNotBlankParameter("--excluded-bound-operations", excludedBoundOperations.get())
.withNotBlankParameter("--excluded-schema-types", excludedSchemaTypes.get())
fun buildODataCliCommand(): GeneralCommandLine = CommandLineBuilder(DotnetToolsUtils.getToolDefaultPath("odata-cli"), "generate")
.withParameter("--metadata-uri", metadataUri.get())
.withParameter("--file-name", fileName.get())
.withParameter("--custom-headers", customHeaders.get())
.withParameter("--proxy", proxy.get())
.withParameter("--namespace-prefix", namespacePrefix.get())
.withParameter("--excluded-operation-imports", excludedOperationImports.get())
.withParameter("--excluded-bound-operations", excludedBoundOperations.get())
.withParameter("--excluded-schema-types", excludedSchemaTypes.get())
.withFlag("--upper-camel-case", upperCamelCase.get())
.withFlag("--internal", internal.get())
.withFlag("--multiple-files", multipleFiles.get())
.withFlag("--ignore-unexpected-elements", ignoreUnexpectedElements.get())
.withNotBlankParameter("--outputdir", getOutputDirectory())
.withParameter("--outputdir", getOutputDirectory())
.build()

fun buildNuGetCommand(): BatchCommandLine = BatchCommandLineBuilder()
.dotnetAddPackageCommand(actionMetadata.projectPath, Constants.MICROSOFT_ODATA_CLIENT_PACKAGE_ID)
.dotnetAddPackageCommand(actionMetadata.projectPath, Constants.MICROSOFT_ODATA_CORE_PACKAGE_ID)
.dotnetAddPackageCommand(actionMetadata.projectPath, Constants.MICROSOFT_ODATA_EDM_PACKAGE_ID)
.dotnetAddPackageCommand(actionMetadata.projectPath, Constants.MICROSOFT_SPATIAL_PACKAGE_ID)
.dotnetAddPackageCommand(dotnetCliPath, actionMetadata.projectPath, Constants.MICROSOFT_ODATA_CLIENT_PACKAGE_ID)
.dotnetAddPackageCommand(dotnetCliPath, actionMetadata.projectPath, Constants.MICROSOFT_ODATA_CORE_PACKAGE_ID)
.dotnetAddPackageCommand(dotnetCliPath, actionMetadata.projectPath, Constants.MICROSOFT_ODATA_EDM_PACKAGE_ID)
.dotnetAddPackageCommand(dotnetCliPath, actionMetadata.projectPath, Constants.MICROSOFT_SPATIAL_PACKAGE_ID)
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class BatchCommandLineBuilder {
return this
}

fun withNotBlankParameter(parameterName: String, parameterValue: String?): BatchCommandLineBuilder {
fun withParameter(parameterName: String, parameterValue: String?): BatchCommandLineBuilder {
if (command == null)
throw IllegalStateException()

Expand All @@ -39,17 +39,6 @@ class BatchCommandLineBuilder {
return this
}

fun withFlag(parameterName: String, parameterValue: Boolean): BatchCommandLineBuilder {
if (command == null)
throw IllegalStateException()

if (!parameterValue)
return this

command!!.addParameter(parameterName)
return this
}

fun build(): BatchCommandLine {
if (commandLine.commands.size != commandsCount) {
commandLine.add(command!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ class CommandLineBuilder(toolName: String, commandName: String) {
command.addParameter(commandName)
}

fun withParameter(parameter: String): CommandLineBuilder {
command.addParameter(parameter)
return this
}

fun withNotBlankParameter(parameterName: String, parameterValue: String?): CommandLineBuilder {
fun withParameter(parameterName: String, parameterValue: String?): CommandLineBuilder {
if (parameterValue.isNullOrBlank())
return this

Expand All @@ -32,11 +27,6 @@ class CommandLineBuilder(toolName: String, commandName: String) {
return this
}

fun withEnvironmentVariable(variableName: String, value: String): CommandLineBuilder {
command.withEnvironment(variableName, value)
return this
}

fun build(): GeneralCommandLine = command

private fun initCommand(): GeneralCommandLine {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ class CommandLineExecutor(
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
val contentType = ConsoleViewContentType.getConsoleViewType(outputType)
if (contentType == ConsoleViewContentType.ERROR_OUTPUT)
hasError = true;
hasError = true

consoleView.print(event.text, contentType)
}

override fun processTerminated(event: ProcessEvent) {
if (event.exitCode != 0)
hasError = true;

super.processTerminated(event)
}
}

val terminalHandler = TerminalProcessHandler(project, commandLine)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.ellizio.odatacliui.utils

import com.intellij.openapi.util.SystemInfo
import kotlin.io.path.Path

object DotnetToolsUtils {
fun getToolDefaultPath(toolName: String): String {
val homePath = System.getenv(if (SystemInfo.isWindows) "USERPROFILE" else "HOME")
return Path(homePath, ".dotnet", "tools", toolName).toString()
}
}

0 comments on commit c0b40b8

Please sign in to comment.