lib/chusaku.rb in chusaku-1.1.0 vs lib/chusaku.rb in chusaku-1.2.0

- old
+ new

@@ -16,11 +16,10 @@ # @param flags [Hash] CLI flags # @return [Integer] 0 on success, 1 on error def call(flags = {}) @flags = flags @routes = Chusaku::Routes.call - @changes = [] @changed_files = [] controllers_pattern = @flags[:controllers_pattern] || "app/controllers/**/*_controller.rb" Dir.glob(Rails.root.join(controllers_pattern)).each do |path| controller = %r{controllers/(.*)_controller\.rb}.match(path)[1] @@ -31,10 +30,19 @@ end output_results end + # Load Rake tasks for Chusaku. Should be called in your project's `Rakefile`. + # + # @return [void] + def load_tasks + Dir[File.join(File.dirname(__FILE__), "tasks", "**/*.rake")].each do |task| + load(task) + end + end + private # Adds annotations to the given file. # # @param path [String] Path to file @@ -42,50 +50,22 @@ # @param actions [Array<String>] List of valid actions for the controller # @return [void] 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<Hash>] [{ - # 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 } # @return {void} def clean_group(group) @@ -189,38 +169,24 @@ # 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 @changed_files.empty? + return "Controller files unchanged." if @changed_files.empty? copy = changes_copy - copy += "\nChusaku has finished running." + copy += "Chusaku 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) 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") + @changed_files.map { |file| "Annotated #{file}" }.join("\n") + "\n" end end end