lib/chusaku.rb in chusaku-0.1.4 vs lib/chusaku.rb in chusaku-0.2.0

- old
+ new

@@ -10,25 +10,29 @@ # # # @route GET /waterlilies/:id (waterlilies) # def show # # ... # end - def self.call + # + # @param {Array<String>} args - CLI flags + # @return {Integer} 0 on success, 1 on error + def self.call(args = []) routes = Chusaku::Routes.call controller_pattern = 'app/controllers/**/*_controller.rb' controller_paths = Dir.glob(Rails.root.join(controller_pattern)) + annotated_paths = [] # Loop over all controller file paths. controller_paths.each do |path| controller = /controllers\/(.*)_controller\.rb/.match(path)[1] actions = routes[controller] next if actions.nil? # Parse the file and iterate over the parsed content, two entries at a # time. parsed_file = Chusaku::Parser.call(path: path, actions: actions.keys) - parsed_file.each_cons(2) do |prev, curr| + parsed_file[:groups].each_cons(2) do |prev, curr| # Remove all @route comments in the previous group. if prev[:type] == :comment prev[:body] = prev[:body].gsub(/^\s*#\s*@route.*$\n/, '') end @@ -48,27 +52,37 @@ curr[:body] = comment + curr[:body] end end # Write to file. - parsed_content = parsed_file.map { |pf| pf[:body] } - write(path, parsed_content.join) + parsed_content = parsed_file[:groups].map { |pf| pf[:body] } + new_content = parsed_content.join + if parsed_file[:content] != new_content + write(path, new_content) unless args.include?(:dry) + annotated_paths << path + end end # Output results to user. - if controller_paths.any? - puts "Annotated #{controller_paths.join(', ')}" + if annotated_paths.any? + puts "Annotated #{annotated_paths.join(', ')}" + if args.include?(:error_on_annotation) + 1 + else + 0 + end else puts "Nothing to annotate" + 0 end end # Write given content to a file. If we're using an overridden version of File, # then use its method instead for testing purposes. # - # @param {String} path - # @param {String} content + # @param {String} path - File path to write to + # @param {String} content - Contents of the file # @return {void} def self.write(path, content) File.open(path, 'r+') do |file| if file.respond_to?(:test_write) file.test_write(content, path) @@ -80,11 +94,11 @@ # Given a hash describing an action, generate an annotation in the form: # # @route GET /waterlilies/:id (waterlilies) # - # @param {Hash} action_info - # @return {String} + # @param {Hash} action_info - Parsed line given by Chusaku::Parser + # @return {String} Annotation for given parsed line def self.annotate(action_info) verb = action_info[:verb] path = action_info[:path] name = action_info[:name] annotation = "@route #{verb} #{path}"