2 // System.Web.Compilation.BuildProvider
5 // Chris Toshok (toshok@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2006 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.CodeDom.Compiler;
36 using System.Collections;
37 using System.Collections.Specialized;
39 using System.Reflection;
40 using System.Web.Configuration;
41 using System.Web.Hosting;
42 using System.Web.Util;
44 namespace System.Web.Compilation {
46 public abstract class BuildProvider {
47 static object locker = new object ();
48 static string private_bin_path;
51 ArrayList ref_assemblies;
53 ICollection vpath_deps;
55 protected BuildProvider()
57 ref_assemblies = new ArrayList ();
60 internal void SetVirtualPath (string path)
65 public virtual void GenerateCode (AssemblyBuilder assemblyBuilder)
69 public virtual string GetCustomString (CompilerResults results)
74 protected CompilerType GetDefaultCompilerType ()
76 CompilationSection config;
77 config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
78 return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage, config);
81 void SetCommonParameters (CompilationSection config, CompilerParameters p)
83 p.IncludeDebugInformation = config.Debug;
84 foreach (AssemblyInfo info in config.Assemblies) {
85 if (info.Assembly != "*") {
86 p.ReferencedAssemblies.Add (info.Assembly);
87 ref_assemblies.Add (info.Assembly);
89 AddAssembliesInBin (p.ReferencedAssemblies);
94 // embedded? linked?/resources
98 static void InitPath ()
101 if (private_bin_path != null)
104 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
105 private_bin_path = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
109 void AddAssembliesInBin (StringCollection coll)
112 if (!Directory.Exists (private_bin_path))
115 string [] binDlls = Directory.GetFiles (private_bin_path, "*.dll");
116 foreach (string s in binDlls) {
118 ref_assemblies.Add (s);
122 internal CompilerType GetDefaultCompilerTypeForLanguage (string language, CompilationSection configSection)
124 // MS throws when accesing a Hashtable, we do here.
125 if (language == null || language == "")
126 throw new ArgumentNullException ("language");
128 CompilationSection config;
129 if (configSection == null)
130 config = WebConfigurationManager.GetSection ("system.web/compilation") as CompilationSection;
132 config = configSection;
134 Compiler compiler = config.Compilers.Get (language);
135 CompilerParameters p;
136 if (compiler != null) {
137 Type type = Type.GetType (compiler.Type, true);
138 p = new CompilerParameters ();
139 p.CompilerOptions = compiler.CompilerOptions;
140 p.WarningLevel = compiler.WarningLevel;
141 SetCommonParameters (config, p);
142 return new CompilerType (type, p);
145 if (!CodeDomProvider.IsDefinedLanguage (language))
146 throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
148 CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
149 CompilerParameters par = info.CreateDefaultCompilerParameters ();
150 SetCommonParameters (config, par);
151 return new CompilerType (info.CodeDomProviderType, par);
154 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
156 return GetDefaultCompilerTypeForLanguage (language, null);
159 public virtual Type GetGeneratedType (CompilerResults results)
164 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
166 return BuildProviderResultFlags.Default;
169 protected TextReader OpenReader ()
171 return OpenReader (VirtualPath);
174 protected TextReader OpenReader (string virtualPath)
176 Stream st = OpenStream (virtualPath);
177 return new StreamReader (st, WebEncoding.FileEncoding);
180 protected Stream OpenStream ()
182 return OpenStream (VirtualPath);
185 protected Stream OpenStream (string virtualPath)
187 // MS also throws a NullReferenceException here when not hosted.
188 return VirtualPathProvider.OpenFile (virtualPath);
191 public virtual CompilerType CodeCompilerType {
192 get { return null; } // Documented to return null
195 protected ICollection ReferencedAssemblies {
196 get { return ref_assemblies; }
199 protected internal string VirtualPath {
200 get { return virtual_path; }
203 public virtual ICollection VirtualPathDependencies {
205 if (vpath_deps == null)
206 vpath_deps = new OneNullCollection ();
213 class OneNullCollection : ICollection {
218 public bool IsSynchronized {
219 get { return false; }
222 public object SyncRoot {
226 public void CopyTo (Array array, int index)
229 throw new ArgumentNullException ();
232 throw new ArgumentOutOfRangeException ();
235 throw new ArgumentException ();
237 int length = array.Length;
238 if (index >= length || index > length - 1)
239 throw new ArgumentException ();
241 array.SetValue (null, index);
244 public IEnumerator GetEnumerator ()