Beispiel #1
0
        public void AddViewsFromDirectory(string sourceDirectory, List <ViewNode> featureViews)
        {
            var viewsSourceDirectory = Path.GetFullPath(sourceDirectory);

            var viewFiles = Directory.GetFiles(viewsSourceDirectory, $"*{Config.ViewsPattern}*", SearchOption.AllDirectories);

            foreach (var viewFile in viewFiles)
            {
                var featureViewPath = Path.GetFullPath(viewFile)
                                      .Split(new[] { $@"{viewsSourceDirectory}\" }, StringSplitOptions.RemoveEmptyEntries)
                                      .Last();

                var parts = featureViewPath
                            .Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);

                var namespaces = parts
                                 .Take(parts.Length - 1)
                                 .Select(Helpers.ToPascalCaseFromKebabCase)
                                 .ToList();

                var featureNamespace = namespaces.First();

                if (featureViews.All(v => v.Name != featureNamespace))
                {
                    featureViews.Add(new ViewNode
                    {
                        Name = featureNamespace
                    });
                }

                var subFeatures = namespaces
                                  .Skip(1)
                                  .ToList();

                var viewNode         = featureViews.Single(v => v.Name == featureNamespace);
                var parentFolderName = subFeatures.LastOrDefault() ?? featureNamespace;

                foreach (var subFeature in subFeatures)
                {
                    var doesViewNodeHasSubFeature = viewNode.ChildViews
                                                    .Any(v => v.Name == subFeature);

                    if (!doesViewNodeHasSubFeature)
                    {
                        var subFeatureNode = new ViewNode
                        {
                            Name = subFeature
                        };

                        viewNode.ChildViews.Add(subFeatureNode);
                    }

                    viewNode = viewNode.ChildViews
                               .Single(v => v.Name == subFeature);
                }

                var fullViewNameInKebabCase = parts
                                              .Last()
                                              .Split(new[] { Config.ViewsPattern }, StringSplitOptions.RemoveEmptyEntries)
                                              .First();

                var fullViewNameInPascalCase = Helpers.ToPascalCaseFromKebabCase(fullViewNameInKebabCase);

                var viewName = fullViewNameInPascalCase != parentFolderName
                    ? Regex.Replace(fullViewNameInPascalCase, $"^{parentFolderName}", string.Empty)
                    : fullViewNameInPascalCase;

                viewNode.ViewEntries.Add(new ViewEntry
                {
                    Name = viewName,
                    Path = featureViewPath.Replace(@"\", "/")
                });
            }
        }
Beispiel #2
0
        public void AddViewsFromDirectory(ViewConfig viewConfig, List <ViewNode> featureViews)
        {
            var prefix    = (viewConfig.Prefix ?? string.Empty);
            var urlEncode = viewConfig.UrlEncodePath;

            var sourceDirectory        = viewConfig.SourceDirectory;
            var viewsIgnoredExtensions = Config.ViewsIgnoredExtensions;
            var viewsSourceDirectory   = Path.GetFullPath(sourceDirectory);

            var viewFiles = Directory.GetFiles(viewsSourceDirectory, $"*{Config.ViewsPattern}*", SearchOption.AllDirectories)
                            .Where(filename => !viewsIgnoredExtensions.Any(ext => filename.EndsWith(ext)))
                            .ToArray();

            foreach (var viewFile in viewFiles)
            {
                var featureViewPath = Path.GetFullPath(viewFile)
                                      .Split(new[] { $@"{viewsSourceDirectory}\" }, StringSplitOptions.RemoveEmptyEntries)
                                      .Last();

                var parts = featureViewPath
                            .Split(new[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);

                var namespaces = parts
                                 .Take(parts.Length - 1)
                                 .Select(Helpers.ToPascalCaseFromKebabCase)
                                 .ToList();

                var featureNamespace = namespaces.First();

                if (featureViews.All(v => v.Name != featureNamespace))
                {
                    featureViews.Add(new ViewNode
                    {
                        Name = featureNamespace
                    });
                }

                var subFeatures = namespaces
                                  .Skip(1)
                                  .ToList();

                var viewNode         = featureViews.Single(v => v.Name == featureNamespace);
                var parentFolderName = subFeatures.LastOrDefault() ?? featureNamespace;

                foreach (var subFeature in subFeatures)
                {
                    var doesViewNodeHasSubFeature = viewNode.ChildViews
                                                    .Any(v => v.Name == subFeature);

                    if (!doesViewNodeHasSubFeature)
                    {
                        var subFeatureNode = new ViewNode
                        {
                            Name = subFeature
                        };

                        viewNode.ChildViews.Add(subFeatureNode);
                    }

                    viewNode = viewNode.ChildViews
                               .Single(v => v.Name == subFeature);
                }

                var fullViewNameInKebabCase = parts
                                              .Last()
                                              .Split(new[] { Config.ViewsPattern }, StringSplitOptions.RemoveEmptyEntries)
                                              .First();

                var fullViewNameInPascalCase = Helpers.ToPascalCaseFromKebabCase(fullViewNameInKebabCase);

                var viewName = fullViewNameInPascalCase != parentFolderName
                    ? Regex.Replace(fullViewNameInPascalCase, $"^{parentFolderName}", string.Empty)
                    : fullViewNameInPascalCase;

                var formattedPath = featureViewPath.Replace(@"\", "/");

                var path = urlEncode
                    ? HttpUtility.UrlEncode(formattedPath)
                    : formattedPath;

                var viewPath = $"{prefix}{path}";

                if (viewConfig.GenerateAsLowercase)
                {
                    viewPath = viewPath.ToLower();
                }

                viewNode.ViewEntries.Add(new ViewEntry
                {
                    Name = viewName,
                    Path = viewPath
                });
            }
        }