CreateCodeFile() public method

public CreateCodeFile ( System.Web.Compilation.BuildProvider buildProvider ) : TextWriter
buildProvider System.Web.Compilation.BuildProvider
return System.IO.TextWriter
Beispiel #1
0
        /// <summary>
        /// Generate code for one .datasvcmap file
        /// </summary>
        /// <param name="mapFilePath">The physical path to the data service map file</param>
        private void GenerateCodeFromDataServiceMapFile(string mapFilePath, AssemblyBuilder assemblyBuilder)
        {
            try
            {
                assemblyBuilder.AddAssemblyReference(typeof(System.Data.Services.Client.DataServiceContext).Assembly);

                DataSvcMapFileLoader loader  = new DataSvcMapFileLoader(mapFilePath);
                DataSvcMapFile       mapFile = loader.LoadMapFile() as DataSvcMapFile;

                if (mapFile.MetadataList[0].ErrorInLoading != null)
                {
                    throw mapFile.MetadataList[0].ErrorInLoading;
                }

                string edmxContent = mapFile.MetadataList[0].Content;

                System.Data.Services.Design.EntityClassGenerator generator = new System.Data.Services.Design.EntityClassGenerator(LanguageOption.GenerateCSharpCode);

                // the EntityClassGenerator works on streams/writers, does not return a CodeDom
                // object, so we use CreateCodeFile instead of compile units.
                using (TextWriter writer = assemblyBuilder.CreateCodeFile(this))
                {
                    // Note: currently GenerateCode never actually returns values
                    // for the error case (even though it returns an IList of error
                    // objects). Instead it throws on error. This may need some tweaking
                    // later on.
#if DEBUG
                    object errors =
#endif
                    generator.GenerateCode(
                        XmlReader.Create(new StringReader(edmxContent)),
                        writer,
                        GetGeneratedNamespace());

#if DEBUG
                    Debug.Assert(
                        errors == null ||
                        !(errors is ICollection) ||
                        ((ICollection)errors).Count == 0,
                        "Errors reported through the return value. Expected an exception");
#endif
                    writer.Flush();
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                errorMessage = String.Format(CultureInfo.CurrentCulture, "{0}: {1}", IO.Path.GetFileName(mapFilePath), errorMessage);
                throw new InvalidOperationException(errorMessage, ex);
            }
        }
Beispiel #2
0
        protected override void GenerateCode(AssemblyBuilder assemblyBuilder, SimpleWebHandlerParser parser, BaseCompiler compiler)
        {
            if (assemblyBuilder == null || parser == null)
            {
                return;
            }

            string programCode = parser.Program.Trim();

            if (String.IsNullOrEmpty(programCode))
            {
                _needLoadFromBin = true;
                return;
            }

            _needLoadFromBin = false;
            using (TextWriter writer = assemblyBuilder.CreateCodeFile(this))
                writer.WriteLine(programCode);
        }
        // see http://msdn.microsoft.com/en-us/library/system.web.compilation.assemblybuilder.createcodefile%28v=vs.90%29.ASPX
        // there _is_ a way to create source code files, so we _could_ use the TextBuilder
        // but here we're assuming that going for CodeDom is faster (?)

        // no, wait -- anyway CodeDomBuilder will never work because umbraco is not
        // up and running at the time GenerateCode is invoked, so the code files have
        // to be generated beforehand and the builder should just insert them in the
        // right place...

        // can it generate them in app_code?
        // idea would be to drop a models.models file in App_Code that would trigger the build
        // this is for ppl who don't have visual studio, anyway... if you have VS you want to
        // build with VS and then copy the code over to the server, based upon models.cs

        // the actual files generation has to be triggered by the UI

        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            // issue: I can't put my files into App_Code or they'll get... compiled... too soon?

            if (!Config.EnableAppCodeModels)
                throw new Exception("Building models from App_Code is not enabled, yet a .models file was found in App_Code.");

            var appData = HostingEnvironment.MapPath("~/App_Data");
            if (appData == null)
                throw new Exception("Could not map path ~/App_Data.");
            if (!Directory.Exists(appData))
                throw new Exception("Could not find ~/App_Data.");
            var modelsDirectory = Path.Combine(appData, "Models");
            if (!Directory.Exists(modelsDirectory))
                return;

            foreach (var file in Directory.GetFiles(modelsDirectory, "*.cs"))
            {
                var text = File.ReadAllText(file);
                var textWriter = assemblyBuilder.CreateCodeFile(this);
                textWriter.Write(text);
                textWriter.Close();
            }
        }
        /// <summary>
        /// Generate code for one .datasvcmap file
        /// </summary>
        /// <param name="mapFilePath">The physical path to the data service map file</param>
        private void GenerateCodeFromDataServiceMapFile(string mapFilePath, AssemblyBuilder assemblyBuilder)
        {
            try
            {
                assemblyBuilder.AddAssemblyReference(typeof(System.Data.Services.Client.DataServiceContext).Assembly);

                DataSvcMapFileLoader loader = new DataSvcMapFileLoader(mapFilePath);
                DataSvcMapFile mapFile = loader.LoadMapFile() as DataSvcMapFile;

                if (mapFile.MetadataList[0].ErrorInLoading != null)
                {
                    throw mapFile.MetadataList[0].ErrorInLoading;
                }

                string edmxContent = mapFile.MetadataList[0].Content;

                System.Data.Services.Design.EntityClassGenerator generator = new System.Data.Services.Design.EntityClassGenerator(LanguageOption.GenerateCSharpCode);

                // the EntityClassGenerator works on streams/writers, does not return a CodeDom
                // object, so we use CreateCodeFile instead of compile units.
                using (TextWriter writer = assemblyBuilder.CreateCodeFile(this))
                {

                    // Note: currently GenerateCode never actually returns values
                    // for the error case (even though it returns an IList of error
                    // objects). Instead it throws on error. This may need some tweaking 
                    // later on.
#if DEBUG
                object errors = 
#endif
                    generator.GenerateCode(
                        XmlReader.Create(new StringReader(edmxContent)),
                        writer,
                        GetGeneratedNamespace());

#if DEBUG
                Debug.Assert(
                    errors == null ||
                    !(errors is ICollection) ||
                    ((ICollection)errors).Count == 0,
                    "Errors reported through the return value. Expected an exception");
#endif
                    writer.Flush();
                }
            }
            catch (Exception ex)
            {
                string errorMessage = ex.Message;
                errorMessage = String.Format(CultureInfo.CurrentCulture, "{0}: {1}", IO.Path.GetFileName(mapFilePath), errorMessage);
                throw new InvalidOperationException(errorMessage, ex);
            }
        }
 void WriteCodeFile(AssemblyBuilder assemblyBuilder, BuildProvider buildProvider, string name)
 {
     using (TextWriter codeFile = assemblyBuilder.CreateCodeFile(buildProvider))
     {
         foreach (string line in File.ReadLines(name))
         {
             codeFile.WriteLine(line);
         }
     }
 }
		/// <summary>
		/// Generate the code from .apgen file.
		/// </summary>
		/// <param name="assemblyBuilder">AssemblyBuilder</param>
		public override void GenerateCode(AssemblyBuilder assemblyBuilder)
		{
			if (!flag && BuildManager.CodeAssemblies != null && getProfileMethodCount > 0)
			{
				getProfileMethodCount = 0;
				flag = true;
			}

			string path = Path.Combine(
				HttpRuntime.AppDomainAppPath,
				VirtualPath.Substring(HttpRuntime.AppDomainAppVirtualPath.Length + (HttpRuntime.AppDomainAppVirtualPath.Length == 1 ? 0 : 1)));

			using (TextWriter writer = assemblyBuilder.CreateCodeFile(this))
			{
				APGen gen = APGenManager.OpenGenDocument(path);
				CodeDomProvider provider = assemblyBuilder.CodeDomProvider;
				provider.GenerateCodeFromCompileUnit(gen.Generate(), writer, null);

				writer.Flush();
				writer.Close();
			}
		}