Inheritance: System.ApplicationException
コード例 #1
0
ファイル: Exceptions.cs プロジェクト: m1090c/cs-script
        /// <summary>
        /// Creates the CompilerException instance from the specified compiler errors.
        /// </summary>
        /// <param name="Errors">The compiler errors.</param>
        /// <param name="hideCompilerWarnings">if set to <c>true</c> hide compiler warnings.</param>
        /// <param name="resolveAutogenFilesRefs">if set to <c>true</c> all references to the path of the derived auto-generated files
        /// (e.g. errors in the decorated classless scripts) will be replaced with the path of the original files (e.g. classless script itself).</param>
        /// <returns></returns>
        public static CompilerException Create(IEnumerable <CompilerError> Errors, bool hideCompilerWarnings, bool resolveAutogenFilesRefs)
        {
            var compileErr = new StringBuilder();

            int errorCount = 0;

            foreach (CompilerError err in Errors)
            {
                if (!err.IsWarning)
                {
                    errorCount++;
                }

                if (err.IsWarning && hideCompilerWarnings)
                {
                    continue;
                }

                if (err.FileName.HasText())
                {
                    string file = err.FileName;
                    int    line = err.Line;

                    if (resolveAutogenFilesRefs)
                    {
                        CoreExtensions.NormaliseFileReference(ref file, ref line);
                    }

                    compileErr.Append(file)
                    .Append("(")
                    .Append(line)
                    .Append(",")
                    .Append(err.Column)
                    .Append("): ");
                }
                else
                {
                    compileErr.Append("BUILD: ");
                }

                if (err.IsWarning)
                {
                    compileErr.Append("warning ");
                }
                else
                {
                    compileErr.Append("error ");
                }
                compileErr.Append(err.ErrorNumber)
                .Append(": ")
                .Append(err.ErrorText)
                .Append(Environment.NewLine);
            }

            var retval = new CompilerException(compileErr.ToString());

            retval.Data.Add("Errors", Errors);
            retval.ErrorCount = errorCount;
            return(retval);
        }
コード例 #2
0
ファイル: CompileErrors.cs プロジェクト: Diullei/Storm
    static void ReportCompilerError(CompilerException e)
    {
        CompilerErrorCollection errors = (CompilerErrorCollection)e.Data["Errors"];

        foreach (CompilerError err in errors)
        {
            Console.WriteLine("{0}({1},{2}): {3} {4}: {5}",
                                err.FileName,
                                err.Line,
                                err.Column,
                                err.IsWarning ? "warning" : "error",
                                err.ErrorNumber,
                                err.ErrorText);
        }
    }
コード例 #3
0
        /// <summary>
        /// Creates the CompilerException instance from the specified compiler errors.
        /// </summary>
        /// <param name="Errors">The compiler errors.</param>
        /// <param name="hideCompilerWarnings">if set to <c>true</c> hide compiler warnings.</param>
        /// <returns></returns>
        public static CompilerException Create(CompilerErrorCollection Errors, bool hideCompilerWarnings)
        {
            StringBuilder compileErr = new StringBuilder();

            int errorCount = 0;

            foreach (CompilerError err in Errors)
            {
                if (!err.IsWarning)
                {
                    errorCount++;
                }

                if (err.IsWarning && hideCompilerWarnings)
                {
                    continue;
                }

                //compileErr.Append(err.ToString());
                compileErr.Append(err.FileName);
                compileErr.Append("(");
                compileErr.Append(err.Line);
                compileErr.Append(",");
                compileErr.Append(err.Column);
                compileErr.Append("): ");
                if (err.IsWarning)
                {
                    compileErr.Append("warning ");
                }
                else
                {
                    compileErr.Append("error ");
                }
                compileErr.Append(err.ErrorNumber);
                compileErr.Append(": ");
                compileErr.Append(err.ErrorText);
                compileErr.Append(Environment.NewLine);
            }
            CompilerException retval = new CompilerException(compileErr.ToString());

            retval.Data.Add("Errors", Errors);
            retval.ErrorCount = errorCount;
            return(retval);
        }
コード例 #4
0
        internal static CompilerException Create(string errorText, string file, CompilerException parentException)
        {
            var error = new CompilerError
            {
                FileName  = file,
                ErrorText = errorText
            };

            var errors = (CompilerErrorCollection)parentException.Data["Errors"];

            errors.Insert(0, error);

            CompilerException retval = new CompilerException(error.ToString() + Environment.NewLine + parentException.Message);

            retval.Data.Add("Errors", errors);
            retval.ErrorCount = errors.Count;

            return(retval);
        }
コード例 #5
0
        internal static CompilerException Create(string errorText, string file, CompilerException parentException)
        {
            var error = new CompilerError
            {
                FileName  = file,
                ErrorText = errorText
            };

            var errors = new List <CompilerError>();

            errors.Add(error);
            errors.AddRange((CompilerError[])parentException.Data["Errors"]);

            var retval = new CompilerException(error.ToString() + Environment.NewLine + parentException.Message);

            retval.Data.Add("Errors", errors.ToArray());
            retval.ErrorCount = errors.Count;

            return(retval);
        }
コード例 #6
0
ファイル: Exceptions.cs プロジェクト: gahadzikwa/GAPP
        /// <summary>
        /// Creates the CompilerException instance from the specified compiler errors errors.
        /// </summary>
        /// <param name="Errors">The compiler errors.</param>
        /// <param name="hideCompilerWarnings">if set to <c>true</c> hide compiler warnings.</param>
        /// <returns></returns>
        public static CompilerException Create(CompilerErrorCollection Errors, bool hideCompilerWarnings)
        {
            StringBuilder compileErr = new StringBuilder();
            foreach (CompilerError err in Errors)
            {
                if (err.IsWarning && hideCompilerWarnings)
                    continue;

                //compileErr.Append(err.ToString());
                compileErr.Append(err.FileName);
                compileErr.Append("(");
                compileErr.Append(err.Line);
                compileErr.Append(",");
                compileErr.Append(err.Column);
                compileErr.Append("): ");
                if (err.IsWarning)
                    compileErr.Append("warning ");
                else
                    compileErr.Append("error ");
                compileErr.Append(err.ErrorNumber);
                compileErr.Append(": ");
                compileErr.Append(err.ErrorText);
                compileErr.Append(Environment.NewLine);
            }
            CompilerException retval = new CompilerException(compileErr.ToString());
            retval.Data.Add("Errors", Errors);
            return retval;
        }
コード例 #7
0
        // UnitTest
        // Make Surrogate scenario to compile conditionally
        // + check and delete the exe before building
        // + set Appartment state
        // + update all ExecutionClients incliding csslib
        // + when starting remove css and //x args
        //+ try to solve limitations with console Input redurectionlimi
        //+ ensure launcher is not build when building dll/exe without execution
        public string BuildSurrogateLauncher(string scriptAssembly, string tragetFramework, CompilerParameters compilerParams, ApartmentState appartmentState, string consoleEncoding)
        {
            //Debug.Assert(false);
#if !net4
            throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
            var provider = CodeDomProvider.CreateProvider("C#", new Dictionary <string, string> {
                { "CompilerVersion", tragetFramework }
            });

            compilerParams.OutputAssembly          = GetLauncherName(scriptAssembly);
            compilerParams.GenerateExecutable      = true;
            compilerParams.GenerateInMemory        = false;
            compilerParams.IncludeDebugInformation = false;

            try
            {
                Utils.FileDelete(compilerParams.OutputAssembly, true);
            }
            catch (Exception e)
            {
                throw new ApplicationException("Cannot build surrogate host application", e);
            }

            if (compilerParams.CompilerOptions != null)
            {
                compilerParams.CompilerOptions = compilerParams.CompilerOptions.Replace("/d:TRACE", "")
                                                 .Replace("/d:DEBUG", "");
            }

            if (!AppInfo.appConsole)
            {
                compilerParams.CompilerOptions += " /target:winexe";
            }

            string refAssemblies = "";
            string appartment    = "[STAThread]";
            if (appartmentState == ApartmentState.MTA)
            {
                appartment = "[" + appartmentState + "Thread]";
            }
            else if (appartmentState == ApartmentState.Unknown)
            {
                appartment = "";
            }

            foreach (string asm in compilerParams.ReferencedAssemblies)
            {
                if (File.Exists(asm)) //ignore GAC (not full path) assemblies
                {
                    refAssemblies += Assembly.ReflectionOnlyLoadFrom(asm).FullName + ":" + asm + ";";
                }
            }

            compilerParams.ReferencedAssemblies.Clear(); //it is important to remove all asms as they can have absolute path to the wrong CLR asms branch
            compilerParams.ReferencedAssemblies.Add("System.dll");

            foreach (var item in compilerParams.ReferencedAssemblies)
            {
                Debug.WriteLine(item);
            }

            string setEncodingSatement = "";

            if (string.Compare(consoleEncoding, Settings.DefaultEncodingName, true) != 0)
            {
                setEncodingSatement = "try { Console.OutputEncoding = System.Text.Encoding.GetEncoding(\"" + consoleEncoding + "\"); } catch {}";
            }

            string code = launcherCode
                          .Replace("${REF_ASSEMBLIES}", refAssemblies)
                          .Replace("${APPARTMENT}", appartment)
                          .Replace("${CONSOLE_ENCODING}", setEncodingSatement)
                          .Replace("${ASM_MANE}", Path.GetFileName(scriptAssembly));

            CompilerResults retval;

            compilerParams.IncludeDebugInformation = true;

            bool debugLauncher = false;
            if (debugLauncher)
            {
                compilerParams.CompilerOptions += " /d:DEBUG";

                string launcherFile = Environment.ExpandEnvironmentVariables(@"C:\Users\%USERNAME%\Desktop\New folder\script.launcher.cs");
                File.WriteAllText(launcherFile, code);
                retval = provider.CompileAssemblyFromFile(compilerParams, launcherFile);
            }
            else
            {
                retval = provider.CompileAssemblyFromSource(compilerParams, code);
            }

            foreach (CompilerError err in retval.Errors)
            {
                if (!err.IsWarning)
                {
                    throw CompilerException.Create(retval.Errors, true, false);
                }
            }

            CSSUtils.SetTimestamp(compilerParams.OutputAssembly, scriptAssembly);
            return(compilerParams.OutputAssembly);
#endif
        }
コード例 #8
0
        // UnitTest
        // Make Surrogate scenario to compile conditionally
        // + check and delete the exe before building
        // + set Appartment state
        // + update all ExecutionClients incliding csslib
        // + when starting remove css and //x args
        //+ try to solve limitations with console Input redurectionlimi
        //+ ensure launcher is not build when building dll/exe without execution
        public string BuildSurrogateLauncher(string scriptAssembly, string tragetFramework, CompilerParameters compilerParams, ApartmentState appartmentState)
        {
            //string
#if !net4
            throw new ApplicationException("Cannot build surrogate host application because this script engine is build against early version of CLR.");
#else
            var provider = CodeDomProvider.CreateProvider("C#", new Dictionary <string, string> {
                { "CompilerVersion", tragetFramework }
            });

            compilerParams.OutputAssembly          = GetLauncherName(scriptAssembly);
            compilerParams.GenerateExecutable      = true;
            compilerParams.GenerateInMemory        = false;
            compilerParams.IncludeDebugInformation = false;


            try
            {
                if (File.Exists(compilerParams.OutputAssembly))
                {
                    File.Delete(compilerParams.OutputAssembly);
                }
            }
            catch (Exception e)
            {
                throw new ApplicationException("Cannot build surrogate host application", e);
            }

            if (compilerParams.CompilerOptions != null)
            {
                compilerParams.CompilerOptions = compilerParams.CompilerOptions.Replace("/d:TRACE", "")
                                                 .Replace("/d:DEBUG", "");
            }

            if (!AppInfo.appConsole)
            {
                compilerParams.CompilerOptions += " /target:winexe";
            }

            string refAssemblies = "";
            string appartment    = "[STAThread]";
            if (appartmentState == ApartmentState.MTA)
            {
                appartment = "[" + appartmentState + "Thread]";
            }
            else if (appartmentState == ApartmentState.Unknown)
            {
                appartment = "";
            }

            foreach (string asm in compilerParams.ReferencedAssemblies)
            {
                if (File.Exists(asm)) //ignore GAC (not full path) assemblies
                {
                    refAssemblies += Assembly.ReflectionOnlyLoadFrom(asm).FullName + ":" + asm + ";";
                }
            }

            string code = launcherCode
                          .Replace("${REF_ASSEMBLIES}", refAssemblies)
                          .Replace("${APPARTMENT}", appartment)
                          .Replace("${ASM_MANE}", Path.GetFileName(scriptAssembly));

            CompilerResults retval;

            bool debugLauncher = false;
            if (debugLauncher)
            {
                compilerParams.IncludeDebugInformation = true;
                compilerParams.CompilerOptions        += " /d:DEBUG";
                //string launcherFile = @"C:\Users\OSH\Desktop\New folder (2)\script.launcher.cs";
                string launcherFile = Path.GetTempFileName();
                File.WriteAllText(launcherFile, code);
                retval = provider.CompileAssemblyFromFile(compilerParams, launcherFile);
            }
            else
            {
                retval = provider.CompileAssemblyFromSource(compilerParams, code);
            }


            if (retval.Errors.Count != 0)
            {
                throw CompilerException.Create(retval.Errors, true);
            }

            CSSUtils.SetTimestamp(compilerParams.OutputAssembly, scriptAssembly);
            return(compilerParams.OutputAssembly);
#endif
        }