diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index b964824..dc0ec00 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -6,9 +6,9 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up Ruby - uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e + uses: ruby/setup-ruby@v1 with: - ruby-version: 3.0.0 + ruby-version: 3.3 bundler-cache: true - name: Run Standard run: bundle exec standardrb --fail-level A diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index 924083e..37dc1f3 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -5,7 +5,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ["2.7", "3.0", "3.1", "3.2", "3.3"] + ruby-version: ["3.1", "3.2", "3.3"] steps: - uses: actions/checkout@v2 - name: Set up Ruby diff --git a/README.md b/README.md index bbb2786..296a88b 100644 --- a/README.md +++ b/README.md @@ -53,10 +53,9 @@ Usage: chusaku [options] --dry-run Run without file modifications --exit-with-error-on-annotation Fail if any file was annotated -c, --controllers-pattern=GLOB Specify alternative controller files glob pattern - --verbose Print all annotations + --verbose Print all annotated files -v, --version Show Chusaku version number and quit -h, --help Show this help message and quit - ``` ### Rake usage diff --git a/lib/chusaku.rb b/lib/chusaku.rb index 5196cad..2055602 100644 --- a/lib/chusaku.rb +++ b/lib/chusaku.rb @@ -18,7 +18,6 @@ class << self def call(flags = {}) @flags = flags @routes = Chusaku::Routes.call - @changes = [] @changed_files = [] controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb" @@ -53,46 +52,18 @@ def load_tasks def annotate_file(path:, controller:, actions:) parsed_file = Chusaku::Parser.call(path: path, actions: actions) parsed_file[:groups].each_cons(2) do |prev, curr| - record_change(group: prev, type: :clean, path: path) + clean_group(prev) next unless curr[:type] == :action route_data = @routes[controller][curr[:action]] next unless route_data.any? - record_change(group: curr, type: :annotate, route_data: route_data, path: path) + annotate_group(group: curr, route_data: route_data) end write_to_file(path: path, parsed_file: parsed_file) end - # Clean or annotate a group and track the group as changed if applicable. - # - # @param group [Hash] { type => Symbol, body => String } - # @param type [Symbol] [:clean, :annotate] - # @param path [String] File path - # @param route_data [Array] [{ - # verb: String, - # path: String, - # name: String }] - # @return [void] - def record_change(group:, type:, path:, route_data: []) - old_body = group[:body] - - case type - when :clean - clean_group(group) - when :annotate - annotate_group(group: group, route_data: route_data) - end - return if old_body == group[:body] - - @changes.push \ - old_body: old_body, - new_body: group[:body], - path: path, - line_number: group[:line_number] - end - # Given a parsed group, clean out its contents. # # @param group [Hash] { type => Symbol, body => String } @@ -209,27 +180,13 @@ def output_copy copy end - # Returns the copy for recorded changes if `--verbose` flag is passed. + # Returns the copy for changed files if `--verbose` flag is passed. # - # @return [String] Copy of recorded changes + # @return [String] Copy for changed files def changes_copy return "" unless @flags.include?(:verbose) - @changes.map do |change| - <<~CHANGE_OUTPUT - [#{change[:path]}:#{change[:line_number]}] - - Before: - ```ruby - #{change[:old_body].chomp} - ``` - - After: - ```ruby - #{change[:new_body].chomp} - ``` - CHANGE_OUTPUT - end.join("\n") + "\n" + @changed_files.map { |file| "Annotated #{file}" }.join("\n") + "\n" end end end diff --git a/lib/chusaku/cli.rb b/lib/chusaku/cli.rb index b2963ad..a5967d1 100644 --- a/lib/chusaku/cli.rb +++ b/lib/chusaku/cli.rb @@ -97,7 +97,7 @@ def add_controllers_pattern_flag(opts) # @param opts [OptionParser] OptionParser instance # @return [void] def add_verbose_flag(opts) - opts.on("--verbose", "Print all annotations") do + opts.on("--verbose", "Print all annotated files") do @options[:verbose] = true end end diff --git a/test/chusaku_test.rb b/test/chusaku_test.rb index de493b3..f703c7a 100644 --- a/test/chusaku_test.rb +++ b/test/chusaku_test.rb @@ -38,23 +38,10 @@ def test_verbose_flag out, _err = capture_io { exit_code = Chusaku.call(verbose: true) } assert_equal(0, exit_code) - assert_includes \ - out, - <<~CHANGES_COPY - [test/mock/app/controllers/api/tacos_controller.rb:2] - - Before: - ```ruby - def show - ``` - - After: - ```ruby - # @route GET / (root) - # @route GET /api/tacos/:id (taco) - def show - ``` - CHANGES_COPY + refute_includes(out, "Annotated test/mock/app/controllers/api/burritos_controller.rb") + assert_includes(out, "Annotated test/mock/app/controllers/api/cakes_controller.rb") + assert_includes(out, "Annotated test/mock/app/controllers/api/tacos_controller.rb") + assert_includes(out, "Annotated test/mock/app/controllers/waterlilies_controller.rb") end def test_mock_app