Skip to content

Commit

Permalink
Add emacs format-all to Editors.md docs (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaslaich authored and belav committed Feb 10, 2024
1 parent b456544 commit b392e10
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Src/CSharpier.VSCode/package-lock.json

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

23 changes: 15 additions & 8 deletions Src/CSharpier.VSCode/src/CSharpierProcessProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Disposable, Extension, TextEditor, window, workspace } from "vscode";
import { Logger } from "./Logger";
import * as path from "path";
import * as semver from "semver";
import { execSync } from "child_process";
import * as convert from "xml-js";
import { ICSharpierProcess, NullCSharpierProcess } from "./CSharpierProcess";
import { CSharpierProcessSingleFile } from "./CSharpierProcessSingleFile";
import { CSharpierProcessPipeMultipleFiles } from "./CSharpierProcessPipeMultipleFiles";
import * as fs from "fs";
import { InstallerService } from "./InstallerService";
import { CustomPathInstaller } from "./CustomPathInstaller";
import { ExecDotNet } from "./DotNetProvider";

export class CSharpierProcessProvider implements Disposable {
warnedForOldVersion = false;
Expand All @@ -19,14 +19,17 @@ export class CSharpierProcessProvider implements Disposable {
warmingByDirectory: Record<string, boolean | undefined> = {};
csharpierVersionByDirectory: Record<string, string | undefined> = {};
csharpierProcessesByVersion: Record<string, ICSharpierProcess | undefined> = {};
execDotNet: ExecDotNet;

constructor(logger: Logger, extension: Extension<unknown>) {
constructor(logger: Logger, extension: Extension<unknown>, execDotNet: ExecDotNet) {
this.logger = logger;
this.customPathInstaller = new CustomPathInstaller(logger);
this.execDotNet = execDotNet;
this.customPathInstaller = new CustomPathInstaller(logger, execDotNet);
this.installerService = new InstallerService(
this.logger,
this.killRunningProcesses,
extension,
execDotNet,
);

window.onDidChangeActiveTextEditor((event: TextEditor | undefined) => {
Expand Down Expand Up @@ -138,16 +141,16 @@ export class CSharpierProcessProvider implements Disposable {
let outputFromCsharpier: string;

try {
outputFromCsharpier = execSync(`dotnet csharpier --version`, {
outputFromCsharpier = this.execDotNet(`dotnet csharpier --version`, {
cwd: directoryThatContainsFile,
env: { ...process.env, DOTNET_NOLOGO: "1" },
})
.toString()
.trim();

this.logger.debug(`dotnet csharpier --version output: ${outputFromCsharpier}`);
const versionWithoutHash = outputFromCsharpier.split("+")[0]
this.logger.debug(`Using ${versionWithoutHash} as the version number.`)
const versionWithoutHash = outputFromCsharpier.split("+")[0];
this.logger.debug(`Using ${versionWithoutHash} as the version number.`);
return versionWithoutHash;
} catch (error: any) {
const message = !error.stderr ? error.toString() : error.stderr.toString();
Expand Down Expand Up @@ -212,7 +215,7 @@ export class CSharpierProcessProvider implements Disposable {
}

if (!this.customPathInstaller.ensureVersionInstalled(version)) {
this.logger.debug(`Unable to validate install of version ${version}`)
this.logger.debug(`Unable to validate install of version ${version}`);
this.displayFailureMessage();
return NullCSharpierProcess.instance;
}
Expand All @@ -230,7 +233,11 @@ export class CSharpierProcessProvider implements Disposable {
}
return new CSharpierProcessSingleFile(this.logger, customPath);
} else {
const csharpierProcess = new CSharpierProcessPipeMultipleFiles(this.logger, customPath, directory);
const csharpierProcess = new CSharpierProcessPipeMultipleFiles(
this.logger,
customPath,
directory,
);
if (csharpierProcess.processFailedToStart) {
this.displayFailureMessage();
}
Expand Down
27 changes: 17 additions & 10 deletions Src/CSharpier.VSCode/src/CustomPathInstaller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Logger } from "./Logger";
import * as path from "path";
import * as fs from "fs";
import { execSync } from "child_process";
import { workspace } from "vscode";
import { ExecDotNet } from "./DotNetProvider";

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

constructor(logger: Logger) {
constructor(logger: Logger, execDotNet: ExecDotNet) {
this.logger = logger;
this.execDotNet = execDotNet;
this.customPath =
workspace.getConfiguration("csharpier").get<string>("dev.customPath") ?? "";
}
Expand Down Expand Up @@ -38,34 +40,39 @@ export class CustomPathInstaller {

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

return this.validateInstall(pathToDirectoryForVersion, version);
}

private validateInstall(pathToDirectoryForVersion: string, version: string): boolean {
try {
const output = execSync(`"${this.getPathForVersion(version)}" --version`, {
// TODO this fails if we use dotnetPath, we really need to set it as DOTNET_ROOT
const output = this.execDotNet(`"${this.getPathForVersion(version)}" --version`, {
env: { ...process.env, DOTNET_NOLOGO: "1" },
})
.toString()
.trim();

this.logger.debug(`"${this.getPathForVersion(version)}" --version output: ${output}`);
const versionWithoutHash = output.split("+")[0]
this.logger.debug(`Using ${versionWithoutHash} as the version number.`)
const versionWithoutHash = output.split("+")[0];
this.logger.debug(`Using ${versionWithoutHash} as the version number.`);

if (versionWithoutHash === version) {
this.logger.debug("CSharpier at " + pathToDirectoryForVersion + " already exists");
return true;
}
else {
this.logger.warn("Version of " + versionWithoutHash + " did not match expected version of " + version)
} else {
this.logger.warn(
"Version of " +
versionWithoutHash +
" did not match expected version of " +
version,
);
}
} catch (error: any) {
const message = !error.stderr ? error.toString() : error.stderr.toString();
this.logger.warn(
"Exception while running 'dotnet csharpier --version' in " +
"Exception while running 'dotnet-csharpier --version' in " +
pathToDirectoryForVersion,
message,
);
Expand Down
127 changes: 127 additions & 0 deletions Src/CSharpier.VSCode/src/DotNetProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { execSync, ExecSyncOptionsWithBufferEncoding } from "child_process";
import { options } from "./Options";
import { Logger } from "./Logger";
import { getDotnetInfo } from "./vscode-csharp/getDotnetInfo";
import { findDotNetFromRuntimes } from "./vscode-csharp/findDotNetFromRuntimes";

export type ExecDotNet = (
command: string,
options?: ExecSyncOptionsWithBufferEncoding | undefined,
) => Buffer;

export const getExecDotNet = async (logger: Logger): Promise<ExecDotNet | null> => {
return await doDotNetInfoWay(logger);
//
//
// const fileName = process.platform === "win32" ? "dotnet.exe" : "dotnet";
// const env = { ...process.env };
//
// // TODO test without this but with a sym link
// // TODO what about using the sh stuff?
// const dotnetPathOption = options.dotnetPath;
// if (dotnetPathOption.length > 0) {
// env.PATH = dotnetPathOption + path.delimiter + env.PATH;
// logger.debug("including " + options.dotnetPath + " in the path to test for dotnet");
// }
// const dotNetCliPaths = options.dotNetCliPaths;
//
// for (const dotnetPath of dotNetCliPaths) {
// env.PATH = env.PATH + path.delimiter + dotnetPath;
// logger.debug("including " + dotnetPath + " in the path to test for dotnet");
// }
//
// let result: { stdout: string; stderr: string } = { stdout: "", stderr: "" };
//
// try {
// result = await promisify(exec)(`${fileName} --version`, { env });
// } catch (exception) {
// result.stderr = exception as any;
// }
//
// let useSH = false;
// let foundDotnet = true;
// if (result.stderr) {
// logger.warn(`Unable to read dotnet version information. \n ${result.stderr}`);
// useSH = true;
// try {
// result = await promisify(exec)(`sh -c "${fileName} --version"`, { env });
// } catch (exception) {
// result.stderr = exception as any;
// }
// if (result.stderr) {
// logger.warn(
// `Unable to read dotnet version information using "sh -c". Error ${result.stderr}`,
// );
// foundDotnet = false;
// }
// }
//
// if (!foundDotnet) {
// return null;
// }
//
// return (command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer => {
// if (useSH) {
// command = `sh -c "${command}"`;
// }
//
// if (options === undefined) {
// options = {};
// }
//
// if (options.env === undefined) {
// options.env = { ...process.env };
// }
//
// options.env.PATH = env.PATH;
//
// return execSync(command, options);
// };
};

// TODO we need to find dotnet via sh if these two options are not set and we can't find it, probably with runDotnetInfo
async function doDotNetInfoWay(logger: Logger) {
const dotNetCliPaths = options.dotNetCliPaths;
const dotnetPathOption = options.dotnetPath;

const dotnetInfo = await getDotnetInfo([dotnetPathOption, ...dotNetCliPaths]);
logger.info(JSON.stringify(dotnetInfo, null, 4));

let dotnetExecutablePath = dotnetInfo.CliPath;
if (!dotnetExecutablePath) {
dotnetExecutablePath = findDotNetFromRuntimes(dotnetInfo);
}

return (command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer => {
if (options === undefined) {
options = {};
}

return execSync(dotnetExecutablePath + " " + command, options);
};
}

/*
bela@ubuntu-two:~/.dotnet$ find /usr -name "dotnet" -type f
bela@ubuntu-two:~/.dotnet$ find ~ -name "dotnet" -type f
whereis
which
install-script put it at with install script seems to go to ~/.dotnet
gh user has it in /usr/bin, which seems like it should be standard
after running
sudo ln -s ~/.dotnet/dotnet /usr/bin/dotnet
then I get
You must install .NET to run this application.
but it installed just fine
global install has the same problem
You must install .NET to run this application.
was missing DOTNET_ROOT, should try to set it automatically? roslynLanguageServer does
*/
18 changes: 15 additions & 3 deletions Src/CSharpier.VSCode/src/Extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { CSharpierProcessProvider } from "./CSharpierProcessProvider";
import { FormattingService } from "./FormattingService";
import { Logger } from "./Logger";
import { NullCSharpierProcess } from "./CSharpierProcess";
import { getExecDotNet } from "./DotNetProvider";

export async function activate(context: ExtensionContext) {
if (!workspace.isTrusted) {
Expand All @@ -18,13 +19,24 @@ const initPlugin = async (context: ExtensionContext) => {
workspace.getConfiguration("csharpier").get<boolean>("enableDebugLogs") ?? false;

const logger = new Logger(enableDebugLogs);
NullCSharpierProcess.create(logger);

const isDevelopment = (process.env as any).MODE === "development";
const execDotNet = await getExecDotNet(logger);
if (execDotNet === null) {
logger.error(
"CSharpier was unable to find a way to run 'dotnet' commands. Check your PATH or set dotnet.dotnetPath or omnisharp.dotNetCliPaths",
);
return;
}

NullCSharpierProcess.create(logger);

logger.info("Initializing " + (process.env as any).EXTENSION_NAME);

const csharpierProcessProvider = new CSharpierProcessProvider(logger, context.extension);
const csharpierProcessProvider = new CSharpierProcessProvider(
logger,
context.extension,
execDotNet,
);
new FormattingService(logger, csharpierProcessProvider);

context.subscriptions.push(csharpierProcessProvider);
Expand Down
17 changes: 12 additions & 5 deletions Src/CSharpier.VSCode/src/InstallerService.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Logger } from "./Logger";
import { Extension, window, workspace } from "vscode";
import { execSync } from "child_process";
import * as path from "path";
import * as fs from "fs";
import * as vscode from "vscode";
import { ExecDotNet } from "./DotNetProvider";

export class InstallerService {
rejectedError = false;
Expand All @@ -13,11 +13,18 @@ export class InstallerService {
logger: Logger;
killRunningProcesses: () => void;
extension: Extension<unknown>;
execDotNet: ExecDotNet;

constructor(logger: Logger, killRunningProcesses: () => void, extension: Extension<unknown>) {
constructor(
logger: Logger,
killRunningProcesses: () => void,
extension: Extension<unknown>,
execDotNet: ExecDotNet,
) {
this.logger = logger;
this.killRunningProcesses = killRunningProcesses;
this.extension = extension;
this.execDotNet = execDotNet;
}

public displayInstallNeededMessage = (directoryThatContainsFile: string) => {
Expand Down Expand Up @@ -72,7 +79,7 @@ export class InstallerService {
if (selection === globalButton) {
const command = "dotnet tool install -g csharpier";
this.logger.info("Installing csharpier globally with " + command);
const output = execSync(command).toString();
const output = this.execDotNet(command).toString();
this.logger.info(output);
} else if (selection === localButton) {
if (solutionRoot) {
Expand All @@ -83,9 +90,9 @@ export class InstallerService {
);
this.logger.info("Installing csharpier in " + manifestPath);
if (!fs.existsSync(manifestPath)) {
execSync("dotnet new tool-manifest", { cwd: solutionRoot });
this.execDotNet("dotnet new tool-manifest", { cwd: solutionRoot });
}
execSync("dotnet tool install csharpier", { cwd: solutionRoot });
this.execDotNet("dotnet tool install csharpier", { cwd: solutionRoot });
} catch (error) {
this.logger.error("Installing failed with ", error);
}
Expand Down
Loading

0 comments on commit b392e10

Please sign in to comment.