lib/chusaku.rb in chusaku-0.6.0 vs lib/chusaku.rb in chusaku-0.6.1

- old
+ new

@@ -1,11 +1,9 @@ -# frozen_string_literal: true +require "chusaku/version" +require "chusaku/parser" +require "chusaku/routes" -require 'chusaku/version' -require 'chusaku/parser' -require 'chusaku/routes' - # Handles core functionality of annotating projects. module Chusaku class << self # The main method to run Chusaku. Annotate all actions in a Rails project as # follows: @@ -19,11 +17,12 @@ # @return [Integer] 0 on success, 1 on error def call(flags = {}) @flags = flags @routes = Chusaku::Routes.call @changes = [] - controllers_pattern = 'app/controllers/**/*_controller.rb' + @changed_files = [] + controllers_pattern = "app/controllers/**/*_controller.rb" Dir.glob(Rails.root.join(controllers_pattern)).each do |path| controller = %r{controllers/(.*)_controller\.rb}.match(path)[1] actions = @routes[controller] next if actions.nil? @@ -90,13 +89,13 @@ # @param group [Hash] { type => Symbol, body => String } # @return {void} def clean_group(group) return unless group[:type] == :comment - group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, '') + group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, "") group[:body] = - group[:body].gsub(%r{^\s*# (GET|POST|PATCH/PUT|DELETE) /\S+$\n}, '') + group[:body].gsub(%r{^\s*# (GET|POST|PATCH/PUT|DELETE) /\S+$\n}, "") end # Add an annotation to the given group given by Chusaku::Parser that looks # like: # @@ -123,12 +122,12 @@ def annotate_route(verb:, path:, name:, defaults:) annotation = "@route #{verb} #{path}" if defaults&.any? defaults_str = defaults - .map { |key, value| "#{key}: #{value.inspect}" } - .join(', ') + .map { |key, value| "#{key}: #{value.inspect}" } + .join(", ") annotation += " {#{defaults_str}}" end annotation += " (#{name})" unless name.nil? annotation end @@ -141,10 +140,11 @@ def write_to_file(path:, parsed_file:) new_content = new_content_for(parsed_file) return if parsed_file[:content] == new_content !@flags.include?(:dry) && perform_write(path: path, content: new_content) + @changed_files.push(path) end # Extracts the new file content for the given parsed file. # # @param parsed_file [Hash] { groups => Array<Hash> } @@ -172,28 +172,28 @@ # When running the test suite, we want to make sure we're not overwriting # any files. `r` mode ensures that, and `w` is used for actual usage. # # @return [String] 'r' or 'w' def file_mode - File.instance_methods.include?(:test_write) ? 'r' : 'w' + File.instance_methods.include?(:test_write) ? "r" : "w" end # Output results to user. # # @return [Integer] 0 for success, 1 for error def output_results puts(output_copy) exit_code = 0 - exit_code = 1 if @changes.any? && @flags.include?(:error_on_annotation) + exit_code = 1 if @changed_files.any? && @flags.include?(:error_on_annotation) exit_code end # Determines the copy to be used in the program output. # # @return [String] Copy to be outputted to user def output_copy - return 'Nothing to annotate.' if @changes.empty? + return "Nothing to annotate." if @changed_files.empty? copy = changes_copy copy += "\nChusaku has finished running." copy += "\nThis was a dry run so no files were changed." if @flags.include?(:dry) copy += "\nExited with status code 1." if @flags.include?(:error_on_annotation) @@ -202,10 +202,10 @@ # Returns the copy for recorded changes if `--verbose` flag is passed. # # @return [String] Copy of recorded changes def changes_copy - return '' unless @flags.include?(:verbose) + return "" unless @flags.include?(:verbose) @changes.map do |change| <<~CHANGE_OUTPUT [#{change[:path]}:#{change[:line_number]}]