示例#1
0
        public string GenerateCode(out CompilerErrorCollection errors)
        {
            errors = new CompilerErrorCollection();

            var engine = new RewritingRazorTemplateEngine(this, new PreprocessedAttributeRewriter());

            // Generate code
            GeneratorResults results;

            try {
                Stream stream = File.OpenRead(FullPath);
                using (var reader = new StreamReader(stream, Encoding.Default, true)) {
                    results = engine.GenerateCode(reader, DefaultClassName, DefaultNamespace, FullPath);
                }
            } catch (Exception e) {
                errors.Add(new CompilerError(FullPath, 1, 1, null, e.ToString()));
                //Returning null signifies that generation has failed
                return(null);
            }

            // Output errors
            foreach (RazorError error in results.ParserErrors)
            {
                errors.Add(new CompilerError(FullPath, error.Location.LineIndex + 1, error.Location.CharacterIndex + 1, null, error.Message));
            }

            try {
                using (var writer = new StringWriter()) {
                    writer.WriteLine("#pragma warning disable 1591");
                    _codeDomProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, codeGeneratorOptions);
                    writer.WriteLine("#pragma warning restore 1591");
                    string s = writer.ToString();
                    return(MakeLineDirectivePathsRelative(Path.GetDirectoryName(FullPath), s));
                }
            } catch (Exception e) {
                errors.Add(new CompilerError(FullPath, 1, 1, null, e.ToString()));
                //Returning null signifies that generation has failed
                return(null);
            }
        }
		public string GenerateCode (out CompilerErrorCollection errors)
		{
			errors = new CompilerErrorCollection ();

			var engine = new RewritingRazorTemplateEngine (this, new PreprocessedAttributeRewriter ());

			// Generate code
			GeneratorResults results;
			try {
				Stream stream = File.OpenRead (FullPath);
				using (var reader = new StreamReader (stream, Encoding.Default, true)) {
					results = engine.GenerateCode (reader, DefaultClassName, DefaultNamespace, FullPath);
				}
			} catch (Exception e) {
				errors.Add (new CompilerError (FullPath, 1, 1, null, e.ToString ()));
				//Returning null signifies that generation has failed
				return null;
			}

			// Output errors
			foreach (RazorError error in results.ParserErrors) {
				errors.Add (new CompilerError (FullPath, error.Location.LineIndex + 1, error.Location.CharacterIndex + 1, null, error.Message));
			}

			try {
				using (var writer = new StringWriter ()) {
					writer.WriteLine ("#pragma warning disable 1591");
					_codeDomProvider.GenerateCodeFromCompileUnit (results.GeneratedCode, writer, codeGeneratorOptions);
					writer.WriteLine ("#pragma warning restore 1591");
					string s = writer.ToString ();
					return MakeLineDirectivePathsRelative (Path.GetDirectoryName (FullPath), s);
				}
			} catch (Exception e) {
				errors.Add (new CompilerError (FullPath, 1, 1, null, e.ToString ()));
				//Returning null signifies that generation has failed
				return null;
			}
		}