From 31713bf1db7747c3220bb1a63d503ba1a2e61dea Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Tue, 8 Apr 2025 17:04:30 +0200 Subject: [PATCH 1/4] Support custom configuration file location --- README.md | 1 + lib/annotate_rb/config_finder.rb | 6 +++- lib/annotate_rb/parser.rb | 6 ++++ lib/annotate_rb/runner.rb | 16 ++++----- .../hook/templates/annotate_rb.rake | 2 ++ spec/lib/annotate_rb/config_finder_spec.rb | 33 +++++++++++++++++++ 6 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 spec/lib/annotate_rb/config_finder_spec.rb diff --git a/README.md b/README.md index a87c6307..2c2f343d 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,7 @@ Additional options that work for annotating models and routes --exclude Do not annotate fixtures, test files, factories, and/or serializers -f [bare|rdoc|yard|markdown], Render Schema Information as plain/RDoc/Yard/Markdown --format + -c, --config_path [path] Path to configuration file (by default, .annotaterb.yml in the root of the project) -p [before|top|after|bottom], Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s) --position --pc, --position-in-class [before|top|after|bottom] diff --git a/lib/annotate_rb/config_finder.rb b/lib/annotate_rb/config_finder.rb index cba01014..6b2c6c3e 100644 --- a/lib/annotate_rb/config_finder.rb +++ b/lib/annotate_rb/config_finder.rb @@ -5,6 +5,8 @@ class ConfigFinder DOTFILE = ".annotaterb.yml" class << self + attr_accessor :config_path + def find_project_root # We should expect this method to be called from a Rails project root and returning it # e.g. "/Users/drwl/personal/annotaterb/dummyapp" @@ -12,9 +14,11 @@ def find_project_root end def find_project_dotfile + return @config_path if @config_path && File.exist?(@config_path) + file_path = File.expand_path(DOTFILE, find_project_root) - return file_path if File.exist?(file_path) + file_path if File.exist?(file_path) end end end diff --git a/lib/annotate_rb/parser.rb b/lib/annotate_rb/parser.rb index 86c5c769..337b9a2e 100644 --- a/lib/annotate_rb/parser.rb +++ b/lib/annotate_rb/parser.rb @@ -421,6 +421,12 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength "Render Schema Information as plain/RDoc/Yard/Markdown") do |format_type| @options["format_#{format_type}".to_sym] = true end + + option_parser.on("-c", + "--config_path [path]", + "Path to configuration file (by default, .annotaterb.yml in the root of the project)") do |path| + @options[:config_path] = path + end end end end diff --git a/lib/annotate_rb/runner.rb b/lib/annotate_rb/runner.rb index 014924c1..e3326955 100644 --- a/lib/annotate_rb/runner.rb +++ b/lib/annotate_rb/runner.rb @@ -9,23 +9,23 @@ def run(args) end def run(args) - config_file_options = ConfigLoader.load_config parser = Parser.new(args, {}) parsed_options = parser.parse remaining_args = parser.remaining_args + AnnotateRb::ConfigFinder.config_path = parsed_options[:config_path] if parsed_options[:config_path] + config_file_options = ConfigLoader.load_config options = config_file_options.merge(parsed_options) - @options = Options.from(options, {working_args: remaining_args}) + @options = Options.from(options, { working_args: remaining_args }) AnnotateRb::RakeBootstrapper.call(@options) - if @options[:command] - @options[:command].call(@options) - else - # TODO - raise "Didn't specify a command" - end + raise "Didn't specify a command" unless @options[:command] + + @options[:command].call(@options) + + # TODO end end end diff --git a/lib/generators/annotate_rb/hook/templates/annotate_rb.rake b/lib/generators/annotate_rb/hook/templates/annotate_rb.rake index 1ad0ec39..c4d4f2d5 100644 --- a/lib/generators/annotate_rb/hook/templates/annotate_rb.rake +++ b/lib/generators/annotate_rb/hook/templates/annotate_rb.rake @@ -4,5 +4,7 @@ if Rails.env.development? && ENV["ANNOTATERB_SKIP_ON_DB_TASKS"].nil? require "annotate_rb" + # Can modify the config path here if needed - by default, it's .annotaterb.yml in the root of the project + # AnnotateRb::ConfigFinder.config_path = "" AnnotateRb::Core.load_rake_tasks end diff --git a/spec/lib/annotate_rb/config_finder_spec.rb b/spec/lib/annotate_rb/config_finder_spec.rb new file mode 100644 index 00000000..367f64f6 --- /dev/null +++ b/spec/lib/annotate_rb/config_finder_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +RSpec.describe AnnotateRb::ConfigFinder do + describe ".find_project_dotfile" do + subject { described_class.find_project_dotfile } + + context "when the config path directory is set" do + before { + allow(File).to receive(:exist?).and_return(true) + described_class.config_path = "spec/fixtures/.annotaterb.yml" + } + after { described_class.config_path = nil } + + it "returns the config path" do + expect(subject).to eq("spec/fixtures/.annotaterb.yml") + end + end + + context "when the config path directory is not set" do + before { allow(File).to receive(:exist?).and_return(true) } + + it "returns the default dotfile path" do + expect(subject).to eq(File.expand_path(".annotaterb.yml", Dir.pwd)) + end + end + + context "when the config path directory is not set and the dotfile does not exist" do + it "returns nil" do + expect(subject).to be_nil + end + end + end +end From a375e6cda10b1e985cab77f271e234d53a29f5d2 Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Wed, 18 Jun 2025 11:24:13 +0200 Subject: [PATCH 2/4] Fixing Tests --- lib/annotate_rb/parser.rb | 3 +-- spec/lib/annotate_rb/parser_spec.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/annotate_rb/parser.rb b/lib/annotate_rb/parser.rb index 337b9a2e..b36dc330 100644 --- a/lib/annotate_rb/parser.rb +++ b/lib/annotate_rb/parser.rb @@ -422,8 +422,7 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength @options["format_#{format_type}".to_sym] = true end - option_parser.on("-c", - "--config_path [path]", + option_parser.on("--config-path [path]", "Path to configuration file (by default, .annotaterb.yml in the root of the project)") do |path| @options[:config_path] = path end diff --git a/spec/lib/annotate_rb/parser_spec.rb b/spec/lib/annotate_rb/parser_spec.rb index c22c857f..53442d14 100644 --- a/spec/lib/annotate_rb/parser_spec.rb +++ b/spec/lib/annotate_rb/parser_spec.rb @@ -590,5 +590,14 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength expect(result).to include(with_column_comments: false) end end + + describe "--config-path" do + let(:option) { "--config-path=../annotaterb.yml" } + let(:args) { [option] } + + it "sets config_path to the path" do + expect(result).to include(config_path: "../annotaterb.yml") + end + end end end From ac338b13a631d8dc5d1776f27357533fc407b57a Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Wed, 18 Jun 2025 11:25:44 +0200 Subject: [PATCH 3/4] Update Readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0d146422..b04b5e0f 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ Additional options that work for annotating models and routes --exclude Do not annotate fixtures, test files, factories, and/or serializers -f [bare|rdoc|yard|markdown], Render Schema Information as plain/RDoc/Yard/Markdown --format - -c, --config_path [path] Path to configuration file (by default, .annotaterb.yml in the root of the project) + --config_path [path] Path to configuration file (by default, .annotaterb.yml in the root of the project) -p [before|top|after|bottom], Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/route/serializer file(s) --position --pc, --position-in-class [before|top|after|bottom] From ca9ffb65d769c1cf91318839265486bd70ed3419 Mon Sep 17 00:00:00 2001 From: Krzysztof Adamski Date: Thu, 19 Jun 2025 09:23:02 +0200 Subject: [PATCH 4/4] Fixing Rubocop Offenses --- lib/annotate_rb/runner.rb | 2 +- spec/lib/annotate_rb/config_finder_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/annotate_rb/runner.rb b/lib/annotate_rb/runner.rb index e3326955..c6082050 100644 --- a/lib/annotate_rb/runner.rb +++ b/lib/annotate_rb/runner.rb @@ -18,7 +18,7 @@ def run(args) config_file_options = ConfigLoader.load_config options = config_file_options.merge(parsed_options) - @options = Options.from(options, { working_args: remaining_args }) + @options = Options.from(options, {working_args: remaining_args}) AnnotateRb::RakeBootstrapper.call(@options) raise "Didn't specify a command" unless @options[:command] diff --git a/spec/lib/annotate_rb/config_finder_spec.rb b/spec/lib/annotate_rb/config_finder_spec.rb index 367f64f6..6d7a03e0 100644 --- a/spec/lib/annotate_rb/config_finder_spec.rb +++ b/spec/lib/annotate_rb/config_finder_spec.rb @@ -22,7 +22,7 @@ it "returns the default dotfile path" do expect(subject).to eq(File.expand_path(".annotaterb.yml", Dir.pwd)) end - end + end context "when the config path directory is not set and the dotfile does not exist" do it "returns nil" do