示例#1
0
        public IEnumerable <HarvestShapeHit> HarvestShape(HarvestShapeInfo info)
        {
            var lastDash = info.FileName.LastIndexOf('-');
            var lastDot  = info.FileName.LastIndexOf('.');

            if (lastDot <= 0 || lastDot < lastDash)
            {
                yield return(new HarvestShapeHit
                {
                    ShapeType = Adjust(info.SubPath, info.FileName, null)
                });
            }
            else
            {
                var displayType = info.FileName.Substring(lastDot + 1);
                yield return(new HarvestShapeHit
                {
                    ShapeType = Adjust(info.SubPath, info.FileName.Substring(0, lastDot), displayType),
                    DisplayType = displayType
                });
            }
        }
        public void Discover(ShapeTableBuilder builder)
        {
            Logger.Info("Start discovering shapes");

            var harvesterInfos = _harvesters.Select(harvester => new { harvester, subPaths = harvester.SubPaths() });

            var availableFeatures = _extensionManager.AvailableFeatures();
            var activeFeatures    = availableFeatures.Where(FeatureIsEnabled);
            var activeExtensions  = Once(activeFeatures);

            var hits = _parallelCacheContext.RunInParallel(activeExtensions, extensionDescriptor =>
            {
                Logger.Info("Start discovering candidate views filenames");
                var pathContexts = harvesterInfos.SelectMany(harvesterInfo => harvesterInfo.subPaths.Select(subPath =>
                {
                    var basePath    = extensionDescriptor.Location.Replace(Path.DirectorySeparatorChar, '/');
                    var virtualPath = Path.Combine(basePath, subPath).Replace(Path.DirectorySeparatorChar, '/');
                    var fileNames   = _cacheManager.Get(virtualPath, ctx =>
                    {
                        if (!_virtualPathProvider.DirectoryExists(virtualPath))
                        {
                            return(new List <string>());
                        }

                        if (!DisableMonitoring)
                        {
                            Logger.DebugFormat("Monitoring virtual path \"{0}\"", virtualPath);
                            ctx.Monitor(_virtualPathMonitor.WhenPathChanges(virtualPath));
                        }

                        return(_virtualPathProvider.ListFiles(virtualPath).Select(Path.GetFileName).ToReadOnlyCollection());
                    });
                    return(new { harvesterInfo.harvester, basePath, subPath, virtualPath, fileNames });
                })).ToList();
                Logger.Info("Done discovering candidate views filenames");

                var fileContexts = pathContexts.SelectMany(pathContext => _shapeTemplateViewEngines.SelectMany(ve =>
                {
                    var fileNames = ve.DetectTemplateFileNames(pathContext.fileNames);
                    return(fileNames.Select(
                               fileName => new
                    {
                        fileName = Path.GetFileNameWithoutExtension(fileName),
                        fileVirtualPath = Path.Combine(pathContext.virtualPath, fileName).Replace(Path.DirectorySeparatorChar, '/'),
                        pathContext
                    }));
                }));

                var shapeContexts = fileContexts.SelectMany(fileContext =>
                {
                    var harvestShapeInfo = new HarvestShapeInfo
                    {
                        SubPath             = fileContext.pathContext.subPath,
                        FileName            = fileContext.fileName,
                        TemplateVirtualPath = fileContext.fileVirtualPath
                    };
                    var harvestShapeHits = fileContext.pathContext.harvester.HarvestShape(harvestShapeInfo);
                    return(harvestShapeHits.Select(harvestShapeHit => new { harvestShapeInfo, harvestShapeHit, fileContext }));
                });

                return(shapeContexts.Select(shapeContext => new { extensionDescriptor, shapeContext }).ToList());
            }).SelectMany(hits2 => hits2);

            foreach (var iter in hits)
            {
                // templates are always associated with the namesake feature of module or theme
                var hit = iter;
                var featureDescriptors = iter.extensionDescriptor.Features.Where(fd => fd.Id == hit.extensionDescriptor.Id);
                foreach (var featureDescriptor in featureDescriptors)
                {
                    Logger.DebugFormat("Binding {0} as shape [{1}] for feature {2}",
                                       hit.shapeContext.harvestShapeInfo.TemplateVirtualPath,
                                       iter.shapeContext.harvestShapeHit.ShapeType,
                                       featureDescriptor.Id);

                    builder.Describe(iter.shapeContext.harvestShapeHit.ShapeType)
                    .From(new Feature {
                        Descriptor = featureDescriptor
                    })
                    .BoundAs(
                        hit.shapeContext.harvestShapeInfo.TemplateVirtualPath,
                        shapeDescriptor => displayContext => Render(shapeDescriptor, displayContext, hit.shapeContext.harvestShapeInfo, hit.shapeContext.harvestShapeHit));
                }
            }

            Logger.Info("Done discovering shapes");
        }
        private IHtmlString Render(ShapeDescriptor shapeDescriptor, DisplayContext displayContext, HarvestShapeInfo harvestShapeInfo, HarvestShapeHit harvestShapeHit)
        {
            Logger.InfoFormat("Rendering template file '{0}'", harvestShapeInfo.TemplateVirtualPath);

            var htmlHelper = new HtmlHelper(displayContext.ViewContext, displayContext.ViewDataContainer);
            var result     = htmlHelper.Partial(harvestShapeInfo.TemplateVirtualPath, displayContext.Value);

            Logger.InfoFormat("Done rendering template file '{0}'", harvestShapeInfo.TemplateVirtualPath);
            return(result);
        }