Exemple #1
0
        protected internal virtual string Eval()
        {
            // output error
            string path = ResourceHandler.EnsureAppRelative(this.Path);

            return(String.Format(JbstExtension.PrefixNotFoundError, path, this.Value));
        }
Exemple #2
0
        public static string FirewallScript(string virtualPath, string source, bool compacted)
        {
            ScriptCompactionSection config = ScriptCompactionSection.GetSettings();

            if (!config.Firewall)
            {
                return(source);
            }

            if (compacted)
            {
                return(String.Concat(
                           ScriptResourceCodeProvider.TryStart,
                           source,
                           ScriptResourceCodeProvider.CatchCompact));
            }

            virtualPath = ResourceHandler.EnsureAppRelative(virtualPath);
            virtualPath = (virtualPath != null) ? virtualPath.Replace("\"", "\\\"") : "script";
            return(String.Concat(
                       Environment.NewLine,
                       ScriptResourceCodeProvider.TryStart,
                       Environment.NewLine,
                       source,
                       Environment.NewLine,
                       ScriptResourceCodeProvider.CatchStart,
                       virtualPath,
                       ScriptResourceCodeProvider.CatchEnd));
        }
        private void AddDependency(string virtualPath)
        {
            if (String.IsNullOrEmpty(virtualPath))
            {
                return;
            }

            virtualPath = ResourceHandler.EnsureAppRelative(virtualPath);

            // attempt to dedup
            if (!this.pathDependencies.Contains(virtualPath))
            {
                this.pathDependencies.Add(virtualPath);
            }
        }
        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);
        }
Exemple #5
0
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            if (String.IsNullOrEmpty(sourceText))
            {
                resource  = null;
                compacted = null;
                return;
            }

            StringBuilder resources = new StringBuilder();
            StringBuilder compacts  = new StringBuilder();

            string[] files = sourceText.Split(LineDelims, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < files.Length; i++)
            {
                try
                {
                    string file       = files[i],
                           compactAlt = null;

                    if (file != null)
                    {
                        file = file.Trim();
                    }

                    // skip blank and comment lines
                    if (String.IsNullOrEmpty(file) ||
                        file.StartsWith("//") ||
                        file.StartsWith("#"))
                    {
                        continue;
                    }

                    MergeResourceCodeProvider.SplitAlternates(file, out file, out compactAlt);

                    if (file.IndexOf("://") >= 0)
                    {
                        string preProcessed, compact;

                        this.ProcessExternalResource(helper, file, out preProcessed, out compact, errors);

                        if (!String.IsNullOrEmpty(compactAlt))
                        {
                            this.ProcessExternalResource(helper, compactAlt, out compactAlt, out compact, errors);
                        }

                        compacts.Append(compact);
                        resources.Append(preProcessed);
                        continue;
                    }

                    // process embedded resource
                    if (file.IndexOf(',') >= 0)
                    {
                        string preProcessed, compact;

                        this.ProcessEmbeddedResource(helper, file, out preProcessed, out compact, errors);

                        if (!String.IsNullOrEmpty(compactAlt))
                        {
                            this.ProcessEmbeddedResource(helper, compactAlt, out compactAlt, out compact, errors);
                        }

                        compacts.Append(compact);
                        resources.Append(preProcessed);
                        continue;
                    }

                    file = ResourceHandler.EnsureAppRelative(file);
                    if (!String.IsNullOrEmpty(compactAlt))
                    {
                        compactAlt = ResourceHandler.EnsureAppRelative(compactAlt);
                    }

                    // try to get as a IOptimizedResult
                    IOptimizedResult result = this.ProcessPrecompiled(helper, file);
                    if (result != null)
                    {
                        resources.Append(result.PrettyPrinted);

                        if (String.IsNullOrEmpty(compactAlt))
                        {
                            compacts.Append(result.Compacted);
                        }
                        else
                        {
                            IOptimizedResult result2 = this.ProcessPrecompiled(helper, compactAlt);
                            if (result2 != null)
                            {
                                compacts.Append(result2.Compacted);
                            }
                        }
                        continue;
                    }

                    // ask BuildManager if compiles down to a string
                    string text = BuildManager.GetCompiledCustomString(file);
                    if (String.IsNullOrEmpty(text))
                    {
                        // use the raw contents of the virtual path
                        text = helper.OpenReader(file).ReadToEnd();
                    }

                    if (!String.IsNullOrEmpty(text))
                    {
                        helper.AddVirtualPathDependency(file);

                        resources.Append(text);

                        if (String.IsNullOrEmpty(compactAlt))
                        {
                            compacts.Append(text);
                        }
                        else
                        {
                            helper.AddVirtualPathDependency(compactAlt);

                            string text2 = BuildManager.GetCompiledCustomString(compactAlt);
                            compacts.Append(text2);
                        }
                        continue;
                    }
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, i + 1, 1, ex));
                }
            }

            resources.AppendLine();
            resources.AppendFormat("/* JsonFx v{0} */", JsonFx.About.Fx.Version);

            resource  = resources.ToString();
            compacted = compacts.ToString();
        }