Пример #1
0
 public TsDelegate(TsScript script, string name, ITsInstance target)
 {
     Target = target;
     Script = script;
     Name   = name;
 }
        /// <summary>
        /// Preprocess a script content
        /// </summary>
        /// <param name="assetContent">Text content of TypeScript-asset</param>
        /// <param name="assetUrl">URL of TypeScript-asset file</param>
        /// <returns>Preprocessed text content of TypeScript-asset</returns>
        public TsScript PreprocessScript(string assetContent, string assetUrl)
        {
            var script = new TsScript(assetUrl, assetContent);

            int contentLength = assetContent.Length;
            if (contentLength == 0)
            {
                return script;
            }

            MatchCollection referenceCommentMatches = _referenceCommentsRegex.Matches(assetContent);
            if (referenceCommentMatches.Count == 0)
            {
                return script;
            }

            var nodeMatches = new List<TsNodeMatch>();

            foreach (Match referenceCommentMatch in referenceCommentMatches)
            {
                var nodeMatch = new TsNodeMatch(referenceCommentMatch.Index,
                    referenceCommentMatch.Length,
                    TsNodeType.ReferenceComment,
                    referenceCommentMatch);
                nodeMatches.Add(nodeMatch);
            }

            MatchCollection multilineCommentMatches = CommonRegExps.CStyleMultilineCommentRegex.Matches(assetContent);

            foreach (Match multilineCommentMatch in multilineCommentMatches)
            {
                var nodeMatch = new TsNodeMatch(multilineCommentMatch.Index,
                    multilineCommentMatch.Length,
                    TsNodeType.MultilineComment,
                    multilineCommentMatch);
                nodeMatches.Add(nodeMatch);
            }

            nodeMatches = nodeMatches
                .OrderBy(n => n.Position)
                .ThenByDescending(n => n.Length)
                .ToList()
                ;

            var contentBuilder = new StringBuilder();
            int endPosition = contentLength - 1;
            int currentPosition = 0;

            foreach (TsNodeMatch nodeMatch in nodeMatches)
            {
                TsNodeType nodeType = nodeMatch.NodeType;
                int nodePosition = nodeMatch.Position;
                Match match = nodeMatch.Match;

                if (nodePosition < currentPosition)
                {
                    continue;
                }

                if (nodeType == TsNodeType.ReferenceComment)
                {
                    ProcessOtherContent(contentBuilder, assetContent,
                        ref currentPosition, nodePosition);

                    GroupCollection referenceCommentGroup = match.Groups;

                    string url = referenceCommentGroup["url"].Value.Trim();
                    string quote = referenceCommentGroup["quote"].Success ?
                        referenceCommentGroup["quote"].Value : @"""";
                    string processedReferenceUrl;

                    string referenceComment = match.Value;
                    string processedReferenceComment = ProcessReferenceComment(assetUrl, url, quote,
                        out processedReferenceUrl);

                    if (!string.IsNullOrWhiteSpace(processedReferenceUrl))
                    {
                        var references = script.References;
                        string urlInUpperCase = processedReferenceUrl.ToUpperInvariant();

                        if (references.Count(r => r.ToUpperInvariant() == urlInUpperCase) == 0)
                        {
                            references.Add(processedReferenceUrl);
                        }
                    }

                    contentBuilder.Append(processedReferenceComment);
                    currentPosition += referenceComment.Length;
                }
                else if (nodeType == TsNodeType.MultilineComment)
                {
                    int nextPosition = nodePosition + match.Length;

                    ProcessOtherContent(contentBuilder, assetContent,
                                        ref currentPosition, nextPosition);
                }
            }

            if (currentPosition > 0 && currentPosition <= endPosition)
            {
                ProcessOtherContent(contentBuilder, assetContent,
                    ref currentPosition, endPosition + 1);
            }

            script.Content = contentBuilder.ToString();

            return script;
        }
Пример #3
0
 public TsDelegate(TsScript script, string name)
 {
     Target = null;
     Script = script;
     Name   = name;
 }
        /// <summary>
        /// Fills the list of TypeScript-files, references to which have been added to a TypeScript-asset
        /// by using the "reference" comments
        /// </summary>
        /// <param name="rootAssetUrl">URL of root TypeScript-asset file</param>
        /// <param name="parentScript">Parent TypeScript-script</param>
        /// <param name="dependencies">List of TypeScript-files, references to which have been
        /// added to a TypeScript-asset by using the "reference" comments</param>
        public void FillDependencies(string rootAssetUrl, TsScript parentScript,
            DependencyCollection dependencies)
        {
            foreach (string referenceUrl in parentScript.References)
            {
                string dependencyUrl = referenceUrl;

                if (string.Equals(dependencyUrl, rootAssetUrl, StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                if (!dependencies.ContainsUrl(dependencyUrl))
                {
                    string dependencyExtension = Path.GetExtension(dependencyUrl);
                    if (TypeScriptFileExtensionHelpers.IsTypeScript(dependencyExtension)
                        || CoreFileExtensionHelpers.IsJavaScript(dependencyExtension))
                    {
                        if (TsScriptExists(dependencyUrl))
                        {
                            TsScript script = GetTsScript(dependencyUrl);

                            var dependency = new Dependency(dependencyUrl, script.Content);
                            dependencies.Add(dependency);

                            FillDependencies(rootAssetUrl, script, dependencies);
                        }
                        else
                        {
                            throw new FileNotFoundException(
                                string.Format(CoreStrings.Common_FileNotExist, dependencyUrl));
                        }
                    }
                }
            }
        }