guard-cookstyle 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5f12320c9b5e0c4747e14b1a13d274e0345e5b45
4
- data.tar.gz: '03394b2dd0bb869b209e14ae38431067d958993a'
3
+ metadata.gz: f959bd06a4864041ef9cbd6d243727ce97b5d79b
4
+ data.tar.gz: 98aa10c88455e5f832ca8e00364b7f84b18906ed
5
5
  SHA512:
6
- metadata.gz: 7319497e4cd71e9ad87e9ca6d2fd39057ef2e9d7cde6b43a737b4d3744292e23bbb44c5a5539a371223be371bcf24105a5632b6ce3b9046ccb64c3d7aa127bd0
7
- data.tar.gz: e6211d1f4086e55ea3ffc0acc9a2bdbc0ffc7a2a9f4a2fc21171a5b50217ad7d1f0a88ac85cb1ddfb0cc3b0a0ff065320b66ef4de4db624fec4e00eb6e9030c6
6
+ metadata.gz: 4bde112d49d6224507022947e0ffc47fd2535cc36f02d6e9309ce816e49c6da536973d1ad36a67655b883d3729bc38d880701b431619d904d09c2a7741cd15c1
7
+ data.tar.gz: 6d1cc236f2fdc71b011b844e310a7eb49c69e64975430ff4924113d14d687b3f6618b2f1ef7f93dd934c7a53a5855752ebdea1ae776cd5cd2ad7472308f65188
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0
4
+
5
+ - use `-r cookstyle` to load `.rubocop.yml`.
6
+
7
+
3
8
  ## 0.2.0
4
9
 
5
10
  I noticed there was https://github.com/chef/cookstyle#guard-rubocop, so I adjusted usage.
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_runtime_dependency 'guard'
24
+ spec.add_runtime_dependency 'guard-rubocop'
24
25
  spec.add_runtime_dependency 'cookstyle'
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.16"
@@ -1,98 +1,57 @@
1
1
  require 'guard'
2
2
  require 'guard/plugin'
3
+ require 'guard/rubocop'
4
+ require 'cookstyle'
3
5
  require "guard/cookstyle/version"
4
6
  require "guard/cookstyle/monkey_patch/listen"
5
7
 
6
8
  module Guard
7
- class Cookstyle < Plugin
8
- autoload :Runner, 'guard/cookstyle/runner'
9
-
10
- attr_reader :options, :failed_paths
11
-
12
- def initialize(options = {})
13
- super
14
-
15
- @options = {
16
- all_on_start: true,
17
- keep_failed: true,
18
- notification: :failed,
19
- cli: nil,
20
- hide_stdout: false,
21
- cookbook_dirs: %w[cookbooks site-cookbooks]
22
- }.merge(options)
23
-
24
- @failed_paths = []
25
- end
26
-
27
- def start
28
- run_all if @options[:all_on_start]
29
- end
30
-
31
- def run_all
32
- UI.info 'Inspecting Chef Cookbook style of all files'
33
- inspect_with_cookstyle
34
- end
35
-
36
- def run_on_additions(paths)
37
- run_partially(paths)
38
- end
39
-
40
- def run_on_modifications(paths)
41
- run_partially(paths)
42
- end
43
-
44
- def reload
45
- @failed_paths = []
46
- end
47
-
48
- private
49
-
50
- def run_partially(paths)
51
- paths += @failed_paths if @options[:keep_failed]
52
- paths = clean_paths(paths)
9
+ class Cookstyle < ::Guard::RuboCop
10
+ class_eval do
11
+ autoload :Runner, 'guard/cookstyle/runner'
12
+
13
+ def initialize(options = {})
14
+ super
15
+
16
+ @options = {
17
+ all_on_start: true,
18
+ keep_failed: true,
19
+ notification: :failed,
20
+ cli: nil,
21
+ hide_stdout: false,
22
+ cookbook_dirs: %w[.]
23
+ }.merge(options)
24
+
25
+ @failed_paths = []
26
+ end
53
27
 
54
- return if paths.empty?
28
+ def run_all
29
+ UI.info 'Inspecting Chef Cookbook style of all files'
30
+ inspect_with_cookstyle
31
+ end
55
32
 
56
- displayed_paths = paths.map { |path| smart_path(path) }
57
- UI.info "Inspecting Chef Cookbook style: #{displayed_paths.join(' ')}"
33
+ private
58
34
 
59
- inspect_with_cookstyle(paths)
60
- end
35
+ def run_partially(paths)
36
+ paths += @failed_paths if @options[:keep_failed]
37
+ paths = clean_paths(paths)
61
38
 
62
- def inspect_with_cookstyle(paths = [])
63
- runner = Runner.new(@options)
64
- passed = runner.run(paths)
65
- @failed_paths = runner.failed_paths
66
- throw :task_has_failed unless passed
67
- rescue StandardError => error
68
- UI.error 'The following exception occurred while running guard-cookstyle: ' \
69
- "#{error.backtrace.first} #{error.message} (#{error.class.name})"
70
- end
39
+ return if paths.empty?
71
40
 
72
- def clean_paths(paths)
73
- paths = paths.dup
74
- paths.map! { |path| File.expand_path(path) }
75
- paths.uniq!
76
- paths.reject! do |path|
77
- next true unless File.exist?(path)
78
- included_in_other_path?(path, paths)
79
- end
80
- paths
81
- end
41
+ displayed_paths = paths.map { |path| smart_path(path) }
42
+ UI.info "Inspecting Chef Cookbook style: #{displayed_paths.join(' ')}"
82
43
 
83
- def included_in_other_path?(target_path, other_paths)
84
- dir_paths = other_paths.select { |path| File.directory?(path) }
85
- dir_paths.delete(target_path)
86
- dir_paths.any? do |dir_path|
87
- target_path.start_with?(dir_path)
44
+ inspect_with_cookstyle(paths)
88
45
  end
89
- end
90
46
 
91
- def smart_path(path)
92
- if path.start_with?(Dir.pwd)
93
- Pathname.new(path).relative_path_from(Pathname.getwd).to_s
94
- else
95
- path
47
+ def inspect_with_cookstyle(paths = [])
48
+ runner = Runner.new(@options)
49
+ passed = runner.run(paths)
50
+ @failed_paths = runner.failed_paths
51
+ throw :task_has_failed unless passed
52
+ rescue StandardError => error
53
+ UI.error 'The following exception occurred while running guard-cookstyle: ' \
54
+ "#{error.backtrace.first} #{error.message} (#{error.class.name})"
96
55
  end
97
56
  end
98
57
  end
@@ -1,146 +1,33 @@
1
1
  require 'json'
2
2
  require 'cookstyle'
3
+ require 'guard/rubocop/runner'
3
4
 
4
5
  module Guard
5
6
  class Cookstyle
6
7
  # This class runs `cookstyle` command, retrieves result and notifies.
7
8
  # An instance of this class is intended to invoke `cookstyle` only once in its lifetime.
8
- class Runner
9
- def initialize(options)
10
- @options = options
11
- end
12
-
13
- def run(paths = [])
14
- command = build_command(paths)
15
- passed = system(*command)
16
-
17
- case @options[:notification]
18
- when :failed
19
- notify(passed) unless passed
20
- when true
21
- notify(passed)
22
- end
23
-
24
- open_launchy_if_needed
25
-
26
- passed
27
- end
28
-
29
- def build_command(paths)
30
- command = ['cookstyle']
31
-
32
- if should_add_default_formatter_for_console?
33
- command.concat(%w[--format progress]) # Keep default formatter for console.
34
- end
35
-
36
- command.concat(['--config', ::Cookstyle.config])
37
- command.concat(['--format', 'json', '--out', json_file_path])
38
- command << '--force-exclusion'
39
- command.concat(args_specified_by_user)
9
+ class Runner < ::Guard::RuboCop::Runner
10
+ class_eval do
11
+ def build_command(paths)
12
+ command = ['rubocop']
40
13
 
41
- if paths.any?
42
- command.concat(paths)
43
- else
44
- command.concat(@options[:cookbook_dirs])
45
- end
46
- command
47
- end
48
-
49
- def should_add_default_formatter_for_console?
50
- !@options[:hide_stdout] && !include_formatter_for_console?(args_specified_by_user)
51
- end
52
-
53
- def args_specified_by_user
54
- @args_specified_by_user ||= begin
55
- args = @options[:cli]
56
- case args
57
- when Array then args
58
- when String then args.shellsplit
59
- when NilClass then []
60
- else raise ArgumentError, ':cli option must be either an array or string'
14
+ if should_add_default_formatter_for_console?
15
+ command.concat(%w[--format progress]) # Keep default formatter for console.
61
16
  end
62
- end
63
- end
64
-
65
- def include_formatter_for_console?(cli_args)
66
- index = -1
67
- formatter_args = cli_args.group_by do |arg|
68
- index += 1 if arg == '--format' || arg.start_with?('-f')
69
- index
70
- end
71
- formatter_args.delete(-1)
72
17
 
73
- formatter_args.each_value.any? do |args|
74
- args.none? { |a| a == '--out' || a.start_with?('-o') }
75
- end
76
- end
18
+ command.concat(['-r', 'cookstyle'])
19
+ command.concat(['--format', 'json', '--out', json_file_path])
20
+ command << '--force-exclusion'
21
+ command.concat(args_specified_by_user)
77
22
 
78
- def json_file_path
79
- @json_file_path ||= begin
80
- # Just generate random tempfile path.
81
- basename = self.class.name.downcase.gsub('::', '_')
82
- tempfile = Tempfile.new(basename)
83
- tempfile.close
84
- tempfile.path
85
- end
86
- end
87
-
88
- def result
89
- @result ||= begin
90
- File.open(json_file_path) do |file|
91
- # Rubinius 2.0.0.rc1 does not support `JSON.load` with 3 args.
92
- JSON.parse(file.read, symbolize_names: true)
23
+ if paths.any?
24
+ command.concat(paths)
25
+ else
26
+ command.concat(@options[:cookbook_dirs])
93
27
  end
28
+ command
94
29
  end
95
30
  end
96
-
97
- def notify(passed)
98
- image = passed ? :success : :failed
99
- Notifier.notify(summary_text, title: 'Cookstyle results', image: image)
100
- end
101
-
102
- def summary_text
103
- summary = result[:summary]
104
-
105
- text = pluralize(summary[:inspected_file_count], 'file')
106
- text << ' inspected, '
107
-
108
- offense_count = summary[:offense_count] || summary[:offence_count]
109
- text << pluralize(offense_count, 'offense', no_for_zero: true)
110
- text << ' detected'
111
- end
112
-
113
- def failed_paths
114
- failed_files = result[:files].reject do |file|
115
- offenses = file[:offenses] || file[:offences]
116
- offenses.empty?
117
- end
118
- failed_files.map do |file|
119
- file[:path]
120
- end
121
- end
122
-
123
- def pluralize(number, thing, options = {})
124
- text = ''
125
-
126
- if number.zero? && options[:no_for_zero]
127
- text = 'no'
128
- else
129
- text << number.to_s
130
- end
131
-
132
- text << " #{thing}"
133
- text << 's' unless number == 1
134
-
135
- text
136
- end
137
-
138
- def open_launchy_if_needed
139
- return unless (output_path = @options[:launchy])
140
- return unless File.exist?(output_path)
141
- require 'launchy'
142
- ::Launchy.open(output_path)
143
- end
144
31
  end
145
32
  end
146
33
  end
@@ -1,6 +1,6 @@
1
1
  require "cookstyle"
2
2
  group :cookbooks do
3
- guard :cookstyle do
3
+ guard :cookstyle, cookbook_dirs: %w[cookbooks site-cookbooks] do
4
4
  watch(/(cookbooks|site-cookbooks).+\.rb$/)
5
5
  watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
6
6
  end
@@ -1,5 +1,5 @@
1
1
  module GuardCookstyleVersion
2
2
  def self.to_s
3
- '0.2.0'
3
+ '0.3.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-cookstyle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sawanoboly
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: guard-rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: cookstyle
29
43
  requirement: !ruby/object:Gem::Requirement