예제 #1
0
        /// <summary>
        /// Initialises a new instance of <see cref="TemplateException"/>
        /// </summary>
        /// <param name="errors">The collection of compilation errors.</param>
        internal TemplateException(CompilerErrorCollection errors)
            : base("Unable to compile template.")
        {
            var list = errors.Cast <CompilerError>().ToList();

            Errors = new ReadOnlyCollection <CompilerError>(list);
        }
        /// <summary>
        /// Initialises a new instance of <see cref="TemplateCompilationException"/>
        /// </summary>
        /// <param name="errors">The collection of compilation errors.</param>
        public TemplateCompilationException(CompilerErrorCollection errors)
            : base("Unable to compile template. Check the Errors list for details.")
        {
            var list = errors.Cast <CompilerError>().ToList();

            Errors = new ReadOnlyCollection <CompilerError>(list);
        }
            private static string FormatMessage(CompilerErrorCollection errors)
            {
                var message = "One or more template processing errors occurred: " + Environment.NewLine;

                message += string.Join(Environment.NewLine, errors.Cast <CompilerError>().Select(x => x.ToString()));
                return(message);
            }
예제 #4
0
        /// <summary>
        /// Converts a <see cref="CompilerErrorCollection"/> to a collection of <see cref="BindingExpressionCompilationError"/> objects.
        /// </summary>
        private static List <BindingExpressionCompilationError> CreateBindingExpressionCompilationErrors(LegacyExpressionCompilerState state,
                                                                                                         IEnumerable <DataSourceWrapperInfo> models, CompilerErrorCollection errors)
        {
            var result = new List <BindingExpressionCompilationError>();

            var workingDirectory = state.GetWorkingDirectory();

            var errorsByFile = errors.Cast <CompilerError>()
                               .Where(x => !x.IsWarning).GroupBy(x => Path.GetFileName(x.FileName)).ToDictionary(x => x.Key, x => x.ToList());

            foreach (var model in models)
            {
                var dataSourceWrapperFilename = Path.GetFileName(state.GetWorkingFileForDataSourceWrapper(model));
                var dataSourceErrors          = default(List <CompilerError>);
                if (errorsByFile.TryGetValue(dataSourceWrapperFilename, out dataSourceErrors))
                {
                    foreach (var dataSourceError in dataSourceErrors)
                    {
                        var fullPathToFile = model.DataSourceWrapperName;
                        if (state.WriteErrorsToFile)
                        {
                            fullPathToFile = Path.GetFullPath(Path.Combine(workingDirectory,
                                                                           Path.ChangeExtension(model.DataSourceWrapperName, "cs")));
                        }

                        result.Add(new BindingExpressionCompilationError(fullPathToFile,
                                                                         dataSourceError.Line, dataSourceError.Column, dataSourceError.ErrorNumber, dataSourceError.ErrorText));
                    }
                }
            }

            return(result);
        }
예제 #5
0
        /// <summary>
        /// Gets a exact error message of the given error collection
        /// </summary>
        /// <param name="errors"></param>
        /// <param name="sourceCode"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        internal static string GetMessage(CompilerErrorCollection errors, string sourceCode, string template)
        {
            var errorMsgs = string.Join("\n\t", errors.Cast <CompilerError>().Select(error => string.Format(" - {0}: ({1}, {2}) {3}", error.IsWarning ? "warning" : "error", error.Line, error.Column, error.ErrorText)));

            return
                (string.Format("Unable to compile template. You can find the generated source code in {0}. \n\t{1}", sourceCode, errorMsgs));
        }
예제 #6
0
        private void displayError(CompilerErrorCollection errors)
        {
#if Display_Error
            if (errors.HasErrors)
            {
                errors.Cast <CompilerError>().ToList().ForEach(x => Console.WriteLine(x));
            }
#endif
        }
        /// <summary>
        ///     Initialises a new instance of <see cref="TemplateCompilationException" />.
        /// </summary>
        /// <param name="errors">The set of compiler errors.</param>
        /// <param name="sourceCode">The source code that wasn't compiled.</param>
        /// <param name="template">The source template that wasn't compiled.</param>
        internal TemplateCompilationException(CompilerErrorCollection errors, string sourceCode, string template)
            : base("Unable to compile template. " + errors[0].ErrorText + "\n\nOther compilation errors may have occurred. Check the Errors property for more information.")
        {
            var list = errors.Cast <CompilerError>().ToList();

            Errors     = new ReadOnlyCollection <CompilerError>(list);
            SourceCode = sourceCode;
            Template   = template;
        }
예제 #8
0
        /// <summary>
        /// Initialises a new instance of <see cref="TemplateCompilationException"/>.
        /// </summary>
        /// <param name="errors">The set of compiler errors.</param>
        /// <param name="sourceCode">The source code that wasn't compiled.</param>
        /// <param name="template">The source template that wasn't compiled.</param>
        internal TemplateCompilationException(CompilerErrorCollection errors, string sourceCode, string template)
            : base(TemplateCompilationException.GetMessage(errors, sourceCode, template))
        {
            var list = errors.Cast <CompilerError>().ToList();

            Errors     = new ReadOnlyCollection <CompilerError>(list);
            SourceCode = sourceCode;
            Template   = template;
        }
예제 #9
0
        private static void AssertSingleError(CompilerErrorCollection errors, params string[] keywords)
        {
            var error = errors.Cast <CompilerError>().Single();

            foreach (string keyword in keywords)
            {
                StringAssert.Contains(error.ErrorText, keyword);
            }
        }
예제 #10
0
 public CompilationResult(string body, CompilerErrorCollection compilerErrors)
 {
     Body           = body;
     CompilerErrors = compilerErrors.Cast <CompilerError>()
                      .OrderBy(i => i.Line)
                      .ThenBy(i => i.Column)
                      .Select(i => new CompilerError(i.FileName, i.Line + CompileHelper.ErrorLineOffset,
                                                     i.Column + CompileHelper.ErrorColumnOffset, i.ErrorNumber, i.ErrorText))
                      .ToList();
 }
예제 #11
0
            public void LogErrors(CompilerErrorCollection errors)
            {
                HasErrors = errors.HasErrors;

                Errors = errors
                         .Cast <CompilerError>()
                         .Select(e => new TemplateError(e.IsWarning, $"Compile {(e.IsWarning ? "warning" : "error")} in {e.FileName}({e.Line},{e.Column}): {e.ErrorText}",
                                                        e.Line, e.Column))
                         .ToImmutableArray();
            }
        public static void HandleErrors(this CompilerErrorCollection errors, string message)
        {
            Contract.Requires(errors != null);
            Contract.Requires(!string.IsNullOrWhiteSpace(message));

            if (errors.HasErrors)
            {
                throw new CompilerErrorException(
                          message,
                          errors.Cast <CompilerError>().ToList());
            }
        }
        public static void HandleErrors(this CompilerErrorCollection errors, string message)
        {
            DebugCheck.NotNull(errors);
            DebugCheck.NotEmpty(message);

            if (errors.HasErrors)
            {
                throw new CompilerErrorException(
                          message,
                          errors.Cast <CompilerError>().ToList());
            }
        }
예제 #14
0
 public CompilerException(CompilerErrorCollection errors) : base("Error compiling code. See Errors property for details.")
 {
     this.Errors = errors
                   .Cast <CompilerError>()
                   .Select(error => new CompilerFail(
                               error.FileName,
                               error.Line,
                               error.Column,
                               error.ErrorNumber,
                               error.ErrorText,
                               error.IsWarning))
                   .ToArray();
 }
예제 #15
0
        private static void Return_Errors(CompilerErrorCollection errors)
        {
            var message           = "Ошибка компиляции" + Environment.NewLine;
            var compilerErrorList = errors.Cast <CompilerError>();

            foreach (var compilerError in compilerErrorList)
            {
                message += string.Format(Environment.NewLine + "line{0}: {1}",
                                         compilerError.Line - CodeCreation.UsingsCount, compilerError.ErrorText);
            }

            MessageBox.Show(message);
        }
        private void AssertSomeConsumerAssemblyCompilationErrors(string consumerCode, string apiAssemblyPath, string expectedErrorSubstring)
        {
            var results = CompileAssembly("ConsumerAssembly.dll", consumerCode, apiAssemblyPath);

            CompilerErrorCollection errors = results.Item1.Errors;

            Assert.Greater(errors.Count, 0);

            var compilerErrors = errors.Cast <CompilerError>().ToArray();

            Assert.True(
                compilerErrors.Any(x => x.ToString().Contains(expectedErrorSubstring)),
                string.Format("Could not find expected error text substring '{0}' in errors collection: {1}", expectedErrorSubstring, ConvertCompilerErrorMessagesToString(errors)));
        }
예제 #17
0
        /// <summary>
        /// Initializes a new instance.
        /// </summary>
        /// <param name="errors"></param>
        internal CompilerErrorException(CompilerErrorCollection errors, string code)
            : base(errors[0].ErrorText)
        {
            if (errors == null)
            {
                throw new ArgumentNullException(nameof(errors));
            }
            if (code == null)
            {
                throw new ArgumentNullException(nameof(code));
            }

            this.errors = errors.Cast <CompilerError>();
            this.code   = code;
        }
예제 #18
0
        public void DisposeReportsInputFileInErrorsWhenSessionHasInputFile()
        {
            const string            InputFile    = "TestInput.cs";
            CompilerErrorCollection actualErrors = null;

            using (var transformation = new FakeTransformation())
                using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment))
                {
                    transformation.Session[TransformationContext.InputFileNameKey] = InputFile;
                    transformation.Host.LoggedErrors = errors => actualErrors = errors;
                    context.Disposed += delegate { throw new TransformationException(); };
                }

            Assert.AreEqual(InputFile, actualErrors.Cast <CompilerError>().Single().FileName);
        }
예제 #19
0
        public void DisposeLogsTransformationExceptionsThrownByOutputFileManager()
        {
            CompilerErrorCollection actualErrors = null;

            using (var transformation = new FakeTransformation())
                using (var context = new TransformationContext(transformation, transformation.GenerationEnvironment))
                {
                    transformation.Host.UpdatedOutputFiles = (input, outputs) => { throw new TransformationException(TestText); };
                    transformation.Host.LoggedErrors       = errors => actualErrors = errors;
                    context.Write(new OutputItem {
                        File = TestFile
                    }, TestText);
                }

            var error = actualErrors.Cast <CompilerError>().Single();

            Assert.AreEqual(TestText, error.ErrorText);
        }
예제 #20
0
        public void ConstructorReportsErrorWhenParameterCannotBeInitializedBecauseSessionIsNull()
        {
            using (var transformation = new FakeTransformationWithStringParameter())
            {
                const string ExpectedValue = "TestValue";
                const string ParameterName = "StringParameter";
                transformation.Host.GetMetadataValue = (hierarchy, fileName, metadataName) => metadataName == ParameterName ? ExpectedValue : null;

                CompilerErrorCollection loggedErrors = null;
                transformation.Host.LoggedErrors = errors => loggedErrors = errors;

                transformation.Session = null;
                using (new TransformationContext(transformation, transformation.GenerationEnvironment))
                {
                    Assert.IsNotNull(loggedErrors);
                    StringAssert.Contains(loggedErrors.Cast <CompilerError>().Single().ErrorText, ParameterName);
                }
            }
        }
예제 #21
0
 private static IEnumerable <CompilerError> ToEnumerable(CompilerErrorCollection collection)
 {
     return(collection == null ? null : collection.Cast <CompilerError>().ToList());
 }
예제 #22
0
        /// <summary>
        /// Compiles the C# code into a in memory DLL.
        /// </summary>
        /// <returns></returns>
        internal bool Compile()
        {
            CSharpCodeProvider compiler = new CSharpCodeProvider(new Dictionary <String, String> {
                { "CompilerVersion", "v4.0" }
            });
            CompilerParameters parms = new CompilerParameters {
                GenerateExecutable = false, GenerateInMemory = true, TreatWarningsAsErrors = false
            };

            foreach (string assembly in _referencedAssemblies)
            {
                parms.ReferencedAssemblies.Add(assembly);
            }

#if DEBUG
            parms.IncludeDebugInformation = true;
            parms.CompilerOptions        += "/debug:pdbonly";
#endif
            CompilerResults results = compiler.CompileAssemblyFromFile(parms, _csFileNames.ToArray());

            CompilerErrorCollection errs = results.Errors;
            bool hardError = false;

            foreach (CompilerError ce in errs.Cast <CompilerError>().Where(ce => !ce.IsWarning))
            {
                hardError = true;
            }

            if (hardError)
            {
                lock (Omni.self.tick)
                    Omni.self.Error(">>csFactory::Error Processing C# Source Code.");
                foreach (CompilerError ce in errs)
                {
                    lock (Omni.self.tick)
                        Omni.self.Error(String.Format(">>csFactory::File {0} Line {1} Column {2} MSG {3} ", ce.FileName, ce.Line, ce.Column, ce.ErrorText, ce.FileName));
                }
                return(false);
            }

            CompiledAssembly = results.CompiledAssembly;

            if (ScriptType == ScriptTypes.Classes)
            {
                return(true);
            }

            Type type = CompiledAssembly.GetType(_className);

            //Updates the Function overrides with the function ptrs.
            if (type == null)
            {
                return(true);
            }
            foreach (CustomClassFunctDef fo in _functionOverrides)
            {
                MethodInfo mi = type.GetMethod(fo.mFunctName);
                if (mi != null)
                {
                    if (!mi.IsStatic)
                    {
                        lock (Omni.self.tick)
                            Omni.self.Error(">>csFactory::Error Processing C# Source Code: Function Not Static:'" + fo.mFunctName + "'.");
                        return(false);
                    }
                    fo.mFunctPtr = mi;
                }
                else
                {
                    lock (Omni.self.tick)
                        Omni.self.Error(">>csFactory::Error Processing C# Source Code: Could Not Find Function:'" + fo.mFunctName + "'.");
                    return(false);
                }
            }

            return(true);
        }
예제 #23
0
 protected static string ConvertCompilerErrorMessagesToString(CompilerErrorCollection compilerErrorCollection)
 {
     return(string.Join(Environment.NewLine, compilerErrorCollection.Cast <CompilerError>().Select(x => x.ToString())));
 }
 public ConfigurationCompilationException(ConfigurationFileInfo configuration, CompilerErrorCollection compilationErrors)
     : this(configuration, compilationErrors.Cast <CompilerError>())
 {
 }
예제 #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TemplateCompileException"/> class.
 /// </summary>
 /// <param name="errors">The compiler errors.</param>
 public TemplateCompileException(CompilerErrorCollection errors) :
     base(string.Format("{0} error{1} occured during template compilation.", errors.Count, errors.Count == 1 ? string.Empty : "s"))
 {
     this.CompilerErrors = errors.Cast <CompilerError>().ToList().AsReadOnly();
 }
예제 #26
0
 internal TemplateCompilationException(CompilerErrorCollection errorCollection)
     : base(GetMessage(errorCollection))
 {
     _errors = new ReadOnlyCollection <CompilerError>(errorCollection.Cast <CompilerError>().ToArray());
 }
예제 #27
0
 private bool GetIsError(CompilerErrorCollection compilerErrors)
 {
     return(compilerErrors.Cast <CompilerError>().Any(error => !error.IsWarning));
 }
예제 #28
0
 public static IEnumerable <CompilerError> AsEnumerable(this CompilerErrorCollection collection)
 {
     return(collection.Cast <CompilerError>());
 }