From 13f4dc5d5b67323d30c12b451577f7e04b0993cb Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 29 Jul 2026 17:11:39 +0000 Subject: [PATCH] Namespace rake task helpers under Bunko::RakeHelpers Bare def inside the namespace blocks of setup.rake, add.rake, install.rake, and sample_data.rake defined helper methods on Object in every host app that bundles the gem, as did the include Bunko::RakeHelpers lines and the top-level BUNKO_STANDARD_PAGES constant. Generic names like add_route invited collisions with host-app rake code or other gems. - Move all rake helper methods into Bunko::RakeHelpers as module_functions (lib/tasks/bunko/helpers.rb) - Call helpers fully qualified from the rake tasks (Bunko::RakeHelpers.render_template(...)) - Remove the include Bunko::RakeHelpers lines from the namespace blocks - Move BUNKO_STANDARD_PAGES to Bunko::RakeHelpers::STANDARD_PAGES - Task bodies are unchanged apart from the qualified helper calls No methods or constants are defined at the top level by any shipped rake file anymore. Closes #58 Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01CAFZmU5SJSERBVyedpsYQW --- lib/tasks/bunko/add.rake | 184 +------------- lib/tasks/bunko/helpers.rb | 424 +++++++++++++++++++++++++++++++ lib/tasks/bunko/install.rake | 99 +------- lib/tasks/bunko/sample_data.rake | 60 +---- lib/tasks/bunko/setup.rake | 113 +------- 5 files changed, 441 insertions(+), 439 deletions(-) diff --git a/lib/tasks/bunko/add.rake b/lib/tasks/bunko/add.rake index a9f50bd..730e7b8 100644 --- a/lib/tasks/bunko/add.rake +++ b/lib/tasks/bunko/add.rake @@ -4,8 +4,6 @@ require "fileutils" require_relative "helpers" namespace :bunko do - include Bunko::RakeHelpers - desc "Add a PostType or Collection (automatically detects which)" task :add, [:name] => :environment do |t, args| unless args[:name] @@ -59,18 +57,18 @@ namespace :bunko do # Step 1: If it's a PostType, create DB entry if pt_config - create_post_type_in_database(name, pt_config[:title]) + Bunko::RakeHelpers.create_post_type_in_database(name, pt_config[:title]) end # Step 2: Always generate artifacts (for both PostTypes and Collections) is_collection = collection_config.present? - generate_artifacts(name, format: format, is_collection: is_collection) + Bunko::RakeHelpers.generate_artifacts(name, format: format, is_collection: is_collection) # Step 3: Add to nav if pt_config - add_to_nav(name, title: pt_config[:title]) + Bunko::RakeHelpers.add_to_nav(name, title: pt_config[:title]) else - add_to_nav(name, title: collection_config[:title]) + Bunko::RakeHelpers.add_to_nav(name, title: collection_config[:title]) end # Success message @@ -82,178 +80,4 @@ namespace :bunko do end puts "Visit: http://localhost:3000/#{name.tr("_", "-")}" end - - # Helper methods - - def create_post_type_in_database(name, title) - post_type = PostType.find_by(name: name) - - if post_type - puts " ✓ PostType already exists: #{title} (#{name})" - else - PostType.create!(name: name, title: title) - puts " ✓ Created PostType: #{title} (#{name})" - end - puts "" - end - - def generate_artifacts(name, format:, is_collection:) - # Step 1: Generate controller - puts "Generating controller..." - generate_controller(name) - puts "" - - # Step 2: Generate views - puts "Generating views..." - generate_views(name, format: format, is_collection: is_collection) - puts "" - - # Step 3: Add route - puts "Adding route..." - add_route(name) - end - - def generate_controller(collection_name) - controller_name = "#{collection_name.camelize}Controller" - controller_file = Rails.root.join("app/controllers/#{collection_name}_controller.rb") - - if File.exist?(controller_file) - puts " - #{collection_name}_controller.rb already exists (skipped)" - return false - end - - controller_content = render_template("controllers/controller.rb.tt", { - controller_name: controller_name, - collection_name: collection_name - }) - - File.write(controller_file, controller_content) - puts " ✓ Created #{collection_name}_controller.rb" - true - end - - def generate_views(collection_name, format:, is_collection:) - views_dir = Rails.root.join("app/views/#{collection_name}") - - if Dir.exist?(views_dir) && Dir.glob("#{views_dir}/*").any? - puts " - #{collection_name} views already exist (skipped)" - return false - end - - FileUtils.mkdir_p(views_dir) - - # Generate index.html.erb - index_content = generate_index_view(collection_name, is_collection: is_collection) - File.write(File.join(views_dir, "index.html.erb"), index_content) - - # Collections only get index view, PostTypes get both index and show - if is_collection - puts " ✓ Created views for #{collection_name} (index only - collection)" - else - # Generate show.html.erb for PostTypes only - show_content = generate_show_view(collection_name, format: format) - File.write(File.join(views_dir, "show.html.erb"), show_content) - puts " ✓ Created views for #{collection_name} (index, show)" - end - - true - end - - def generate_index_view(collection_name, is_collection:) - is_plural = collection_name.pluralize == collection_name - - render_template("views/collections/index.html.erb.tt", { - collection_name: collection_name, - collection_title: collection_name.titleize, - path_helper: "#{collection_name.singularize}_path", - index_path_helper: is_plural ? "#{collection_name}_path" : "#{collection_name}_index_path", - is_collection: is_collection - }) - end - - def generate_show_view(collection_name, format:) - is_plural = collection_name.pluralize == collection_name - - render_template("views/collections/show.html.erb.tt", { - collection_name: collection_name, - collection_title: collection_name.titleize, - index_path_helper: is_plural ? "#{collection_name}_path" : "#{collection_name}_index_path", - format: format - }) - end - - def add_route(collection_name) - routes_file = Rails.root.join("config/routes.rb") - routes_content = File.read(routes_file) - - route_line = " bunko_collection :#{collection_name}" - - if routes_content.include?(route_line.strip) - puts " - Route for :#{collection_name} already exists (skipped)" - return false - end - - # Find the last 'end' in the file and insert before it - lines = routes_content.lines - last_end_index = lines.rindex { |line| line.match?(/^end\s*$/) } - - if last_end_index - lines.insert(last_end_index, "#{route_line}\n") - updated_content = lines.join - else - # Fallback: append before the last line if no 'end' found - updated_content = routes_content.sub(/\z/, "#{route_line}\n") - end - - File.write(routes_file, updated_content) - puts " ✓ Added route for :#{collection_name}" - true - end - - def add_to_nav(name, title:) - shared_dir = Rails.root.join("app/views/shared") - nav_file = shared_dir.join("_bunko_nav.html.erb") - - # If nav doesn't exist, generate from scratch (edge case) - unless File.exist?(nav_file) - FileUtils.mkdir_p(shared_dir) - nav_content = render_template("views/layouts/bunko_nav.html.erb.tt", { - post_types: Bunko.configuration.post_types, - collections: Bunko.configuration.collections - }) - File.write(nav_file, nav_content) - puts " ✓ Created shared/_bunko_nav.html.erb" - return true - end - - # Nav exists - append new link to existing file - nav_content = File.read(nav_file) - - # Generate the new link - is_plural = name.pluralize == name - path_helper = is_plural ? "#{name}_path" : "#{name}_index_path" - new_link = " <%= link_to \"#{title}\", #{path_helper} %>\n" - - # Check if link already exists (check for the title and path, not exact match) - if nav_content.match?(/link_to\s+"#{Regexp.escape(title)}",\s+#{path_helper}/) - puts " - #{title} already in nav (skipped)" - return false - end - - # Try to insert before the marker comment (preferred) - marker = "<%# bunko_collection_links - additional collections will be added here unless you delete this line %>" - nav_content = if nav_content.include?(marker) - nav_content.sub(marker, "#{new_link} #{marker}") - else - # Fallback: Find the closing before and insert the new link before it - nav_content.sub(/(\s*)<\/div>\s*<\/nav>/) do - indent = $1 - "#{new_link}#{indent}\n" - end - end - - File.write(nav_file, nav_content) - puts " ✓ Added #{title} to shared/_bunko_nav.html.erb" - true - end end diff --git a/lib/tasks/bunko/helpers.rb b/lib/tasks/bunko/helpers.rb index 8dd4774..f1fd93c 100644 --- a/lib/tasks/bunko/helpers.rb +++ b/lib/tasks/bunko/helpers.rb @@ -1,9 +1,30 @@ # frozen_string_literal: true require "erb" +require "fileutils" module Bunko + # Helper methods for Bunko's rake tasks (install, setup, add, sample_data). + # + # All helpers are module_functions called fully qualified from the rake + # files (e.g. Bunko::RakeHelpers.render_template(...)) so that nothing + # leaks onto Object in host applications that bundle the gem. module RakeHelpers + # Standard static pages generated by bunko:sample_data + STANDARD_PAGES = [ + {slug: "home", title: "Home"}, + {slug: "about", title: "About"}, + {slug: "contact", title: "Contact"}, + {slug: "faq", title: "FAQ"}, + {slug: "privacy-policy", title: "Privacy Policy"}, + {slug: "cookie-policy", title: "Cookie Policy"}, + {slug: "terms-of-service", title: "Terms of Service"} + ].freeze + + module_function + + # -- Shared helpers ------------------------------------------------------ + def render_template(template_name, locals = {}) template_path = File.expand_path("../templates/#{template_name}", __dir__) @@ -21,5 +42,408 @@ def render_template(template_name, locals = {}) ERB.new(template_content, trim_mode: "-").result(context.instance_eval { binding }) end + + # -- bunko:install helpers ----------------------------------------------- + + def create_post_types_migration(skip_seo:, json_content:) + timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") + migration_file = Rails.root.join("db/migrate/#{timestamp}_create_post_types.rb") + + if Dir.glob(Rails.root.join("db/migrate/*_create_post_types.rb")).any? + puts " - create_post_types migration already exists (skipped)" + return false + end + + migration_content = render_template("db/migrate/create_post_types.rb.tt", { + include_seo_fields?: !skip_seo, + use_json_content?: json_content + }) + + File.write(migration_file, migration_content) + puts " ✓ Created db/migrate/#{timestamp}_create_post_types.rb" + true + end + + def create_posts_migration(skip_seo:, json_content:) + timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") + migration_file = Rails.root.join("db/migrate/#{timestamp}_create_posts.rb") + + if Dir.glob(Rails.root.join("db/migrate/*_create_posts.rb")).any? + puts " - create_posts migration already exists (skipped)" + return false + end + + migration_content = render_template("db/migrate/create_posts.rb.tt", { + include_seo_fields?: !skip_seo, + use_json_content?: json_content + }) + + File.write(migration_file, migration_content) + puts " ✓ Created db/migrate/#{timestamp}_create_posts.rb" + true + end + + def create_models + # Create Post model + post_file = Rails.root.join("app/models/post.rb") + if File.exist?(post_file) + puts " - app/models/post.rb already exists (skipped)" + else + post_content = render_template("models/post.rb.tt", {}) + File.write(post_file, post_content) + puts " ✓ Created app/models/post.rb" + end + + # Create PostType model + post_type_file = Rails.root.join("app/models/post_type.rb") + if File.exist?(post_type_file) + puts " - app/models/post_type.rb already exists (skipped)" + else + post_type_content = render_template("models/post_type.rb.tt", {}) + File.write(post_type_file, post_type_content) + puts " ✓ Created app/models/post_type.rb" + end + end + + def create_initializer + initializer_dir = Rails.root.join("config/initializers") + initializer_file = initializer_dir.join("bunko.rb") + + if File.exist?(initializer_file) + puts " - config/initializers/bunko.rb already exists (skipped)" + return false + end + + FileUtils.mkdir_p(initializer_dir) + initializer_content = render_template("config/initializers/bunko.rb.tt", {}) + File.write(initializer_file, initializer_content) + puts " ✓ Created config/initializers/bunko.rb" + true + end + + def show_install_instructions + instructions_path = File.expand_path("../templates/INSTALL.md", __dir__) + instructions = File.read(instructions_path) + + puts "=" * 79 + puts instructions + puts "=" * 79 + end + + # -- bunko:setup helpers ------------------------------------------------- + + def generate_shared_nav + shared_dir = Rails.root.join("app/views/shared") + nav_file = shared_dir.join("_bunko_nav.html.erb") + + if File.exist?(nav_file) + puts " - _bunko_nav.html.erb already exists (skipped)" + return false + end + + FileUtils.mkdir_p(shared_dir) + + nav_content = render_template("views/layouts/bunko_nav.html.erb.tt", {}) + File.write(nav_file, nav_content) + + puts " ✓ Created shared/_bunko_nav.html.erb" + true + end + + def generate_shared_styles + shared_dir = Rails.root.join("app/views/shared") + styles_file = shared_dir.join("_bunko_styles.html.erb") + + if File.exist?(styles_file) + puts " - _bunko_styles.html.erb already exists (skipped)" + return false + end + + FileUtils.mkdir_p(shared_dir) + + styles_content = render_template("views/layouts/bunko_styles.html.erb.tt", {}) + File.write(styles_file, styles_content) + + puts " ✓ Created shared/_bunko_styles.html.erb" + true + end + + def generate_shared_footer + shared_dir = Rails.root.join("app/views/shared") + footer_file = shared_dir.join("_bunko_footer.html.erb") + + if File.exist?(footer_file) + puts " - _bunko_footer.html.erb already exists (skipped)" + return false + end + + FileUtils.mkdir_p(shared_dir) + + footer_content = render_template("views/layouts/bunko_footer.html.erb.tt", {}) + File.write(footer_file, footer_content) + + puts " ✓ Created shared/_bunko_footer.html.erb" + true + end + + def setup_static_pages + # Create "pages" PostType in database + PostType.find_or_create_by!(name: "pages") do |pt| + pt.title = "Pages" + end + puts " ✓ Created 'pages' PostType in database" + + # Generate PagesController + generate_pages_controller + + # Generate pages/show.html.erb view + generate_pages_show_view + end + + def generate_pages_controller + controller_path = Rails.root.join("app/controllers/pages_controller.rb") + + if File.exist?(controller_path) + puts " - pages_controller.rb already exists (skipped)" + return false + end + + controller_content = render_template("controllers/pages_controller.rb.tt", {}) + File.write(controller_path, controller_content) + + puts " ✓ Created app/controllers/pages_controller.rb" + true + end + + def generate_pages_show_view + views_dir = Rails.root.join("app/views/pages") + show_file = views_dir.join("show.html.erb") + + if File.exist?(show_file) + puts " - pages/show.html.erb already exists (skipped)" + return false + end + + FileUtils.mkdir_p(views_dir) + + show_content = render_template("views/pages/show.html.erb.tt", {}) + File.write(show_file, show_content) + + puts " ✓ Created app/views/pages/show.html.erb" + true + end + + # -- bunko:add helpers --------------------------------------------------- + + def create_post_type_in_database(name, title) + post_type = PostType.find_by(name: name) + + if post_type + puts " ✓ PostType already exists: #{title} (#{name})" + else + PostType.create!(name: name, title: title) + puts " ✓ Created PostType: #{title} (#{name})" + end + puts "" + end + + def generate_artifacts(name, format:, is_collection:) + # Step 1: Generate controller + puts "Generating controller..." + generate_controller(name) + puts "" + + # Step 2: Generate views + puts "Generating views..." + generate_views(name, format: format, is_collection: is_collection) + puts "" + + # Step 3: Add route + puts "Adding route..." + add_route(name) + end + + def generate_controller(collection_name) + controller_name = "#{collection_name.camelize}Controller" + controller_file = Rails.root.join("app/controllers/#{collection_name}_controller.rb") + + if File.exist?(controller_file) + puts " - #{collection_name}_controller.rb already exists (skipped)" + return false + end + + controller_content = render_template("controllers/controller.rb.tt", { + controller_name: controller_name, + collection_name: collection_name + }) + + File.write(controller_file, controller_content) + puts " ✓ Created #{collection_name}_controller.rb" + true + end + + def generate_views(collection_name, format:, is_collection:) + views_dir = Rails.root.join("app/views/#{collection_name}") + + if Dir.exist?(views_dir) && Dir.glob("#{views_dir}/*").any? + puts " - #{collection_name} views already exist (skipped)" + return false + end + + FileUtils.mkdir_p(views_dir) + + # Generate index.html.erb + index_content = generate_index_view(collection_name, is_collection: is_collection) + File.write(File.join(views_dir, "index.html.erb"), index_content) + + # Collections only get index view, PostTypes get both index and show + if is_collection + puts " ✓ Created views for #{collection_name} (index only - collection)" + else + # Generate show.html.erb for PostTypes only + show_content = generate_show_view(collection_name, format: format) + File.write(File.join(views_dir, "show.html.erb"), show_content) + puts " ✓ Created views for #{collection_name} (index, show)" + end + + true + end + + def generate_index_view(collection_name, is_collection:) + is_plural = collection_name.pluralize == collection_name + + render_template("views/collections/index.html.erb.tt", { + collection_name: collection_name, + collection_title: collection_name.titleize, + path_helper: "#{collection_name.singularize}_path", + index_path_helper: is_plural ? "#{collection_name}_path" : "#{collection_name}_index_path", + is_collection: is_collection + }) + end + + def generate_show_view(collection_name, format:) + is_plural = collection_name.pluralize == collection_name + + render_template("views/collections/show.html.erb.tt", { + collection_name: collection_name, + collection_title: collection_name.titleize, + index_path_helper: is_plural ? "#{collection_name}_path" : "#{collection_name}_index_path", + format: format + }) + end + + def add_route(collection_name) + routes_file = Rails.root.join("config/routes.rb") + routes_content = File.read(routes_file) + + route_line = " bunko_collection :#{collection_name}" + + if routes_content.include?(route_line.strip) + puts " - Route for :#{collection_name} already exists (skipped)" + return false + end + + # Find the last 'end' in the file and insert before it + lines = routes_content.lines + last_end_index = lines.rindex { |line| line.match?(/^end\s*$/) } + + if last_end_index + lines.insert(last_end_index, "#{route_line}\n") + updated_content = lines.join + else + # Fallback: append before the last line if no 'end' found + updated_content = routes_content.sub(/\z/, "#{route_line}\n") + end + + File.write(routes_file, updated_content) + puts " ✓ Added route for :#{collection_name}" + true + end + + def add_to_nav(name, title:) + shared_dir = Rails.root.join("app/views/shared") + nav_file = shared_dir.join("_bunko_nav.html.erb") + + # If nav doesn't exist, generate from scratch (edge case) + unless File.exist?(nav_file) + FileUtils.mkdir_p(shared_dir) + nav_content = render_template("views/layouts/bunko_nav.html.erb.tt", { + post_types: Bunko.configuration.post_types, + collections: Bunko.configuration.collections + }) + File.write(nav_file, nav_content) + puts " ✓ Created shared/_bunko_nav.html.erb" + return true + end + + # Nav exists - append new link to existing file + nav_content = File.read(nav_file) + + # Generate the new link + is_plural = name.pluralize == name + path_helper = is_plural ? "#{name}_path" : "#{name}_index_path" + new_link = " <%= link_to \"#{title}\", #{path_helper} %>\n" + + # Check if link already exists (check for the title and path, not exact match) + if nav_content.match?(/link_to\s+"#{Regexp.escape(title)}",\s+#{path_helper}/) + puts " - #{title} already in nav (skipped)" + return false + end + + # Try to insert before the marker comment (preferred) + marker = "<%# bunko_collection_links - additional collections will be added here unless you delete this line %>" + nav_content = if nav_content.include?(marker) + nav_content.sub(marker, "#{new_link} #{marker}") + else + # Fallback: Find the closing before and insert the new link before it + nav_content.sub(/(\s*)<\/div>\s*<\/nav>/) do + indent = $1 + "#{new_link}#{indent}\n" + end + end + + File.write(nav_file, nav_content) + puts " ✓ Added #{title} to shared/_bunko_nav.html.erb" + true + end + + # -- bunko:sample_data helpers ------------------------------------------- + + def root_route_exists? + Rails.application.routes.named_routes[:root].present? + end + + def add_bunko_page_route(slug, path: nil) + routes_file = Rails.root.join("config/routes.rb") + routes_content = File.read(routes_file) + + # Build the route line + route_line = if path + " bunko_page :#{slug.tr("-", "_")}, path: \"#{path}\"" + else + " bunko_page :#{slug.tr("-", "_")}" + end + + # Check if this route already exists + slug_symbol = ":#{slug.tr("-", "_")}" + if routes_content.match?(/bunko_page\s+#{Regexp.escape(slug_symbol)}/) + return false + end + + # Find the last 'end' in the file and insert before it + lines = routes_content.lines + last_end_index = lines.rindex { |line| line.match?(/^end\s*$/) } + + if last_end_index + lines.insert(last_end_index, "#{route_line}\n") + updated_content = lines.join + else + # Fallback: append before the last line if no 'end' found + updated_content = routes_content.sub(/\z/, "#{route_line}\n") + end + + File.write(routes_file, updated_content) + true + end end end diff --git a/lib/tasks/bunko/install.rake b/lib/tasks/bunko/install.rake index cd5d8f2..6046d22 100644 --- a/lib/tasks/bunko/install.rake +++ b/lib/tasks/bunko/install.rake @@ -4,8 +4,6 @@ require "fileutils" require_relative "helpers" namespace :bunko do - include Bunko::RakeHelpers - desc "Install Bunko by creating migrations, models, and initializer" task install: :environment do puts "Installing Bunko..." @@ -17,109 +15,22 @@ namespace :bunko do # Step 1: Create migrations puts "Creating migrations..." - create_post_types_migration(skip_seo: skip_seo, json_content: json_content) + Bunko::RakeHelpers.create_post_types_migration(skip_seo: skip_seo, json_content: json_content) sleep 1 # Ensure different timestamps - create_posts_migration(skip_seo: skip_seo, json_content: json_content) + Bunko::RakeHelpers.create_posts_migration(skip_seo: skip_seo, json_content: json_content) puts "" # Step 2: Create models puts "Creating models..." - create_models + Bunko::RakeHelpers.create_models puts "" # Step 3: Create initializer puts "Creating initializer..." - create_initializer + Bunko::RakeHelpers.create_initializer puts "" # Step 4: Show instructions - show_install_instructions - end - - # Helper methods - - def create_post_types_migration(skip_seo:, json_content:) - timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") - migration_file = Rails.root.join("db/migrate/#{timestamp}_create_post_types.rb") - - if Dir.glob(Rails.root.join("db/migrate/*_create_post_types.rb")).any? - puts " - create_post_types migration already exists (skipped)" - return false - end - - migration_content = render_template("db/migrate/create_post_types.rb.tt", { - include_seo_fields?: !skip_seo, - use_json_content?: json_content - }) - - File.write(migration_file, migration_content) - puts " ✓ Created db/migrate/#{timestamp}_create_post_types.rb" - true - end - - def create_posts_migration(skip_seo:, json_content:) - timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S") - migration_file = Rails.root.join("db/migrate/#{timestamp}_create_posts.rb") - - if Dir.glob(Rails.root.join("db/migrate/*_create_posts.rb")).any? - puts " - create_posts migration already exists (skipped)" - return false - end - - migration_content = render_template("db/migrate/create_posts.rb.tt", { - include_seo_fields?: !skip_seo, - use_json_content?: json_content - }) - - File.write(migration_file, migration_content) - puts " ✓ Created db/migrate/#{timestamp}_create_posts.rb" - true - end - - def create_models - # Create Post model - post_file = Rails.root.join("app/models/post.rb") - if File.exist?(post_file) - puts " - app/models/post.rb already exists (skipped)" - else - post_content = render_template("models/post.rb.tt", {}) - File.write(post_file, post_content) - puts " ✓ Created app/models/post.rb" - end - - # Create PostType model - post_type_file = Rails.root.join("app/models/post_type.rb") - if File.exist?(post_type_file) - puts " - app/models/post_type.rb already exists (skipped)" - else - post_type_content = render_template("models/post_type.rb.tt", {}) - File.write(post_type_file, post_type_content) - puts " ✓ Created app/models/post_type.rb" - end - end - - def create_initializer - initializer_dir = Rails.root.join("config/initializers") - initializer_file = initializer_dir.join("bunko.rb") - - if File.exist?(initializer_file) - puts " - config/initializers/bunko.rb already exists (skipped)" - return false - end - - FileUtils.mkdir_p(initializer_dir) - initializer_content = render_template("config/initializers/bunko.rb.tt", {}) - File.write(initializer_file, initializer_content) - puts " ✓ Created config/initializers/bunko.rb" - true - end - - def show_install_instructions - instructions_path = File.expand_path("../templates/INSTALL.md", __dir__) - instructions = File.read(instructions_path) - - puts "=" * 79 - puts instructions - puts "=" * 79 + Bunko::RakeHelpers.show_install_instructions end end diff --git a/lib/tasks/bunko/sample_data.rake b/lib/tasks/bunko/sample_data.rake index 0952e98..916a3cb 100644 --- a/lib/tasks/bunko/sample_data.rake +++ b/lib/tasks/bunko/sample_data.rake @@ -3,20 +3,7 @@ require_relative "../support/sample_data_generator" require_relative "helpers" -# Standard static pages to generate -BUNKO_STANDARD_PAGES = [ - {slug: "home", title: "Home"}, - {slug: "about", title: "About"}, - {slug: "contact", title: "Contact"}, - {slug: "faq", title: "FAQ"}, - {slug: "privacy-policy", title: "Privacy Policy"}, - {slug: "cookie-policy", title: "Cookie Policy"}, - {slug: "terms-of-service", title: "Terms of Service"} -].freeze - namespace :bunko do - include Bunko::RakeHelpers - desc "Generate sample posts for all configured post types" task sample_data: :environment do # Warn if running in production @@ -155,10 +142,10 @@ namespace :bunko do puts "" else # Determine which pages to create - pages_to_create = BUNKO_STANDARD_PAGES.dup + pages_to_create = Bunko::RakeHelpers::STANDARD_PAGES.dup # Remove home if root route already exists - if root_route_exists? + if Bunko::RakeHelpers.root_route_exists? pages_to_create.reject! { |page| page[:slug] == "home" } puts " - Skipping 'home' page (root route already exists)" end @@ -204,12 +191,12 @@ namespace :bunko do pages_to_create.each do |page_def| # Home gets special treatment with path: "/" if page_def[:slug] == "home" - if add_bunko_page_route(page_def[:slug], path: "/") + if Bunko::RakeHelpers.add_bunko_page_route(page_def[:slug], path: "/") puts " ✓ Added route: bunko_page :home, path: \"/\"" else puts " - Route for :home already exists (skipped)" end - elsif add_bunko_page_route(page_def[:slug]) + elsif Bunko::RakeHelpers.add_bunko_page_route(page_def[:slug]) puts " ✓ Added route: bunko_page :#{page_def[:slug].tr("-", "_")}" else puts " - Route for :#{page_def[:slug].tr("-", "_")} already exists (skipped)" @@ -233,43 +220,4 @@ namespace :bunko do puts " rake bunko:sample_data CLEAR=true # Clear existing first" puts "" end - - # Helper methods - - def root_route_exists? - Rails.application.routes.named_routes[:root].present? - end - - def add_bunko_page_route(slug, path: nil) - routes_file = Rails.root.join("config/routes.rb") - routes_content = File.read(routes_file) - - # Build the route line - route_line = if path - " bunko_page :#{slug.tr("-", "_")}, path: \"#{path}\"" - else - " bunko_page :#{slug.tr("-", "_")}" - end - - # Check if this route already exists - slug_symbol = ":#{slug.tr("-", "_")}" - if routes_content.match?(/bunko_page\s+#{Regexp.escape(slug_symbol)}/) - return false - end - - # Find the last 'end' in the file and insert before it - lines = routes_content.lines - last_end_index = lines.rindex { |line| line.match?(/^end\s*$/) } - - if last_end_index - lines.insert(last_end_index, "#{route_line}\n") - updated_content = lines.join - else - # Fallback: append before the last line if no 'end' found - updated_content = routes_content.sub(/\z/, "#{route_line}\n") - end - - File.write(routes_file, updated_content) - true - end end diff --git a/lib/tasks/bunko/setup.rake b/lib/tasks/bunko/setup.rake index d349237..afdd661 100644 --- a/lib/tasks/bunko/setup.rake +++ b/lib/tasks/bunko/setup.rake @@ -4,8 +4,6 @@ require "fileutils" require_relative "helpers" namespace :bunko do - include Bunko::RakeHelpers - desc "Set up Bunko by creating all configured PostTypes and Collections" task setup: :environment do puts "Setting up Bunko..." @@ -31,15 +29,15 @@ namespace :bunko do # Generate shared partials once puts "Generating shared partials..." - generate_shared_nav - generate_shared_styles - generate_shared_footer + Bunko::RakeHelpers.generate_shared_nav + Bunko::RakeHelpers.generate_shared_styles + Bunko::RakeHelpers.generate_shared_footer puts "" # Set up static pages if enabled if allow_static_pages puts "Setting up static pages..." - setup_static_pages + Bunko::RakeHelpers.setup_static_pages puts "" end @@ -80,107 +78,4 @@ namespace :bunko do puts " rails bunko:add[name]" puts "=" * 79 end - - # Helper methods - - def generate_shared_nav - shared_dir = Rails.root.join("app/views/shared") - nav_file = shared_dir.join("_bunko_nav.html.erb") - - if File.exist?(nav_file) - puts " - _bunko_nav.html.erb already exists (skipped)" - return false - end - - FileUtils.mkdir_p(shared_dir) - - nav_content = render_template("views/layouts/bunko_nav.html.erb.tt", {}) - File.write(nav_file, nav_content) - - puts " ✓ Created shared/_bunko_nav.html.erb" - true - end - - def generate_shared_styles - shared_dir = Rails.root.join("app/views/shared") - styles_file = shared_dir.join("_bunko_styles.html.erb") - - if File.exist?(styles_file) - puts " - _bunko_styles.html.erb already exists (skipped)" - return false - end - - FileUtils.mkdir_p(shared_dir) - - styles_content = render_template("views/layouts/bunko_styles.html.erb.tt", {}) - File.write(styles_file, styles_content) - - puts " ✓ Created shared/_bunko_styles.html.erb" - true - end - - def generate_shared_footer - shared_dir = Rails.root.join("app/views/shared") - footer_file = shared_dir.join("_bunko_footer.html.erb") - - if File.exist?(footer_file) - puts " - _bunko_footer.html.erb already exists (skipped)" - return false - end - - FileUtils.mkdir_p(shared_dir) - - footer_content = render_template("views/layouts/bunko_footer.html.erb.tt", {}) - File.write(footer_file, footer_content) - - puts " ✓ Created shared/_bunko_footer.html.erb" - true - end - - def setup_static_pages - # Create "pages" PostType in database - PostType.find_or_create_by!(name: "pages") do |pt| - pt.title = "Pages" - end - puts " ✓ Created 'pages' PostType in database" - - # Generate PagesController - generate_pages_controller - - # Generate pages/show.html.erb view - generate_pages_show_view - end - - def generate_pages_controller - controller_path = Rails.root.join("app/controllers/pages_controller.rb") - - if File.exist?(controller_path) - puts " - pages_controller.rb already exists (skipped)" - return false - end - - controller_content = render_template("controllers/pages_controller.rb.tt", {}) - File.write(controller_path, controller_content) - - puts " ✓ Created app/controllers/pages_controller.rb" - true - end - - def generate_pages_show_view - views_dir = Rails.root.join("app/views/pages") - show_file = views_dir.join("show.html.erb") - - if File.exist?(show_file) - puts " - pages/show.html.erb already exists (skipped)" - return false - end - - FileUtils.mkdir_p(views_dir) - - show_content = render_template("views/pages/show.html.erb.tt", {}) - File.write(show_file, show_content) - - puts " ✓ Created app/views/pages/show.html.erb" - true - end end