Ejemplo n.º 1
0
        /// <summary>
        /// Gets a script reference given a path.
        /// </summary>
        ///
        /// <exception cref="FileNotFoundException">
        /// Thrown when the requested file is not present.
        /// </exception>
        ///
        /// <param name="name">
        /// The name to use for this script.
        /// </param>
        /// <param name="virtualPath">
        /// Virtual path to the script.
        /// </param>
        ///
        /// <returns>
        /// The script reference.
        /// </returns>

        protected ScriptRef GetScriptRef(string name, string virtualPath)
        {
            ScriptRef scriptRef;
            string    normalizedPath = PathList.NormalizePath(virtualPath);
            string    normalizedName = PathList.NormalizeName(name);

            if (ResolvedDependencies.TryGetValue(normalizedName, out scriptRef))
            {
                return(scriptRef);
            }

            string       fileName;
            ScriptParser parser = null;

            try
            {
                fileName = MapPath(normalizedPath);
                parser   = new ScriptParser(fileName);
            }
            catch (FileNotFoundException e)
            {
                if (!IgnoreErrors)
                {
                    throw e;
                }
            }

            scriptRef = new ScriptRef
            {
                Name = normalizedName,
                Path = normalizedPath
            };

            // Parser can be null if there was an error loading the script, but IgnoreErrors=true. If this
            // is the case just skip everything, there will be no dependencies.
            //
            if (parser != null)
            {
                var options = new HashSet <string>();

                using (parser)
                {
                    string line;
                    while ((line = parser.ReadLine()) != null &&
                           !parser.AnyCodeYet)
                    {
                        var match        = Patterns.Dependency.Match(line);
                        var matchOptions = Patterns.Options.Match(line);

                        if (match.Success)
                        {
                            string depName  = match.Groups["dep"].Value;
                            var    optGroup = match.Groups["opt"];

                            scriptRef.Dependencies.Add(new ScriptRef
                            {
                                Name      = PathList.NormalizeName(depName),
                                Path      = null,
                                NoCombine = optGroup.Captures.Any <Capture>(item => item.Value == "nocombine")
                            });
                        }
                        else if (matchOptions.Success)
                        {
                            foreach (Group grp in matchOptions.Groups)
                            {
                                options.Add(grp.Value.ToLower());
                            }
                        }
                    }

                    scriptRef.ScriptHash = parser.FileHash;
                    scriptRef.NoCombine  = options.Contains("nocombine");
                }
            }

            if (!NoCache)
            {
                ResolvedDependencies[normalizedName] = scriptRef;
            }

            return(scriptRef);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a script reference given a path.
        /// </summary>
        ///
        /// <param name="virtualPath">
        /// Virtual path to the script.
        /// </param>
        ///
        /// <returns>
        /// The script reference.
        /// </returns>

        protected ScriptRef GetScriptRef(string virtualPath)
        {
            ScriptRef scriptRef;

            var uniquePath = ScriptEnvironment.UniquePath(virtualPath);

            if (ResolvedDependencies.TryGetValue(uniquePath, out scriptRef))
            {
                return(scriptRef);
            }

            ScriptParser parser = new ScriptParser(ScriptEnvironment, uniquePath);

            scriptRef = new ScriptRef
            {
                Path = uniquePath
            };

            if (parser.IsPhysicalFile)
            {
                string textOnly = "";

                using (parser)
                {
                    string line;
                    while ((line = parser.ReadLine()) != null &&
                           !parser.AnyCodeYet)
                    {
                        textOnly += line + System.Environment.NewLine;

                        ScriptRef dependencyRef;
                        if (TryGetDependencyRef_UsingFormat(scriptRef, line, out dependencyRef))
                        {
                            scriptRef.Dependencies.Add(dependencyRef);
                        }
                    }


                    scriptRef.ScriptHash = parser.FileHash;
                }

                // now parse the entire text again for the XML format

                if (Options.HasFlag(ViewEngineOptions.ResolveXmlReferences))
                {
                    var xml = CQ.CreateFragment(textOnly);
                    foreach (var el in xml["reference"])
                    {
                        ScriptRef dependencyRef;
                        if (TryGetDependencyRef_CQ(scriptRef, el, out dependencyRef))
                        {
                            scriptRef.Dependencies.Add(dependencyRef);
                        }
                    }
                }
            }

            if (!Options.HasFlag(ViewEngineOptions.NoCache))
            {
                ResolvedDependencies[uniquePath] = scriptRef;
            }

            return(scriptRef);
        }