AddCodeFile() private méthode

private AddCodeFile ( string path ) : void
path string
Résultat void
        public override void GenerateCode(AssemblyBuilder assemblyBuilder)
        {
            if (!_codeGenerated)
            {
                GenerateCode();
            }

            TParser parser = Parse();

            OverrideAssemblyPrefix(parser, assemblyBuilder);

            string codeBehindSource = GetCodeBehindSource(parser);

            if (codeBehindSource != null)
            {
                assemblyBuilder.AddCodeFile(codeBehindSource, this, true);
            }

            List <string> refasms = GetReferencedAssemblies(parser);

            if (refasms != null && refasms.Count > 0)
            {
                foreach (string loc in refasms)
                {
                    assemblyBuilder.AddAssemblyReference(loc);
                }
            }

            GenerateCode(assemblyBuilder, parser, _compiler);
        }
Exemple #2
0
        // Build and add the assembly to the BuildManager's
        // CodeAssemblies collection
        public void Build(string[] binAssemblies)
        {
            Type          compilerProvider = null;
            CompilerInfo  compilerInfo = null, cit;
            string        extension, language, cpfile = null;
            List <string> knownfiles   = new List <string>();
            List <string> unknownfiles = new List <string>();

            // First make sure all the files are in the same
            // language
            bool known = false;

            foreach (string f in files)
            {
                known    = true;
                language = null;

                extension = Path.GetExtension(f);
                if (String.IsNullOrEmpty(extension) || !CodeDomProvider.IsDefinedExtension(extension))
                {
                    known = false;
                }
                if (known)
                {
                    language = CodeDomProvider.GetLanguageFromExtension(extension);
                    if (!CodeDomProvider.IsDefinedLanguage(language))
                    {
                        known = false;
                    }
                }
                if (!known || language == null)
                {
                    unknownfiles.Add(f);
                    continue;
                }

                cit = CodeDomProvider.GetCompilerInfo(language);
                if (cit == null || !cit.IsCodeDomProviderTypeValid)
                {
                    continue;
                }
                if (compilerProvider == null)
                {
                    cpfile           = f;
                    compilerProvider = cit.CodeDomProviderType;
                    compilerInfo     = cit;
                }
                else if (compilerProvider != cit.CodeDomProviderType)
                {
                    throw new HttpException(
                              String.Format(
                                  "Files {0} and {1} are in different languages - they cannot be compiled into the same assembly",
                                  Path.GetFileName(cpfile),
                                  Path.GetFileName(f)));
                }
                knownfiles.Add(f);
            }

            CodeDomProvider    provider           = null;
            CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection("system.web/compilation") as CompilationSection;

            if (compilerInfo == null)
            {
                if (!CodeDomProvider.IsDefinedLanguage(compilationSection.DefaultLanguage))
                {
                    throw new HttpException("Failed to retrieve default source language");
                }
                compilerInfo = CodeDomProvider.GetCompilerInfo(compilationSection.DefaultLanguage);
                if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid)
                {
                    throw new HttpException("Internal error while initializing application");
                }
            }

            provider = compilerInfo.CreateProvider();
            if (provider == null)
            {
                throw new HttpException("A code provider error occurred while initializing application.");
            }

            AssemblyBuilder abuilder = new AssemblyBuilder(provider);

            foreach (string file in knownfiles)
            {
                abuilder.AddCodeFile(file);
            }
            foreach (CodeCompileUnit unit in units)
            {
                abuilder.AddCodeCompileUnit(unit);
            }

            BuildProvider      bprovider;
            CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters();

            parameters.IncludeDebugInformation = compilationSection.Debug;

            if (binAssemblies != null && binAssemblies.Length > 0)
            {
                StringCollection parmRefAsm = parameters.ReferencedAssemblies;
                foreach (string binAsm in binAssemblies)
                {
                    if (parmRefAsm.Contains(binAsm))
                    {
                        continue;
                    }

                    parmRefAsm.Add(binAsm);
                }
            }

            if (compilationSection != null)
            {
                foreach (AssemblyInfo ai in compilationSection.Assemblies)
                {
                    if (ai.Assembly != "*")
                    {
                        try {
                            parameters.ReferencedAssemblies.Add(
                                AssemblyPathResolver.GetAssemblyPath(ai.Assembly));
                        } catch (Exception ex) {
                            throw new HttpException(
                                      String.Format("Could not find assembly {0}.", ai.Assembly),
                                      ex);
                        }
                    }
                }

                BuildProviderCollection buildProviders = compilationSection.BuildProviders;

                foreach (string file in unknownfiles)
                {
                    bprovider = GetBuildProviderFor(file, buildProviders);
                    if (bprovider == null)
                    {
                        continue;
                    }
                    bprovider.GenerateCode(abuilder);
                }
            }

            if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0)
            {
                return;
            }

            outputAssemblyName = (string)FileUtils.CreateTemporaryFile(
                AppDomain.CurrentDomain.SetupInformation.DynamicBase,
                name, "dll", OnCreateTemporaryAssemblyFile);
            parameters.OutputAssembly = outputAssemblyName;
            foreach (Assembly a in BuildManager.TopLevelAssemblies)
            {
                parameters.ReferencedAssemblies.Add(a.Location);
            }
            CompilerResults results = abuilder.BuildAssembly(parameters);

            if (results == null)
            {
                return;
            }

            if (results.NativeCompilerReturnValue == 0)
            {
                BuildManager.CodeAssemblies.Add(results.CompiledAssembly);
                BuildManager.TopLevelAssemblies.Add(results.CompiledAssembly);
                HttpRuntime.WritePreservationFile(results.CompiledAssembly, name);
            }
            else
            {
                if (HttpContext.Current.IsCustomErrorEnabled)
                {
                    throw new HttpException("An error occurred while initializing application.");
                }
                throw new CompilationException(null, results.Errors, null);
            }
        }
Exemple #3
0
		// Build and add the assembly to the BuildManager's
		// CodeAssemblies collection
		public void Build (string[] binAssemblies)
		{
			Type compilerProvider = null;
			CompilerInfo compilerInfo = null, cit;
			string extension, language, cpfile = null;
			List<string> knownfiles = new List<string>();
			List<string> unknownfiles = new List<string>();
			
			// First make sure all the files are in the same
			// language
			bool known = false;
			foreach (string f in files) {
				known = true;
				language = null;
				
				extension = Path.GetExtension (f);
				if (String.IsNullOrEmpty (extension) || !CodeDomProvider.IsDefinedExtension (extension))
					known = false;
				if (known) {
					language = CodeDomProvider.GetLanguageFromExtension(extension);
					if (!CodeDomProvider.IsDefinedLanguage (language))
						known = false;
				}
				if (!known || language == null) {
					unknownfiles.Add (f);
					continue;
				}
				
				cit = CodeDomProvider.GetCompilerInfo (language);
				if (cit == null || !cit.IsCodeDomProviderTypeValid)
					continue;
				if (compilerProvider == null) {
					cpfile = f;
					compilerProvider = cit.CodeDomProviderType;
					compilerInfo = cit;
				} else if (compilerProvider != cit.CodeDomProviderType)
					throw new HttpException (
						String.Format (
							"Files {0} and {1} are in different languages - they cannot be compiled into the same assembly",
							Path.GetFileName (cpfile),
							Path.GetFileName (f)));
				knownfiles.Add (f);
			}

			CodeDomProvider provider = null;
			CompilationSection compilationSection = WebConfigurationManager.GetWebApplicationSection ("system.web/compilation") as CompilationSection;
			if (compilerInfo == null) {
				if (!CodeDomProvider.IsDefinedLanguage (compilationSection.DefaultLanguage))
					throw new HttpException ("Failed to retrieve default source language");
				compilerInfo = CodeDomProvider.GetCompilerInfo (compilationSection.DefaultLanguage);
				if (compilerInfo == null || !compilerInfo.IsCodeDomProviderTypeValid)
					throw new HttpException ("Internal error while initializing application");
			}

			provider = compilerInfo.CreateProvider ();
			if (provider == null)
				throw new HttpException ("A code provider error occurred while initializing application.");

			AssemblyBuilder abuilder = new AssemblyBuilder (provider);
			foreach (string file in knownfiles)
				abuilder.AddCodeFile (file);
			foreach (CodeCompileUnit unit in units)
				abuilder.AddCodeCompileUnit (unit);
			
			BuildProvider bprovider;
			CompilerParameters parameters = compilerInfo.CreateDefaultCompilerParameters ();
			parameters.IncludeDebugInformation = compilationSection.Debug;
			
			if (binAssemblies != null && binAssemblies.Length > 0)
				parameters.ReferencedAssemblies.AddRange (binAssemblies);
			
			if (compilationSection != null) {
				foreach (AssemblyInfo ai in compilationSection.Assemblies)
					if (ai.Assembly != "*") {
						try {
							parameters.ReferencedAssemblies.Add (
								AssemblyPathResolver.GetAssemblyPath (ai.Assembly));
						} catch (Exception ex) {
							throw new HttpException (
								String.Format ("Could not find assembly {0}.", ai.Assembly),
								ex);
						}
					}
				
				BuildProviderCollection buildProviders = compilationSection.BuildProviders;
				
				foreach (string file in unknownfiles) {
					bprovider = GetBuildProviderFor (file, buildProviders);
					if (bprovider == null)
						continue;
					bprovider.GenerateCode (abuilder);
				}
			}

			if (knownfiles.Count == 0 && unknownfiles.Count == 0 && units.Count == 0)
				return;
			
			outputAssemblyName = (string)FileUtils.CreateTemporaryFile (
				AppDomain.CurrentDomain.SetupInformation.DynamicBase,
				name, "dll", OnCreateTemporaryAssemblyFile);
			parameters.OutputAssembly = outputAssemblyName;
			foreach (Assembly a in BuildManager.TopLevelAssemblies)
				parameters.ReferencedAssemblies.Add (a.Location);
			CompilerResults results = abuilder.BuildAssembly (parameters);
			if (results == null)
				return;
			
			if (results.NativeCompilerReturnValue == 0) {
				BuildManager.CodeAssemblies.Add (results.CompiledAssembly);
				BuildManager.TopLevelAssemblies.Add (results.CompiledAssembly);
				HttpRuntime.WritePreservationFile (results.CompiledAssembly, name);
			} else {
				if (HttpContext.Current.IsCustomErrorEnabled)
					throw new HttpException ("An error occurred while initializing application.");
				throw new CompilationException (null, results.Errors, null);
			}
		}
Exemple #4
0
		Assembly GetAssemblyFromSource (string vpath)
		{			
			vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
			string realPath = MapPath (vpath, false);
			if (!File.Exists (realPath))
				ThrowParseException ("File " + vpath + " not found");

			AddSourceDependency (vpath);
			
			CompilerResults result;
			string tmp;
			CompilerParameters parameters;
			CodeDomProvider provider = BaseCompiler.CreateProvider (HttpContext.Current, language, out parameters, out tmp);
			if (provider == null)
				throw new HttpException ("Cannot find provider for language '" + language + "'.");
			
			AssemblyBuilder abuilder = new AssemblyBuilder (provider);
			abuilder.CompilerOptions = parameters;
			abuilder.AddAssemblyReference (BuildManager.GetReferencedAssemblies () as List <Assembly>);
			abuilder.AddCodeFile (realPath);
			result = abuilder.BuildAssembly (new VirtualPath (vpath));

			if (result.NativeCompilerReturnValue != 0) {
				using (StreamReader reader = new StreamReader (realPath)) {
					throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
				}
			}

			AddAssembly (result.CompiledAssembly, true);
			return result.CompiledAssembly;
		}