Skip to content

Commit fa3e5f8

Browse files
committed
Merge branch 'master' of github.com:codeclimate/codeclimate into skip_required_yaml_on_engines_flag
2 parents 3d3e8a0 + cde2362 commit fa3e5f8

17 files changed

+456
-127
lines changed

.codeclimate.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ engines:
1010
ratings:
1111
paths:
1212
- "**.rb"
13-
exclude_paths: []
13+
exclude_paths:
14+
- .bundle/**/*

.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.git
2+
.bundle

.rubocop.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Style/ClassAndModuleChildren:
1818
Exclude:
1919
- 'spec/**/*'
2020

21+
Metrics/MethodLength:
22+
Exclude:
23+
- 'spec/**/*'
24+
2125
Metrics/ModuleLength:
2226
Exclude:
2327
- 'spec/**/*'
@@ -34,6 +38,9 @@ Style/DotPosition:
3438
Style/SignalException:
3539
Enabled: false
3640

41+
Style/TrailingUnderscoreVariable:
42+
Enabled: false
43+
3744
Metrics/AbcSize:
3845
Enabled: false
3946

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
codeclimate (0.6.4)
4+
codeclimate (0.7.0)
55
activesupport (~> 4.2, >= 4.2.1)
66
codeclimate-yaml (~> 0.2.3)
77
faraday (~> 0.9.1)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.6.4
1+
0.7.0

config/engines.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ rubocop:
1313
image: codeclimate/codeclimate-rubocop
1414
description: A Ruby static code analyzer, based on the community Ruby style guide.
1515
community: false
16+
upgrade_languages:
17+
- Ruby
1618
enable_regexps:
1719
- \.rb$
1820
default_ratings_paths:
@@ -24,6 +26,8 @@ duplication:
2426
default_config:
2527
languages:
2628
- ruby
29+
upgrade_languages:
30+
- Ruby
2731
enable_regexps:
2832
- \.rb$
2933
default_ratings_paths:
@@ -64,6 +68,8 @@ eslint:
6468
image: codeclimate/codeclimate-eslint
6569
description: A JavaScript/JSX linting utility
6670
community: false
71+
upgrade_languages:
72+
- JavaScript
6773
enable_regexps:
6874
- \.js$
6975
- \.jsx$
@@ -96,14 +102,18 @@ bundler-audit:
96102
image: codeclimate/codeclimate-bundler-audit
97103
description: Patch-level verification for Bundler
98104
community: false
99-
enable_patterns:
100-
- Gemfile.lock
105+
upgrade_languages:
106+
- Ruby
107+
enable_regexps:
108+
- Gemfile\.lock
101109
default_ratings_paths:
102110
- Gemfile.lock
103111
phpcodesniffer:
104112
image: codeclimate/codeclimate-phpcodesniffer
105113
description: PHP Code Sniffer
106114
community: false
115+
upgrade_languages:
116+
- PHP
107117
enable_regexps:
108118
- \.php$
109119
- \.module$
@@ -116,6 +126,8 @@ phpmd:
116126
image: codeclimate/codeclimate-phpmd
117127
description: PHP Mess Detector
118128
community: false
129+
upgrade_languages:
130+
- PHP
119131
enable_regexps:
120132
- \.php$
121133
- \.module$

lib/cc/cli/config.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ def add_engine(engine_name, engine_config)
2121

2222
def add_exclude_paths(paths)
2323
config["exclude_paths"] ||= []
24-
config["exclude_paths"] += paths.map { |path| "#{path}/**/*" }
24+
config["exclude_paths"] += paths.map do |path|
25+
if path.ends_with?("/")
26+
"#{path}**/*"
27+
else
28+
path
29+
end
30+
end
2531
end
2632

2733
private

lib/cc/cli/config_generator.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module CC
2+
module CLI
3+
class ConfigGenerator
4+
CODECLIMATE_YAML = Command::CODECLIMATE_YAML
5+
AUTO_EXCLUDE_PATHS = %w(config/ db/ features/ node_modules/ script/ spec/ test/ vendor/).freeze
6+
7+
def self.for(filesystem, engine_registry, upgrade_requested)
8+
if upgrade_requested && upgrade_needed?(filesystem)
9+
UpgradeConfigGenerator.new(filesystem, engine_registry)
10+
else
11+
ConfigGenerator.new(filesystem, engine_registry)
12+
end
13+
end
14+
15+
def initialize(filesystem, engine_registry)
16+
@filesystem = filesystem
17+
@engine_registry = engine_registry
18+
end
19+
20+
def eligible_engines
21+
return @eligible_engines if @eligible_engines
22+
23+
engines = engine_registry.list
24+
@eligible_engines = engines.each_with_object({}) do |(name, config), result|
25+
if engine_eligible?(config)
26+
result[name] = config
27+
end
28+
end
29+
end
30+
31+
def exclude_paths
32+
AUTO_EXCLUDE_PATHS.select { |path| filesystem.exist?(path) }
33+
end
34+
35+
def post_generation_verb
36+
"generated"
37+
end
38+
39+
private
40+
41+
attr_reader :engine_registry, :filesystem
42+
43+
def self.upgrade_needed?(filesystem)
44+
if filesystem.exist?(CODECLIMATE_YAML)
45+
YAML.safe_load(File.read(CODECLIMATE_YAML))["languages"].present?
46+
end
47+
end
48+
49+
def engine_eligible?(engine)
50+
!engine["community"] && engine["enable_regexps"].present? && files_exist?(engine)
51+
end
52+
53+
def files_exist?(engine)
54+
filesystem.any? do |path|
55+
engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
56+
end
57+
end
58+
end
59+
end
60+
end

lib/cc/cli/init.rb

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
require 'cc/cli/config'
2+
require 'cc/cli/config_generator'
3+
require 'cc/cli/upgrade_config_generator'
24

35
module CC
46
module CLI
57
class Init < Command
68
include CC::Analyzer
79

8-
AUTO_EXCLUDE_PATHS = %w[config db features node_modules script spec test vendor].freeze
9-
1010
def run
11-
if filesystem.exist?(CODECLIMATE_YAML)
11+
if !upgrade? && filesystem.exist?(CODECLIMATE_YAML)
1212
say "Config file .codeclimate.yml already present.\nTry running 'validate-config' to check configuration."
13+
elsif upgrade? && engines_enabled?
14+
say "--upgrade should not be used on a .codeclimate.yml configured for the Platform.\nTry running 'validate-config' to check configuration."
1315
else
1416
create_codeclimate_yaml
15-
say "Config file .codeclimate.yml successfully generated.\nEdit and then try running 'validate-config' to check configuration."
17+
say "Config file .codeclimate.yml successfully #{config_generator.post_generation_verb}.\nEdit and then try running 'validate-config' to check configuration."
1618
create_default_configs
1719
end
1820
end
1921

2022
private
2123

24+
def upgrade?
25+
@args.include?("--upgrade")
26+
end
27+
2228
def create_codeclimate_yaml
2329
config = CC::CLI::Config.new
2430

25-
eligible_engines.each do |(engine_name, engine_config)|
31+
config_generator.eligible_engines.each do |(engine_name, engine_config)|
2632
config.add_engine(engine_name, engine_config)
2733
end
2834

29-
config.add_exclude_paths(auto_exclude_paths)
35+
config.add_exclude_paths(config_generator.exclude_paths)
3036
filesystem.write_path(CODECLIMATE_YAML, config.to_yaml)
3137
end
3238

@@ -43,37 +49,27 @@ def create_default_configs
4349
end
4450

4551
def available_configs
46-
all_paths = eligible_engines.flat_map do |engine_name, _|
52+
all_paths = config_generator.eligible_engines.flat_map do |engine_name, _|
4753
engine_directory = File.expand_path("../../../../config/#{engine_name}", __FILE__)
4854
Dir.glob("#{engine_directory}/*", File::FNM_DOTMATCH)
4955
end
5056

5157
all_paths.reject { |path| ['.', '..'].include?(File.basename(path)) }
5258
end
5359

54-
def engine_eligible?(engine)
55-
!engine["community"] && engine["enable_regexps"].present? && has_files?(engine)
56-
end
57-
58-
def has_files?(engine)
59-
filesystem.any? do |path|
60-
engine["enable_regexps"].any? { |re| Regexp.new(re).match(path) }
60+
def engines_enabled?
61+
unless @engines_enabled.nil?
62+
return @engines_enabled
6163
end
62-
end
6364

64-
def auto_exclude_paths
65-
AUTO_EXCLUDE_PATHS.select { |path| filesystem.exist?(path) }
65+
if filesystem.exist?(CODECLIMATE_YAML)
66+
config = CC::Analyzer::Config.new(File.read(CODECLIMATE_YAML))
67+
@engines_enabled ||= config.engine_names.any?
68+
end
6669
end
6770

68-
def eligible_engines
69-
return @eligible_engines if @eligible_engines
70-
71-
engines = engine_registry.list
72-
@eligible_engines = engines.each_with_object({}) do |(name, config), result|
73-
if engine_eligible?(config)
74-
result[name] = config
75-
end
76-
end
71+
def config_generator
72+
@config_generator ||= ConfigGenerator.for(filesystem, engine_registry, upgrade?)
7773
end
7874
end
7975
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "cc/cli/config_generator"
2+
3+
module CC
4+
module CLI
5+
class UpgradeConfigGenerator < ConfigGenerator
6+
def exclude_paths
7+
existing_yaml["exclude_paths"] || []
8+
end
9+
10+
def post_generation_verb
11+
"upgraded"
12+
end
13+
14+
private
15+
16+
def engine_eligible?(engine)
17+
base_eligble = super
18+
if engine["upgrade_languages"].present?
19+
base_eligble && (engine["upgrade_languages"] & classic_languages).any?
20+
else
21+
base_eligble
22+
end
23+
end
24+
25+
def classic_languages
26+
@classic_languages ||= existing_yaml["languages"].reject { |_, v| !v }.map(&:first)
27+
end
28+
29+
def existing_yaml
30+
@existing_yml ||= YAML.safe_load(File.read(CODECLIMATE_YAML))
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)