Skip to content

Commit

Permalink
Merge branch 'main' into ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
shocklateboy92 authored Nov 20, 2023
2 parents 0b45cde + 14d9a79 commit 8512332
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 32 deletions.
4 changes: 1 addition & 3 deletions .idea/.idea.CSharpier/.idea/indexLayout.xml

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

1 change: 1 addition & 0 deletions CSharpier.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Concated/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpier/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpierignore/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpierrc/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=externs/@EntryIndexedValue">True</s:Boolean>
Expand Down
6 changes: 6 additions & 0 deletions Src/CSharpier.Cli/EditorConfig/EditorConfigParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace CSharpier.Cli.EditorConfig;

// TODO for a given directory find the config in it and going up til root
// keep track of directories + their corresponding configs
// if searching a new directory and we go up to a parent that contains the first config, use that
// how many directories would we run into? enough to matter?
// TODO !!!!!!!!!!!!!!!!!! an easy fix, is to pass a flag to not go deeper when running this via piping files!!!
internal static class EditorConfigParser
{
/// <summary>Finds all configs above the given directory as well as within the subtree of this directory</summary>
Expand All @@ -17,6 +22,7 @@ IgnoreFile ignoreFile
}

var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(directoryName);
// TODO this is probably killing performance if nothing else when piping a single file
var editorConfigFiles = directoryInfo
.EnumerateFiles(".editorconfig", SearchOption.AllDirectories)
.Where(x => !ignoreFile.IsIgnored(x.FullName))
Expand Down
2 changes: 2 additions & 0 deletions Src/CSharpier.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ CancellationToken cancellationToken

var exitCode = 0;

// TODO warm file somewhere around here

while (true)
{
while (true)
Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier.Rider/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Useful links
- https://plugins.jetbrains.com/docs/intellij/welcome.html
- https://developerlife.com/2021/03/13/ij-idea-plugin-advanced/

Initial Setup
- need the java 17 jdk

Local testing
- use "Run Plugin" configuration while in intellij to launch the plugin into a dev instance of rider
- To see logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public void ensureVersionInstalled(String version) throws Exception {
}
}
catch (Exception ex) {
// TODO somehow I got a bunch of the versions to install that were missing dotnet-csharpier in the root of the custom path
// this needs to do a better job of figuring that out and reporting it.
// I think when this fails to install, then the other stuff gets stuck in an infinite loop
logger.warn("Exception while running 'dotnet csharpier --version' in " + pathToDirectoryForVersion, ex);
}

Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier.VSCode/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.5.0]
- Improved error handling and reporting around csharpier failing to install or run

## [1.3.6]
- Fix bug where 2nd instance of VSCode was not able to format code

Expand Down
7 changes: 6 additions & 1 deletion Src/CSharpier.VSCode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "csharpier-vscode",
"displayName": "CSharpier - Code formatter",
"description": "Code formatter using csharpier",
"version": "1.3.6",
"version": "1.5.0",
"publisher": "csharpier",
"author": "CSharpier",
"homepage": "https://marketplace.visualstudio.com/items?itemName=csharpier.csharpier-vscode",
Expand Down Expand Up @@ -47,6 +47,11 @@
"type": "boolean",
"default": false,
"description": "Enable debug logs."
},
"csharpier.dev.customPath": {
"type": "string",
"default": "",
"description": "Path to directory containing dotnet-csharpier - used for testing the extension with new versions of csharpier."
}
}
}
Expand Down
32 changes: 30 additions & 2 deletions Src/CSharpier.VSCode/src/CSharpierProcessPipeMultipleFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
private callbacks: ((result: string) => void)[] = [];
private logger: Logger;
private nextFile: string = "";
private processFailedToStart = false;

constructor(logger: Logger, csharpierPath: string, workingDirectory: string) {
this.logger = logger;
Expand All @@ -26,11 +27,29 @@ export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {
env: { ...process.env, DOTNET_NOLOGO: "1" },
});

csharpierProcess.on("error", data => {
this.logger.warn(
"Failed to spawn the needed csharpier process. Formatting cannot occur.",
data,
);
this.processFailedToStart = true;
while (this.callbacks.length > 0) {
const callback = this.callbacks.shift();
if (callback) {
callback("");
}
}
});

csharpierProcess.stderr.on("data", chunk => {
this.logger.warn("Received data on stderr from the running charpier process", chunk);
});

csharpierProcess.stdout.on("data", chunk => {
this.logger.debug("Got chunk of size " + chunk.length);
this.nextFile += chunk;
const number = this.nextFile.indexOf("\u0003");
if (number >= 0) {
let number = this.nextFile.indexOf("\u0003");
while (number >= 0) {
this.logger.debug("Got last chunk with ETX at " + number);
const result = this.nextFile.substring(0, number);
this.nextFile = this.nextFile.substring(number + 1);
Expand All @@ -44,13 +63,22 @@ export class CSharpierProcessPipeMultipleFiles implements ICSharpierProcess {

callback(result);
}

number = this.nextFile.indexOf("\u0003");
}
});

return csharpierProcess;
};

formatFile(content: string, filePath: string): Promise<string> {
if (this.processFailedToStart) {
this.logger.warn("CSharpier proccess failed to start. Formatting cannot occur.");
return new Promise<string>(resolve => {
resolve("");
});
}

this.process.stdin.write(filePath);
this.process.stdin.write("\u0003");
this.process.stdin.write(content);
Expand Down
8 changes: 7 additions & 1 deletion Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,13 @@ export class CSharpierProcessProvider implements Disposable {
return NullCSharpierProcess.instance;
}

this.customPathInstaller.ensureVersionInstalled(version);
if (!this.customPathInstaller.ensureVersionInstalled(version)) {
window.showErrorMessage(
"CSharpier could not be set up properly so formatting is not currently supported. See Output - CSharpier for details.",
);
return NullCSharpierProcess.instance;
}

const customPath = this.customPathInstaller.getPathForVersion(version);

this.logger.debug(`Adding new version ${version} process for ${directory}`);
Expand Down
72 changes: 47 additions & 25 deletions Src/CSharpier.VSCode/src/CustomPathInstaller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,77 @@ import { Logger } from "./Logger";
import * as path from "path";
import * as fs from "fs";
import { execSync } from "child_process";
import { workspace } from "vscode";

export class CustomPathInstaller {
logger: Logger;
customPath: string;

constructor(logger: Logger) {
this.logger = logger;
this.customPath =
workspace.getConfiguration("csharpier").get<string>("dev.customPath") ?? "";
}

public ensureVersionInstalled(version: string) {
public ensureVersionInstalled(version: string): boolean {
if (!version) {
return;
return false;
}
if (this.customPath !== "") {
this.logger.debug("Using csharpier at a custom path of " + this.customPath);
return true;
}

const pathToDirectoryForVersion = this.getDirectoryForVersion(version);
if (fs.existsSync(pathToDirectoryForVersion)) {
try {
const output = execSync(`${this.getPathForVersion(version)} --version`, {
env: { ...process.env, DOTNET_NOLOGO: "1" },
})
.toString()
.trim();

this.logger.debug("dotnet csharpier --version output: " + output);

if (output === version) {
this.logger.debug(
"CSharpier at " + pathToDirectoryForVersion + " already exists",
);
return;
}
} catch (error: any) {
const message = !error.stderr ? error.toString() : error.stderr.toString();
this.logger.warn(
"Exception while running 'dotnet csharpier --version' in " +
pathToDirectoryForVersion,
message,
);
if (this.validateInstall(pathToDirectoryForVersion, version)) {
return true;
}

this.logger.debug(
`Removing directory at ${pathToDirectoryForVersion} because it appears to be corrupted`,
);
fs.rmdirSync(pathToDirectoryForVersion, { recursive: true });
fs.rmSync(pathToDirectoryForVersion, { recursive: true, force: true });
}

const command = `dotnet tool install csharpier --version ${version} --tool-path "${pathToDirectoryForVersion}"`;
this.logger.debug("Running " + command);
execSync(command);

return this.validateInstall(pathToDirectoryForVersion, version);
}

private validateInstall(pathToDirectoryForVersion: string, version: string): boolean {
try {
const output = execSync(`${this.getPathForVersion(version)} --version`, {
env: { ...process.env, DOTNET_NOLOGO: "1" },
})
.toString()
.trim();

this.logger.debug("dotnet csharpier --version output: " + output);

if (output.split("+")[0] === version) {
this.logger.debug("CSharpier at " + pathToDirectoryForVersion + " already exists");
return true;
}
} catch (error: any) {
const message = !error.stderr ? error.toString() : error.stderr.toString();
this.logger.warn(
"Exception while running 'dotnet csharpier --version' in " +
pathToDirectoryForVersion,
message,
);
}

return false;
}

private getDirectoryForVersion(version: string) {
if (this.customPath !== "") {
return this.customPath;
}

const result =
process.platform !== "win32"
? path.resolve(process.env.HOME!, ".cache/csharpier", version)
Expand Down

0 comments on commit 8512332

Please sign in to comment.