Ejemplo n.º 1
0
 static TypeScriptCompilationService()
 {
     DefaultCompilationOptions = new TypeScriptCompilationOptions
     {
         ToolPath = GetToolPath(),
         OutDir   = ".output"
     };
 }
        public async Task Emit_ReturnsExpectedPath()
        {
            var options = new TypeScriptCompilationOptions
            {
                OutDir  = "outdir",
                RootDir = @"c:\root\directory",
                Target  = "test.ts"
            };

            string inputFile   = @"c:\root\directory\functionname\inputfile.ts";
            var    compilation = await TypeScriptCompilation.CompileAsync(inputFile, options, new TestCompiler());

            string result = await compilation.EmitAsync(CancellationToken.None);

            Assert.Equal(@"c:\root\directory\functionname\outdir\functionname\inputfile.js", result);
        }
 public Task <ImmutableArray <Diagnostic> > CompileAsync(string inputFile, TypeScriptCompilationOptions options)
 {
     return(Task.FromResult(ImmutableArray <Diagnostic> .Empty));
 }
 public TypeScriptCompilationService(TypeScriptCompilationOptions compilationOptions)
 {
     _compilationOptions = compilationOptions;
 }
        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;
        }
        public static async Task <TypeScriptCompilation> CompileAsync(string inputFile, TypeScriptCompilationOptions options, ITypeScriptCompiler compiler = null)
        {
            var compilation = new TypeScriptCompilation(inputFile, options, compiler ?? _defaultCompiler.Value);
            await compilation.CompileAsync();

            return(compilation);
        }
 private TypeScriptCompilation(string inputFilePath, TypeScriptCompilationOptions options, ITypeScriptCompiler compiler)
 {
     _compiler      = compiler;
     _inputFilePath = inputFilePath;
     _options       = options;
 }
Ejemplo n.º 8
0
 private TypeScriptCompilation(string inputFilePath, TypeScriptCompilationOptions options)
 {
     _inputFilePath = inputFilePath;
     _options       = options;
 }
Ejemplo n.º 9
0
        public static async Task <TypeScriptCompilation> CompileAsync(string inputFile, TypeScriptCompilationOptions options)
        {
            var compilation = new TypeScriptCompilation(inputFile, options);
            await compilation.CompileAsync();

            return(compilation);
        }