Skip to content

Commit

Permalink
VS - Use dotnet tool list to find both local and global installs of c…
Browse files Browse the repository at this point in the history
…sharpier. (#1326)

#1267
  • Loading branch information
belav authored Aug 18, 2024
1 parent 1cdcf0a commit 688aeb7
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 50 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.7.4" Language="en-US" Publisher="CSharpier" />
<Identity Id="83d6b6a0-9e25-4034-80f3-38445d8a8837" Version="1.8.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.7.4" Language="en-US" Publisher="CSharpier" />
<Identity Id="edd8b38c-baa1-46c6-b82e-1da7a0ba597b" Version="1.8.0" Language="en-US" Publisher="CSharpier" />
<DisplayName>CSharpier 2019</DisplayName>
<Description xml:space="preserve">CSharpier is an opinionated code formatter for c#. It uses Roslyn to parse your code and re-prints it using its own rules.</Description>
<MoreInfo>https://github.com/belav/csharpier</MoreInfo>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<Compile Include="$(MSBuildThisFileDirectory)CSharpierProcessProvider.cs" />
<Compile Include="$(MSBuildThisFileDirectory)CustomPathInstaller.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FormattingService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FunctionRunner.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ICSharpierProcess.cs" />
<Compile Include="$(MSBuildThisFileDirectory)InfoBarService.cs" />
<Compile Include="$(MSBuildThisFileDirectory)InstallerService.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Newtonsoft.Json;

namespace CSharpier.VisualStudio
{
Expand Down Expand Up @@ -47,14 +39,14 @@ public void FindAndWarmProcess(string filePath)
var directory = new FileInfo(filePath).DirectoryName;
if (directory == null)
{
this.logger.Warn("There was no directory for " + filePath);
this.logger.Warn($"There was no directory for {filePath}");
return;
}
if (this.warmingByDirectory.TryGetValue(directory, out var warming) && warming)
{
return;
}
this.logger.Debug("Ensure there is a csharpier process for " + directory);
this.logger.Debug($"Ensure there is a csharpier process for {directory}");
this.warmingByDirectory[directory] = true;
if (!this.csharpierVersionByDirectory.TryGetValue(directory, out var version))
{
Expand Down Expand Up @@ -100,40 +92,39 @@ public ICSharpierProcess GetProcessFor(string filePath)

private string GetCSharpierVersion(string directoryThatContainsFile)
{
var csharpierVersion = FunctionRunner.RunUntilNonNull(
() => this.FindVersionInCsProjOfParentsDirectories(directoryThatContainsFile),
() => this.FindCSharpierVersionInToolOutput(directoryThatContainsFile, false),
() => this.FindCSharpierVersionInToolOutput(directoryThatContainsFile, true)
);

if (csharpierVersion == null)
{
return "";
}

var versionWithoutHash = csharpierVersion.Split('+')[0];
this.logger.Debug($"Using {versionWithoutHash} as the version number.");

return versionWithoutHash;
}

private string? FindVersionInCsProjOfParentsDirectories(string directoryThatContainsFile)
{
this.logger.Debug(
$"Looking for csproj in or above {directoryThatContainsFile} that references CSharpier.MsBuild"
);
var currentDirectory = new DirectoryInfo(directoryThatContainsFile);
try
{
while (true)
while (currentDirectory != null)
{
var csProjVersion = this.FindVersionInCsProj(currentDirectory);
if (csProjVersion != null)
{
return csProjVersion;
}

var dotnetToolsPath = Path.Combine(
currentDirectory.FullName,
".config/dotnet-tools.json"
);
this.logger.Debug("Looking for " + dotnetToolsPath);
if (File.Exists(dotnetToolsPath))
{
var data = File.ReadAllText(dotnetToolsPath);
var toolsManifest = JsonConvert.DeserializeObject<ToolsManifest>(data);
var versionFromTools = toolsManifest?.Tools?.Csharpier?.Version;
if (versionFromTools != null)
{
this.logger.Debug(
"Found version " + versionFromTools + " in " + dotnetToolsPath
);
return versionFromTools;
}
}

if (currentDirectory.Parent == null)
{
break;
}
currentDirectory = currentDirectory.Parent;
}
}
Expand All @@ -142,24 +133,43 @@ private string GetCSharpierVersion(string directoryThatContainsFile)
this.logger.Error(ex);
}

this.logger.Debug(
"Unable to find dotnet-tools.json, falling back to running dotnet csharpier --version"
);
return null;
}

private string? FindCSharpierVersionInToolOutput(
string directoryThatContainsFile,
bool isGlobal
)
{
var env = new Dictionary<string, string> { { "DOTNET_NOLOGO", "1" } };

var versionFromCommand = ProcessHelper.ExecuteCommand(
var output = ProcessHelper.ExecuteCommand(
"dotnet",
"csharpier --version",
$"tool list{(isGlobal ? " -g" : "")}",
env,
directoryThatContainsFile
);

this.logger.Debug("dotnet csharpier --version output: " + versionFromCommand);
var versionWithoutHash = versionFromCommand.Split('+')[0];
this.logger.Debug("Using " + versionWithoutHash + " as the version number.");
this.logger.Debug(
$"Running 'dotnet tool list{(isGlobal ? "-g" : "")}' to look for version"
);
this.logger.Debug($"Output was: \n {output}");

return versionWithoutHash;
var lines = output.Split('\n').Select(o => o.Trim()).ToList();

// The first two lines are headers, so we start at index 2
for (var i = 2; i < lines.Count; i++)
{
var columns = Regex.Split(lines[i], "\\s{2,}"); // Split by 2 or more spaces
if (columns.Length >= 2)
{
if (columns[0].ToLower() == "csharpier")
{
return columns[1];
}
}
}

return null;
}

private string? FindVersionInCsProj(DirectoryInfo currentDirectory)
Expand Down Expand Up @@ -189,7 +199,7 @@ private string GetCSharpierVersion(string directoryThatContainsFile)
if (versionOfMsBuildPackage != null)
{
this.logger.Debug(
"Found version " + versionOfMsBuildPackage + " in " + pathToCsProj.FullName
$"Found version {versionOfMsBuildPackage} in {pathToCsProj.FullName}"
);
return versionOfMsBuildPackage;
}
Expand All @@ -214,7 +224,7 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
}
var customPath = this.customPathInstaller.GetPathForVersion(version);

this.logger.Debug("Adding new version " + version + " process for " + directory);
this.logger.Debug($"Adding new version {version} process for {directory}");
var installedVersion = this.GetInstalledVersion(version);
var pipeFilesVersion = new Version("0.12.0");
var serverVersion = new Version("0.29.0");
Expand Down Expand Up @@ -329,7 +339,7 @@ public void KillRunningProcesses()
foreach (var version in this.csharpierProcessesByVersion.Keys)
{
this.logger.Debug(
"disposing of process for version " + (version == "" ? "null" : version)
$"disposing of process for version {(version == "" ? "null" : version)}"
);
this.csharpierProcessesByVersion[version].Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

namespace CSharpier.VisualStudio
{
using System.Linq;

public static class FunctionRunner
{
public static string? RunUntilNonNull(params Func<string?>[] functions)
{
return functions
.Select(possibleFunction => possibleFunction())
.OfType<string>()
.FirstOrDefault();
}
}
}
5 changes: 4 additions & 1 deletion Src/CSharpier.VisualStudio/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [1.7.4]
## [1.8.0]
- Use dotnet tool list to find both local and global installs of csharpier.

## [1.7.4]
- Support for semver
- Only use CSharpier Server on 0.29.0+

Expand Down

0 comments on commit 688aeb7

Please sign in to comment.