|  | 
|  | 1 | +import { ChildProcessWithoutNullStreams, spawn } from "child_process"; | 
|  | 2 | +import { Logger } from "./Logger"; | 
|  | 3 | +import { FormatFileParameter, FormatFileResult, ICSharpierProcess2 } from "./ICSharpierProcess"; | 
|  | 4 | + | 
|  | 5 | +export class CSharpierProcessServer implements ICSharpierProcess2 { | 
|  | 6 | +    private csharpierPath: string; | 
|  | 7 | +    private logger: Logger; | 
|  | 8 | +    private port: number = 0; | 
|  | 9 | +    private process: ChildProcessWithoutNullStreams; | 
|  | 10 | +    private processFailedToStart = false; | 
|  | 11 | +    private version: string; | 
|  | 12 | + | 
|  | 13 | +    constructor(logger: Logger, csharpierPath: string, workingDirectory: string, version: string) { | 
|  | 14 | +        this.logger = logger; | 
|  | 15 | +        this.csharpierPath = csharpierPath; | 
|  | 16 | +        this.process = this.spawnProcess(csharpierPath, workingDirectory); | 
|  | 17 | +        this.version = version; | 
|  | 18 | + | 
|  | 19 | +        this.logger.debug("Warm CSharpier with initial format"); | 
|  | 20 | +        // warm by formatting a file twice, the 3rd time is when it gets really fast | 
|  | 21 | +        this.formatFile("public class ClassName { }", "/Temp/Test.cs"); | 
|  | 22 | +        this.formatFile("public class ClassName { }", "/Temp/Test.cs"); | 
|  | 23 | +    } | 
|  | 24 | + | 
|  | 25 | +    private spawnProcess( | 
|  | 26 | +        csharpierPath: string, | 
|  | 27 | +        workingDirectory: string, | 
|  | 28 | +    ): ChildProcessWithoutNullStreams { | 
|  | 29 | +        const csharpierProcess = spawn(csharpierPath, ["--server"], { | 
|  | 30 | +            stdio: "pipe", | 
|  | 31 | +            cwd: workingDirectory, | 
|  | 32 | +            env: { ...process.env, DOTNET_NOLOGO: "1" }, | 
|  | 33 | +        }); | 
|  | 34 | + | 
|  | 35 | +        let output = ""; | 
|  | 36 | +        const regex = /^Started on (\d+)/; | 
|  | 37 | + | 
|  | 38 | +        // TODO look at pipe / https://github.com/belav/csharpier/pull/1127/files#diff-844ad2632d6fd1e83ce639f4f1ef23e8dad4f547b7a524b9e1ed715feff04958 | 
|  | 39 | +        csharpierProcess.stdout.on("data", chunk => { | 
|  | 40 | +            this.logger.debug("Got chunk of size " + chunk.length); | 
|  | 41 | +            output += chunk; | 
|  | 42 | +            this.logger.debug("Got " + output); | 
|  | 43 | +            if (regex.test(output)) { | 
|  | 44 | +                this.port = parseInt(output.match(regex)![1], 10); | 
|  | 45 | +            } | 
|  | 46 | +        }); | 
|  | 47 | + | 
|  | 48 | +        return csharpierProcess; | 
|  | 49 | +    } | 
|  | 50 | + | 
|  | 51 | +    // private void StartProcess() | 
|  | 52 | +    // { | 
|  | 53 | +    //     try | 
|  | 54 | +    //     { | 
|  | 55 | +    //         var processStartInfo = new ProcessStartInfo(this.csharpierPath, "--server") | 
|  | 56 | +    //         { | 
|  | 57 | +    //             RedirectStandardOutput = true, | 
|  | 58 | +    //                 RedirectStandardError = true, | 
|  | 59 | +    //                 UseShellExecute = false, | 
|  | 60 | +    //                 CreateNoWindow = true, | 
|  | 61 | +    //                 Environment = { ["DOTNET_NOLOGO"] = "1" } | 
|  | 62 | +    //         }; | 
|  | 63 | +    //         this.process = Process.Start(processStartInfo); | 
|  | 64 | +    // | 
|  | 65 | +    //         var output = string.Empty; | 
|  | 66 | +    // | 
|  | 67 | +    //         var task = Task.Run(() => | 
|  | 68 | +    //         { | 
|  | 69 | +    //             output = this.process!.StandardOutput.ReadLine(); | 
|  | 70 | +    //         }); | 
|  | 71 | +    // | 
|  | 72 | +    //         if (!task.Wait(TimeSpan.FromSeconds(2))) | 
|  | 73 | +    //         { | 
|  | 74 | +    //             this.logger.Warn( | 
|  | 75 | +    //                 "Spawning the csharpier server timed out. Formatting cannot occur." | 
|  | 76 | +    //             ); | 
|  | 77 | +    //             this.process!.Kill(); | 
|  | 78 | +    //             return; | 
|  | 79 | +    //         } | 
|  | 80 | +    // | 
|  | 81 | +    //         if (this.process!.HasExited) | 
|  | 82 | +    //         { | 
|  | 83 | +    //             this.logger.Warn( | 
|  | 84 | +    //                 "Spawning the csharpier server failed because it exited. " | 
|  | 85 | +    //                 + this.process!.StandardError.ReadToEnd() | 
|  | 86 | +    //             ); | 
|  | 87 | +    //             this.ProcessFailedToStart = true; | 
|  | 88 | +    //             return; | 
|  | 89 | +    //         } | 
|  | 90 | +    // | 
|  | 91 | +    //         var portString = output.Replace("Started on ", ""); | 
|  | 92 | +    //         this.port = int.Parse(portString); | 
|  | 93 | +    // | 
|  | 94 | +    //         this.logger.Debug("Connecting via port " + portString); | 
|  | 95 | +    //     } | 
|  | 96 | +    //     catch (Exception e) | 
|  | 97 | +    //     { | 
|  | 98 | +    //         this.logger.Warn("Failed to spawn the needed csharpier server." + e); | 
|  | 99 | +    //         this.ProcessFailedToStart = true; | 
|  | 100 | +    //     } | 
|  | 101 | +    // } | 
|  | 102 | + | 
|  | 103 | +    public async formatFile(content: string, filePath: string): Promise<string> { | 
|  | 104 | +        const parameter = { | 
|  | 105 | +            fileName: filePath, | 
|  | 106 | +            fileContents: content, | 
|  | 107 | +        }; | 
|  | 108 | +        const result = await this.formatFile2(parameter); | 
|  | 109 | +        return result?.formattedFile ?? ""; | 
|  | 110 | +    } | 
|  | 111 | + | 
|  | 112 | +    public async formatFile2(parameter: FormatFileParameter): Promise<FormatFileResult | null> { | 
|  | 113 | +        if (this.processFailedToStart) { | 
|  | 114 | +            this.logger.warn("CSharpier process failed to start. Formatting cannot occur."); | 
|  | 115 | +            return null; | 
|  | 116 | +        } | 
|  | 117 | + | 
|  | 118 | +        const url = "http://localhost:" + this.port + "/format"; | 
|  | 119 | + | 
|  | 120 | +        try { | 
|  | 121 | +            // var request = (HttpWebRequest)WebRequest.Create(url); | 
|  | 122 | +            // request.Method = "POST"; | 
|  | 123 | +            // request.ContentType = "application/json; charset=utf-8"; | 
|  | 124 | +            // | 
|  | 125 | +            // using (var streamWriter = new StreamWriter(request.GetRequestStream())) | 
|  | 126 | +            // { | 
|  | 127 | +            //     streamWriter.Write(JsonConvert.SerializeObject(parameter)); | 
|  | 128 | +            // } | 
|  | 129 | +            // | 
|  | 130 | +            // var response = (HttpWebResponse)request.GetResponse(); | 
|  | 131 | +            // | 
|  | 132 | +            // if (response.StatusCode != HttpStatusCode.OK) | 
|  | 133 | +            // { | 
|  | 134 | +            //     this.logger.Warn( | 
|  | 135 | +            //         "Csharpier server returned non-200 status code of " + response.StatusCode | 
|  | 136 | +            //     ); | 
|  | 137 | +            //     response.Close(); | 
|  | 138 | +            //     return null; | 
|  | 139 | +            // } | 
|  | 140 | +            // | 
|  | 141 | +            // using (var streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) | 
|  | 142 | +            // { | 
|  | 143 | +            //     var result = JsonConvert.DeserializeObject<FormatFileResult>( | 
|  | 144 | +            //         streamReader.ReadToEnd() | 
|  | 145 | +            //     ); | 
|  | 146 | +            //     return result; | 
|  | 147 | +            // } | 
|  | 148 | +        } catch (e) { | 
|  | 149 | +            this.logger.warn("Failed posting to the csharpier server. " + e); | 
|  | 150 | +        } | 
|  | 151 | + | 
|  | 152 | +        return null; | 
|  | 153 | +    } | 
|  | 154 | + | 
|  | 155 | +    dispose() { | 
|  | 156 | +        (this.process.stdin as any).pause(); | 
|  | 157 | +        this.process.kill(); | 
|  | 158 | +    } | 
|  | 159 | + | 
|  | 160 | +    getVersion(): string { | 
|  | 161 | +        return this.version; | 
|  | 162 | +    } | 
|  | 163 | +} | 
0 commit comments