Exemplo n.º 1
0
        /// <summary>
        /// Validates the views.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="viewsDirectoryPath">The views directory path.</param>
        /// <returns>List of LocalizedFileItems</returns>
        public static List <LocalizedFileItem> Validate(string projectPath, string resourcesDirectoryPath = @"\Resources\Views", string viewsDirectoryPath = @"\Views")
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, viewsDirectoryPath, "", "*.cshtml");

            var localizedFileItems = new List <LocalizedFileItem>();

            var regex = new Regex(@"ViewLocalization\[""(?<name>\S+)("",|""])");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var item = new LocalizedFileItem(projectPath, viewsDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (Match match in regex.Matches(content))
                {
                    item.Names.Add(match.Groups["name"].Value);
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }
        /// <summary>
        /// Validates the models.
        /// </summary>
        /// <param name="assembly">The assembly.</param>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="modelsDirectoryPath">The models directory path.</param>
        /// <returns>List of LocalizedFileItems</returns>
        public static List <LocalizedFileItem> Validate(Assembly assembly, string projectPath, string resourcesDirectoryPath = @"\Resources\Models", string modelsDirectoryPath = @"\Models")
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, modelsDirectoryPath, "", "*.cs");

            var localizedFileItems = new List <LocalizedFileItem>();

            var namespaceRegex = new Regex(@"namespace (?<namespace>\S+)");
            var classRegex     = new Regex(@"public class (?<class>\S+)");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var @namespace = namespaceRegex.Match(content).Groups["namespace"].Value;
                var @class     = classRegex.Match(content).Groups["class"].Value;

                var type = assembly.GetType($"{@namespace}.{@class}");

                var item = new LocalizedFileItem(projectPath, modelsDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (var property in type.GetRuntimeProperties())
                {
                    var displayAttribute = property.GetCustomAttribute <DisplayAttribute>();

                    if (displayAttribute == null)
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(displayAttribute.Name))
                    {
                        item.Names.Add(displayAttribute.Name);
                    }
                    if (!string.IsNullOrWhiteSpace(displayAttribute.Prompt))
                    {
                        item.Names.Add(displayAttribute.Prompt);
                    }
                    if (!string.IsNullOrWhiteSpace(displayAttribute.Description))
                    {
                        item.Names.Add(displayAttribute.Description);
                    }
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }
        /// <summary>
        /// Validates the controllers.
        /// </summary>
        /// <param name="projectPath">The project path.</param>
        /// <param name="resourcesDirectoryPath">The resources directory path.</param>
        /// <param name="controllersDirectoryPath">The controllers directory path.</param>
        /// <param name="predefinedLocalizedFileItems">The predefined localized file items.</param>
        /// <returns>
        /// List of LocalizedFileItems
        /// </returns>
        public static List <LocalizedFileItem> Validate(string projectPath, string resourcesDirectoryPath = @"\Resources\Controllers", string controllersDirectoryPath = @"\Controllers", List <LocalizedFileItem> predefinedLocalizedFileItems = null)
        {
            var fileItems = FileItemHelpers.GetFileItems(projectPath, controllersDirectoryPath, "", "*Controller.cs");

            var localizedFileItems = new List <LocalizedFileItem>();

            var regex = new Regex(@"_controllerLocalization\[""(?<name>\S+)("",|""])");

            foreach (var fileItem in fileItems)
            {
                var content = File.ReadAllText(fileItem.GetFullPath());

                var item = new LocalizedFileItem(projectPath, controllersDirectoryPath, fileItem.RelativePath, fileItem.FileName);

                foreach (Match match in regex.Matches(content))
                {
                    item.Names.Add(match.Groups["name"].Value);
                }

                var predefinedLocalizedFileItem = predefinedLocalizedFileItems?.FirstOrDefault(x => x.RelativePath == item.RelativePath && x.FileNameWithoutExtension == item.FileNameWithoutExtension);

                if (predefinedLocalizedFileItem != null)
                {
                    foreach (var name in predefinedLocalizedFileItem.Names)
                    {
                        item.Names.Add(name);
                    }
                }

                if (item.Names.Any())
                {
                    localizedFileItems.Add(item);
                }
            }

            LocalizationAssert.Validate(projectPath, resourcesDirectoryPath, localizedFileItems);

            return(localizedFileItems);
        }