public void SingleProductWithSingleWordName()
        {
            var indexGenerator = new IndexGenerator <Product, int>(
                new NonNullImmutableList <ContentRetriever <Product, int> >(new[]
            {
                new ContentRetriever <Product, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <Product>(new[]
            {
                new Product()
                {
                    Key = 1, Name = "Product"
                }
            }));

            var expected = new NonNullImmutableList <WeightedEntry <int> >(new[]
            {
                new WeightedEntry <int>(1, 1f, (new[] { new SourceFieldLocation(0, 0, 0, 7, 1f) }).ToNonNullImmutableList())
            });

            EnsureIndexDataMatchesExpectations(
                expected,
                index.GetMatches("Product")
                );
        }
Example #2
0
        public static int Run(string[] args)
        {
            var sw = Stopwatch.StartNew();

            int loadResult = LoadOptions(args);

            if (loadResult != 0)
            {
                return(loadResult);
            }

            _basePath = AbsolutePath(_options.Source);
            AppDomain.CurrentDomain.AssemblyResolve += ResolveAssembly;            // Look in the base path for referenced .dll files instead of the startup path

            Console.Write("Scanning for DTO objects in {0}...  ", _basePath);
            var apiControllers   = GetApiControllers(_basePath);
            var controllerModels = new HashSet <Type>(apiControllers.SelectMany(GetModelsFromController));           // return and parameter models
            var allModels        = GetAllModelsToGenerate(controllerModels);

            Console.WriteLine("Found {0}", allModels.Count);

            var targetPath = AbsolutePath(_options.Destination);
            // Invoke all generators and pass the results to the index generator
            var allGeneratedNames = IndexGenerator.Generate(targetPath,
                                                            EntityGenerator.Generate(targetPath, allModels, _options),
                                                            DataServiceGenerator.Generate(apiControllers, controllerModels, targetPath, _options)
                                                            );

            RemoveNonGeneratedFiles(targetPath, allGeneratedNames);

            Console.WriteLine("Done in {0:N3}s", sw.Elapsed.TotalSeconds);
            return(0);
        }
        public void IfTheFirstContentRetrieverReturnsNoContentThenNoSourceFieldIndexZeroSourceLocationsAreReturned()
        {
            var indexGenerator = new IndexGenerator <ProductWithDescription, int>(
                new NonNullImmutableList <ContentRetriever <ProductWithDescription, int> >(new[]
            {
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    ),
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Description),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <ProductWithDescription>(new[]
            {
                new ProductWithDescription()
                {
                    Key = 1, Name = "", Description = "Product"
                }
            }));

            var expected = new NonNullImmutableList <WeightedEntry <int> >(new[]
            {
                new WeightedEntry <int>(
                    1,
                    1f,
                    (new[]
                {
                    new SourceFieldLocation(1, 0, 0, 7, 1f)                              // Match in Description field (source field index 1)
                }).ToNonNullImmutableList())
            });

            EnsureIndexDataMatchesExpectations(
                expected,
                index.GetMatches("Product")
                );
        }
        public void TestRemovalOfResultFromIndex()
        {
            var indexGenerator = new IndexGenerator <ProductWithDescription, int>(
                new NonNullImmutableList <ContentRetriever <ProductWithDescription, int> >(new[]
            {
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Name),
                    token => 1f
                    ),
                new ContentRetriever <ProductWithDescription, int>(
                    p => new PreBrokenContent <int>(p.Key, p.Description),
                    token => 1f
                    )
            }),
                new DefaultEqualityComparer <int>(),
                new CaseInsensitiveStringNormaliser(),
                new WhiteSpaceTokenBreaker(),
                weightedValues => weightedValues.Sum(),
                captureSourceLocations: true,
                logger: new NullLogger()
                );
            var index = indexGenerator.Generate(new NonNullImmutableList <ProductWithDescription>(new[]
            {
                new ProductWithDescription()
                {
                    Key = 1, Name = "", Description = "Product"
                },
                new ProductWithDescription()
                {
                    Key = 2, Name = "", Description = "Product"
                }
            }));

            Assert.Equal(2, index.GetMatches("Product").Count);                         // Should get two matches for "Product" at this point
            Assert.Equal(1, index.Remove(key => key == 2).GetMatches("Product").Count); // Should get only one if remove results for Key 2
        }
        public void GenerateSitemaps(Guid website, string baseUrl, string baseSitemapUrl, params CultureInfo[] cultures)
        {
            Log.Info("Starting sitemaps generation");

            var stopwatch = Stopwatch.StartNew();

            Guard.NotNullOrWhiteSpace(baseUrl, nameof(baseUrl));
            Guard.NotNullOrEmpty(cultures, nameof(cultures));
            if (website == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(website));
            }

            lock (_exclusiveLock)
            {
                var sitemapDirectory = Config.GetSitemapDirectory(website);
                EnsureDirectoryExists(sitemapDirectory);

                var tasks        = new List <Task>();
                var sitemapNames = new List <string>();

                try
                {
                    EnsureDirectoryExists(Config.GetWorkingDirectory(website));

                    foreach (var culture in cultures)
                    {
                        try
                        {
                            string scope = SiteConfiguration.GetPublishedScopeId(culture, website);

                            foreach (var provider in Providers)
                            {
                                // Start a new task for each provider.
                                // For example we can generate content + product sitemaps at the same time.
                                tasks.Add(Task.Factory.StartNew(() =>
                                {
                                    Log.Info($"Generating sitemap (type:{provider.GetType()}) for {culture.Name} in {scope} scope.");
                                    var sitemapParams = new SitemapParams
                                    {
                                        Website = website,
                                        BaseUrl = baseUrl,
                                        Scope   = scope,
                                        Culture = culture
                                    };

                                    try
                                    {
                                        foreach (var sitemap in provider.GenerateSitemaps(sitemapParams))
                                        {
                                            // Write sitemap to disk
                                            Log.Info($"Writing sitemap {sitemap.Name}");
                                            WriteSitemap(sitemap, website);

                                            // Add sitemap name to the list for the index creation later
                                            lock (sitemapNames)
                                            {
                                                sitemapNames.Add(sitemap.Name);
                                            }
                                        }
                                    }
                                    catch (Exception e) {
                                        Log.Error(e.ToString());
                                    }
                                }));
                            }
                        }
                        catch (ArgumentException) { }
                    }

                    Task.WhenAll(tasks).Wait();

                    // Write sitemap index
                    var index = IndexGenerator.Generate(baseSitemapUrl, sitemapNames);
                    WriteSitemapIndex(index, website);

                    // Deploy sitemaps and sitemap index
                    Log.Info($"Deploying sitemaps to {sitemapDirectory}");
                    DeploySitemaps(website);
                }
                finally
                {
                    DeleteWorkingDirectory();
                }

                // Log stopwatch duration
                Log.Info($"Sitemaps generation completed. Generation took {stopwatch.Elapsed.TotalSeconds} seconds.");
            }
        }