Skip to content

Commit

Permalink
Add rubocop (#16)
Browse files Browse the repository at this point in the history
* fixing up some rubocop

* adding rubocop excludes

* adding changelog and fixing travis
  • Loading branch information
rshade authored May 9, 2017
1 parent 8e10c3c commit 06daec7
Show file tree
Hide file tree
Showing 20 changed files with 160 additions and 110 deletions.
32 changes: 32 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ rvm:
- 2.3.1
os:
- linux

matrix:
include:
- rvm: 2.3.1
env:
RUBOCOP: 1
script: $(which bundle) exec rubocop
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "bundler/gem_tasks"
task :default => [:build]
require 'bundler/gem_tasks'
task default: [:build]
21 changes: 10 additions & 11 deletions lib/yard-chef.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/<cookbook_name>/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/<cookbook_name>/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

Expand Down
6 changes: 3 additions & 3 deletions lib/yard-chef/code_objects/chef_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions lib/yard-chef/code_objects/provider_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/yard-chef/code_objects/recipe_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/yard-chef/code_objects/resource_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/yard-chef/handlers/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ 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
action = ChefObject.register(resource_obj, name, :action)
action.docstring = statement.docstring
end

log.info "Found [Actions] in #{parser.file.to_s}"
log.info "Found [Actions] in #{parser.file}"
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/yard-chef/handlers/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions lib/yard-chef/handlers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/yard-chef/handlers/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/yard-chef/handlers/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand Down
28 changes: 14 additions & 14 deletions templates/default/cookbook/html/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 06daec7

Please sign in to comment.