public override void Process(string path, string basePath, LocalizableStringCollection strings)
        {
            var codeMetadataProvider = new CodeMetadataProvider(basePath);
            var csharpWalker         = new ExtractingCodeWalker(
                new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(codeMetadataProvider),
                new PluralStringExtractor(codeMetadataProvider),
                new DataAnnotationStringExtractor(codeMetadataProvider)
            }, strings);

            foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories).OrderBy(file => file))
            {
                if (Path.GetFileName(file).EndsWith(".cshtml.g.cs"))
                {
                    continue;
                }

                using (var stream = File.OpenRead(file)) {
                    using (var reader = new StreamReader(stream)) {
                        var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file);

                        csharpWalker.Visit(syntaxTree.GetRoot());
                    }
                }
            }

            base.Process(path, basePath, strings);
        }
        public void Process(string path, string basePath, LocalizableStringCollection strings)
        {
            if (string.IsNullOrEmpty(path))
            {
                path = _defaultPath;
            }

            if (string.IsNullOrEmpty(basePath))
            {
                basePath = _defaultPath;
            }

            var codeMetadataProvider = new CodeMetadataProvider(basePath);
            var csharpWalker         = new ExtractingCodeWalker(
                new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(codeMetadataProvider),
                new PluralStringExtractor(codeMetadataProvider),
                new DataAnnotationStringExtractor(codeMetadataProvider)
            }, strings);

            foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories).OrderBy(file => file))
            {
                using (var stream = File.OpenRead(file))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file);

                        csharpWalker.Visit(syntaxTree.GetRoot());
                    }
                }
            }
        }
        public void Process(string path, string basePath, LocalizableStringCollection strings)
        {
            /* C# */
            var codeMetadataProvider = new CodeMetadataProvider(basePath);
            var csharpWalker         = new ExtractingCodeWalker(
                new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(codeMetadataProvider),
                new PluralStringExtractor(codeMetadataProvider)
            }, strings);

            foreach (var file in Directory.EnumerateFiles(path, "*.cs", SearchOption.AllDirectories))
            {
                if (Path.GetFileName(file).EndsWith(".cshtml.g.cs"))
                {
                    continue;
                }

                using (var stream = File.OpenRead(file)) {
                    using (var reader = new StreamReader(stream)) {
                        var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file);

                        csharpWalker.Visit(syntaxTree.GetRoot());
                    }
                }
            }

            /* CSHTML */
            var razorMetadataProvider = new RazorMetadataProvider(basePath);
            var razorWalker           = new ExtractingCodeWalker(
                new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(razorMetadataProvider),
                new PluralStringExtractor(razorMetadataProvider)
            }, strings);

            var compiledViews = ViewCompiler.CompileViews(path);

            foreach (var view in compiledViews)
            {
                try {
                    var syntaxTree = CSharpSyntaxTree.ParseText(view.GeneratedCode, path: view.FilePath);
                    razorWalker.Visit(syntaxTree.GetRoot());
                } catch (Exception) {
                    Console.WriteLine("Process failed for: {0}", view.FilePath);
                }
            }
        }
Esempio n. 4
0
        private static void ProcessProject(string projectPath, string projectBasePath, string outputFilePath, bool parseLiquid)
        {
            var strings = new LocalizableStringCollection();

            var codeMetadataProvider      = new CodeMetadataProvider(projectBasePath);
            var localizedStringsCollector = new CSharpCodeVisitor(
                new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(codeMetadataProvider),
                new PluralStringExtractor(codeMetadataProvider)
            }, strings);

            foreach (var file in Directory.EnumerateFiles(projectPath, "*.cs", SearchOption.AllDirectories))
            {
                if (Path.GetFileName(file).EndsWith(".g.cshtml.cs"))
                {
                    continue;
                }

                using (var stream = File.OpenRead(file)) {
                    using (var reader = new StreamReader(stream)) {
                        var syntaxTree = CSharpSyntaxTree.ParseText(reader.ReadToEnd(), path: file);

                        localizedStringsCollector.Visit(syntaxTree.GetRoot());
                    }
                }
            }

            if (parseLiquid)
            {
                var liquidMetadataProvider = new LiquidMetadataProvider(projectBasePath);
                var liquidVisitor          = new LiquidVisitor(new[] { new LiquidStringExtractor(liquidMetadataProvider) }, strings);
                var liquidParser           = _liquidParseFactory.CreateParser();

                foreach (var file in Directory.EnumerateFiles(projectPath, "*.liquid", SearchOption.AllDirectories))
                {
                    using (var stream = File.OpenRead(file)) {
                        using (var reader = new StreamReader(stream)) {
                            if (liquidParser.TryParse(reader.ReadToEnd(), true, out var ast, out var errors))
                            {
                                foreach (var statement in ast)
                                {
                                    liquidVisitor.Visit(new LiquidStatementContext()
                                    {
                                        Statement = statement, FilePath = file
                                    });
                                }
                            }
                        }
                    }
                }
            }

            var razorMetadataProvider = new RazorMetadataProvider(projectBasePath);

            localizedStringsCollector.Extractors = new IStringExtractor <SyntaxNode>[] {
                new SingularStringExtractor(razorMetadataProvider),
                new PluralStringExtractor(razorMetadataProvider)
            };

            var compiledViews = ViewCompiler.CompileViews(projectPath);
            var cs            = new CSharpCodeProvider();

            foreach (var view in compiledViews)
            {
                try {
                    var syntaxTree = CSharpSyntaxTree.ParseText(view.GeneratedCode, path: view.FilePath);
                    localizedStringsCollector.Visit(syntaxTree.GetRoot());
                } catch (Exception ex) {
                    Console.WriteLine("Compile fail for: {0}", view.FilePath);
                }
            }

            if (strings.Values.Any())
            {
                var outputDirectory = Path.GetDirectoryName(outputFilePath);
                Directory.CreateDirectory(outputDirectory);

                using (var potFile = new PotFile(outputFilePath)) {
                    potFile.WriteRecord(strings.Values);
                }
            }

            Console.WriteLine($"{Path.GetFileName(projectPath)}: Found {strings.Values.Count()} strings.");
        }