Inheritance: JsonFx.Compilation.ResourceCodeProvider
コード例 #1
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;
            }
        }
コード例 #2
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();
        }