Example #1
0
        public CompilerResult CompileFromPlainText(Participant participant, string code)
        {
            var workingFolder = WorkingFolder.GetWorkingFolder(participant.Id);

            const string fileName = "Solution" + ".java";

            Directory.CreateDirectory(workingFolder);
            var filePath = Path.Combine(workingFolder, fileName);

            File.WriteAllText(filePath, code);
            return(CompilerProcess.Compile(string.Format("\"{0}\"", filePath)));
        }
        /// <summary>
        /// Compile a script (or set of scripts), and return information about
        /// the compilation request.
        /// </summary>
        /// <param name="Filespec">Supplies a filespec of files to compile,
        /// such as *.nss.</param>
        /// <param name="Options">Optionally supplies additional compiler
        /// options (e.g. -e -v1.70).</param>
        /// <param name="ParseLine">Optionally supplies a function to parse
        /// each line printed by the compiler.</param>
        /// <returns>A compiler result descriptor is returned.</returns>
        public static CompilerResult CompileScript(string Filespec, string Options, CompilerParseLineDelegate ParseLine = null)
        {
            CompilerResult Result = new CompilerResult();

            Result.Compiled = false;
            Result.Warnings = new List <string>();
            Result.Errors   = new List <string>();

            try
            {
                //
                // Construct the command line for the compiler instance.
                //

                string           CmdLine = CreateCompilerCommandLine(Filespec, Options);
                Process          CompilerProcess;
                ProcessStartInfo StartInfo = new ProcessStartInfo(GetCompilerExe(), CmdLine);
                string           Line;

                //
                // Start the compiler process and begin reading stdout until
                // end of file is reached.  In the absence of any error
                // messages observed, the operation is assumed to have
                // completed successfully.
                //

                StartInfo.CreateNoWindow         = true;
                StartInfo.UseShellExecute        = false;
                StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                StartInfo.RedirectStandardOutput = true;
                StartInfo.WorkingDirectory       = ALFA.SystemInfo.GetModuleDirectory();

                CompilerProcess = Process.Start(StartInfo);
                Result.Compiled = true;

                while ((Line = CompilerProcess.StandardOutput.ReadLine()) != null)
                {
                    //
                    // Parse each line, examining it for a compiler diagnostic
                    // indicator.  Accumulate errors and warnings into their
                    // respective diagnostic message lists for the caller to
                    // examine.
                    //

                    if (ParseLine != null && ParseLine(Line))
                    {
                        continue;
                    }

                    if (Line.StartsWith("Error:"))
                    {
                        Result.Errors.Add(Line);
                        Result.Compiled = false;
                    }
                    else if (Line.Contains("): Error: NSC"))
                    {
                        Result.Errors.Add(Line);
                        Result.Compiled = false;
                    }
                    else if (Line.StartsWith("Warning:"))
                    {
                        Result.Warnings.Add(Line);
                    }
                    else if (Line.Contains("): Warning: NSC"))
                    {
                        Result.Warnings.Add(Line);
                    }
                }

                CompilerProcess.WaitForExit();
            }
            catch (Exception e)
            {
                Result.Errors.Add(String.Format(
                                      "ALFA.ScriptCompiler.CompileScript({0}, {1}): Exception: '{2}'.",
                                      Filespec,
                                      Options,
                                      e));
                Result.Compiled = false;
            }

            return(Result);
        }