Powered by SmartDoc

4.2 annotate コマンド

  1. とりあえずPATHを通す

    kiri@smtp:~/projects/rails/sample_view[1859]% pkg info -aI|grep annotate
    rubygem-annotate-3.2.0         Write model schema information as a comment
    kiri@smtp:~/projects/rails/sample_view[1860]% pkg info -l rubygem-annotate-3.2.0
    rubygem-annotate-3.2.0:
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/AUTHORS.md
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/CHANGELOG.md
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/LICENSE.txt
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/README.md
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/RELEASE.md
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/annotate.gemspec
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/bin/annotate
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/active_record_patch.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/annotate_models.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/annotate_models/file_patterns.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/annotate_routes.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/annotate_routes/header_generator.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/annotate_routes/helpers.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/constants.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/helpers.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/parser.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/tasks.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/annotate/version.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/generators/annotate/USAGE
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/generators/annotate/install_generator.rb
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/generators/annotate/templates/auto_annotate_models.rake
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/tasks/annotate_models.rake
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/tasks/annotate_models_migrate.rake
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/lib/tasks/annotate_routes.rake
            /usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/potato.md
            /usr/local/lib/ruby/gems/3.3/specifications/annotate-3.2.0.gemspec
            /usr/local/share/licenses/rubygem-annotate-3.2.0/LICENSE
            /usr/local/share/licenses/rubygem-annotate-3.2.0/MIT
            /usr/local/share/licenses/rubygem-annotate-3.2.0/catalog.mk
    kiri@smtp:~/projects/rails/sample_view[1861]% which annotate
    /usr/local/bin/annotate
    kiri@smtp:~/projects/rails/sample_view[1862]% setenv PATH "/usr/local/lib/ruby/gems/3.3/gems/annotate-3.2.0/bin:$PATH"

  2. annotateのコマンドライン

    kiri@smtp:~/projects/rails/sample_view[1863]% bundle exec annotate --help
    Usage: annotate [options] [model_file]*
            --additional-file-patterns path1,path2,path3
                                         Additional file paths or globs to annotate, separated by commas (e.g. `/foo/bar/%model_name%/*.rb,/baz/%model_name%.rb`)
        -d, --delete                     Remove annotations from all model files or the routes.rb file
        -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]
                                         Place the annotations at the top (before) or the bottom (after) of the model file
            --pf, --position-in-factory [before|top|after|bottom]
                                        Place the annotations at the top (before) or the bottom (after) of any factory files
            --px, --position-in-fixture [before|top|after|bottom]
                                         Place the annotations at the top (before) or the bottom (after) of any fixture files
            --pt, --position-in-test [before|top|after|bottom]
                                         Place the annotations at the top (before) or the bottom (after) of any test files
            --pr, --position-in-routes [before|top|after|bottom]
                                         Place the annotations at the top (before) or the bottom (after) of the routes.rb file
            --ps, --position-in-serializer [before|top|after|bottom]
                                         Place the annotations at the top (before) or the bottom (after) of the serializer files
            --w, --wrapper STR           Wrap annotation with the text passed as parameter.
                                         If --w option is used, the same text will be used as opening and closing
            --wo, --wrapper-open STR     Annotation wrapper opening.
            --wc, --wrapper-close STR    Annotation wrapper closing
        -r, --routes                     Annotate routes.rb with the output of 'rake routes'
            --models                     Annotate ActiveRecord models
        -a, --active-admin               Annotate active_admin models
        -v, --version                    Show the current version of this gem
        -m, --show-migration             Include the migration version number in the annotation
        -k, --show-foreign-keys          List the table's foreign key constraints in the annotation
            --ck, --complete-foreign-keys
                                         Complete foreign key names in the annotation
        -i, --show-indexes               List the table's database indexes in the annotation
        -s, --simple-indexes             Concat the column's related indexes in the annotation
            --model-dir dir              Annotate model files stored in dir rather than app/models, separate multiple dirs with commas
            --root-dir dir               Annotate files stored within root dir projects, separate multiple dirs with commas
            --ignore-model-subdirects    Ignore subdirectories of the models directory
            --sort                       Sort columns alphabetically, rather than in creation order
            --classified-sort            Sort columns alphabetically, but first goes id, then the rest columns, then the timestamp columns and then the association columns
        -R, --require path               Additional file to require before loading models, may be used multiple times
        -e [tests,fixtures,factories,serializers],
            --exclude                    Do not annotate fixtures, test files, factories, and/or serializers
        -f [bare|rdoc|yard|markdown],    Render Schema Infomation as plain/RDoc/Yard/Markdown
            --format
            --force                      Force new annotations even if there are no changes.
            --frozen                     Do not allow to change annotations. Exits non-zero if there are going to be changes to files.
            --timestamp                  Include timestamp in (routes) annotation
            --trace                      If unable to annotate a file, print the full stack trace, not just the exception message.
        -I, --ignore-columns REGEX       don't annotate columns that match a given REGEX (i.e., `annotate -I '^(id|updated_at|created_at)'`
            --ignore-routes REGEX        don't annotate routes that match a given REGEX (i.e., `annotate -I '(mobile|resque|pghero)'`
            --hide-limit-column-types VALUES
                                         don't show limit for given column types, separated by commas (i.e., `integer,boolean,text`)
            --hide-default-column-types VALUES
                                         don't show default for given column types, separated by commas (i.e., `json,jsonb,hstore`)
            --ignore-unknown-models      don't display warnings for bad model files
            --with-comment               include database comments in model annotations
    kiri@smtp:~/projects/rails/sample_view[1864]% 

  3. ルーティングに関しては以下のバグがある(portでは修正済み)

    --- lib/annotate/annotate_routes/header_generator.rb.org	2024-04-03 14:33:35.130623000 +0900
    +++ lib/annotate/annotate_routes/header_generator.rb	2024-04-03 14:34:01.047391000 +0900
    @@ -16,7 +16,7 @@
           private
     
           def routes_map(options)
    -        result = `rake 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

  4. 吐き出す情報を制御(auto_annotate_models.rake)
    1. まずannotate:installする

      kiri@smtp:~/projects/rails/sample_view[1866]% bundle exec rails generate annotate:install
      Running via Spring preloader in process 9626
            create  lib/tasks/auto_annotate_models.rake
      kiri@smtp:~/projects/rails/sample_view[1867]% 

    2. lib/tasks/auto_annotate_models.rakeに生成される

      # NOTE: only doing this in development as some production environments (Heroku)
      # NOTE: are sensitive to local FS writes, and besides -- it's just not proper
      # NOTE: to have a dev-mode tool do its thing in production.
      if Rails.env.development?
        require 'annotate'
        task :set_annotation_options do
          # You can override any of these by setting an environment variable of the
          # same name.
          Annotate.set_defaults(
            'active_admin'                => 'false',
            'additional_file_patterns'    => [],
            'routes'                      => 'false',
            'models'                      => 'true',
            'position_in_routes'          => 'before',
            'position_in_class'           => 'before',
            'position_in_test'            => 'before',
            'position_in_fixture'         => 'before',
            'position_in_factory'         => 'before',
            'position_in_serializer'      => 'before',
            'show_foreign_keys'           => 'true',
            'show_complete_foreign_keys'  => 'false',
            'show_indexes'                => 'true',
            'simple_indexes'              => 'false',
            'model_dir'                   => 'app/models',
            'root_dir'                    => '',
            'include_version'             => 'false',
            'require'                     => '',
            'exclude_tests'               => 'false',
            'exclude_fixtures'            => 'false',
            'exclude_factories'           => 'false',
            'exclude_serializers'         => 'false',
            'exclude_scaffolds'           => 'true',
            'exclude_controllers'         => 'true',
            'exclude_helpers'             => 'true',
            'exclude_sti_subclasses'      => 'false',
            'ignore_model_sub_dir'        => 'false',
            'ignore_columns'              => nil,
            'ignore_routes'               => nil,
            'ignore_unknown_models'       => 'false',
            'hide_limit_column_types'     => 'integer,bigint,boolean',
            'hide_default_column_types'   => 'json,jsonb,hstore',
            'skip_on_db_migrate'          => 'false',
            'format_bare'                 => 'true',
            'format_rdoc'                 => 'false',
            'format_yard'                 => 'false',
            'format_markdown'             => 'false',
            'sort'                        => 'false',
            'force'                       => 'false',
            'frozen'                      => 'false',
            'classified_sort'             => 'true',
            'trace'                       => 'false',
            'wrapper_open'                => nil,
            'wrapper_close'               => nil,
            'with_comment'                => 'true'
          )
        end
      
        Annotate.load_tasks
      end

    3. "'routes'=> 'true'"にする

      kiri@smtp:~/projects/rails/sample_view[1867]% diff -u lib/tasks/auto_annotate_models.rake.org lib/tasks/auto_annotate_models.rake
      --- lib/tasks/auto_annotate_models.rake.org     2024-04-02 11:10:37.000000000 +0900
      +++ lib/tasks/auto_annotate_models.rake 2024-04-08 16:45:08.363430000 +0900
      @@ -9,7 +9,7 @@
           Annotate.set_defaults(
             'active_admin'                => 'false',
             'additional_file_patterns'    => [],
      -      'routes'                      => 'false',
      +      'routes'                      => 'true',
             'models'                      => 'true',
             'position_in_routes'          => 'before',
             'position_in_class'           => 'before',
      kiri@smtp:~/projects/rails/sample_view[1868]%