Beispiel #1
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)
        {
            bool compilingJava = Name == Language.Java6;
            ProjectHelper.ValidateFileExists(sourceFilePath, "sourceFilePath");
            sourceFilePath = Path.GetFullPath(sourceFilePath);

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

                    //set compilation arguments
                    process.StartInfo.FileName = location;
                    process.StartInfo.Arguments = arguments.
                        Replace(Compiler.CompilerDirectory, shortLocation).
                        Replace(Compiler.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
                    string standardOutput = process.StandardOutput.ReadToEnd();
                    string standardError = process.StandardError.ReadToEnd();
                    process.WaitForExit();

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


                    //create and return the result of compilation
                    CompileResult result = new CompileResult(compiled, standardOutput, standardError);
                    return result;
                }
            }
            catch (Exception e)
            {
                throw new CompileException("Error occurred during compilation", e);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Tests provided program.
        /// </summary>
        ///
        /// <param name="program">
        /// Program to test.
        /// </param>
        ///
        /// <returns>
        /// Result of program testing.
        /// </returns>
        public Result TestProgram(Program program)
        {
            //search for apropriate compiler
            Compile.Compiler currentCompiler = null;
            foreach (Compile.Compiler compiler in Settings.Compilers)
            {
                if (compiler.Name == program.Language)
                {
                    currentCompiler = compiler;
                    break;
                }
            }
            //if no compiler found - throw the exception
            if (currentCompiler == null)
            {
                throw new Compile.CompileException("No compiler found for specified language");
            }

            //write source into a file
            string programDirectory = Path.Combine(Settings.TestingDirectory, ProgramName);

            Directory.CreateDirectory(programDirectory);

            string programPath = Path.Combine(programDirectory, ProgramName + "." + currentCompiler.Extension);

            if (program.Language == Language.Java6)
            {
                const string classStr     = "class";
                const string packageStr   = "package";
                int          packageIndex = program.Source.IndexOf(packageStr);
                if (packageIndex != -1)
                {
                    try
                    {
                        program.Source = program.Source.Remove
                                             (packageIndex, program.Source.IndexOf(";", packageIndex) - packageIndex + 1);
                    }
                    catch
                    {
                    }
                }
                string className;
                try
                {
                    className = program.Source.Substring(
                        program.Source.IndexOf(classStr) + classStr.Length,
                        program.Source.IndexOf("{", program.Source.IndexOf(classStr)) - (program.Source.IndexOf(classStr) + classStr.Length)
                        );
                    className = className.Trim();
                }
                catch
                {
                    className = ProgramName;
                }
                foreach (char invalidChar in Path.GetInvalidFileNameChars())
                {
                    if (className.Contains(invalidChar.ToString()))
                    {
                        className = ProgramName;
                        break;
                    }
                }


                programPath = Path.Combine(programDirectory, className + "." + currentCompiler.Extension);
            }
            StreamWriter writer = new StreamWriter(programPath);

            writer.Write(program.Source);
            writer.Close();

            //copmile the source
            Compile.CompileResult compileResult = currentCompiler.Compile(programPath);

            Result testResult = null;

            if (compileResult.Compiled)
            {
                testResult = Runner.Execute(Path.ChangeExtension(programPath, "exe"), program);
            }
            else
            {
                testResult = new Result();
                testResult.ProgramStatus = Status.CompilationError;
                testResult.Output        = compileResult.StandartOutput;
            }


            try
            {
                Directory.Delete(programDirectory, true);
            }
            catch
            { }
            return(testResult);
        }