コード例 #1
0
        public CompileResult Compile(CompileSource sourceCode, string workingDirectory)
        {
            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }

            CreateFile(sourceCode, workingDirectory);

            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceCode.FileName);
            var fileExtension            = Path.GetExtension(sourceCode.FileName).Substring(1);

            var options = CompilerOptionsTemplate.Replace(TemplateKeys.FileName, fileNameWithoutExtension)
                          .Replace(TemplateKeys.FileNameExtension, fileExtension);

            this.logger.Info($"Compile options: {options}");

            var startInfo = new ProcessStartInfo(CompilerPath, options)
            {
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory,
                CreateNoWindow         = true,
                ErrorDialog            = false
            };

            var output = new StringBuilder();
            int exitCode;

            using (var p = new Process {
                StartInfo = startInfo
            })
            {
                p.OutputDataReceived += (s, e) =>
                {
                    output.AppendLine(e.Data);
                };

                p.ErrorDataReceived += (s, e) =>
                {
                    output.AppendLine(e.Data);
                };

                p.Start();
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                p.WaitForExit(30 * 1000);
                exitCode = p.ExitCode;
            }

            if (exitCode == 0)
            {
                return(CompileResult.Success(output.ToString(), OutputFileTemplate.Replace(TemplateKeys.FileName, fileNameWithoutExtension)));
            }

            return(CompileResult.Error(output.ToString()));
        }
コード例 #2
0
        public CompileResult Compile(CompileSource sourceCode, string workingDirectory)
        {
            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }

            CreateFile(sourceCode, workingDirectory);

            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(sourceCode.FileName);
            var fileExtension            = Path.GetExtension(sourceCode.FileName).Substring(1);

            var options = CompilerOptionsTemplate.Replace(TemplateKeys.FileName, fileNameWithoutExtension)
                          .Replace(TemplateKeys.FileNameExtension, fileExtension);

            var startInfo = new ProcessStartInfo(CompilerPath, options)
            {
                UseShellExecute        = false,
                RedirectStandardOutput = true,
                RedirectStandardError  = true,
                WorkingDirectory       = workingDirectory,
                CreateNoWindow         = true,
                ErrorDialog            = false
            };

            string output;
            int    exitCode;

            using (var p = new Process {
                StartInfo = startInfo
            })
            {
                p.Start();

                output = p.StandardOutput.ReadToEnd();
                if (string.IsNullOrWhiteSpace(output))
                {
                    output = p.StandardError.ReadToEnd();
                }
                p.WaitForExit(30 * 1000);
                exitCode = p.ExitCode;
            }

            if (exitCode == 0)
            {
                return(CompileResult.Success(output, OutputFileTemplate.Replace(TemplateKeys.FileName, fileNameWithoutExtension)));
            }

            return(CompileResult.Error(output));
        }