diff --git a/README.md b/README.md index ff2f947a..b04b5e0f 100644 --- a/README.md +++ b/README.md @@ -162,6 +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 + --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 d1175f7a..11f83c58 100644 --- a/lib/annotate_rb/parser.rb +++ b/lib/annotate_rb/parser.rb @@ -426,6 +426,11 @@ 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("--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..c6082050 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}) 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..6d7a03e0 --- /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 diff --git a/spec/lib/annotate_rb/parser_spec.rb b/spec/lib/annotate_rb/parser_spec.rb index 0fa0c7a4..534c2242 100644 --- a/spec/lib/annotate_rb/parser_spec.rb +++ b/spec/lib/annotate_rb/parser_spec.rb @@ -591,6 +591,15 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength 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 + describe "--nested-position" do let(:option) { "--nested-position" } let(:args) { [option] }