From 06daec77026a491bf12487e10bd8cd2b0548c4b7 Mon Sep 17 00:00:00 2001 From: Richard Shade Date: Tue, 9 May 2017 10:09:10 -0500 Subject: [PATCH] Add rubocop (#16) * fixing up some rubocop * adding rubocop excludes * adding changelog and fixing travis --- .rubocop.yml | 32 ++++++++++++++ .travis.yml | 7 +++ CHANGELOG.md | 8 ++++ Gemfile | 7 +-- Rakefile | 4 +- lib/yard-chef.rb | 21 +++++---- lib/yard-chef/code_objects/chef_object.rb | 6 +-- lib/yard-chef/code_objects/provider_object.rb | 13 +++--- lib/yard-chef/code_objects/recipe_object.rb | 2 +- lib/yard-chef/code_objects/resource_object.rb | 2 +- lib/yard-chef/handlers/actions.rb | 4 +- lib/yard-chef/handlers/attribute.rb | 9 ++-- lib/yard-chef/handlers/base.rb | 28 ++++++------ lib/yard-chef/handlers/cookbook.rb | 8 ++-- lib/yard-chef/handlers/recipe.rb | 4 +- templates/default/cookbook/html/setup.rb | 28 ++++++------ templates/default/fulldoc/html/setup.rb | 43 +++++++++---------- templates/default/layout/html/setup.rb | 18 ++++---- templates/default/resource/html/setup.rb | 2 +- yard-chef.gemspec | 24 +++++++---- 20 files changed, 160 insertions(+), 110 deletions(-) create mode 100644 .rubocop.yml create mode 100644 CHANGELOG.md diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..62babd6 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,32 @@ +Metrics/LineLength: + Enabled: false + +Style/ClassAndModuleChildren: + Enabled: false + +Metrics/MethodLength: + Enabled: false + +Metrics/AbcSize: + Enabled: false + +Lint/UselessAssignment: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/GuardClause: + Enabled: false + +Metrics/CyclomaticComplexity: + Enabled: false + +Metrics/PerceivedComplexity: + Enabled: false + +Style/ClassVars: + Enabled: false + +Style/FileName: + Enabled: false diff --git a/.travis.yml b/.travis.yml index 2bf9535..8e0818d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,3 +6,10 @@ rvm: - 2.3.1 os: - linux + +matrix: + include: + - rvm: 2.3.1 + env: + RUBOCOP: 1 + script: $(which bundle) exec rubocop diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..c8a9612 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# yard-chef CHANGELOG + +This file is used to list changes made in each version of the yard-chef. + +## [unreleased] + +## [1.0.0] (04-18-2013) +- Initial Release diff --git a/Gemfile b/Gemfile index 5794cf1..b75c5a2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,12 @@ -source "https://rubygems.org" +source 'https://rubygems.org' gemspec - gem 'rake', '~> 12.0' +gem 'rubocop', '~> 0.48.1' + group :development do - gem 'ripper', '~> 1.0', :platforms => [:ruby_18, :mingw_18] + gem 'ripper', '~> 1.0', platforms: %i[ruby_18 mingw_18] end # vim: filetype=ruby diff --git a/Rakefile b/Rakefile index a69f4b2..c510e4a 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,2 @@ -require "bundler/gem_tasks" -task :default => [:build] +require 'bundler/gem_tasks' +task default: [:build] diff --git a/lib/yard-chef.rb b/lib/yard-chef.rb index d8ce593..5e7c7a2 100644 --- a/lib/yard-chef.rb +++ b/lib/yard-chef.rb @@ -44,21 +44,20 @@ module YARD::CodeObjects::Chef # be taken care of in the handler. # TODO: Investigate if YARD handlers can be invoked if parser is in a # specific directory. - YARD::Parser::SourceParser.before_parse_list do |files, globals| + YARD::Parser::SourceParser.before_parse_list do |files, _globals| files.each do |file| path_arr = File.expand_path(file).to_s.split('/') - unless (index = path_arr.index('recipes')).nil? - # Cookbook name can be derived from file path - # cookbook//recipes/recipe_name.rb - cookbook_name = path_arr[index - 1] - cookbook = ChefObject.register(CHEF, cookbook_name, :cookbook) + next if (index = path_arr.index('recipes')).nil? + # Cookbook name can be derived from file path + # cookbook//recipes/recipe_name.rb + cookbook_name = path_arr[index - 1] + cookbook = ChefObject.register(CHEF, cookbook_name, :cookbook) - recipe_name = path_arr.last.to_s.sub('.rb','') - recipe = ChefObject.register(cookbook, recipe_name, :recipe) + recipe_name = path_arr.last.to_s.sub('.rb', '') + recipe = ChefObject.register(cookbook, recipe_name, :recipe) - recipe.source = IO.read(file) - recipe.add_file(file, 1) - end + recipe.source = IO.read(file) + recipe.add_file(file, 1) end end diff --git a/lib/yard-chef/code_objects/chef_object.rb b/lib/yard-chef/code_objects/chef_object.rb index 91a681e..c92ffa9 100644 --- a/lib/yard-chef/code_objects/chef_object.rb +++ b/lib/yard-chef/code_objects/chef_object.rb @@ -70,8 +70,8 @@ def self.register(namespace, name, type) element_obj = YARD::Registry.resolve(:root, "#{namespace}::#{name}") if element_obj.nil? element_obj = element.new(namespace, name) - log.info "Created [#{type.to_s.capitalize}]" + - " #{element_obj.name} => #{element_obj.namespace}" + log.info "Created [#{type.to_s.capitalize}]" \ + " #{element_obj.name} => #{element_obj.namespace}" end element_obj else @@ -87,7 +87,7 @@ def self.register(namespace, name, type) # def children_by_type(type) children = YARD::Registry.all(type) - children.reject { |child| child.parent != self } + children.select { |child| child.parent == self } end # Gets all Chef cookbooks. diff --git a/lib/yard-chef/code_objects/provider_object.rb b/lib/yard-chef/code_objects/provider_object.rb index d264fbc..3465b07 100644 --- a/lib/yard-chef/code_objects/provider_object.rb +++ b/lib/yard-chef/code_objects/provider_object.rb @@ -54,7 +54,7 @@ def long_name else name = @name.to_s.capitalize end - namespace = @namespace.to_s.split('::').map { |str| str.capitalize } + namespace = @namespace.to_s.split('::').map(&:capitalize) "#{namespace.join('::')}::#{name}" end @@ -65,12 +65,11 @@ def long_name def map_resource(file) file_handle = File.open(File.expand_path(file), 'r') file_handle.readlines.each do |line| - if line =~ /#\s@resource/ - resource_name = line.split(%r{@resource })[1].strip - @resource = ChefObject.register(RESOURCE, resource_name, :resource) - @resource.providers.push(self) unless @resource.providers.include?(self) - break - end + next unless line =~ /#\s@resource/ + resource_name = line.split(/@resource /)[1].strip + @resource = ChefObject.register(RESOURCE, resource_name, :resource) + @resource.providers.push(self) unless @resource.providers.include?(self) + break end end diff --git a/lib/yard-chef/code_objects/recipe_object.rb b/lib/yard-chef/code_objects/recipe_object.rb index 232a0eb..2d8cced 100644 --- a/lib/yard-chef/code_objects/recipe_object.rb +++ b/lib/yard-chef/code_objects/recipe_object.rb @@ -45,7 +45,7 @@ def initialize(namespace, name) # @return [String] recipe name # def name - self.parent.name.to_s << '::' << @name.to_s + parent.name.to_s << '::' << @name.to_s end end end diff --git a/lib/yard-chef/code_objects/resource_object.rb b/lib/yard-chef/code_objects/resource_object.rb index dac790f..98b1eb0 100644 --- a/lib/yard-chef/code_objects/resource_object.rb +++ b/lib/yard-chef/code_objects/resource_object.rb @@ -56,7 +56,7 @@ def long_name name = @name.to_s.capitalize end - namespace = @namespace.to_s.split('::').map { |str| str.capitalize } + namespace = @namespace.to_s.split('::').map(&:capitalize) "#{namespace.join('::')}::#{name}" end diff --git a/lib/yard-chef/handlers/actions.rb b/lib/yard-chef/handlers/actions.rb index 789d839..0e47071 100644 --- a/lib/yard-chef/handlers/actions.rb +++ b/lib/yard-chef/handlers/actions.rb @@ -42,7 +42,7 @@ def process # if multiple actions listed in same line, split the actions and # register them if statement.first_line =~ /,/ - statement.first_line.split(%r{,?\s*:}).each do |action| + statement.first_line.split(/,?\s*:/).each do |_action| action = ChefObject.register(resource_obj, name, :action) end else @@ -50,7 +50,7 @@ def process action.docstring = statement.docstring end - log.info "Found [Actions] in #{parser.file.to_s}" + log.info "Found [Actions] in #{parser.file}" end end end diff --git a/lib/yard-chef/handlers/attribute.rb b/lib/yard-chef/handlers/attribute.rb index 9a58913..f2657fd 100644 --- a/lib/yard-chef/handlers/attribute.rb +++ b/lib/yard-chef/handlers/attribute.rb @@ -58,17 +58,16 @@ def process # @return [YARD::Docstring] docstring for the attribute # def docstring - description = "" + description = '' path_array = parser.file.to_s.split('/') if path_array.include?('metadata.rb') # Suppose :description string have concatenation operator '+' then # YARD builds an abstract syntax tree (AST). We need to traverse the # tree to get the whole description string statement.parameters[1].children.each do |ast_node| - if ast_node.jump(:ident).source == "description" - ast_node.traverse do |child| - description << child.jump(:string_content).source if child.type == :string_content - end + next unless ast_node.jump(:ident).source == 'description' + ast_node.traverse do |child| + description << child.jump(:string_content).source if child.type == :string_content end end else diff --git a/lib/yard-chef/handlers/base.rb b/lib/yard-chef/handlers/base.rb index e2e11e6..8c0f0cf 100644 --- a/lib/yard-chef/handlers/base.rb +++ b/lib/yard-chef/handlers/base.rb @@ -39,13 +39,13 @@ def name # @return [CookbookObject] the CookbookObject # def cookbook - cookbook_name = "" + cookbook_name = '' path_array = File.expand_path(statement.file).to_s.split('/') - if path_array.include?('metadata.rb') - cookbook_name = path_array[path_array.index('metadata.rb') - 1] - else - cookbook_name = path_array[path_array.length - 3] - end + cookbook_name = if path_array.include?('metadata.rb') + path_array[path_array.index('metadata.rb') - 1] + else + path_array[path_array.length - 3] + end ChefObject.register(CHEF, cookbook_name, :cookbook) end @@ -57,23 +57,23 @@ def cookbook # def lwrp path_array = File.expand_path(statement.file).to_s.split('/') - if path_array.include?("resources") + if path_array.include?('resources') type = RESOURCE type_sym = :resource - elsif path_array.include?("providers") + elsif path_array.include?('providers') type = PROVIDER type_sym = :provider else raise "Invalid LWRP type #{@path_array.join(',')}" end - file_name = path_array.last.to_s.sub('.rb','') + file_name = path_array.last.to_s.sub('.rb', '') cookbook_obj = cookbook - if file_name == "default" - lwrp_name = cookbook_obj.name - else - lwrp_name = "#{cookbook_obj.name}_#{file_name}" - end + lwrp_name = if file_name == 'default' + cookbook_obj.name + else + "#{cookbook_obj.name}_#{file_name}" + end ChefObject.register(type, lwrp_name, type_sym) end end diff --git a/lib/yard-chef/handlers/cookbook.rb b/lib/yard-chef/handlers/cookbook.rb index 08a62bf..96f190a 100644 --- a/lib/yard-chef/handlers/cookbook.rb +++ b/lib/yard-chef/handlers/cookbook.rb @@ -60,7 +60,7 @@ def process # @return [String] the method name # def name - string = "" + string = '' # YARD builds an abstract syntax tree (AST) which we need to traverse # to obtain the complete docstring statement.parameters.first.traverse do |child| @@ -77,17 +77,17 @@ def docstring(base_dir) type = '' string = '' readme_path = base_dir + '/README.md' - if File.exists?(readme_path) + if File.exist?(readme_path) type = :markdown string = IO.read(readme_path) else readme_path = base_dir + '/README.rdoc' - if File.exists?(readme_path) + if File.exist?(readme_path) type = :rdoc string = IO.read(readme_path) end end - return YARD::DocstringParser.new.parse(string).to_docstring, type + [YARD::DocstringParser.new.parse(string).to_docstring, type] end end end diff --git a/lib/yard-chef/handlers/recipe.rb b/lib/yard-chef/handlers/recipe.rb index 478937a..a224cf9 100644 --- a/lib/yard-chef/handlers/recipe.rb +++ b/lib/yard-chef/handlers/recipe.rb @@ -40,7 +40,7 @@ def process # def name recipe = statement.parameters.first.jump(:string_content, :ident).source - recipe = recipe.split("::")[1] if recipe =~ /::/ + recipe = recipe.split('::')[1] if recipe =~ /::/ recipe = 'default' if recipe == cookbook.name.to_s recipe end @@ -51,7 +51,7 @@ def name # @return [YARD::Docsting] the docstring # def docstring - description = "" + description = '' # YARD builds an abstract syntax tree (AST) which we need to traverse # to obtain the complete docstring statement.parameters[1].traverse do |child| diff --git a/templates/default/cookbook/html/setup.rb b/templates/default/cookbook/html/setup.rb index 166433c..bc00578 100644 --- a/templates/default/cookbook/html/setup.rb +++ b/templates/default/cookbook/html/setup.rb @@ -23,18 +23,18 @@ def init sections :cookbook_title, - [ - :docstring, - :generated_docs, - [ - :recipes, - T('resource'), - T('provider'), - T('attribute'), - :definitions, - :libraries, - :element_details, - [T('recipe'), T('action'), T('definition')] - ] - ] + [ + :docstring, + :generated_docs, + [ + :recipes, + T('resource'), + T('provider'), + T('attribute'), + :definitions, + :libraries, + :element_details, + [T('recipe'), T('action'), T('definition')] + ] + ] end diff --git a/templates/default/fulldoc/html/setup.rb b/templates/default/fulldoc/html/setup.rb index b12b4d1..853bcc2 100644 --- a/templates/default/fulldoc/html/setup.rb +++ b/templates/default/fulldoc/html/setup.rb @@ -50,13 +50,13 @@ def generate_resources_list def generate_definitions_list cookbooks = YARD::Registry.all(:cookbook).sort_by { |cookbook| cookbook.name.to_s } definitions = [] - cookbooks.each { |cookbook| definitions = definitions + cookbook.definitions } + cookbooks.each { |cookbook| definitions += cookbook.definitions } generate_full_list(definitions, 'Definition', 'definitions') end # Called by menu_lists in layout/html/setup.rb by default def generate_cookbooks_list - cookbooks = YARD::Registry.all(:cookbook).sort_by{|cookbook| cookbook.name.to_s} + cookbooks = YARD::Registry.all(:cookbook).sort_by { |cookbook| cookbook.name.to_s } generate_full_list(cookbooks, 'Cookbooks', 'cookbooks') end @@ -69,31 +69,30 @@ def generate_libraries_list def generate_full_list(objects, title, type) @items = objects @list_title = "#{title} List" - @list_type = "#{type}" + @list_type = type.to_s asset(url_for_list(@list_type), erb(:full_list)) end def class_list(root = YARD::Registry.root) - out = "" + out = '' children = run_verifier(root.children) - children.reject {|c| c.nil? }.sort_by {|child| child.path }.map do |child| - if child.is_a?(CodeObjects::ModuleObject) - name = child.name - has_children = child.children.any? {|o| o.is_a?(CodeObjects::ModuleObject) } - out << "
  • " - out << " " if has_children - out << linkify(child, name) - out << " < #{child.superclass.name}" if child.is_a?(CodeObjects::ClassObject) && child.superclass - out << "" - if !child.namespace || child.namespace.root? - out << "Top Level Namespace" - else - out << child.namespace.path - end - out << "" - out << "
  • " - out << "
      #{class_list(child)}
    " if has_children - end + children.reject(&:nil?).sort_by(&:path).map do |child| + next unless child.is_a?(CodeObjects::ModuleObject) + name = child.name + has_children = child.children.any? { |o| o.is_a?(CodeObjects::ModuleObject) } + out << '
  • ' + out << " " if has_children + out << linkify(child, name) + out << " < #{child.superclass.name}" if child.is_a?(CodeObjects::ClassObject) && child.superclass + out << "" + out << if !child.namespace || child.namespace.root? + 'Top Level Namespace' + else + child.namespace.path + end + out << '' + out << '
  • ' + out << "
      #{class_list(child)}
    " if has_children end out end diff --git a/templates/default/layout/html/setup.rb b/templates/default/layout/html/setup.rb index 2ba4f6c..bdc400c 100644 --- a/templates/default/layout/html/setup.rb +++ b/templates/default/layout/html/setup.rb @@ -26,9 +26,9 @@ def init if @file.attributes[:namespace] @object = options.object = Registry.at(@file.attributes[:namespace]) || Registry.root end - @breadcrumb_title = "File: " + @file.title - @page_title = "Cookbook Documentation" - sections :layout, [:title, [:diskfile, :cookbook_table]] + @breadcrumb_title = 'File: ' + @file.title + @page_title = 'Cookbook Documentation' + sections :layout, [:title, %i[diskfile cookbook_table]] elsif object == '_index.html' sections :layout, [:title, [T('chef')]] end @@ -37,16 +37,16 @@ def init # Add yard-chef specific menus # Methods generate_#{type}_list must be defined in fulldoc/html/setup.rb def menu_lists -[ { :type => 'cookbooks', :title => 'Cookbooks', :search_title => 'Cookbook List'}, - { :type => 'recipes', :title => 'Recipes', :search_title => 'Recipe List'}, - { :type => 'resources', :title => 'Resources', :search_title => 'Resource List'}, - { :type => 'definitions', :title => 'Definitions', :search_title => 'Definitions List' }, - { :type => 'class', :title => 'Libraries', :search_title => 'Libraries List' } ] + [{ type: 'cookbooks', title: 'Cookbooks', search_title: 'Cookbook List' }, + { type: 'recipes', title: 'Recipes', search_title: 'Recipe List' }, + { type: 'resources', title: 'Resources', search_title: 'Resource List' }, + { type: 'definitions', title: 'Definitions', search_title: 'Definitions List' }, + { type: 'class', title: 'Libraries', search_title: 'Libraries List' }] end def page_title if object == '_index.html' - "Cookbook Documentation" + 'Cookbook Documentation' elsif object.is_a? CodeObjects::Base case object.type when :cookbook diff --git a/templates/default/resource/html/setup.rb b/templates/default/resource/html/setup.rb index ee358cd..463c147 100644 --- a/templates/default/resource/html/setup.rb +++ b/templates/default/resource/html/setup.rb @@ -32,6 +32,6 @@ def init # def link_to_provider(provider) url = url_for(provider.cookbook, provider.long_name) - url.slice!("../") + url.slice!('../') url end diff --git a/yard-chef.gemspec b/yard-chef.gemspec index a297c85..79d6f1c 100644 --- a/yard-chef.gemspec +++ b/yard-chef.gemspec @@ -1,14 +1,20 @@ # -*- encoding: utf-8 -*- + Gem::Specification.new do |gem| - gem.name = "yard-chef" - gem.version = IO.read(File.join(File.dirname(__FILE__), "VERSION")).chomp + gem.name = 'yard-chef' + gem.version = IO.read(File.join(File.dirname(__FILE__), 'VERSION')).chomp gem.platform = Gem::Platform::RUBY gem.authors = ['Douglas Thrift', 'Nick Stakanov', 'Nitin Mohan'] - gem.email = ["douglas@rightscale.com", "nitin@rightscale.com", "support@rightscale.com"] - gem.homepage = "https://github.com/rightscale-cookbooks/yard-chef" - gem.summary = %q{YARD plugin for Chef} - gem.description = %q{yard-chef is a YARD plugin for Chef that adds support for documenting Chef cookbooks, resources, providers, and definitions.} - gem.license = "MIT" + gem.email = [ + 'douglas@rightscale.com', + 'nitin@rightscale.com', + 'support@rightscale.com' + ] + gem.homepage = 'https://github.com/rightscale-cookbooks/yard-chef' + gem.summary = 'YARD plugin for Chef' + gem.description = 'yard-chef is a YARD plugin for Chef that adds support for documenting Chef cookbooks, + resources, providers, and definitions.' + gem.license = 'MIT' gem.add_runtime_dependency 'yard', '~> 0.9.9' gem.add_runtime_dependency 'redcarpet', '>= 2.1.1' @@ -17,7 +23,7 @@ Gem::Specification.new do |gem| gem.files += Dir.glob('templates/**/*.rb') gem.files += Dir.glob('templates/**/*.css') gem.files += Dir.glob('lib/**/*.rb') - gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.require_path = "lib" + gem.require_path = 'lib' end