예제 #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
        private static void CreateFile(CompileSource sourceCode, string workingDirectory)
        {
            var filePath = Path.Combine(workingDirectory, sourceCode.FileName);

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            using (var sw = new StreamWriter(filePath))
            {
                sw.Write(sourceCode.SourceCode);
            }
        }
예제 #3
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));
        }