From 620e3eeea280ad95e9d6e10ee795cb0aa3bb587a Mon Sep 17 00:00:00 2001 From: Paulo Fidalgo Date: Sun, 31 May 2026 15:22:35 +0100 Subject: [PATCH] fix: improve sitemap create output Report each generated sitemap location and link count from the create task so CLI output is easier to scan. Preserve the existing create result keys and add focused task runner coverage for the new sitemap metadata. Verification: bundle exec rake test; bundle exec rake standard. --- lib/indexmap/task_runner.rb | 29 ++++++++++++++++++++++++++++- lib/tasks/indexmap_tasks.rake | 10 +++++++++- test/indexmap/task_runner_test.rb | 14 +++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/indexmap/task_runner.rb b/lib/indexmap/task_runner.rb index 2ab011d..efbf024 100644 --- a/lib/indexmap/task_runner.rb +++ b/lib/indexmap/task_runner.rb @@ -13,7 +13,12 @@ def create index_now_key_filename = write_index_now_key if configuration.index_now.write_key_file? configuration.run_after_create_callbacks - {files: written_files.map(&:to_s), written_files: written_files, index_now_key_filename: index_now_key_filename} + { + files: written_files.map(&:to_s), + written_files: written_files, + sitemaps: sitemap_details(written_files), + index_now_key_filename: index_now_key_filename + } end def format @@ -60,5 +65,27 @@ def default_output def sitemap_files storage.list(prefix: "sitemap", suffix: ".xml") end + + def sitemap_details(files) + files.map do |filename| + { + filename: filename.to_s, + location: sitemap_location(filename), + link_count: sitemap_link_count(filename) + } + end + end + + def sitemap_location(filename) + return storage.public_url(filename) if storage.respond_to?(:public_url) + + filename.to_s + end + + def sitemap_link_count(filename) + document = Nokogiri::XML(storage.read(filename.to_s)) + document.remove_namespaces! + document.xpath("//loc").count + end end end diff --git a/lib/tasks/indexmap_tasks.rake b/lib/tasks/indexmap_tasks.rake index 3f1da8d..78ecb2d 100644 --- a/lib/tasks/indexmap_tasks.rake +++ b/lib/tasks/indexmap_tasks.rake @@ -5,7 +5,11 @@ namespace :indexmap do runner = Indexmap::TaskRunner.new create_result = runner.create - puts "Created, formatted, and validated #{file_count(create_result[:files])} in #{storage_description(runner)}." + puts "Created, formatted, and validated #{file_count(create_result[:files])}." + puts "Files created:" + create_result[:sitemaps].each do |sitemap| + puts " - #{sitemap[:location]} (#{link_count(sitemap[:link_count])})" + end puts "IndexNow key file: #{create_result[:index_now_key_filename]}" if create_result[:index_now_key_filename] end @@ -91,6 +95,10 @@ namespace :indexmap do runner.storage end + def link_count(count) + "#{count} #{(count == 1) ? "link" : "links"}" + end + def format_google_ping_failure(failure) case failure[:reason] when :unauthorized diff --git a/test/indexmap/task_runner_test.rb b/test/indexmap/task_runner_test.rb index 646cb5f..e2bc299 100644 --- a/test/indexmap/task_runner_test.rb +++ b/test/indexmap/task_runner_test.rb @@ -16,7 +16,7 @@ def read_file(filename) end def test_create_writes_new_sitemap_and_key_file_without_deleting_unrelated_files - storage = Indexmap::Storage::Memory.new + storage = Indexmap::Storage::Memory.new(public_url: "https://cdn.example.com/sitemaps") storage.write("sitemap-pages.xml.gz", "old") storage.write("sitemap-extra.xml", "existing") @@ -31,6 +31,18 @@ def test_create_writes_new_sitemap_and_key_file_without_deleting_unrelated_files assert_equal VALID_KEY, storage.read("#{VALID_KEY}.txt") assert_equal ["sitemap-pages.xml", "sitemap.xml"], result[:files] assert_equal ["sitemap-pages.xml", "sitemap.xml"], result[:written_files] + assert_equal [ + { + filename: "sitemap-pages.xml", + location: "https://cdn.example.com/sitemaps/sitemap-pages.xml", + link_count: 1 + }, + { + filename: "sitemap.xml", + location: "https://cdn.example.com/sitemaps/sitemap.xml", + link_count: 1 + } + ], result[:sitemaps] assert_equal "#{VALID_KEY}.txt", result[:index_now_key_filename] end