Beispiel #1
0
        // Add the referenced assemblies into the compileParameters. Notice that buildProviders do not have
        // the correct referenced assemblies and we don't cache them since the assemblies could change
        // between appdomains. (removing assemblies from bin, etc)
        private void FixupReferencedAssemblies(VirtualPath virtualPath, CompilerParameters compilerParameters)
        {
            CompilationSection compConfig = MTConfigUtil.GetCompilationConfig(virtualPath);

            ICollection referencedAssemblies = BuildManager.GetReferencedAssemblies(compConfig);
            Util.AddAssembliesToStringCollection(referencedAssemblies, compilerParameters.ReferencedAssemblies);
        }
 internal WebDirectoryBatchCompiler(VirtualDirectory vdir)
 {
     this._vdir                 = vdir;
     this._utcStart             = DateTime.UtcNow;
     this._compConfig           = MTConfigUtil.GetCompilationConfig(this._vdir.VirtualPath);
     this._referencedAssemblies = BuildManager.GetReferencedAssemblies(this._compConfig);
 }
 internal BuildProvidersCompiler(VirtualPath configPath, bool supportLocalization, string generatedFilesDir, int index)
 {
     this._configPath           = configPath;
     this._supportLocalization  = supportLocalization;
     this._compConfig           = MTConfigUtil.GetCompilationConfig(this._configPath);
     this._referencedAssemblies = BuildManager.GetReferencedAssemblies(this.CompConfig, index);
     this._generatedFilesDir    = generatedFilesDir;
 }
 internal BuildProvidersCompiler(VirtualPath configPath, bool supportLocalization, string outputAssemblyName)
 {
     this._configPath           = configPath;
     this._supportLocalization  = supportLocalization;
     this._compConfig           = MTConfigUtil.GetCompilationConfig(this._configPath);
     this._referencedAssemblies = BuildManager.GetReferencedAssemblies(this.CompConfig);
     this._outputAssemblyName   = outputAssemblyName;
 }
        private System.Web.Compilation.BuildProvider GetCompilerParamsAndBuildProvider(VirtualPath virtualPath, out Type codeDomProviderType, out CompilerParameters compilerParameters)
        {
            virtualPath.CombineWithAppRoot();
            CompilationSection compilationConfig    = MTConfigUtil.GetCompilationConfig(virtualPath);
            ICollection        referencedAssemblies = BuildManager.GetReferencedAssemblies(compilationConfig);

            System.Web.Compilation.BuildProvider provider = null;
            if (StringUtil.EqualsIgnoreCase(virtualPath.VirtualPathString, BuildManager.GlobalAsaxVirtualPath.VirtualPathString))
            {
                ApplicationBuildProvider provider2 = new ApplicationBuildProvider();
                provider2.SetVirtualPath(virtualPath);
                provider2.SetReferencedAssemblies(referencedAssemblies);
                provider = provider2;
            }
            else
            {
                provider = BuildManager.CreateBuildProvider(virtualPath, compilationConfig, referencedAssemblies, true);
            }
            provider.IgnoreParseErrors       = true;
            provider.IgnoreControlProperties = true;
            provider.ThrowOnFirstParseError  = false;
            CompilerType codeCompilerType = provider.CodeCompilerType;

            if (codeCompilerType == null)
            {
                codeDomProviderType = null;
                compilerParameters  = null;
                return(null);
            }
            codeDomProviderType = codeCompilerType.CodeDomProviderType;
            compilerParameters  = codeCompilerType.CompilerParameters;
            IAssemblyDependencyParser assemblyDependencyParser = provider.AssemblyDependencyParser;

            if ((assemblyDependencyParser != null) && (assemblyDependencyParser.AssemblyDependencies != null))
            {
                Util.AddAssembliesToStringCollection(assemblyDependencyParser.AssemblyDependencies, compilerParameters.ReferencedAssemblies);
            }
            AssemblyBuilder.FixUpCompilerParameters(codeDomProviderType, compilerParameters);
            return(provider);
        }
        internal CompilerResults BuildAssembly(VirtualPath virtualPath, CompilerParameters options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            options.TempFiles = temp_files;
            if (options.OutputAssembly == null)
            {
                options.OutputAssembly = OutputAssemblyName;
            }

            ProcessPartialTypes();

            CompilerResults results;

            CodeUnit [] units = GetUnitsAsArray();

            // Since we may have some source files and some code
            // units, we generate code from all of them and then
            // compile the assembly from the set of temporary source
            // files. This also facilates possible debugging for the
            // end user, since they get the code beforehand.
            List <string> files = SourceFiles;
            Dictionary <string, string> resources = ResourceFiles;

            if (units.Length == 0 && files.Count == 0 && resources.Count == 0 && options.EmbeddedResources.Count == 0)
            {
                return(null);
            }

            string compilerOptions = options.CompilerOptions;

            if (options.IncludeDebugInformation)
            {
                if (String.IsNullOrEmpty(compilerOptions))
                {
                    compilerOptions = "/d:DEBUG";
                }
                else if (compilerOptions.IndexOf("d:DEBUG", StringComparison.OrdinalIgnoreCase) == -1)
                {
                    compilerOptions += " /d:DEBUG";
                }

                options.CompilerOptions = compilerOptions;
            }

            if (String.IsNullOrEmpty(compilerOptions))
            {
                compilerOptions = "/noconfig";
            }
            else if (compilerOptions.IndexOf("noconfig", StringComparison.OrdinalIgnoreCase) == -1)
            {
                compilerOptions += " /noconfig";
            }
            options.CompilerOptions = compilerOptions;

            string       filename;
            StreamWriter sw = null;

            foreach (CodeUnit unit in units)
            {
                filename = GetTempFilePhysicalPath(provider.FileExtension);
                try {
                    sw = new StreamWriter(File.OpenWrite(filename), Encoding.UTF8);
                    provider.GenerateCodeFromCompileUnit(unit.Unit, sw, null);
                    files.Add(filename);
                } catch {
                    throw;
                } finally {
                    if (sw != null)
                    {
                        sw.Flush();
                        sw.Close();
                    }
                }

                if (unit.BuildProvider != null)
                {
                    AddPathToBuilderMap(filename, unit.BuildProvider);
                }
            }

            foreach (KeyValuePair <string, string> de in resources)
            {
                options.EmbeddedResources.Add(de.Value);
            }

            AddAssemblyReference(BuildManager.GetReferencedAssemblies());
            var assembliesToReference  = new List <Assembly> ();
            var moduleGuidCache        = new Dictionary <Guid, bool> ();
            StringCollection optRefAsm = options.ReferencedAssemblies;

            ReferenceAssemblies(moduleGuidCache, assembliesToReference, ReferencedAssemblies);
            ReferenceAssemblies(moduleGuidCache, assembliesToReference, optRefAsm);
            Type appType = HttpApplicationFactory.AppType;

            if (appType != null)
            {
                ReferenceAssembly(moduleGuidCache, assembliesToReference, appType.Assembly);
            }

            optRefAsm.Clear();
            foreach (Assembly refasm in assembliesToReference)
            {
                string path         = new Uri(refasm.CodeBase).LocalPath;
                string originalPath = refasm.Location;
                if (!optRefAsm.Contains(path) && !optRefAsm.Contains(originalPath))
                {
                    optRefAsm.Add(path);
                }
            }

            results = provider.CompileAssemblyFromFile(options, files.ToArray());

            if (results.NativeCompilerReturnValue != 0)
            {
                string fileText = null;
                CompilerErrorCollection errors = results.Errors;
                try {
                    if (errors != null && errors.Count > 0)
                    {
                        using (StreamReader sr = File.OpenText(results.Errors [0].FileName))
                            fileText = sr.ReadToEnd();
                    }
                } catch (Exception) {}

#if DEBUG
                Console.Error.WriteLine("********************************************************************");
                Console.Error.WriteLine("Compilation failed.");
                Console.Error.WriteLine("Output:");
                foreach (string s in results.Output)
                {
                    Console.Error.WriteLine("  " + s);
                }
                Console.Error.WriteLine("\nErrors:");
                foreach (CompilerError err in results.Errors)
                {
                    Console.Error.WriteLine(err);
                }
                if (errors != null && errors.Count > 0)
                {
                    Console.Error.WriteLine("File name: {0}", results.Errors [0].FileName);
                }
                else
                {
                    Console.Error.WriteLine("File name not available");
                }
                if (!String.IsNullOrEmpty(fileText))
                {
                    Console.Error.WriteLine("File text:\n{0}\n", fileText);
                }
                else
                {
                    Console.Error.WriteLine("No file text available");
                }
                Console.Error.WriteLine("********************************************************************");
#endif
                throw new CompilationException(virtualPath != null ? virtualPath.Original : String.Empty, results, fileText);
            }

            Assembly assembly = results.CompiledAssembly;
            if (assembly == null)
            {
                if (!File.Exists(options.OutputAssembly))
                {
                    results.TempFiles.Delete();
                    throw new CompilationException(virtualPath != null ? virtualPath.Original : String.Empty, results.Errors,
                                                   "No assembly returned after compilation!?");
                }

                try {
                    results.CompiledAssembly = Assembly.LoadFrom(options.OutputAssembly);
                } catch (Exception ex) {
                    results.TempFiles.Delete();
                    throw new HttpException("Unable to load compiled assembly", ex);
                }
            }

            if (!KeepFiles)
            {
                results.TempFiles.Delete();
            }
            return(results);
        }
        private BuildProvider GetCompilerParamsAndBuildProvider(VirtualPath virtualPath,
                                                                out Type codeDomProviderType, out CompilerParameters compilerParameters)
        {
            virtualPath.CombineWithAppRoot();

            CompilationSection compConfig = MTConfigUtil.GetCompilationConfig(virtualPath);

            ICollection referencedAssemblies = BuildManager.GetReferencedAssemblies(compConfig);

            // Create the buildprovider for the passed in virtualPath
            BuildProvider buildProvider = null;

            // Special case global asax build provider here since we do not want to compile every files with ".asax" extension.
            if (StringUtil.EqualsIgnoreCase(virtualPath.VirtualPathString, BuildManager.GlobalAsaxVirtualPath.VirtualPathString))
            {
                ApplicationBuildProvider provider = new ApplicationBuildProvider();
                provider.SetVirtualPath(virtualPath);
                provider.SetReferencedAssemblies(referencedAssemblies);
                buildProvider = provider;
            }
            else
            {
                buildProvider = BuildManager.CreateBuildProvider(virtualPath, compConfig,
                                                                 referencedAssemblies, true /*failIfUnknown*/);
            }

            // DevDiv 69017
            // The methods restricted to internalBuildProvider have been moved up to BuildProvider
            // to allow WCFBuildProvider to support .svc syntax highlighting.

            // Ignore parse errors, since they should not break the designer
            buildProvider.IgnoreParseErrors = true;

            // Ignore all control properties, since we do not generate code for the properties
            buildProvider.IgnoreControlProperties = true;

            // Process as many errors as possible, do not rethrow on first error
            buildProvider.ThrowOnFirstParseError = false;

            // Get the language (causes the file to be parsed)
            CompilerType compilerType = buildProvider.CodeCompilerType;

            // compilerType could be null in the no-compile case (VSWhidbey 221749)
            if (compilerType == null)
            {
                codeDomProviderType = null;
                compilerParameters  = null;
                return(null);
            }

            // Return the provider type and compiler params
            codeDomProviderType = compilerType.CodeDomProviderType;
            compilerParameters  = compilerType.CompilerParameters;

            IAssemblyDependencyParser parser = buildProvider.AssemblyDependencyParser;

            // Add all the assemblies that the page depends on (e.g. user controls)
            if (parser != null && parser.AssemblyDependencies != null)
            {
                Util.AddAssembliesToStringCollection(parser.AssemblyDependencies,
                                                     compilerParameters.ReferencedAssemblies);
            }

            // Make any fix up adjustments to the CompilerParameters to work around some issues
            AssemblyBuilder.FixUpCompilerParameters(compConfig, codeDomProviderType, compilerParameters);

            return(buildProvider);
        }
 private void FixupReferencedAssemblies(VirtualPath virtualPath, CompilerParameters compilerParameters)
 {
     Util.AddAssembliesToStringCollection(BuildManager.GetReferencedAssemblies(MTConfigUtil.GetCompilationConfig(virtualPath)), compilerParameters.ReferencedAssemblies);
 }