Example #1
0
        private Task CompileAsync()
        {
            var tcs = new TaskCompletionSource <object>();

            try
            {
                string inputDirectory = Path.GetDirectoryName(_inputFilePath);

                var startInfo = new ProcessStartInfo
                {
                    FileName = _options.ToolPath,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true,
                    UseShellExecute        = false,
                    ErrorDialog            = false,
                    WorkingDirectory       = inputDirectory,
                    Arguments = _options.ToArgumentString(_inputFilePath)
                };

                var process = new Process {
                    StartInfo = startInfo
                };
                process.ErrorDataReceived  += ProcessDataReceived;
                process.OutputDataReceived += ProcessDataReceived;
                process.EnableRaisingEvents = true;
                process.Exited += (s, e) =>
                {
                    process.WaitForExit();
                    process.Close();

                    if (_diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
                    {
                        tcs.SetException(new CompilationErrorException("Compilation failed.", GetDiagnostics()));
                    }
                    else
                    {
                        tcs.SetResult(null);
                    }
                };

                process.Start();

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }
            catch (Exception exc)
            {
                tcs.SetException(exc);
            }

            return(tcs.Task);
        }
        public Task<ImmutableArray<Diagnostic>> CompileAsync(string inputFile, TypeScriptCompilationOptions options)
        {
            var tcs = new TaskCompletionSource<ImmutableArray<Diagnostic>>();

            try
            {
                var diagnostics = new List<Diagnostic>();
                string inputDirectory = Path.GetDirectoryName(inputFile);

                var startInfo = new ProcessStartInfo
                {
                    FileName = options.ToolPath,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    ErrorDialog = false,
                    WorkingDirectory = inputDirectory,
                    Arguments = options.ToArgumentString(inputFile)
                };

                void ProcessDataReceived(object sender, DataReceivedEventArgs e)
                {
                    if (e.Data != null && TryParseDiagnostic(e.Data, out Diagnostic diagnostic))
                    {
                        diagnostics.Add(diagnostic);
                    }
                }

                var process = new Process { StartInfo = startInfo };
                process.ErrorDataReceived += ProcessDataReceived;
                process.OutputDataReceived += ProcessDataReceived;
                process.EnableRaisingEvents = true;
                process.Exited += (s, e) =>
                {
                    process.WaitForExit();
                    process.Close();

                    var diagnosticResult = ImmutableArray.Create(diagnostics.ToArray());
                    if (diagnosticResult.Any(d => d.Severity == DiagnosticSeverity.Error))
                    {
                        tcs.SetException(new CompilationErrorException("Compilation failed.", diagnosticResult));
                    }
                    else
                    {
                        tcs.SetResult(diagnosticResult);
                    }
                };

                process.Start();

                process.BeginErrorReadLine();
                process.BeginOutputReadLine();
            }
            catch (Exception exc)
            {
                tcs.SetException(exc);
            }

            return tcs.Task;
        }