public void Process()
        {
            try
            {
                var parser = new JavaScriptParser();
                var program = parser.Parse(OriginalString);
                var visitor = new RequireVisitor();
                var result = visitor.Visit(program, RelativeFileName);

                var lines = GetScriptLines(OriginalString);

                var flattenedResults = result.GetFlattened();

                var deps =
                    flattenedResults.SelectMany(r => r.Dependencies)
                        .Where(r => !r.Contains("!"))
                        .Except(new List<string> { "require", "module", "exports" });

                Dependencies = deps.Select(r => GetModulePath(r)).ToList();
                var shim = this.GetShim(RelativeFileName);
                if (shim != null)
                {
                    Dependencies.AddRange(shim.Dependencies.Select(r => this.GetModulePath(r.Dependency)));
                }

                Dependencies = Dependencies.Distinct().ToList();

                flattenedResults.ForEach(
                    x =>
                    {
                        EnsureHasRange(x.ParentNode.Node, lines);
                        EnsureHasRange(x.DependencyArrayNode, lines);
                        EnsureHasRange(x.ModuleDefinitionNode, lines);
                        EnsureHasRange(x.ModuleIdentifierNode, lines);
                        EnsureHasRange(x.SingleDependencyNode, lines);
                        var arguments = x.ParentNode.Node.As<CallExpression>().Arguments;
                        foreach (var arg in arguments)
                        {
                            EnsureHasRange(arg, lines);
                        }
                    });

                var transformations = this.GetTransformations(result, flattenedResults);
                var text = OriginalString;
                transformations.ExecuteAll(ref text);
                ProcessedString = text;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while processing {0}: {1}", RelativeFileName, ex.Message), ex);
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var filePath = GetPath(@"Scripts\BForms\Bundles\js\components.js");
            var minimalPath = GetPath(@"Scripts\minimal.js");
            var initUIPath = GetPath(@"Scripts\BForms\Plugins\UI\bforms.initUI.js");

            var text = File.ReadAllText(initUIPath);
            var parser = new JavaScriptParser();
            var program = parser.Parse(text);
            var visitor = new RequireVisitor();
            var result = visitor.Visit(program, initUIPath);

            var lines = GetScriptLines(text);

            var flattenedResults = result.GetFlattened();

            flattenedResults.ForEach(
                x =>
                    {
                        EnsureHasRange(x.ParentNode.Node, lines);
                        EnsureHasRange(x.DependencyArrayNode, lines);
                        EnsureHasRange(x.ModuleDefinitionNode, lines);
                        EnsureHasRange(x.ModuleIdentifierNode, lines);
                        EnsureHasRange(x.SingleDependencyNode, lines);
                        var arguments = x.ParentNode.Node.As<CallExpression>().Arguments;
                        foreach (var arg in arguments)
                        {
                            EnsureHasRange(arg, lines);    
                        }
                    });

            var reqLines = flattenedResults.Select(x => GetTextFromFullScript(x.ParentNode.Node.Range, text)).ToList();
            
            var modifiedString = text;
            var transformCollection = new TransformationCollection();
            foreach (var req in flattenedResults)
            {
                transformCollection.Add(ToDefineTransformation.Create(req));
                if (req.Type != RequireCallType.Define)
                {
                    transformCollection.Add(AddIdentifierTransformation.Create(req, "gogu"));
                }
            }

            transformCollection.ExecuteAll(ref modifiedString);    
        }
        public void Process()
        {
            try
            {
                var parser = new JavaScriptParser();
                var program = parser.Parse(OriginalString);
                var visitor = new RequireVisitor();
                var result = visitor.Visit(program, RelativeFileName);

                var lines = GetScriptLines(OriginalString);

                var flattenedResults = result.GetFlattened();

                var deps = flattenedResults
                    .SelectMany(r => r.Dependencies)
                    .Where(r => !r.Contains("!"))
                    .Except(new List<string> { "require", "module", "exports" });

                //Make existing relative paths relative to the Scripts folder
                var scriptPath = System.IO.Path.GetDirectoryName(RelativeFileName).Replace(@"\", "/") + "/";
                deps = deps.Select(r =>
                {
                    if (r.StartsWith("."))
                    {
                        r = scriptPath + r;
                    }
                    return r;
                });

                //Expand named paths relative to the scripts folder
                Dependencies = deps.Select(r => ExpandPaths(r, configuration)).ToList();

                var shim = this.GetShim(RelativeFileName);
                if (shim != null)
                {
                    Dependencies.AddRange(shim.Dependencies.Select(r => ExpandPaths(r.Dependency, configuration)));
                }

                Dependencies = Dependencies.Distinct().ToList();

                flattenedResults.ForEach(
                    x =>
                    {
                        EnsureHasRange(x.ParentNode.Node, lines);
                        EnsureHasRange(x.DependencyArrayNode, lines);
                        EnsureHasRange(x.ModuleDefinitionNode, lines);
                        EnsureHasRange(x.ModuleIdentifierNode, lines);
                        EnsureHasRange(x.SingleDependencyNode, lines);
                        var arguments = x.ParentNode.Node.As<CallExpression>().Arguments;
                        foreach (var arg in arguments)
                        {
                            EnsureHasRange(arg, lines);
                        }
                    });

                var transformations = this.GetTransformations(result, flattenedResults);
                var text = OriginalString;
                transformations.ExecuteAll(ref text);
                ProcessedString = text;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error while processing {0}: {1}", RelativeFileName, ex.Message), ex);
            }
        }