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)
70 public virtual string GetCustomString (CompilerResults results)
72 throw new NotImplementedException ();
75 protected CompilerType GetDefaultCompilerType ()
77 CompilationSection config;
78 config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
79 return GetDefaultCompilerTypeForLanguage (config.DefaultLanguage);
82 void SetCommonParameters (CompilationSection config, CompilerParameters p)
84 p.IncludeDebugInformation = config.Debug;
85 foreach (AssemblyInfo info in config.Assemblies) {
86 if (info.Assembly != "*") {
87 p.ReferencedAssemblies.Add (info.Assembly);
88 ref_assemblies.Add (info.Assembly);
90 AddAssembliesInBin (p.ReferencedAssemblies);
95 // embedded? linked?/resources
99 static void InitPath ()
102 if (private_bin_path != null)
105 AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
106 private_bin_path = Path.Combine (setup.ApplicationBase, setup.PrivateBinPath);
110 void AddAssembliesInBin (StringCollection coll)
113 if (!Directory.Exists (private_bin_path))
116 string [] binDlls = Directory.GetFiles (private_bin_path, "*.dll");
117 foreach (string s in binDlls) {
119 ref_assemblies.Add (s);
123 protected CompilerType GetDefaultCompilerTypeForLanguage (string language)
125 // MS throws when accesing a Hashtable, we do here.
126 if (language == null || language == "")
127 throw new ArgumentNullException ("language");
129 CompilationSection config;
130 config = (CompilationSection) WebConfigurationManager.GetSection ("system.web/compilation");
131 Compiler compiler = config.Compilers.Get (language);
132 CompilerParameters p;
133 if (compiler != null) {
134 Type type = Type.GetType (compiler.Type, true);
135 p = new CompilerParameters ();
136 p.CompilerOptions = compiler.CompilerOptions;
137 p.WarningLevel = compiler.WarningLevel;
138 SetCommonParameters (config, p);
139 return new CompilerType (type, p);
142 if (!CodeDomProvider.IsDefinedLanguage (language))
143 throw new HttpException (String.Format ("No compiler for language '{0}'.", language));
145 CompilerInfo info = CodeDomProvider.GetCompilerInfo (language);
146 CompilerParameters par = info.CreateDefaultCompilerParameters ();
147 SetCommonParameters (config, par);
148 return new CompilerType (info.CodeDomProviderType, par);
151 public virtual Type GetGeneratedType (CompilerResults results)
156 public virtual BuildProviderResultFlags GetResultFlags (CompilerResults results)
158 return BuildProviderResultFlags.Default;
161 protected TextReader OpenReader ()
163 return OpenReader (VirtualPath);
166 protected TextReader OpenReader (string virtualPath)
168 Stream st = OpenStream (virtualPath);
169 return new StreamReader (st, WebEncoding.FileEncoding);
172 protected Stream OpenStream ()
174 return OpenStream (VirtualPath);
177 protected Stream OpenStream (string virtualPath)
179 // MS also throws a NullReferenceException here when not hosted.
180 return VirtualPathProvider.OpenFile (virtualPath);
183 public virtual CompilerType CodeCompilerType {
184 get { return null; } // Documented to return null
187 protected ICollection ReferencedAssemblies {
188 get { return ref_assemblies; }
191 protected internal string VirtualPath {
192 get { return virtual_path; }
195 public virtual ICollection VirtualPathDependencies {
197 if (vpath_deps == null)
198 vpath_deps = new OneNullCollection ();
205 class OneNullCollection : ICollection {
210 public bool IsSynchronized {
211 get { return false; }
214 public object SyncRoot {
218 public void CopyTo (Array array, int index)
221 throw new ArgumentNullException ();
224 throw new ArgumentOutOfRangeException ();
227 throw new ArgumentException ();
229 int length = array.Length;
230 if (index >= length || index > length - 1)
231 throw new ArgumentException ();
233 array.SetValue (null, index);
236 public IEnumerator GetEnumerator ()