diff --git a/.gitignore b/.gitignore index e2ca8b3b..b6949ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .gems .rbenv-version .ruby-* +.tool-versions .idea/ /.rbx /.rvmrc diff --git a/MIGRATION_GUIDE.md b/MIGRATION_GUIDE.md index e44e54d0..54517196 100644 --- a/MIGRATION_GUIDE.md +++ b/MIGRATION_GUIDE.md @@ -74,17 +74,17 @@ Additional options that work for annotating models and routes ### Passing options via Environment Variables Annotate also reads options from ENV. For example, this command line argument `ANNOTATE_SKIP_ON_DB_MIGRATE=1 rake db:migrate` would affect Annotate's behavior. -The reading from ENV / environment variables has **been removed** in favor of reading configuration from `.annotaterb.yml` file in your Rails project root. +The reading from ENV / environment variables has **been removed** in favor of reading configuration from `.annotate_rb.yml` file in your Rails project root. ```yml -# .annotaterb.yml +# .annotate_rb.yml position: after ``` This change was done to reduce complexity in configuration and make the gem easier to maintain. -**Note: `.annotaterb.yml` is optional.** In its, AnnotateRb will use command line arguments and then the defaults. The defaults are implemented in `AnnotateRb::Options`. +**Note: `.annotate_rb.yml` is optional.** In its, AnnotateRb will use command line arguments and then the defaults. The defaults are implemented in `AnnotateRb::Options`. ---------- @@ -116,8 +116,8 @@ $ bin/rails g annotate_rb:install to install AnnotateRb's equivalent file into your Rails project. -#### Default .annotaterb.yml -When running the install generator command, `bin/rails g annotate_rb:install`, an `.annotaterb.yml` file gets automatically generated for your project using the defaults from the gem. +#### Default .annotate_rb.yml +When running the install generator command, `bin/rails g annotate_rb:install`, an `.annotate_rb.yml` file gets automatically generated for your project using the defaults from the gem. It _should_ match the old Annotate defaults however there may be differences. diff --git a/README.md b/README.md index ba347aff..762602e6 100644 --- a/README.md +++ b/README.md @@ -59,10 +59,10 @@ $ bin/rails g annotate_rb:install This will copy a rake task into your Rails project's `lib/tasks` directory that will hook into the Rails project rake tasks, automatically running AnnotateRb after database migration rake tasks. -To skip the automatic annotation that happens after a db task, pass the environment variable `ANNOTATERB_SKIP_ON_DB_TASKS=1` before your command. +To skip the automatic annotation that happens after a db task, pass the environment variable `ANNOTATE_RB_SKIP_ON_DB_TASKS=1` before your command. ```sh -$ ANNOTATERB_SKIP_ON_DB_TASKS=1 bin/rails db:migrate +$ ANNOTATE_RB_SKIP_ON_DB_TASKS=1 bin/rails db:migrate ``` ### Added Rails generators @@ -83,7 +83,7 @@ AnnotateRb: ``` `bin/rails g annotate_rb:config` -- Generates a new configuration file, `.annotaterb.yml`, using defaults from the gem. +- Generates a new configuration file, `.annotate_rb.yml`, using defaults from the gem. `bin/rails g annotate_rb:hook` - Installs the Rake file to automatically annotate Rails models on a database task (e.g. AnnotateRb will automatically run after running `bin/rails db:migrate`). @@ -92,7 +92,7 @@ AnnotateRb: - Runs the `config` and `hook` generator commands `bin/rails g annotate_rb:update_config` -- Appends to `.annotaterb.yml` any configuration key-value pairs that are used by the Gem. This is useful when there's a drift between the config file values and the gem defaults (i.e. when new features get added). +- Appends to `.annotate_rb.yml` any configuration key-value pairs that are used by the Gem. This is useful when there's a drift between the config file values and the gem defaults (i.e. when new features get added). ## Migrating from the annotate gem Refer to the [migration guide](MIGRATION_GUIDE.md). @@ -103,7 +103,7 @@ AnnotateRb has a CLI that you can use to add or remove annotations. ```sh # To show the CLI options -$ bundle exec annotaterb +$ bundle exec annotaterb Usage: annotaterb [command] [options] @@ -184,10 +184,10 @@ Additional options that work for annotating models and routes ## Configuration ### Storing default options -Previously in the [Annotate](https://github.com/ctran/annotate_models) you could pass options through the CLI or store them as environment variables. Annotaterb removes dependency on the environment variables and instead can read values from a `.annotaterb.yml` file stored in the Rails project root. +Previously in the [Annotate](https://github.com/ctran/annotate_models) you could pass options through the CLI or store them as environment variables. Annotaterb removes dependency on the environment variables and instead can read values from a `.annotate_rb.yml` file stored in the Rails project root. ```yml -# .annotaterb.yml +# .annotate_rb.yml position: after ``` diff --git a/lib/annotate_rb/config_finder.rb b/lib/annotate_rb/config_finder.rb index cba01014..da44fa40 100644 --- a/lib/annotate_rb/config_finder.rb +++ b/lib/annotate_rb/config_finder.rb @@ -2,19 +2,25 @@ module AnnotateRb class ConfigFinder - DOTFILE = ".annotaterb.yml" + DOTFILE = ".annotate_rb.yml" + LEGACY_DOTFILE = ".annotaterb.yml" class << self 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" + # e.g. "/Users/drwl/personal/annotate_rb/dummyapp" Dir.pwd end def find_project_dotfile file_path = File.expand_path(DOTFILE, find_project_root) + legacy_file_path = File.expand_path(LEGACY_DOTFILE, find_project_root) - return file_path if File.exist?(file_path) + if File.exist?(file_path) + file_path + elsif File.exist?(legacy_file_path) + legacy_file_path + end end end end diff --git a/lib/annotate_rb/model_annotator/annotation/schema_header.rb b/lib/annotate_rb/model_annotator/annotation/schema_header.rb index dccfc1f6..6dec4105 100644 --- a/lib/annotate_rb/model_annotator/annotation/schema_header.rb +++ b/lib/annotate_rb/model_annotator/annotation/schema_header.rb @@ -52,7 +52,7 @@ def display_table_comments? def name if display_table_comments? && table_comment - formatted_comment = "(#{table_comment.gsub(/\n/, "\\n")})" + formatted_comment = "(#{table_comment.gsub("\n", "\\n")})" "#{table_name}#{formatted_comment}" else diff --git a/lib/annotate_rb/model_annotator/annotation_decider.rb b/lib/annotate_rb/model_annotator/annotation_decider.rb index e6bdc24e..f5442b46 100644 --- a/lib/annotate_rb/model_annotator/annotation_decider.rb +++ b/lib/annotate_rb/model_annotator/annotation_decider.rb @@ -26,7 +26,7 @@ def annotate? klass_is_not_abstract = klass.respond_to?(:abstract_class?) && !klass.abstract_class? klass_table_exists = klass.respond_to?(:table_exists?) && klass.table_exists? - not_sure_this_conditional = (!@options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name)) + not_sure_this_conditional = !@options[:exclude_sti_subclasses] || !(klass.superclass < ActiveRecord::Base && klass.table_name == klass.superclass.table_name) annotate_conditions = [ klass_is_a_class, diff --git a/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb b/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb index 18a4a788..a22e06d3 100644 --- a/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb +++ b/lib/annotate_rb/model_annotator/column_annotation/annotation_builder.rb @@ -23,7 +23,7 @@ def build display_column_comments = @options[:with_comment] && @options[:with_column_comments] col_name = if display_column_comments && @model.with_comments? && @column.comment - "#{@column.name}(#{@column.comment.gsub(/\n/, '\\n')})" + "#{@column.name}(#{@column.comment.gsub("\n", '\\n')})" else @column.name end diff --git a/lib/annotate_rb/parser.rb b/lib/annotate_rb/parser.rb index abaab1eb..9b90dc67 100644 --- a/lib/annotate_rb/parser.rb +++ b/lib/annotate_rb/parser.rb @@ -412,14 +412,14 @@ def add_options_to_parser(option_parser) # rubocop:disable Metrics/MethodLength Array, "Do not annotate fixtures, test files, factories, and/or serializers") do |exclusions| exclusions ||= EXCLUSION_LIST - exclusions.each { |exclusion| @options["exclude_#{exclusion}".to_sym] = true } + exclusions.each { |exclusion| @options[:"exclude_#{exclusion}"] = true } end option_parser.on("-f", "--format [bare|rdoc|yard|markdown]", FORMAT_TYPES, "Render Schema Information as plain/RDoc/Yard/Markdown") do |format_type| - @options["format_#{format_type}".to_sym] = true + @options[:"format_#{format_type}"] = true end end end diff --git a/lib/annotate_rb/route_annotator/base_processor.rb b/lib/annotate_rb/route_annotator/base_processor.rb index faabab58..fe7e911f 100644 --- a/lib/annotate_rb/route_annotator/base_processor.rb +++ b/lib/annotate_rb/route_annotator/base_processor.rb @@ -70,7 +70,7 @@ def strip_annotations(content) mode = :content header_position = 0 - content.split(/\n/, -1).each_with_index do |line, line_number| + content.split("\n", -1).each_with_index do |line, line_number| if mode == :header && line !~ /\s*#/ mode = :content real_content << line unless line.blank? diff --git a/lib/annotate_rb/route_annotator/header_generator.rb b/lib/annotate_rb/route_annotator/header_generator.rb index f55f3abb..e9c0d970 100644 --- a/lib/annotate_rb/route_annotator/header_generator.rb +++ b/lib/annotate_rb/route_annotator/header_generator.rb @@ -15,7 +15,7 @@ def generate(options = {}) private def routes_map(options) - result = `rails routes`.chomp("\n").split(/\n/, -1) + result = `rails routes`.chomp("\n").split("\n", -1) # In old versions of Rake, the first line of output was the cwd. Not so # much in newer ones. We ditch that line if it exists, and if not, we diff --git a/lib/annotate_rb/route_annotator/helper.rb b/lib/annotate_rb/route_annotator/helper.rb index edad9d48..e49228a7 100644 --- a/lib/annotate_rb/route_annotator/helper.rb +++ b/lib/annotate_rb/route_annotator/helper.rb @@ -69,7 +69,7 @@ def strip_annotations(content) mode = :content header_position = 0 - content.split(/\n/, -1).each_with_index do |line, line_number| + content.split("\n", -1).each_with_index do |line, line_number| if mode == :header && line !~ /\s*#/ mode = :content real_content << line unless line.blank? diff --git a/lib/generators/annotate_rb/config/USAGE b/lib/generators/annotate_rb/config/USAGE index e94a9ab8..6d5bea03 100644 --- a/lib/generators/annotate_rb/config/USAGE +++ b/lib/generators/annotate_rb/config/USAGE @@ -1,5 +1,5 @@ Description: - Generates a default configuration file, `.annotaterb.yml` in your + Generates a default configuration file, `.annotate_rb.yml` in your Rails app project root. Example: diff --git a/lib/generators/annotate_rb/hook/templates/annotate_rb.rake b/lib/generators/annotate_rb/hook/templates/annotate_rb.rake index 1ad0ec39..e2efd133 100644 --- a/lib/generators/annotate_rb/hook/templates/annotate_rb.rake +++ b/lib/generators/annotate_rb/hook/templates/annotate_rb.rake @@ -1,7 +1,7 @@ # This rake task was added by annotate_rb gem. -# Can set `ANNOTATERB_SKIP_ON_DB_TASKS` to be anything to skip this -if Rails.env.development? && ENV["ANNOTATERB_SKIP_ON_DB_TASKS"].nil? +# Can set `ANNOTATE_RB_SKIP_ON_DB_TASKS` to be anything to skip this +if Rails.env.development? && ENV["ANNOTATE_RB_SKIP_ON_DB_TASKS"].nil? require "annotate_rb" AnnotateRb::Core.load_rake_tasks diff --git a/lib/generators/annotate_rb/update_config/USAGE b/lib/generators/annotate_rb/update_config/USAGE index f9f92ae4..a2f194a2 100644 --- a/lib/generators/annotate_rb/update_config/USAGE +++ b/lib/generators/annotate_rb/update_config/USAGE @@ -1,5 +1,5 @@ Description: - Appends to .annotaterb.yml any missing default configuration + Appends to .annotate_rb.yml any missing default configuration key-value pairs. Example: diff --git a/spec/dummyapp/config/database.yml b/spec/dummyapp/config/database.yml index a77b734b..a4f5b5d3 100644 --- a/spec/dummyapp/config/database.yml +++ b/spec/dummyapp/config/database.yml @@ -10,7 +10,7 @@ development: primary: <<: *default - database: annotaterb_development + database: annotate_rb_development <% end %> <% if ENV['DATABASE_ADAPTER'] == 'pg' %> @@ -25,7 +25,7 @@ development: primary: <<: *default - database: annotaterb_development + database: annotate_rb_development <% end %> <% if ENV['DATABASE_ADAPTER'] == 'sqlite3' %> diff --git a/spec/integration/rails_generator_install_spec.rb b/spec/integration/rails_generator_install_spec.rb index 08c55dce..3b04e188 100644 --- a/spec/integration/rails_generator_install_spec.rb +++ b/spec/integration/rails_generator_install_spec.rb @@ -5,7 +5,7 @@ let(:rake_task_file) { "lib/tasks/annotate_rb.rake" } let(:rake_task) { File.join(aruba.config.root_directory, "lib/generators/annotate_rb/hook/templates/annotate_rb.rake") } - let(:config_file) { ".annotaterb.yml" } + let(:config_file) { ".annotate_rb.yml" } let(:generator_install_command) { "bin/rails generate annotate_rb:install" } diff --git a/spec/integration/rails_generator_update_config_spec.rb b/spec/integration/rails_generator_update_config_spec.rb index 7ecccf05..bd93fbec 100644 --- a/spec/integration/rails_generator_update_config_spec.rb +++ b/spec/integration/rails_generator_update_config_spec.rb @@ -5,7 +5,7 @@ RSpec.describe "Generator appends to config file", type: "aruba" do let(:command_timeout_seconds) { 10 } - let(:config_file) { ".annotaterb.yml" } + let(:config_file) { ".annotate_rb.yml" } let(:config_file_content) do <<~YML.strip --- diff --git a/spec/lib/annotate_rb/parser_spec.rb b/spec/lib/annotate_rb/parser_spec.rb index c22c857f..1e83a8df 100644 --- a/spec/lib/annotate_rb/parser_spec.rb +++ b/spec/lib/annotate_rb/parser_spec.rb @@ -440,7 +440,7 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength describe option do Parser::FORMAT_TYPES.each do |format_type| context "when passing in format type '#{format_type}'" do - let(:format_key) { "format_#{format_type}".to_sym } + let(:format_key) { :"format_#{format_type}" } let(:args) { [option, format_type] } it "sets key for the format type to true" do