Exemplo n.º 1
0
        /// <summary>
        /// Delegates compilation to the compiler implementation
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="virtualPath"></param>
        /// <param name="preProcessed"></param>
        /// <param name="compacted"></param>
        /// <returns>original source</returns>
        protected internal string CompileResource(
            IResourceBuildHelper helper,
            string virtualPath,
            out string preProcessed,
            out string compacted)
        {
            // clear any previously stored state (leave errors for later reporting)
            this.ResetCodeProvider();

            string source;

            // read the resource contents
            using (TextReader reader = helper.OpenReader(virtualPath))
            {
                source = reader.ReadToEnd();
            }

            // preprocess the resource
            this.ProcessResource(
                helper,
                virtualPath,
                source,
                out preProcessed,
                out compacted,
                this.errors);

            return(source);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Processes the source.
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="virtualPath"></param>
 /// <param name="sourceText"></param>
 /// <param name="resource"></param>
 /// <param name="compacted"></param>
 /// <param name="errors"></param>
 protected internal abstract void ProcessResource(
     IResourceBuildHelper helper,
     string virtualPath,
     string source,
     out string resource,
     out string compacted,
     List <ParseException> errors);
 protected internal override void ProcessExternalResource(
     IResourceBuildHelper helper,
     string url,
     out string preProcessed,
     out string compacted,
     List<ParseException> errors)
 {
     compacted = preProcessed = String.Format("@import url('{0}');", url);
 }
Exemplo n.º 4
0
 protected internal override void ProcessExternalResource(
     IResourceBuildHelper helper,
     string url,
     out string preProcessed,
     out string compacted,
     List <ParseException> errors)
 {
     compacted = preProcessed = String.Format("@import url('{0}');", url);
 }
Exemplo n.º 5
0
        protected internal override void ProcessExternalResource(
            IResourceBuildHelper helper,
            string url,
            out string preProcessed,
            out string compacted,
            List <ParseException> errors)
        {
            compacted = preProcessed = String.Empty;

            Uri uri;

            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                throw new ArgumentException("Invalid external URL");
            }

            string       ext      = Path.GetExtension(uri.AbsolutePath).Trim('.');
            CompilerType compiler = helper.GetDefaultCompilerTypeForLanguage(ext);

            if (!typeof(ResourceCodeProvider).IsAssignableFrom(compiler.CodeDomProviderType))
            {
                // don't know how to process any further
                return;
            }

            ResourceCodeProvider provider = (ResourceCodeProvider)Activator.CreateInstance(compiler.CodeDomProviderType);

            try
            {
                // concatenate the preprocessed source for current merge phase
                provider.ProcessExternalResource(
                    helper,
                    url,
                    out preProcessed,
                    out compacted,
                    errors);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, url, 0, 0, ex));
            }

            if (!this.isMimeSet &&
                !String.IsNullOrEmpty(provider.ContentType) &&
                !String.IsNullOrEmpty(provider.FileExtension))
            {
                this.contentType   = provider.ContentType;
                this.fileExtension = provider.FileExtension;
                this.isMimeSet     = true;
            }
        }
Exemplo n.º 6
0
        protected internal override void ProcessExternalResource(
            IResourceBuildHelper helper,
            string url,
            out string preProcessed,
            out string compacted,
            List <ParseException> errors)
        {
            compacted = preProcessed = String.Format(ScriptResourceCodeProvider.ExternalImport, url);

            preProcessed = ScriptResourceCodeProvider.FirewallScript(url, preProcessed, true);
            compacted    = ScriptResourceCodeProvider.FirewallScript(url, compacted, true);
        }
Exemplo n.º 7
0
        protected internal override void GenerateCodeExtensions(IResourceBuildHelper helper, CodeTypeDeclaration resourceType)
        {
            base.GenerateCodeExtensions(helper, resourceType);

            #region public ResourceType() : base(virtualPath) {}

            CodeConstructor ctor = new CodeConstructor();
            ctor.Attributes = MemberAttributes.Public;
            ctor.BaseConstructorArgs.Add(new CodePrimitiveExpression(helper.VirtualPath));
            resourceType.Members.Add(ctor);

            #endregion public ResourceType() : base(virtualPath) {}
        }
        protected internal override void GenerateCodeExtensions(IResourceBuildHelper helper, CodeTypeDeclaration resourceType)
        {
            base.GenerateCodeExtensions(helper, resourceType);

            #region public ResourceType() : base(virtualPath) {}

            CodeConstructor ctor = new CodeConstructor();
            ctor.Attributes = MemberAttributes.Public;
            ctor.BaseConstructorArgs.Add(new CodePrimitiveExpression(helper.VirtualPath));
            resourceType.Members.Add(ctor);

            #endregion public ResourceType() : base(virtualPath) {}
        }
Exemplo n.º 9
0
        protected override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            // parse JBST markup
            this.jbstWriter = new JbstWriter(virtualPath);

            try
            {
                HtmlDistiller parser = new HtmlDistiller();
                parser.EncodeNonAscii      = false;
                parser.BalanceTags         = false;
                parser.NormalizeWhitespace = false;
                parser.HtmlWriter          = this.jbstWriter;
                parser.HtmlFilter          = new NullHtmlFilter();
                parser.Parse(sourceText);

                // determine which globalization keys were used
                JbstCodeProvider.ExtractGlobalizationKeys(this.jbstWriter.JbstParseTree, this.GlobalizationKeys);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
            }

            string renderedTemplate;

            using (StringWriter sw = new StringWriter())
            {
                // render the pretty-printed version
                this.jbstWriter.Render(sw);
                sw.Flush();
                renderedTemplate = sw.ToString();

                resource = ScriptResourceCodeProvider.FirewallScript(virtualPath, renderedTemplate, false);
            }

            // min the output for better compaction
            compacted = ScriptCompactionAdapter.Compact(virtualPath, renderedTemplate, errors);
            compacted = ScriptResourceCodeProvider.FirewallScript(virtualPath, compacted, true);
        }
Exemplo n.º 10
0
        protected internal virtual void GenerateCodeExtensions(IResourceBuildHelper helper, CodeTypeDeclaration resourceType)
        {
            if (this.g11nKeys.Count <= 0)
            {
                // no globalization strings were needed so don't gen code for the property
                return;
            }

            resourceType.BaseTypes.Add(typeof(IGlobalizedBuildResult));

            #region private static readonly string[] g11nKeys

            CodeMemberField field = new CodeMemberField();
            field.Name       = "g11nKeys";
            field.Type       = new CodeTypeReference(typeof(string[]));
            field.Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final;

            CodeArrayCreateExpression arrayInit = new CodeArrayCreateExpression(field.Type, this.g11nKeys.Count);
            foreach (string key in this.g11nKeys)
            {
                arrayInit.Initializers.Add(new CodePrimitiveExpression(key));
            }
            field.InitExpression = arrayInit;

            resourceType.Members.Add(field);

            #endregion private static readonly string[] g11nKeys

            #region IEnumerable<string> IGlobalizedBuildResult.GlobalizationKeys { get; }

            // add a readonly property returning the static data
            CodeMemberProperty property = new CodeMemberProperty();
            property.Name = "GlobalizationKeys";
            property.Type = new CodeTypeReference(typeof(IEnumerable <string>));
            property.PrivateImplementationType = new CodeTypeReference(typeof(IGlobalizedBuildResult));
            property.HasGet = true;
            // get { return g11nKeys; }
            property.GetStatements.Add(new CodeMethodReturnStatement(
                                           new CodeFieldReferenceExpression(
                                               new CodeTypeReferenceExpression(resourceType.Name),
                                               field.Name)));
            resourceType.Members.Add(property);

            #endregion IEnumerable<string> IGlobalizedBuildResult.GlobalizationKeys { get; }
        }
Exemplo n.º 11
0
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            using (StringWriter writer = new StringWriter())
            {
                IList <ParseException> parseErrors;
                try
                {
                    parseErrors = CssCompactor.Compact(
                        virtualPath,
                        sourceText,
                        writer,
                        null,
                        null,
                        CssCompactor.Options.None);
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                    parseErrors = null;
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
                    parseErrors = null;
                }

                if (parseErrors != null && parseErrors.Count > 0)
                {
                    errors.AddRange(parseErrors);
                }

                writer.Flush();

                resource  = sourceText;
                compacted = writer.ToString();
            }
        }
Exemplo n.º 12
0
        private IOptimizedResult ProcessPrecompiled(IResourceBuildHelper helper, string file)
        {
            IOptimizedResult result = ResourceHandler.Create <IOptimizedResult>(file);

            if (result != null)
            {
                if (!this.isMimeSet &&
                    !String.IsNullOrEmpty(result.ContentType) &&
                    !String.IsNullOrEmpty(result.FileExtension))
                {
                    this.contentType   = result.ContentType;
                    this.fileExtension = result.FileExtension;
                    this.isMimeSet     = true;
                }

                if (result is IGlobalizedBuildResult)
                {
                    this.GlobalizationKeys.AddRange(((IGlobalizedBuildResult)result).GlobalizationKeys);
                }

                helper.AddVirtualPathDependency(file);

                ICollection dependencies = BuildManager.GetVirtualPathDependencies(file);
                if (dependencies != null)
                {
                    foreach (string dependency in dependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }

                if (result is IDependentResult)
                {
                    foreach (string dependency in ((IDependentResult)result).VirtualPathDependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 13
0
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List <ParseException> errors)
        {
            using (StringWriter writer = new StringWriter())
            {
                IList <ParseException> parseErrors;
                try
                {
                    parseErrors = ScriptCompactionAdapter.Compact(
                        virtualPath,
                        sourceText,
                        writer);
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                    parseErrors = null;
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
                    parseErrors = null;
                }

                if (parseErrors != null && parseErrors.Count > 0)
                {
                    errors.AddRange(parseErrors);
                }

                writer.Flush();

                resource  = ScriptResourceCodeProvider.FirewallScript(virtualPath, sourceText, false);
                compacted = ScriptResourceCodeProvider.FirewallScript(virtualPath, writer.ToString(), true);

                this.ExtractGlobalizationKeys(compacted);
            }
        }
Exemplo n.º 14
0
        protected override void GenerateCodeExtensions(IResourceBuildHelper helper, CodeTypeDeclaration resourceType)
        {
            if (this.jbstWriter == null)
            {
                throw new InvalidOperationException("JbstCodeProvider: JbstWriter is missing");
            }

            base.GenerateCodeExtensions(helper, resourceType);

            string         jbstName   = this.jbstWriter.JbstName;
            AutoMarkupType autoMarkup = this.jbstWriter.AutoMarkup;

            #region private static readonly EcmaScriptIdentifier jbstName

            CodeMemberField field = new CodeMemberField();
            field.Name       = "JbstName";
            field.Type       = new CodeTypeReference(typeof(EcmaScriptIdentifier));
            field.Attributes = MemberAttributes.Private | MemberAttributes.Static | MemberAttributes.Final;

            field.InitExpression = new CodePrimitiveExpression(jbstName);

            resourceType.Members.Add(field);

            #endregion private static readonly EcmaScriptIdentifier jbstName

            #region public ResourceType() : base(ResourceType.JbstName, autoMarkup) {}

            CodeConstructor ctor = new CodeConstructor();
            ctor.Attributes = MemberAttributes.Public;
            ctor.BaseConstructorArgs.Add(new CodeFieldReferenceExpression(
                                             new CodeTypeReferenceExpression(resourceType.Name),
                                             "JbstName"));
            ctor.BaseConstructorArgs.Add(new CodeFieldReferenceExpression(
                                             new CodeTypeReferenceExpression(typeof(AutoMarkupType)), autoMarkup.ToString()));

            resourceType.Members.Add(ctor);

            #endregion public ResourceType() : base(ResourceType.JbstName, ResourceType.AutoMarkup) {}
        }
        protected internal override void ProcessExternalResource(
            IResourceBuildHelper helper,
            string url,
            out string preProcessed,
            out string compacted,
            List<ParseException> errors)
        {
            compacted = preProcessed = String.Empty;

            Uri uri;
            if (!Uri.TryCreate(url, UriKind.Absolute, out uri))
            {
                throw new ArgumentException("Invalid external URL");
            }

            string ext = Path.GetExtension(uri.AbsolutePath).Trim('.');
            CompilerType compiler = helper.GetDefaultCompilerTypeForLanguage(ext);
            if (!typeof(ResourceCodeProvider).IsAssignableFrom(compiler.CodeDomProviderType))
            {
                // don't know how to process any further
                return;
            }

            ResourceCodeProvider provider = (ResourceCodeProvider)Activator.CreateInstance(compiler.CodeDomProviderType);

            try
            {
                // concatenate the preprocessed source for current merge phase
                provider.ProcessExternalResource(
                    helper,
                    url,
                    out preProcessed,
                    out compacted,
                    errors);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, url, 0, 0, ex));
            }

            if (!this.isMimeSet &&
                !String.IsNullOrEmpty(provider.ContentType) &&
                !String.IsNullOrEmpty(provider.FileExtension))
            {
                this.contentType = provider.ContentType;
                this.fileExtension = provider.FileExtension;
                this.isMimeSet = true;
            }
        }
Exemplo n.º 16
0
 /// <summary>
 /// Process as external resources
 /// </summary>
 /// <param name="helper"></param>
 /// <param name="url"></param>
 /// <param name="preProcessed"></param>
 /// <param name="compacted"></param>
 /// <param name="errors"></param>
 protected internal abstract void ProcessExternalResource(
     IResourceBuildHelper helper,
     string url,
     out string preProcessed,
     out string compacted,
     List <ParseException> errors);
Exemplo n.º 17
0
        protected override void GenerateCodeExtensions(IResourceBuildHelper helper, CodeTypeDeclaration resourceType)
        {
            if (this.jbstWriter == null)
            {
                throw new InvalidOperationException("JbstCodeProvider: JbstWriter is missing");
            }

            base.GenerateCodeExtensions(helper, resourceType);

            string jbstName = this.jbstWriter.JbstName;
            AutoMarkupType autoMarkup = this.jbstWriter.AutoMarkup;

            #region private static readonly EcmaScriptIdentifier jbstName

            CodeMemberField field = new CodeMemberField();
            field.Name = "JbstName";
            field.Type = new CodeTypeReference(typeof(EcmaScriptIdentifier));
            field.Attributes = MemberAttributes.Private|MemberAttributes.Static|MemberAttributes.Final;

            field.InitExpression = new CodePrimitiveExpression(jbstName);

            resourceType.Members.Add(field);

            #endregion private static readonly EcmaScriptIdentifier jbstName

            #region public ResourceType() : base(ResourceType.JbstName, autoMarkup) {}

            CodeConstructor ctor = new CodeConstructor();
            ctor.Attributes = MemberAttributes.Public;
            ctor.BaseConstructorArgs.Add(new CodeFieldReferenceExpression(
                new CodeTypeReferenceExpression(resourceType.Name),
                "JbstName"));
            ctor.BaseConstructorArgs.Add(new CodeFieldReferenceExpression(
                new CodeTypeReferenceExpression(typeof(AutoMarkupType)), autoMarkup.ToString()));

            resourceType.Members.Add(ctor);

            #endregion public ResourceType() : base(ResourceType.JbstName, ResourceType.AutoMarkup) {}
        }
Exemplo n.º 18
0
        protected override void ProcessExternalResource(
            IResourceBuildHelper helper,
            string url,
            out string preProcessed,
            out string compacted,
            List<ParseException> errors)
        {
            compacted = preProcessed = String.Format(ScriptResourceCodeProvider.ExternalImport, url);

            preProcessed = ScriptResourceCodeProvider.FirewallScript(url, preProcessed, true);
            compacted = ScriptResourceCodeProvider.FirewallScript(url, compacted, true);
        }
Exemplo n.º 19
0
        protected override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List<ParseException> errors)
        {
            // parse JBST markup
            this.jbstWriter = new JbstWriter(virtualPath);

            try
            {
                HtmlDistiller parser = new HtmlDistiller();
                parser.EncodeNonAscii = false;
                parser.BalanceTags = false;
                parser.NormalizeWhitespace = false;
                parser.HtmlWriter = this.jbstWriter;
                parser.HtmlFilter = new NullHtmlFilter();
                parser.Parse(sourceText);

                // determine which globalization keys were used
                JbstCodeProvider.ExtractGlobalizationKeys(this.jbstWriter.JbstParseTree, this.GlobalizationKeys);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
            }

            string renderedTemplate;
            using (StringWriter sw = new StringWriter())
            {
                // render the pretty-printed version
                this.jbstWriter.Render(sw);
                sw.Flush();
                renderedTemplate = sw.ToString();

                resource = ScriptResourceCodeProvider.FirewallScript(virtualPath, renderedTemplate, false);
            }

            // min the output for better compaction
            compacted = ScriptCompactionAdapter.Compact(virtualPath, renderedTemplate, errors);
            compacted = ScriptResourceCodeProvider.FirewallScript(virtualPath, compacted, true);
        }
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List<ParseException> errors)
        {
            using (StringWriter writer = new StringWriter())
            {
                IList<ParseException> parseErrors;
                try
                {
                    parseErrors = CssCompactor.Compact(
                        virtualPath,
                        sourceText,
                        writer,
                        null,
                        null,
                        CssCompactor.Options.None);
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                    parseErrors = null;
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
                    parseErrors = null;
                }

                if (parseErrors != null && parseErrors.Count > 0)
                {
                    errors.AddRange(parseErrors);
                }

                writer.Flush();

                resource = sourceText;
                compacted = writer.ToString();
            }
        }
Exemplo n.º 21
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();
        }
        private IOptimizedResult ProcessPrecompiled(IResourceBuildHelper helper, string file)
        {
            IOptimizedResult result = ResourceHandler.Create<IOptimizedResult>(file);
            if (result != null)
            {
                if (!this.isMimeSet &&
                    !String.IsNullOrEmpty(result.ContentType) &&
                    !String.IsNullOrEmpty(result.FileExtension))
                {
                    this.contentType = result.ContentType;
                    this.fileExtension = result.FileExtension;
                    this.isMimeSet = true;
                }

                if (result is IGlobalizedBuildResult)
                {
                    this.GlobalizationKeys.AddRange(((IGlobalizedBuildResult)result).GlobalizationKeys);
                }

                helper.AddVirtualPathDependency(file);

                ICollection dependencies = BuildManager.GetVirtualPathDependencies(file);
                if (dependencies != null)
                {
                    foreach (string dependency in dependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }

                if (result is IDependentResult)
                {
                    foreach (string dependency in ((IDependentResult)result).VirtualPathDependencies)
                    {
                        helper.AddVirtualPathDependency(dependency);
                    }
                }
            }

            return result;
        }
        private void ProcessEmbeddedResource(
            IResourceBuildHelper helper,
            string source,
            out string preProcessed,
            out string compacted,
            List<ParseException> errors)
        {
            preProcessed = source.Replace(" ", "");
            string[] parts = preProcessed.Split(TypeDelims, 2, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length < 2 ||
                String.IsNullOrEmpty(parts[0]) ||
                String.IsNullOrEmpty(parts[1]))
            {
                compacted = preProcessed = null;
                return;
            }

            parts[0] = MergeResourceCodeProvider.ScrubResourceName(parts[0]);

            // load resources from Assembly
            Assembly assembly = Assembly.Load(parts[1]);
            helper.AddAssemblyDependency(assembly);

            ManifestResourceInfo info = assembly.GetManifestResourceInfo(parts[0]);
            if (info == null)
            {
                compacted = preProcessed = null;
                return;
            }

            using (Stream stream = assembly.GetManifestResourceStream(parts[0]))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    preProcessed = reader.ReadToEnd();
                    compacted = null;
                }
            }

            string ext = Path.GetExtension(parts[0]).Trim('.');
            CompilerType compiler = helper.GetDefaultCompilerTypeForLanguage(ext);
            if (!typeof(ResourceCodeProvider).IsAssignableFrom(compiler.CodeDomProviderType))
            {
                // don't know how to process any further
                compacted = preProcessed;
                return;
            }

            ResourceCodeProvider provider = (ResourceCodeProvider)Activator.CreateInstance(compiler.CodeDomProviderType);

            try
            {
                // concatenate the preprocessed source for current merge phase
                provider.ProcessResource(
                    helper,
                    parts[0],
                    preProcessed,
                    out preProcessed,
                    out compacted,
                    errors);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, parts[0], 0, 0, ex));
            }

            if (!this.isMimeSet &&
                !String.IsNullOrEmpty(provider.ContentType) &&
                !String.IsNullOrEmpty(provider.FileExtension))
            {
                this.contentType = provider.ContentType;
                this.fileExtension = provider.FileExtension;
                this.isMimeSet = true;
            }
        }
Exemplo n.º 24
0
        private void ProcessEmbeddedResource(
            IResourceBuildHelper helper,
            string source,
            out string preProcessed,
            out string compacted,
            List <ParseException> errors)
        {
            preProcessed = source.Replace(" ", "");
            string[] parts = preProcessed.Split(TypeDelims, 2, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length < 2 ||
                String.IsNullOrEmpty(parts[0]) ||
                String.IsNullOrEmpty(parts[1]))
            {
                compacted = preProcessed = null;
                return;
            }

            parts[0] = MergeResourceCodeProvider.ScrubResourceName(parts[0]);

            // load resources from Assembly
            Assembly assembly = Assembly.Load(parts[1]);

            helper.AddAssemblyDependency(assembly);

            ManifestResourceInfo info = assembly.GetManifestResourceInfo(parts[0]);

            if (info == null)
            {
                compacted = preProcessed = null;
                return;
            }

            using (Stream stream = assembly.GetManifestResourceStream(parts[0]))
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    preProcessed = reader.ReadToEnd();
                    compacted    = null;
                }
            }

            string       ext      = Path.GetExtension(parts[0]).Trim('.');
            CompilerType compiler = helper.GetDefaultCompilerTypeForLanguage(ext);

            if (!typeof(ResourceCodeProvider).IsAssignableFrom(compiler.CodeDomProviderType))
            {
                // don't know how to process any further
                compacted = preProcessed;
                return;
            }

            ResourceCodeProvider provider = (ResourceCodeProvider)Activator.CreateInstance(compiler.CodeDomProviderType);

            try
            {
                // concatenate the preprocessed source for current merge phase
                provider.ProcessResource(
                    helper,
                    parts[0],
                    preProcessed,
                    out preProcessed,
                    out compacted,
                    errors);
            }
            catch (ParseException ex)
            {
                errors.Add(ex);
            }
            catch (Exception ex)
            {
                errors.Add(new ParseError(ex.Message, parts[0], 0, 0, ex));
            }

            if (!this.isMimeSet &&
                !String.IsNullOrEmpty(provider.ContentType) &&
                !String.IsNullOrEmpty(provider.FileExtension))
            {
                this.contentType   = provider.ContentType;
                this.fileExtension = provider.FileExtension;
                this.isMimeSet     = true;
            }
        }
        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();
        }
        protected internal override void ProcessResource(
            IResourceBuildHelper helper,
            string virtualPath,
            string sourceText,
            out string resource,
            out string compacted,
            List<ParseException> errors)
        {
            using (StringWriter writer = new StringWriter())
            {
                IList<ParseException> parseErrors;
                try
                {
                    parseErrors = ScriptCompactionAdapter.Compact(
                        virtualPath,
                        sourceText,
                        writer);
                }
                catch (ParseException ex)
                {
                    errors.Add(ex);
                    parseErrors = null;
                }
                catch (Exception ex)
                {
                    errors.Add(new ParseError(ex.Message, virtualPath, 0, 0, ex));
                    parseErrors = null;
                }

                if (parseErrors != null && parseErrors.Count > 0)
                {
                    errors.AddRange(parseErrors);
                }

                writer.Flush();

                resource = ScriptResourceCodeProvider.FirewallScript(virtualPath, sourceText, false);
                compacted = ScriptResourceCodeProvider.FirewallScript(virtualPath, writer.ToString(), true);

                this.ExtractGlobalizationKeys(compacted);
            }
        }