-
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes #1109
- Loading branch information
Showing
8 changed files
with
301 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
import { ChildProcessWithoutNullStreams, spawn } from "child_process"; | ||
import { Logger } from "./Logger"; | ||
import { FormatFileParameter, FormatFileResult, ICSharpierProcess2 } from "./ICSharpierProcess"; | ||
|
||
export class CSharpierProcessServer implements ICSharpierProcess2 { | ||
private csharpierPath: string; | ||
private logger: Logger; | ||
private port: number = 0; | ||
private process: ChildProcessWithoutNullStreams; | ||
private processFailedToStart = false; | ||
private version: string; | ||
|
||
constructor(logger: Logger, csharpierPath: string, workingDirectory: string, version: string) { | ||
this.logger = logger; | ||
this.csharpierPath = csharpierPath; | ||
this.process = this.spawnProcess(csharpierPath, workingDirectory); | ||
this.version = version; | ||
|
||
this.logger.debug("Warm CSharpier with initial format"); | ||
// warm by formatting a file twice, the 3rd time is when it gets really fast | ||
this.formatFile("public class ClassName { }", "/Temp/Test.cs"); | ||
this.formatFile("public class ClassName { }", "/Temp/Test.cs"); | ||
} | ||
|
||
private spawnProcess( | ||
csharpierPath: string, | ||
workingDirectory: string, | ||
): ChildProcessWithoutNullStreams { | ||
const csharpierProcess = spawn(csharpierPath, ["--server"], { | ||
stdio: "pipe", | ||
cwd: workingDirectory, | ||
env: { ...process.env, DOTNET_NOLOGO: "1" }, | ||
}); | ||
|
||
let output = ""; | ||
const regex = /^Started on (\d+)/; | ||
|
||
// TODO look at pipe / https://github.com/belav/csharpier/pull/1127/files#diff-844ad2632d6fd1e83ce639f4f1ef23e8dad4f547b7a524b9e1ed715feff04958 | ||
csharpierProcess.stdout.on("data", chunk => { | ||
this.logger.debug("Got chunk of size " + chunk.length); | ||
output += chunk; | ||
this.logger.debug("Got " + output); | ||
if (regex.test(output)) { | ||
this.port = parseInt(output.match(regex)![1], 10); | ||
} | ||
}); | ||
|
||
return csharpierProcess; | ||
} | ||
|
||
// private void StartProcess() | ||
// { | ||
// try | ||
// { | ||
// var processStartInfo = new ProcessStartInfo(this.csharpierPath, "--server") | ||
// { | ||
// RedirectStandardOutput = true, | ||
// RedirectStandardError = true, | ||
// UseShellExecute = false, | ||
// CreateNoWindow = true, | ||
// Environment = { ["DOTNET_NOLOGO"] = "1" } | ||
// }; | ||
// this.process = Process.Start(processStartInfo); | ||
// | ||
// var output = string.Empty; | ||
// | ||
// var task = Task.Run(() => | ||
// { | ||
// output = this.process!.StandardOutput.ReadLine(); | ||
// }); | ||
// | ||
// if (!task.Wait(TimeSpan.FromSeconds(2))) | ||
// { | ||
// this.logger.Warn( | ||
// "Spawning the csharpier server timed out. Formatting cannot occur." | ||
// ); | ||
// this.process!.Kill(); | ||
// return; | ||
// } | ||
// | ||
// if (this.process!.HasExited) | ||
// { | ||
// this.logger.Warn( | ||
// "Spawning the csharpier server failed because it exited. " | ||
// + this.process!.StandardError.ReadToEnd() | ||
// ); | ||
// this.ProcessFailedToStart = true; | ||
// return; | ||
// } | ||
// | ||
// var portString = output.Replace("Started on ", ""); | ||
// this.port = int.Parse(portString); | ||
// | ||
// this.logger.Debug("Connecting via port " + portString); | ||
// } | ||
// catch (Exception e) | ||
// { | ||
// this.logger.Warn("Failed to spawn the needed csharpier server." + e); | ||
// this.ProcessFailedToStart = true; | ||
// } | ||
// } | ||
|
||
public async formatFile(content: string, filePath: string): Promise<string> { | ||
const parameter = { | ||
fileName: filePath, | ||
fileContents: content, | ||
}; | ||
const result = await this.formatFile2(parameter); | ||
return result?.formattedFile ?? ""; | ||
} | ||
|
||
public async formatFile2(parameter: FormatFileParameter): Promise<FormatFileResult | null> { | ||
if (this.processFailedToStart) { | ||
this.logger.warn("CSharpier process failed to start. Formatting cannot occur."); | ||
return null; | ||
} | ||
|
||
const url = "http://localhost:" + this.port + "/format"; | ||
|
||
try { | ||
// var request = (HttpWebRequest)WebRequest.Create(url); | ||
// request.Method = "POST"; | ||
// request.ContentType = "application/json; charset=utf-8"; | ||
// | ||
// using (var streamWriter = new StreamWriter(request.GetRequestStream())) | ||
// { | ||
// streamWriter.Write(JsonConvert.SerializeObject(parameter)); | ||
// } | ||
// | ||
// var response = (HttpWebResponse)request.GetResponse(); | ||
// | ||
// if (response.StatusCode != HttpStatusCode.OK) | ||
// { | ||
// this.logger.Warn( | ||
// "Csharpier server returned non-200 status code of " + response.StatusCode | ||
// ); | ||
// response.Close(); | ||
// return null; | ||
// } | ||
// | ||
// using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) | ||
// { | ||
// var result = JsonConvert.DeserializeObject<FormatFileResult>( | ||
// streamReader.ReadToEnd() | ||
// ); | ||
// return result; | ||
// } | ||
} catch (e) { | ||
this.logger.warn("Failed posting to the csharpier server. " + e); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
dispose() { | ||
(this.process.stdin as any).pause(); | ||
this.process.kill(); | ||
} | ||
|
||
getVersion(): string { | ||
return this.version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.