Class: RailsAiContext::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- RailsAiContext::Generators::InstallGenerator
- Defined in:
- lib/generators/rails_ai_context/install/install_generator.rb
Constant Summary collapse
- AI_TOOLS =
RailsAiContext::Install::AiTool.all.to_h { |t| [ t.number, { key: t.key, name: t.name, files: t.files, format: t.key } ] }.freeze
- BARE_GUARD_PATTERN =
The initializer guard written before this gem checked respond_to?(:configure). A path:/git: gemspec is evaluated in-process by Bundler in every environment, defining a VERSION-only stub
RailsAiContextmodule even when the gem itself isn't in the current Bundler group - sodefined?(RailsAiContext)alone doesn't prove.configureexists. /^([ \t]*)if defined\?\(RailsAiContext\)$/- CONFIG_SECTIONS =
All config sections with their marker comment and content. Each section is identified by its marker (e.g., "── AI Tools ──"). On re-install, only sections NOT already present are appended.
{ "AI Tools" => <<~SECTION, "Introspection" => <<~SECTION, "Models & Filtering" => <<~SECTION, "MCP Server" => <<~SECTION, "File Size Limits" => <<~SECTION, "Extensibility" => <<~SECTION, "Security" => <<~SECTION, "Database Query Tool" => <<~SECTION, "Log Reading" => <<~SECTION, "Hydration" => <<~SECTION, "Search" => <<~SECTION, "Frontend" => <<~SECTION # ── Frontend Framework Detection ───────────────────────────────── # Auto-detected from package.json, config/vite.json, etc. Override only if needed. # config.frontend_paths = ["app/frontend", "../web-client"] SECTION }.freeze
Instance Method Summary collapse
- #add_to_gitignore ⇒ Object
- #cleanup_removed_tools ⇒ Object
- #create_initializer ⇒ Object
- #create_mcp_config ⇒ Object
-
#create_yaml_config ⇒ Object
no_tasks.
- #generate_context_files ⇒ Object
- #install_validation_hook ⇒ Object
- #select_ai_tools ⇒ Object
- #select_tool_mode ⇒ Object
- #show_instructions ⇒ Object
Instance Method Details
#add_to_gitignore ⇒ Object
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 535 def add_to_gitignore gitignore = Rails.root.join(".gitignore") return unless File.exist?(gitignore) content = File.read(gitignore) lines = [] unless content.include?(".ai-context.json") lines << "" lines << "# rails-ai-context (JSON cache - markdown files should be committed)" lines << ".ai-context.json" end unless content.include?(".codex/config.toml") lines << "" lines << "# rails-ai-context (embeds this machine's Ruby PATH/GEM_HOME - do not share)" lines << ".codex/config.toml" end if lines.any? File.open(gitignore, "a") { |f| lines.each { |line| f.puts line } } say "Updated .gitignore", :green end end |
#cleanup_removed_tools ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 82 def cleanup_removed_tools @previous_formats = read_previous_ai_tools return unless @previous_formats&.any? removed = @previous_formats - @selected_formats return if removed.empty? say "" say "These AI tools were removed from your selection:", :yellow removed.each_with_index do |fmt, idx| tool = AI_TOOLS.values.find { |t| t[:format] == fmt } say " #{idx + 1}. #{tool[:name]} (#{tool[:files]})" if tool end say "" say "Remove their generated files?", :yellow say " y - remove all listed above" say " n - keep all (default)" say " 1,2 - remove only specific ones by number" say "" input = ask_safe("Enter choice:").strip.downcase return if input.empty? || input == "n" || input == "no" to_remove = if input == "y" || input == "yes" || input == "a" removed else nums = input.split(/[\s,]+/).filter_map { |n| n.to_i - 1 } nums.filter_map { |i| removed[i] if i >= 0 && i < removed.size } end return if to_remove.empty? to_remove.each do |fmt| tool = RailsAiContext::Install::AiTool.find(fmt) removed_paths = RailsAiContext::Install::Cleanup.remove( tools: [ fmt ], keeping: @selected_formats, root: Rails.root ) removed_paths.each { |path| say " Removed #{path}", :red } # Merge-safe MCP config cleanup - removes only the rails-ai-context entry cleaned = RailsAiContext::McpConfigGenerator.remove(tools: [ fmt ], output_dir: Rails.root.to_s) cleaned.each { |f| say " Removed MCP entry from #{Pathname.new(f).relative_path_from(Rails.root)}", :red } say " ✓ #{tool.name} files removed", :green if tool end end |
#create_initializer ⇒ Object
326 327 328 329 330 331 332 333 334 335 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 326 def create_initializer initializer_path = "config/initializers/rails_ai_context.rb" full_path = Rails.root.join(initializer_path) if File.exist?(full_path) update_existing_initializer(full_path) else create_new_initializer(initializer_path) end end |
#create_mcp_config ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 150 def create_mcp_config # No explicit standalone: flag - the generator detects the install # mode from Gemfile.lock, so this writes the same command form as the # standalone CLI init for the same app (no config ping-pong). generator = RailsAiContext::McpConfigGenerator.new( tools: @selected_formats, output_dir: Rails.root.to_s, tool_mode: @tool_mode ) result = generator.call result[:written].each do |f| rel = Pathname.new(f).relative_path_from(Rails.root) say "Created/Updated #{rel}", :green end result[:skipped].each do |f| rel = Pathname.new(f).relative_path_from(Rails.root) say "#{rel} unchanged - skipped", :yellow end if @tool_mode == :cli say "Skipped MCP config files (CLI-only mode)", :yellow end end |
#create_yaml_config ⇒ Object
no_tasks
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 519 def create_yaml_config # `initializer: false` because this generator writes that file itself, # a few steps earlier and better: it replaces the commented-out # default in place, where the module would insert a line and leave the # comment behind. One writer per file, and it is not this call. result = RailsAiContext::Install::SelectionRecord.write( @selected_formats, root: Rails.root, extra_yaml: { "tool_mode" => @tool_mode.to_s }, initializer: false ) RailsAiContext::Install::SelectionRecord.(result).each do |level, text| say text, { ok: :green, muted: :yellow, warn: :red }.fetch(level) end end |
#generate_context_files ⇒ Object
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 614 def generate_context_files say "" say "Generating AI context files...", :yellow unless Rails.application say " Skipped (Rails app not fully loaded). Run `rails ai:context` after install.", :yellow return end require "rails_ai_context" # One-time v5.0.0 legacy UI-pattern files cleanup prompt RailsAiContext::LegacyCleanup.prompt_legacy_files( @selected_formats, root: Rails.root ) # Generate every selected format in ONE call so ContextFileSerializer's # cross-format dedup applies (opencode and codex share AGENTS.md and # its split rules - generating them one format at a time defeats that # dedup and reports the same file as both written and unchanged). begin result = RailsAiContext.generate_context(format: @selected_formats) (result[:written] || []).each { |f| say " ✅ #{f}", :green } (result[:skipped] || []).each { |f| say " ⏭️ #{f} (unchanged)", :yellow } rescue => e say " ❌ #{@selected_formats.join(', ')}: #{e.}", :red end end |
#install_validation_hook ⇒ Object
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 558 def install_validation_hook git_dir = Rails.root.join(".git") return unless Dir.exist?(git_dir) hooks_dir = git_dir.join("hooks") hook_path = hooks_dir.join("pre-commit") if File.exist?(hook_path) && !File.read(hook_path).include?("rails-ai-context") say " Skipped pre-commit hook (existing hook found - add manually)", :yellow return end return if File.exist?(hook_path) && File.read(hook_path).include?("rails-ai-context") answer = ask_safe("Install a pre-commit hook that validates Rails references? (y/N)").strip.downcase return unless answer == "y" # Standalone installs have no `ai:*` rake tasks, so the hook must call # the gem's own binary; in-Gemfile installs go through rake as usual. if RailsAiContext::InstallMode.standalone? hook_binary = "rails-ai-context" validate_command = %(rails-ai-context tool validate --files "$files") else hook_binary = "rails" validate_command = %(rails 'ai:tool[validate]' files="$files") end FileUtils.mkdir_p(hooks_dir) File.write(hook_path, <<~HOOK) #!/bin/bash # rails-ai-context: validate Rails references before commit # Catches hallucinated columns, missing models, and schema drift. # Remove this file or the rails-ai-context section to disable. changed_files=$(git diff --cached --name-only | grep -E '\\.(rb|erb)$' || true) if [ -z "$changed_files" ]; then exit 0 fi if command -v #{hook_binary} &> /dev/null; then files=$(printf '%s\\n' "$changed_files" | tr '\\n' ',') #{validate_command} 2>/dev/null exit_code=$? if [ $exit_code -ne 0 ]; then echo "" echo "rails-ai-context validation found issues." echo "Fix them or skip with: git commit --no-verify" exit $exit_code fi fi HOOK FileUtils.chmod(0o755, hook_path) say " Installed pre-commit validation hook", :green end |
#select_ai_tools ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 53 def select_ai_tools say "" say "Which AI tools do you use? (select all that apply)", :yellow say "" AI_TOOLS.each do |num, info| say " #{num}. #{info[:name].ljust(16)} → #{info[:files]}" end say " a. All of the above" say "" input = ask_safe("Enter numbers separated by commas (e.g. 1,2) or 'a' for all:").strip.downcase @selected_formats = if input == "a" || input == "all" AI_TOOLS.values.map { |t| t[:format] } else nums = input.split(/[\s,]+/) nums.filter_map { |n| AI_TOOLS[n]&.dig(:format) } end if @selected_formats.empty? say "No tools selected - defaulting to all.", :yellow @selected_formats = AI_TOOLS.values.map { |t| t[:format] } end selected_names = AI_TOOLS.values.select { |t| @selected_formats.include?(t[:format]) }.map { |t| t[:name] } say "" say "Selected: #{selected_names.join(', ')}", :green end |
#select_tool_mode ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 131 def select_tool_mode say "" say "Do you also want MCP server support?", :yellow say "" say " 1. Yes - MCP primary + CLI fallback (generates per-tool MCP config files)" say " 2. No - CLI only (no server needed)" say "" input = ask_safe("Enter number (default: 1):").strip @tool_mode = case input when "2" then :cli else :mcp end mode_label = @tool_mode == :mcp ? "MCP + CLI fallback" : "CLI only" say "Selected: #{mode_label}", :green end |
#show_instructions ⇒ Object
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/generators/rails_ai_context/install/install_generator.rb', line 643 def show_instructions say "" say "=" * 50, :cyan say " rails-ai-context installed!", :cyan say "=" * 50, :cyan say "" say "Your setup:", :yellow AI_TOOLS.each_value do |info| next unless @selected_formats.include?(info[:format]) say " ✅ #{info[:name].ljust(16)} → #{info[:files]}" end say "" say "Commands:", :yellow say " rails ai:context # Regenerate context files" tool_count = RailsAiContext::Server.builtin_tools.size say " rails 'ai:tool[schema]' # Run any of the #{CountPhrase.call(tool_count, "tool")} from CLI" if @tool_mode == :mcp say " rails ai:serve # Start MCP server (#{CountPhrase.call(tool_count, "live tool")})" end say " rails ai:facts # Print concise schema facts summary" say " rails 'ai:preset[arch]' # Run multi-tool presets (architecture, debugging, migration)" say " rails ai:doctor # Check AI readiness" say " rails ai:inspect # Print introspection summary" say "" if @tool_mode == :mcp say "MCP auto-discovery:", :yellow say " Each AI tool gets its own config file - auto-detected on project open." say " No manual config needed." else say "CLI tools:", :yellow say " AI agents can run `rails 'ai:tool[schema]' table=users` directly." say " No MCP server needed - tools work from the terminal." end say "" say "To add more AI tools later:", :yellow say " rails ai:context:cursor # Generate for Cursor" say " rails ai:context:copilot # Generate for Copilot" say " rails generate rails_ai_context:install # Re-run to pick tools" say "" say "Standalone (no Gemfile needed):", :yellow say " gem install rails-ai-context" say " rails-ai-context init # interactive setup" say " rails-ai-context serve # start MCP server" say "" if @selected_formats.include?(:codex) say "Commit context files and MCP configs so your team benefits! (.codex/config.toml stays local - it embeds machine-specific paths; add it to .gitignore)", :green else say "Commit context files and MCP config files so your team benefits!", :green end end |