public override void GenerateCode(AssemblyBuilder assemblyBuilder) { assemblyBuilder.AddAssemblyReference(typeof(SimpleWeb).Assembly); assemblyBuilder.AddAssemblyReference(typeof(SimpleTemplateBase).Assembly); assemblyBuilder.AddCodeCompileUnit(this, GeneratedCode); assemblyBuilder.GenerateTypeFactory(String.Format(CultureInfo.InvariantCulture, "{0}.{1}", Host.DefaultNamespace, "Foot")); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { if (this.Parser.RequiresCompilation) { BaseCodeDomTreeGenerator generator = this.CreateCodeDomTreeGenerator(this._parser); CodeCompileUnit ccu = generator.GetCodeDomTree(assemblyBuilder.CodeDomProvider, assemblyBuilder.StringResourceBuilder, base.VirtualPathObject); if (ccu != null) { if (this._parser.AssemblyDependencies != null) { foreach (Assembly assembly in (IEnumerable)this._parser.AssemblyDependencies) { assemblyBuilder.AddAssemblyReference(assembly, ccu); } } assemblyBuilder.AddCodeCompileUnit(this, ccu); } this._instantiatableFullTypeName = generator.GetInstantiatableFullTypeName(); if (this._instantiatableFullTypeName != null) { assemblyBuilder.GenerateTypeFactory(this._instantiatableFullTypeName); } this._intermediateFullTypeName = generator.GetIntermediateFullTypeName(); } }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { if (this.Parser.RequiresCompilation) { BaseCodeDomTreeGenerator generator = this.CreateCodeDomTreeGenerator(this._parser); CodeCompileUnit ccu = generator.GetCodeDomTree(assemblyBuilder.CodeDomProvider, assemblyBuilder.StringResourceBuilder, base.VirtualPathObject); if (ccu != null) { if (this._parser.AssemblyDependencies != null) { foreach (Assembly assembly in (IEnumerable) this._parser.AssemblyDependencies) { assemblyBuilder.AddAssemblyReference(assembly, ccu); } } assemblyBuilder.AddCodeCompileUnit(this, ccu); } this._instantiatableFullTypeName = generator.GetInstantiatableFullTypeName(); if (this._instantiatableFullTypeName != null) { assemblyBuilder.GenerateTypeFactory(this._instantiatableFullTypeName); } this._intermediateFullTypeName = generator.GetIntermediateFullTypeName(); } }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { if (this._generatedCode == null) { this._generatedCode = GenerateCode(); } assemblyBuilder.AddAssemblyReference(typeof(SimpleWeb).Assembly); assemblyBuilder.AddAssemblyReference(typeof(SimpleTemplateBase).Assembly); assemblyBuilder.AddCodeCompileUnit(this, this._generatedCode); assemblyBuilder.GenerateTypeFactory(this._typeName); }
/// <summary> /// Generates source code for the virtual path of the build provider, and adds the source code to a specified assembly builder. /// </summary> /// <param name="assemblyBuilder">The assembly builder that references the source code generated by the build provider.</param> public override void GenerateCode(AssemblyBuilder assemblyBuilder) { try { assemblyBuilder.AddCodeCompileUnit(this, this.GetGeneratedCode()); assemblyBuilder.GenerateTypeFactory(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", new object[] { this.host.DefaultNamespace, this.host.DefaultClassName })); } catch (Exception ex) { Log.Error("GenerateCode(): ", ex); } }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { // Don't generate any code for no-compile pages if (!Parser.RequiresCompilation) { return; } BaseCodeDomTreeGenerator treeGenerator = CreateCodeDomTreeGenerator(_parser); CodeCompileUnit ccu = treeGenerator.GetCodeDomTree(assemblyBuilder.CodeDomProvider, assemblyBuilder.StringResourceBuilder, VirtualPathObject); if (ccu != null) { // Add all the assemblies if (_parser.AssemblyDependencies != null) { foreach (Assembly assembly in _parser.AssemblyDependencies) { assemblyBuilder.AddAssemblyReference(assembly, ccu); } } assemblyBuilder.AddCodeCompileUnit(this, ccu); } // Get the name of the generated type that can be instantiated. It may be null // in updatable compilation scenarios. _instantiatableFullTypeName = treeGenerator.GetInstantiatableFullTypeName(); // tell the assembly builder to generate a fast factory for this type if (_instantiatableFullTypeName != null) { assemblyBuilder.GenerateTypeFactory(_instantiatableFullTypeName); } _intermediateFullTypeName = treeGenerator.GetIntermediateFullTypeName(); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { // Don't generate any code for no-compile pages if (!Parser.RequiresCompilation) return; BaseCodeDomTreeGenerator treeGenerator = CreateCodeDomTreeGenerator(_parser); CodeCompileUnit ccu = treeGenerator.GetCodeDomTree(assemblyBuilder.CodeDomProvider, assemblyBuilder.StringResourceBuilder, VirtualPathObject); if (ccu != null) { // Add all the assemblies if (_parser.AssemblyDependencies != null) { foreach (Assembly assembly in _parser.AssemblyDependencies) { assemblyBuilder.AddAssemblyReference(assembly, ccu); } } assemblyBuilder.AddCodeCompileUnit(this, ccu); } // Get the name of the generated type that can be instantiated. It may be null // in updatable compilation scenarios. _instantiatableFullTypeName = treeGenerator.GetInstantiatableFullTypeName(); // tell the assembly builder to generate a fast factory for this type if (_instantiatableFullTypeName != null) assemblyBuilder.GenerateTypeFactory(_instantiatableFullTypeName); _intermediateFullTypeName = treeGenerator.GetIntermediateFullTypeName(); }
//////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// // BuildProvider implementation public override void GenerateCode(AssemblyBuilder assemblyBuilder) { Hashtable properties = ProfileBase.GetPropertiesForCompilation(); CodeCompileUnit compileUnit = new CodeCompileUnit(); Hashtable groups = new Hashtable(); Type baseType = Type.GetType(ProfileBase.InheritsFromTypeString, false); // namespace ASP { // CodeNamespace ns = new CodeNamespace(); // ns.Name = BaseCodeDomTreeGenerator.defaultNamespace; //GEN: using System; ns.Imports.Add(new CodeNamespaceImport("System")); //GEN: using System.Web; ns.Imports.Add(new CodeNamespaceImport("System.Web")); //GEN: using System.Web.Profile; ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile")); // class Profile : System.Web.Security.ProfileBase { // CodeTypeDeclaration type = new CodeTypeDeclaration(); type.Name = ProfileTypeName; if (baseType != null) { type.BaseTypes.Add(new CodeTypeReference(baseType)); assemblyBuilder.AddAssemblyReference(baseType.Assembly, compileUnit); } else { type.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString)); ProfileSection config = MTConfigUtil.GetProfileAppConfig(); if (config != null) { PropertyInformation prop = config.ElementInformation.Properties["inherits"]; if (prop != null && prop.Source != null && prop.LineNumber > 0) type.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(prop.Source), prop.LineNumber); } } // tell the assemblyBuilder to generate a fast factory for this type assemblyBuilder.GenerateTypeFactory(/*ns.Name + "." + */ ProfileTypeName); foreach(DictionaryEntry de in properties) { ProfileNameTypeStruct property = (ProfileNameTypeStruct)de.Value; if (property.PropertyType != null) assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, compileUnit); int pos = property.Name.IndexOf('.'); if (pos < 0) { // public string Color { get { return (string) GetProperty("Color"); } set { SetProperty("Color", value); } } CreateCodeForProperty(assemblyBuilder, type, property); } else { string grpName = property.Name.Substring(0, pos); if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(grpName)) throw new ConfigurationErrorsException(SR.GetString(SR.Profile_bad_group, grpName), property.FileName, property.LineNumber); if (groups[grpName] == null) { groups.Add(grpName, property.Name); } else { groups[grpName] = ((string)groups[grpName]) + ";" + property.Name; } } } foreach(DictionaryEntry de in groups) { // public ProfileGroupFooClass Foo { get { return ProfileGroupSomething; }} // // public class ProfileGroupFoo : ProfileGroup { // Properties // } AddPropertyGroup(assemblyBuilder, (string) de.Key, (string) de.Value, properties, type, ns); } // public ASP.Profile GetProfileForUser(string username) { // return (ASP.Profile) this.GetUserProfile(username); // } AddCodeForGetProfileForUser(type); // } // ns.Types.Add(type); compileUnit.Namespaces.Add(ns); assemblyBuilder.AddCodeCompileUnit(this, compileUnit); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { assemblyBuilder.AddCodeCompileUnit(this, this.GeneratedCode); assemblyBuilder.GenerateTypeFactory(string.Format(CultureInfo.InvariantCulture, "{0}.{1}", Compiler.Namespace, Compiler.ClassName)); }
private void GenerateServiceProxyCode(AssemblyBuilder assemblyBuilder, Type serviceType) { IResourceNameGenerator nameGenerator = assemblyBuilder.CodeDomProvider as IResourceNameGenerator; if (nameGenerator != null) { this.ResourceFullName = nameGenerator.GenerateResourceName(base.VirtualPath); } else { this.ResourceFullName = ResourceBuildProvider.GenerateTypeNameFromPath(base.VirtualPath); } // TODO: consolidate app relative path conversion // calculate the service end-point path string proxyPath = ResourceHandler.EnsureAppRelative(base.VirtualPath).TrimStart('~'); // build proxy from main service type JsonServiceDescription desc = new JsonServiceDescription(serviceType, proxyPath); JsonServiceProxyGenerator proxy = new JsonServiceProxyGenerator(desc); string proxyOutput = proxy.OutputProxy(false); proxyOutput = ScriptResourceCodeProvider.FirewallScript(proxyPath, proxyOutput, true); string debugProxyOutput = proxy.OutputProxy(true); debugProxyOutput = ScriptResourceCodeProvider.FirewallScript(proxyPath, debugProxyOutput, false); byte[] gzippedBytes, deflatedBytes; ResourceBuildProvider.Compress(proxyOutput, out gzippedBytes, out deflatedBytes); string hash = ResourceBuildProvider.ComputeHash(proxyOutput); // generate a service factory CodeCompileUnit generatedUnit = new CodeCompileUnit(); #region namespace ResourceNamespace CodeNamespace ns = new CodeNamespace(this.ResourceNamespace); generatedUnit.Namespaces.Add(ns); #endregion namespace ResourceNamespace #region public sealed class ResourceTypeName : JsonServiceInfo CodeTypeDeclaration resourceType = new CodeTypeDeclaration(); resourceType.IsClass = true; resourceType.Name = this.ResourceTypeName; resourceType.Attributes = MemberAttributes.Public|MemberAttributes.Final; resourceType.BaseTypes.Add(typeof(IJsonServiceInfo)); resourceType.BaseTypes.Add(typeof(IOptimizedResult)); ns.Types.Add(resourceType); #endregion public sealed class ResourceTypeName : CompiledBuildResult #region [BuildPath(virtualPath)] string virtualPath = base.VirtualPath; if (HttpRuntime.AppDomainAppVirtualPath.Length > 1) { virtualPath = virtualPath.Substring(HttpRuntime.AppDomainAppVirtualPath.Length); } virtualPath = "~"+virtualPath; CodeAttributeDeclaration attribute = new CodeAttributeDeclaration( new CodeTypeReference(typeof(BuildPathAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(virtualPath))); resourceType.CustomAttributes.Add(attribute); #endregion [BuildPath(virtualPath)] #region private static readonly byte[] GzippedBytes CodeMemberField field = new CodeMemberField(); field.Name = "GzippedBytes"; field.Type = new CodeTypeReference(typeof(byte[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; CodeArrayCreateExpression arrayInit = new CodeArrayCreateExpression(field.Type, gzippedBytes.Length); foreach (byte b in gzippedBytes) { arrayInit.Initializers.Add(new CodePrimitiveExpression(b)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static static readonly byte[] GzippedBytes #region private static readonly byte[] DeflatedBytes field = new CodeMemberField(); field.Name = "DeflatedBytes"; field.Type = new CodeTypeReference(typeof(byte[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; arrayInit = new CodeArrayCreateExpression(field.Type, deflatedBytes.Length); foreach (byte b in deflatedBytes) { arrayInit.Initializers.Add(new CodePrimitiveExpression(b)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static readonly byte[] DeflatedBytes #region string IOptimizedResult.Source { get; } // add a readonly property with the original resource source CodeMemberProperty property = new CodeMemberProperty(); property.Name = "Source"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return debugProxyOutput; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(debugProxyOutput))); resourceType.Members.Add(property); #endregion string IOptimizedResult.Source { get; } #region string IOptimizedResult.PrettyPrinted { get; } // add a readonly property with the debug proxy code string property = new CodeMemberProperty(); property.Name = "PrettyPrinted"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return ((IOptimizedResult)this).Source; } CodeExpression thisRef = new CodeCastExpression(typeof(IOptimizedResult), new CodeThisReferenceExpression()); CodePropertyReferenceExpression sourceProperty = new CodePropertyReferenceExpression(thisRef, "Source"); property.GetStatements.Add(new CodeMethodReturnStatement(sourceProperty)); resourceType.Members.Add(property); #endregion string IOptimizedResult.PrettyPrinted { get; } #region string IOptimizedResult.Compacted { get; } // add a readonly property with the proxy code string property = new CodeMemberProperty(); property.Name = "Compacted"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return proxyOutput; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(proxyOutput))); resourceType.Members.Add(property); #endregion string IOptimizedResult.Compacted { get; } #region byte[] IOptimizedResult.Gzipped { get; } // add a readonly property with the gzipped proxy code property = new CodeMemberProperty(); property.Name = "Gzipped"; property.Type = new CodeTypeReference(typeof(byte[])); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return GzippedBytes; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(this.ResourceTypeName), "GzippedBytes"))); resourceType.Members.Add(property); #endregion byte[] IOptimizedResult.Gzipped { get; } #region byte[] IOptimizedResult.Deflated { get; } // add a readonly property with the deflated proxy code property = new CodeMemberProperty(); property.Name = "Deflated"; property.Type = new CodeTypeReference(typeof(byte[])); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return DeflatedBytes; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(this.ResourceTypeName), "DeflatedBytes"))); resourceType.Members.Add(property); #endregion byte[] IOptimizedResult.Deflated { get; } #region string IBuildResultMeta.Hash { get; } // add a readonly property with the hash of the resource data property = new CodeMemberProperty(); property.Name = "Hash"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return hash; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(hash))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.Hash { get; } #region string IBuildResultMeta.ContentType { get; } // add a readonly property with the MIME of the resource data property = new CodeMemberProperty(); property.Name = "ContentType"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return ScriptResourceCodeProvider.MimeType; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(ScriptResourceCodeProvider.MimeType))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.ContentType { get; } #region string IBuildResultMeta.FileExtension { get; } // add a readonly property with the extension of the resource data property = new CodeMemberProperty(); property.Name = "FileExtension"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return ScriptResourceCodeProvider.FileExt; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(ScriptResourceCodeProvider.FileExt))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.FileExtension { get; } #region public Type IJrpcServiceInfo.ServiceType { get; } // add a static field with the service type property = new CodeMemberProperty(); property.Name = "ServiceType"; property.Type = new CodeTypeReference(typeof(Type)); property.Attributes = MemberAttributes.Public; property.ImplementationTypes.Add(new CodeTypeReference(typeof(IJsonServiceInfo))); property.HasGet = true; // get { return typeof(serviceType); } property.GetStatements.Add(new CodeMethodReturnStatement(new CodeTypeOfExpression(serviceType.FullName))); resourceType.Members.Add(property); #endregion public Type IJrpcServiceInfo.ServiceType { get; } #region object IJrpcServiceInfo.CreateService(); CodeMemberMethod codeMethod = new CodeMemberMethod(); codeMethod.Name = "CreateService"; codeMethod.PrivateImplementationType = new CodeTypeReference(typeof(IJsonServiceInfo)); codeMethod.ReturnType = new CodeTypeReference(typeof(Object)); // return new serviceType(); codeMethod.Statements.Add(new CodeMethodReturnStatement(new CodeObjectCreateExpression(serviceType))); resourceType.Members.Add(codeMethod); #endregion object IJrpcServiceInfo.CreateService(); #region MethodInfo IJrpcServiceInfo.ResolveMethodName(string name); codeMethod = new CodeMemberMethod(); codeMethod.Name = "ResolveMethodName"; codeMethod.PrivateImplementationType = new CodeTypeReference(typeof(IJsonServiceInfo)); codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "name")); codeMethod.ReturnType = new CodeTypeReference(typeof(MethodInfo)); CodeVariableReferenceExpression nameParam = new CodeVariableReferenceExpression("name"); // if (String.IsNullOrEmpty(name)) { return null; } CodeConditionStatement nullCheck = new CodeConditionStatement(); nullCheck.Condition = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(String)), "IsNullOrEmpty", nameParam); nullCheck.TrueStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null))); codeMethod.Statements.Add(nullCheck); Dictionary<string, MethodInfo> methodMap = JsonServiceBuildProvider.CreateMethodMap(serviceType); foreach (string name in methodMap.Keys) { CodeConditionStatement nameTest = new CodeConditionStatement(); // if (String.Equals(name, methodName)) { ... } nameTest.Condition = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(String)), "Equals", nameParam, new CodePrimitiveExpression(name)); // this.ServiceType CodePropertyReferenceExpression serviceTypeRef = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ServiceType"); // method name CodePrimitiveExpression methodNameRef = new CodePrimitiveExpression(methodMap[name].Name); // this.ServiceType.GetMethod(methodNameRef) CodeMethodInvokeExpression methodInfoRef = new CodeMethodInvokeExpression(serviceTypeRef, "GetMethod", methodNameRef); // return MethodInfo; nameTest.TrueStatements.Add(new CodeMethodReturnStatement(methodInfoRef)); codeMethod.Statements.Add(nameTest); } codeMethod.Statements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(null))); resourceType.Members.Add(codeMethod); #endregion MethodInfo IJrpcServiceInfo.ResolveMethodName(string name); #region string[] IJrpcServiceInfo.GetMethodParams(string name); codeMethod = new CodeMemberMethod(); codeMethod.Name = "GetMethodParams"; codeMethod.PrivateImplementationType = new CodeTypeReference(typeof(IJsonServiceInfo)); codeMethod.Parameters.Add(new CodeParameterDeclarationExpression(typeof(String), "name")); codeMethod.ReturnType = new CodeTypeReference(typeof(String[])); CodeVariableReferenceExpression nameParam2 = new CodeVariableReferenceExpression("name"); // if (String.IsNullOrEmpty(name)) { return new string[0]; } CodeConditionStatement nullCheck2 = new CodeConditionStatement(); nullCheck2.Condition = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(String)), "IsNullOrEmpty", nameParam); nullCheck2.TrueStatements.Add(new CodeMethodReturnStatement(new CodeArrayCreateExpression(typeof(String[]), 0))); codeMethod.Statements.Add(nullCheck2); foreach (MethodInfo method in methodMap.Values) { string[] paramMap = JsonServiceBuildProvider.CreateParamMap(method); if (paramMap.Length < 1) continue; CodeConditionStatement nameTest = new CodeConditionStatement(); // if (String.Equals(name, method.Name)) { ... } nameTest.Condition = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(String)), "Equals", nameParam2, new CodePrimitiveExpression(method.Name)); // = {...} CodePrimitiveExpression[] paramList = new CodePrimitiveExpression[paramMap.Length]; for (int i=0; i<paramMap.Length; i++) { paramList[i] = new CodePrimitiveExpression(paramMap[i]); } // new string[] = {...} CodeArrayCreateExpression paramArray = new CodeArrayCreateExpression(typeof(String[]), paramList); // return string[]; nameTest.TrueStatements.Add(new CodeMethodReturnStatement(paramArray)); codeMethod.Statements.Add(nameTest); } codeMethod.Statements.Add(new CodeMethodReturnStatement(new CodeArrayCreateExpression(typeof(String[]), 0))); resourceType.Members.Add(codeMethod); #endregion string[] IJrpcServiceInfo.GetMethodParams(string name); if (this.VirtualPathDependencies.Count > 0) { resourceType.BaseTypes.Add(typeof(IDependentResult)); #region private static readonly string[] Dependencies field = new CodeMemberField(); field.Name = "Dependencies"; field.Type = new CodeTypeReference(typeof(string[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; arrayInit = new CodeArrayCreateExpression(field.Type, this.VirtualPathDependencies.Count); foreach (string key in this.VirtualPathDependencies) { arrayInit.Initializers.Add(new CodePrimitiveExpression(key)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static readonly string[] Dependencies #region IEnumerable<string> IDependentResult.VirtualPathDependencies { get; } // add a readonly property returning the static data property = new CodeMemberProperty(); property.Name = "VirtualPathDependencies"; property.Type = new CodeTypeReference(typeof(IEnumerable<string>)); property.PrivateImplementationType = new CodeTypeReference(typeof(IDependentResult)); property.HasGet = true; // get { return Dependencies; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(resourceType.Name), "Dependencies"))); resourceType.Members.Add(property); #endregion IEnumerable<string> IDependentResult.VirtualPathDependencies { get; } } // Generate _ASP FastObjectFactory assemblyBuilder.GenerateTypeFactory(this.ResourceFullName); assemblyBuilder.AddCodeCompileUnit(this, generatedUnit); }
//////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////// // BuildProvider implementation public override void GenerateCode(AssemblyBuilder assemblyBuilder) { Hashtable properties = ProfileBase.GetPropertiesForCompilation(); CodeCompileUnit compileUnit = new CodeCompileUnit(); Hashtable groups = new Hashtable(); Type baseType = Type.GetType(ProfileBase.InheritsFromTypeString, false); // namespace ASP { // CodeNamespace ns = new CodeNamespace(); // ns.Name = BaseCodeDomTreeGenerator.defaultNamespace; //GEN: using System; ns.Imports.Add(new CodeNamespaceImport("System")); //GEN: using System.Web; ns.Imports.Add(new CodeNamespaceImport("System.Web")); //GEN: using System.Web.Profile; ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile")); // class Profile : System.Web.Security.ProfileBase { // CodeTypeDeclaration type = new CodeTypeDeclaration(); type.Name = ProfileTypeName; if (baseType != null) { type.BaseTypes.Add(new CodeTypeReference(baseType)); assemblyBuilder.AddAssemblyReference(baseType.Assembly, compileUnit); } else { type.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString)); ProfileSection config = MTConfigUtil.GetProfileAppConfig(); if (config != null) { PropertyInformation prop = config.ElementInformation.Properties["inherits"]; if (prop != null && prop.Source != null && prop.LineNumber > 0) { type.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(prop.Source), prop.LineNumber); } } } // tell the assemblyBuilder to generate a fast factory for this type assemblyBuilder.GenerateTypeFactory(/*ns.Name + "." + */ ProfileTypeName); foreach (DictionaryEntry de in properties) { ProfileNameTypeStruct property = (ProfileNameTypeStruct)de.Value; if (property.PropertyType != null) { assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, compileUnit); } int pos = property.Name.IndexOf('.'); if (pos < 0) { // public string Color { get { return (string) GetProperty("Color"); } set { SetProperty("Color", value); } } CreateCodeForProperty(assemblyBuilder, type, property); } else { string grpName = property.Name.Substring(0, pos); if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(grpName)) { throw new ConfigurationErrorsException(SR.GetString(SR.Profile_bad_group, grpName), property.FileName, property.LineNumber); } if (groups[grpName] == null) { groups.Add(grpName, property.Name); } else { groups[grpName] = ((string)groups[grpName]) + ";" + property.Name; } } } foreach (DictionaryEntry de in groups) { // public ProfileGroupFooClass Foo { get { return ProfileGroupSomething; }} // // public class ProfileGroupFoo : ProfileGroup { // Properties // } AddPropertyGroup(assemblyBuilder, (string)de.Key, (string)de.Value, properties, type, ns); } // public ASP.Profile GetProfileForUser(string username) { // return (ASP.Profile) this.GetUserProfile(username); // } AddCodeForGetProfileForUser(type); // } // ns.Types.Add(type); compileUnit.Namespaces.Add(ns); assemblyBuilder.AddCodeCompileUnit(this, compileUnit); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { string contentType, fileExtension; string originalSource, prettyPrintResource, compactedResource; ResourceCodeProvider provider = assemblyBuilder.CodeDomProvider as ResourceCodeProvider; if (provider != null) { originalSource = provider.CompileResource( this, base.VirtualPath, out prettyPrintResource, out compactedResource); contentType = provider.ContentType; fileExtension = provider.FileExtension; } else { // read the resource contents using (TextReader reader = this.OpenReader()) { originalSource = compactedResource = prettyPrintResource = reader.ReadToEnd(); } contentType = "text/plain"; fileExtension = "txt"; } IResourceNameGenerator nameGenerator = assemblyBuilder.CodeDomProvider as IResourceNameGenerator; if (nameGenerator != null) { this.ResourceFullName = nameGenerator.GenerateResourceName(base.VirtualPath); } else { this.ResourceFullName = ResourceBuildProvider.GenerateTypeNameFromPath(base.VirtualPath); } byte[] gzippedBytes, deflatedBytes; ResourceBuildProvider.Compress(compactedResource, out gzippedBytes, out deflatedBytes); string hash = ResourceBuildProvider.ComputeHash(compactedResource); // generate a resource container CodeCompileUnit generatedUnit = new CodeCompileUnit(); #region namespace ResourceNamespace CodeNamespace ns = new CodeNamespace(this.ResourceNamespace); generatedUnit.Namespaces.Add(ns); #endregion namespace ResourceNamespace #region public sealed class ResourceTypeName CodeTypeDeclaration resourceType = new CodeTypeDeclaration(); resourceType.IsClass = true; resourceType.Name = this.ResourceTypeName; resourceType.Attributes = MemberAttributes.Public|MemberAttributes.Final; provider.SetBaseClass(resourceType); resourceType.BaseTypes.Add(typeof(IOptimizedResult)); ns.Types.Add(resourceType); #endregion public sealed class ResourceTypeName #region [BuildPath(virtualPath)] string virtualPath = base.VirtualPath; if (HttpRuntime.AppDomainAppVirtualPath.Length > 1) { virtualPath = virtualPath.Substring(HttpRuntime.AppDomainAppVirtualPath.Length); } virtualPath = "~"+virtualPath; CodeAttributeDeclaration attribute = new CodeAttributeDeclaration( new CodeTypeReference(typeof(BuildPathAttribute)), new CodeAttributeArgument(new CodePrimitiveExpression(virtualPath))); resourceType.CustomAttributes.Add(attribute); #endregion [BuildPath(virtualPath)] #region private static readonly byte[] GzippedBytes CodeMemberField field = new CodeMemberField(); field.Name = "GzippedBytes"; field.Type = new CodeTypeReference(typeof(byte[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; CodeArrayCreateExpression arrayInit = new CodeArrayCreateExpression(field.Type, gzippedBytes.Length); foreach (byte b in gzippedBytes) { arrayInit.Initializers.Add(new CodePrimitiveExpression(b)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static static readonly byte[] GzippedBytes #region private static readonly byte[] DeflatedBytes; field = new CodeMemberField(); field.Name = "DeflatedBytes"; field.Type = new CodeTypeReference(typeof(byte[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; arrayInit = new CodeArrayCreateExpression(field.Type, deflatedBytes.Length); foreach (byte b in deflatedBytes) { arrayInit.Initializers.Add(new CodePrimitiveExpression(b)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static readonly byte[] DeflatedBytes; #region string IOptimizedResult.Source { get; } // add a readonly property with the original resource source CodeMemberProperty property = new CodeMemberProperty(); property.Name = "Source"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return originalSource; } if (originalSource == null || originalSource.Length <= 0x5DC) { property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(originalSource))); } else { string escaped = ResourceBuildProvider.QuoteSnippetStringCStyle(originalSource); property.GetStatements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(escaped))); } resourceType.Members.Add(property); #endregion string IOptimizedResult.Source { get; } #region string IOptimizedResult.PrettyPrinted { get; } // add a readonly property with the resource data property = new CodeMemberProperty(); property.Name = "PrettyPrinted"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; if (String.Equals(originalSource, prettyPrintResource)) { // get { return ((IOptimizedResult)this).Source; } CodeExpression thisRef = new CodeCastExpression(typeof(IOptimizedResult), new CodeThisReferenceExpression()); CodePropertyReferenceExpression sourceProperty = new CodePropertyReferenceExpression(thisRef, "Source"); property.GetStatements.Add(new CodeMethodReturnStatement(sourceProperty)); } else { // get { return prettyPrintResource; } if (prettyPrintResource == null || prettyPrintResource.Length <= 0x5DC) { property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(prettyPrintResource))); } else { string escaped = ResourceBuildProvider.QuoteSnippetStringCStyle(prettyPrintResource); property.GetStatements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(escaped))); } } resourceType.Members.Add(property); #endregion string IOptimizedResult.PrettyPrinted { get; } #region string IOptimizedResult.Compacted { get; } // add a readonly property with the compacted resource data property = new CodeMemberProperty(); property.Name = "Compacted"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return compactedResource; } if (compactedResource == null || compactedResource.Length <= 0x5DC) { property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(compactedResource))); } else { string escaped = ResourceBuildProvider.QuoteSnippetStringCStyle(compactedResource); property.GetStatements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(escaped))); } resourceType.Members.Add(property); #endregion string IOptimizedResult.Compacted { get; } #region byte[] IOptimizedResult.Gzipped { get; } // add a readonly property with the gzipped resource data property = new CodeMemberProperty(); property.Name = "Gzipped"; property.Type = new CodeTypeReference(typeof(byte[])); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return GzippedBytes; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(this.ResourceTypeName), "GzippedBytes"))); resourceType.Members.Add(property); #endregion byte[] IOptimizedResult.Gzipped { get; } #region byte[] IOptimizedResult.Deflated { get; } // add a readonly property with the deflated resource data property = new CodeMemberProperty(); property.Name = "Deflated"; property.Type = new CodeTypeReference(typeof(byte[])); property.PrivateImplementationType = new CodeTypeReference(typeof(IOptimizedResult)); property.HasGet = true; // get { return DeflatedBytes; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(this.ResourceTypeName), "DeflatedBytes"))); resourceType.Members.Add(property); #endregion byte[] IOptimizedResult.Deflated { get; } #region string IBuildResultMeta.ContentType { get; } // add a readonly property with the MIME type property = new CodeMemberProperty(); property.Name = "ContentType"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return contentType; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(contentType))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.ContentType { get; } #region string IBuildResultMeta.FileExtension { get; } // add a readonly property with the MIME type property = new CodeMemberProperty(); property.Name = "FileExtension"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return fileExtension; } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(fileExtension))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.FileExtension { get; } #region string IBuildResultMeta.Hash { get; } // add a readonly property with the resource data property = new CodeMemberProperty(); property.Name = "Hash"; property.Type = new CodeTypeReference(typeof(String)); property.PrivateImplementationType = new CodeTypeReference(typeof(IBuildResult)); property.HasGet = true; // get { return hash); } property.GetStatements.Add(new CodeMethodReturnStatement(new CodePrimitiveExpression(hash))); resourceType.Members.Add(property); #endregion string IBuildResultMeta.Hash { get; } if (this.VirtualPathDependencies.Count > 0) { resourceType.BaseTypes.Add(typeof(IDependentResult)); #region private static readonly string[] Dependencies field = new CodeMemberField(); field.Name = "Dependencies"; field.Type = new CodeTypeReference(typeof(string[])); field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final; arrayInit = new CodeArrayCreateExpression(field.Type, this.VirtualPathDependencies.Count); foreach (string key in this.VirtualPathDependencies) { arrayInit.Initializers.Add(new CodePrimitiveExpression(key)); } field.InitExpression = arrayInit; resourceType.Members.Add(field); #endregion private static readonly string[] Dependencies #region IEnumerable<string> IDependentResult.VirtualPathDependencies { get; } // add a readonly property returning the static data property = new CodeMemberProperty(); property.Name = "VirtualPathDependencies"; property.Type = new CodeTypeReference(typeof(IEnumerable<string>)); property.PrivateImplementationType = new CodeTypeReference(typeof(IDependentResult)); property.HasGet = true; // get { return Dependencies; } property.GetStatements.Add(new CodeMethodReturnStatement( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(resourceType.Name), "Dependencies"))); resourceType.Members.Add(property); #endregion IEnumerable<string> IDependentResult.VirtualPathDependencies { get; } } // allow the code provider to extend with additional properties provider.GenerateCodeExtensions(this, resourceType); // Generate _ASP FastObjectFactory assemblyBuilder.GenerateTypeFactory(this.ResourceFullName); assemblyBuilder.AddCodeCompileUnit(this, generatedUnit); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { Hashtable propertiesForCompilation = ProfileBase.GetPropertiesForCompilation(); CodeCompileUnit ccu = new CodeCompileUnit(); Hashtable hashtable2 = new Hashtable(); Type type = Type.GetType(ProfileBase.InheritsFromTypeString, false); CodeNamespace ns = new CodeNamespace(); ns.Imports.Add(new CodeNamespaceImport("System")); ns.Imports.Add(new CodeNamespaceImport("System.Web")); ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile")); CodeTypeDeclaration declaration = new CodeTypeDeclaration { Name = "ProfileCommon" }; if (type != null) { declaration.BaseTypes.Add(new CodeTypeReference(type)); assemblyBuilder.AddAssemblyReference(type.Assembly, ccu); } else { declaration.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString)); ProfileSection profileAppConfig = MTConfigUtil.GetProfileAppConfig(); if (profileAppConfig != null) { PropertyInformation information = profileAppConfig.ElementInformation.Properties["inherits"]; if (((information != null) && (information.Source != null)) && (information.LineNumber > 0)) { declaration.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(information.Source), information.LineNumber); } } } assemblyBuilder.GenerateTypeFactory("ProfileCommon"); foreach (DictionaryEntry entry in propertiesForCompilation) { ProfileNameTypeStruct property = (ProfileNameTypeStruct) entry.Value; if (property.PropertyType != null) { assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, ccu); } int index = property.Name.IndexOf('.'); if (index < 0) { this.CreateCodeForProperty(assemblyBuilder, declaration, property); } else { string str = property.Name.Substring(0, index); if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(str)) { throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_bad_group", new object[] { str }), property.FileName, property.LineNumber); } if (hashtable2[str] == null) { hashtable2.Add(str, property.Name); } else { hashtable2[str] = ((string) hashtable2[str]) + ";" + property.Name; } } } foreach (DictionaryEntry entry2 in hashtable2) { this.AddPropertyGroup(assemblyBuilder, (string) entry2.Key, (string) entry2.Value, propertiesForCompilation, declaration, ns); } this.AddCodeForGetProfileForUser(declaration); ns.Types.Add(declaration); ccu.Namespaces.Add(ns); assemblyBuilder.AddCodeCompileUnit(this, ccu); }
public override void GenerateCode(AssemblyBuilder assemblyBuilder) { Hashtable propertiesForCompilation = ProfileBase.GetPropertiesForCompilation(); CodeCompileUnit ccu = new CodeCompileUnit(); Hashtable hashtable2 = new Hashtable(); Type type = Type.GetType(ProfileBase.InheritsFromTypeString, false); CodeNamespace ns = new CodeNamespace(); ns.Imports.Add(new CodeNamespaceImport("System")); ns.Imports.Add(new CodeNamespaceImport("System.Web")); ns.Imports.Add(new CodeNamespaceImport("System.Web.Profile")); CodeTypeDeclaration declaration = new CodeTypeDeclaration { Name = "ProfileCommon" }; if (type != null) { declaration.BaseTypes.Add(new CodeTypeReference(type)); assemblyBuilder.AddAssemblyReference(type.Assembly, ccu); } else { declaration.BaseTypes.Add(new CodeTypeReference(ProfileBase.InheritsFromTypeString)); ProfileSection profileAppConfig = MTConfigUtil.GetProfileAppConfig(); if (profileAppConfig != null) { PropertyInformation information = profileAppConfig.ElementInformation.Properties["inherits"]; if (((information != null) && (information.Source != null)) && (information.LineNumber > 0)) { declaration.LinePragma = new CodeLinePragma(HttpRuntime.GetSafePath(information.Source), information.LineNumber); } } } assemblyBuilder.GenerateTypeFactory("ProfileCommon"); foreach (DictionaryEntry entry in propertiesForCompilation) { ProfileNameTypeStruct property = (ProfileNameTypeStruct)entry.Value; if (property.PropertyType != null) { assemblyBuilder.AddAssemblyReference(property.PropertyType.Assembly, ccu); } int index = property.Name.IndexOf('.'); if (index < 0) { this.CreateCodeForProperty(assemblyBuilder, declaration, property); } else { string str = property.Name.Substring(0, index); if (!assemblyBuilder.CodeDomProvider.IsValidIdentifier(str)) { throw new ConfigurationErrorsException(System.Web.SR.GetString("Profile_bad_group", new object[] { str }), property.FileName, property.LineNumber); } if (hashtable2[str] == null) { hashtable2.Add(str, property.Name); } else { hashtable2[str] = ((string)hashtable2[str]) + ";" + property.Name; } } } foreach (DictionaryEntry entry2 in hashtable2) { this.AddPropertyGroup(assemblyBuilder, (string)entry2.Key, (string)entry2.Value, propertiesForCompilation, declaration, ns); } this.AddCodeForGetProfileForUser(declaration); ns.Types.Add(declaration); ccu.Namespaces.Add(ns); assemblyBuilder.AddCodeCompileUnit(this, ccu); }