Пример #1
0
 public static void ValidateCorrectCompilationResult(CompileResult result)
 {
     Assert.AreEqual(result.Compiled,true);
 }
Пример #2
0
 public static void ValidateIncorrectCompilationResult(CompileResult result)
 {
     Assert.AreEqual(result.Compiled, false);
     Assert.AreNotEqual(result.StandartOutput, "");
 }
Пример #3
0
        /// <summary>
        /// Compiles provided source file into *.exe
        /// </summary>
        /// 
        /// <param name="sourceFilePath">
        /// Path of source to compile.
        /// </param>
        /// 
        /// <returns>
        /// <see cref="CompileResult"/> class that represents the compilation result.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="sourceFilePath"/> is null.
        /// </exception>
        /// <exception cref="FileNotFoundException">
        /// If <paramref name="sourceFilePath"/> is incorrect.
        /// </exception>
        public CompileResult Compile(string sourceFilePath)
        {
            var compilingJava = (Name == Language.Java6);
            
            ProjectHelper.ValidateFileExists(sourceFilePath, "sourceFilePath");
            
            sourceFilePath = Path.GetFullPath(sourceFilePath);

            try
            {
                using (var process = new Process())
                {
                    //get shot pathes for compilers (some of them dont' work with long names)
                    var shortLocation = ToShortPathName(Path.GetDirectoryName(Location));
                    var shortSourceFilePath = compilingJava ? sourceFilePath : ToShortPathName(sourceFilePath);

                    //set compilation arguments
                    process.StartInfo.FileName = Location;
                    process.StartInfo.Arguments =
                        Arguments.Replace(CompilerDirectory, shortLocation).Replace(SourceFilePath, shortSourceFilePath);
                    process.StartInfo.WorkingDirectory = Path.GetDirectoryName(sourceFilePath);

                    //set up process info nad start
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.Start();

                    //get output and error info
                    var standardOutput = process.StandardOutput.ReadToEnd();
                    var standardError = process.StandardError.ReadToEnd();
                    
                    process.WaitForExit();

                    var compiled = compilingJava
                                       ? File.Exists(Path.ChangeExtension(sourceFilePath, "class"))
                                       : File.Exists(Path.ChangeExtension(sourceFilePath, "exe"));


                    //create and return the result of compilation
                    var result = new CompileResult(compiled, standardOutput, standardError);
                    
                    return result;
                }
            }
            catch (Exception e)
            {
                throw new CompileException("Error occurred during compilation", e);
            }
        }