churn_vs_complexity 1.8.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +46 -23
  4. data/bin/churn_vs_complexity +6 -1
  5. data/lib/churn_vs_complexity/analysis_scope.rb +20 -0
  6. data/lib/churn_vs_complexity/churn.rb +41 -8
  7. data/lib/churn_vs_complexity/cli/main.rb +58 -19
  8. data/lib/churn_vs_complexity/cli/parser.rb +85 -121
  9. data/lib/churn_vs_complexity/cli.rb +2 -3
  10. data/lib/churn_vs_complexity/complexity/flog_calculator.rb +0 -3
  11. data/lib/churn_vs_complexity/complexity/lizard_calculator.rb +64 -0
  12. data/lib/churn_vs_complexity/complexity.rb +1 -7
  13. data/lib/churn_vs_complexity/complexity_validator.rb +1 -16
  14. data/lib/churn_vs_complexity/concurrent_calculator.rb +20 -23
  15. data/lib/churn_vs_complexity/delta/checker.rb +35 -21
  16. data/lib/churn_vs_complexity/delta/commit_hydrator.rb +1 -1
  17. data/lib/churn_vs_complexity/delta/complexity_annotator.rb +23 -5
  18. data/lib/churn_vs_complexity/delta/config.rb +15 -16
  19. data/lib/churn_vs_complexity/delta/factory.rb +2 -2
  20. data/lib/churn_vs_complexity/delta/multi_checker.rb +24 -25
  21. data/lib/churn_vs_complexity/delta/serializer.rb +7 -4
  22. data/lib/churn_vs_complexity/delta.rb +11 -49
  23. data/lib/churn_vs_complexity/diff/checker.rb +10 -3
  24. data/lib/churn_vs_complexity/diff/config.rb +29 -20
  25. data/lib/churn_vs_complexity/file_selector.rb +63 -114
  26. data/lib/churn_vs_complexity/focus/config.rb +42 -32
  27. data/lib/churn_vs_complexity/focus/serializer.rb +17 -4
  28. data/lib/churn_vs_complexity/gate/config.rb +35 -25
  29. data/lib/churn_vs_complexity/gate/serializer.rb +9 -2
  30. data/lib/churn_vs_complexity/git_strategy.rb +29 -10
  31. data/lib/churn_vs_complexity/hotspots/config.rb +29 -25
  32. data/lib/churn_vs_complexity/language_registry.rb +108 -0
  33. data/lib/churn_vs_complexity/language_validator.rb +1 -3
  34. data/lib/churn_vs_complexity/normal/config.rb +31 -100
  35. data/lib/churn_vs_complexity/normal/serializer/graph.rb +3 -2
  36. data/lib/churn_vs_complexity/normal/serializer/summary.rb +18 -14
  37. data/lib/churn_vs_complexity/normal/serializer/summary_hash.rb +36 -21
  38. data/lib/churn_vs_complexity/normal/serializer.rb +2 -1
  39. data/lib/churn_vs_complexity/normal.rb +17 -13
  40. data/lib/churn_vs_complexity/risk_classifier.rb +3 -19
  41. data/lib/churn_vs_complexity/timetravel/config.rb +34 -47
  42. data/lib/churn_vs_complexity/timetravel/schedule.rb +15 -0
  43. data/lib/churn_vs_complexity/timetravel/serializer/stats_calculator.rb +36 -32
  44. data/lib/churn_vs_complexity/timetravel/serializer.rb +16 -24
  45. data/lib/churn_vs_complexity/timetravel/traveller.rb +46 -15
  46. data/lib/churn_vs_complexity/timetravel.rb +36 -14
  47. data/lib/churn_vs_complexity/triage/checker.rb +24 -28
  48. data/lib/churn_vs_complexity/triage/collaborator_set.rb +11 -0
  49. data/lib/churn_vs_complexity/triage/config.rb +29 -26
  50. data/lib/churn_vs_complexity/triage.rb +1 -0
  51. data/lib/churn_vs_complexity/validator_set.rb +9 -0
  52. data/lib/churn_vs_complexity/version.rb +1 -1
  53. data/lib/churn_vs_complexity.rb +3 -0
  54. metadata +18 -59
  55. data/.devcontainer/devcontainer.json +0 -26
  56. data/.rubocop.yml +0 -25
  57. data/CLAUDE.md +0 -60
  58. data/Dockerfile +0 -34
  59. data/Dockerfile.dev +0 -27
  60. data/Rakefile +0 -6
  61. data/TODO +0 -11
  62. data/lib/churn_vs_complexity/complexity/eslint_calculator.rb +0 -40
  63. data/lib/churn_vs_complexity/complexity/go_calculator.rb +0 -48
  64. data/lib/churn_vs_complexity/complexity/kotlin_calculator.rb +0 -54
  65. data/lib/churn_vs_complexity/complexity/pmd/files_calculator.rb +0 -26
  66. data/lib/churn_vs_complexity/complexity/pmd/folder_calculator.rb +0 -20
  67. data/lib/churn_vs_complexity/complexity/pmd.rb +0 -54
  68. data/lib/churn_vs_complexity/complexity/python_calculator.rb +0 -50
  69. data/lib/churn_vs_complexity/complexity/rust_calculator.rb +0 -54
  70. data/lib/churn_vs_complexity/complexity/swift_calculator.rb +0 -54
  71. data/package-lock.json +0 -6
  72. data/tmp/eslint-support/complexity-calculator.js +0 -51
  73. data/tmp/eslint-support/package.json +0 -11
  74. data/tmp/pmd-support/ruleset.xml +0 -21
  75. data/tmp/test-support/delta/ruby-summary.txt +0 -55
  76. data/tmp/test-support/delta/ruby.csv +0 -12
  77. data/tmp/test-support/go/main.go +0 -11
  78. data/tmp/test-support/go/utils.go +0 -13
  79. data/tmp/test-support/java/small-example/src/main/java/org/example/Main.java +0 -10
  80. data/tmp/test-support/java/small-example/src/main/java/org/example/spice/Checker.java +0 -28
  81. data/tmp/test-support/javascript/complex.js +0 -43
  82. data/tmp/test-support/javascript/moderate.js +0 -12
  83. data/tmp/test-support/javascript/simple.js +0 -5
  84. data/tmp/test-support/javascript/typescript-example.ts +0 -26
  85. data/tmp/test-support/kotlin/Main.kt +0 -7
  86. data/tmp/test-support/kotlin/Utils.kt +0 -11
  87. data/tmp/test-support/python/example.py +0 -6
  88. data/tmp/test-support/python/utils.py +0 -9
  89. data/tmp/test-support/rust/main.rs +0 -7
  90. data/tmp/test-support/rust/utils.rs +0 -20
  91. data/tmp/test-support/swift/main.swift +0 -7
  92. data/tmp/test-support/swift/utils.swift +0 -24
  93. data/tmp/test-support/txt/abc.txt +0 -0
  94. data/tmp/test-support/txt/d.txt +0 -0
  95. data/tmp/test-support/txt/ef.txt +0 -0
  96. data/tmp/test-support/txt/ghij.txt +0 -0
  97. data/tmp/test-support/txt/klm.txt +0 -0
  98. data/tmp/test-support/txt/nopq.txt +0 -0
  99. data/tmp/test-support/txt/r.txt +0 -0
  100. data/tmp/test-support/txt/st.txt +0 -0
  101. data/tmp/test-support/txt/uvx.txt +0 -0
  102. data/tmp/test-support/txt/yz.txt +0 -0
  103. data/tmp/timetravel/.keep +0 -0
@@ -5,43 +5,42 @@ module ChurnVsComplexity
5
5
  CONCURRENCY = Etc.nprocessors * 2
6
6
 
7
7
  class MultiChecker
8
- def initialize(serializer:, factory:, commits:, language:, excluded:)
8
+ def initialize(scope:, serializer:, factory:, commits:)
9
+ @scope = scope
9
10
  @serializer = serializer
10
- @excluded = excluded
11
11
  @factory = factory
12
12
  @commits = commits
13
- @language = language
14
13
  end
15
14
 
16
15
  def check(folder:)
17
- indexed_commits = @commits.map.with_index do |commit, index|
18
- [commit, index]
19
- end
20
-
21
- results = []
22
-
23
- concurrency = [CONCURRENCY, indexed_commits.size].min
24
-
25
- concurrency.times.map do |ci|
26
- Thread.new do
27
- loop do
28
- commit, index = indexed_commits.shift
29
- break if commit.nil?
30
-
31
- result = check_commit(commit:, data_isolation_id: ci, folder:)
32
- results[index] = result
33
- end
34
- end
35
- end.each(&:join)
36
-
16
+ queue = @commits.map.with_index { |commit, index| [commit, index] }
17
+ results = collect_commit_results(queue, folder:)
37
18
  @serializer.serialize(results)
38
19
  end
39
20
 
40
21
  private
41
22
 
23
+ def collect_commit_results(queue, folder:)
24
+ worker_count = [CONCURRENCY, queue.size].min
25
+ threads = worker_count.times.map do |worker_id|
26
+ Thread.new { process_queue(queue, data_isolation_id: worker_id, folder:) }
27
+ end
28
+ indexed_results = threads.flat_map(&:value)
29
+ indexed_results.sort_by(&:first).map(&:last)
30
+ end
31
+
32
+ def process_queue(queue, data_isolation_id:, folder:)
33
+ processed = []
34
+ while (entry = queue.shift)
35
+ commit, index = entry
36
+ processed << [index, check_commit(commit:, data_isolation_id:, folder:)]
37
+ end
38
+ processed
39
+ end
40
+
42
41
  def check_commit(commit:, data_isolation_id:, folder:)
43
- Checker.new(serializer: Serializer::PassThrough, factory: @factory, commit:, language: @language,
44
- excluded: @excluded, data_isolation_id:,).check(folder:)
42
+ Checker.new(scope: @scope, commit:, serializer: Serializer::PassThrough, factory: @factory)
43
+ .check(folder:, data_isolation_id:)
45
44
  end
46
45
  end
47
46
  end
@@ -27,7 +27,7 @@ module ChurnVsComplexity
27
27
  rows.join("\n")
28
28
  end
29
29
 
30
- def has_commit_summary? = false
30
+ def self.commit_summary? = false
31
31
  end
32
32
 
33
33
  module Summary
@@ -37,14 +37,16 @@ module ChurnVsComplexity
37
37
  results.map { |r| serialize_single(r) }.join("\n\n\n\n")
38
38
  end
39
39
 
40
- def has_commit_summary? = true
40
+ def commit_summary? = true
41
41
 
42
42
  private
43
43
 
44
44
  def serialize_single(result)
45
45
  changes = result[:changes]
46
46
 
47
- commit_text = "Commit: #{result[:commit]}\nParent: #{result[:parent]}\nNext: #{result[:next_commit]}"
47
+ commit_text = "Commit: #{result[:commit]}\n" \
48
+ "Parent: #{result[:parent]}\n" \
49
+ "Next: #{result[:next_commit]}"
48
50
  change_text = changes.empty? ? '(No changes)' : describe(changes)
49
51
 
50
52
  "#{commit_text}\n\n\n#{change_text}"
@@ -62,7 +64,8 @@ module ChurnVsComplexity
62
64
 
63
65
  module PassThrough
64
66
  extend Normal::Serializer::None
65
- def self.has_commit_summary? = true
67
+
68
+ def self.commit_summary? = true
66
69
  end
67
70
  end
68
71
  end
@@ -11,62 +11,24 @@ require_relative 'delta/commit_hydrator'
11
11
  module ChurnVsComplexity
12
12
  module Delta
13
13
  module SerializerValidator
14
- def self.validate!(serializer:); end
14
+ SUPPORTED = %i[none csv summary].freeze
15
+
16
+ def self.validate!(serializer:)
17
+ raise ValidationError, "Unsupported serializer: #{serializer}" unless SUPPORTED.include?(serializer)
18
+ end
15
19
  end
16
20
 
17
21
  class << self
18
- def engine(cache_components:, language:, excluded:, files:)
19
- file_selector = file_selector(language:, included: files, excluded:)
22
+ def engine(language:, excluded:, files:)
23
+ entry = LanguageRegistry.fetch(language)
20
24
  Engine.concurrent(
21
- since: nil, complexity: complexity(language, cache_components), churn: Churn::Disabled,
25
+ since: nil,
26
+ complexity: entry.calculator,
27
+ churn: Churn::Disabled,
22
28
  serializer: Normal::Serializer::None,
23
- file_selector:,
29
+ file_selector: entry.file_selector.predefined(included: files, excluded:),
24
30
  )
25
31
  end
26
-
27
- private
28
-
29
- def file_selector(language:, included:, excluded:)
30
- case language
31
- when :java
32
- FileSelector::Java.predefined(included:, excluded:)
33
- when :ruby
34
- FileSelector::Ruby.predefined(included:, excluded:)
35
- when :javascript
36
- FileSelector::JavaScript.predefined(included:, excluded:)
37
- when :python
38
- FileSelector::Python.predefined(included:, excluded:)
39
- when :go
40
- FileSelector::Go.predefined(included:, excluded:)
41
- when :kotlin
42
- FileSelector::Kotlin.predefined(included:, excluded:)
43
- when :rust
44
- FileSelector::Rust.predefined(included:, excluded:)
45
- when :swift
46
- FileSelector::Swift.predefined(included:, excluded:)
47
- end
48
- end
49
-
50
- def complexity(language, cache_components)
51
- case language
52
- when :java
53
- Complexity::PMD::FilesCalculator.new(cache_components:)
54
- when :ruby
55
- Complexity::FlogCalculator
56
- when :javascript
57
- Complexity::ESLintCalculator
58
- when :python
59
- Complexity::PythonCalculator
60
- when :go
61
- Complexity::GoCalculator
62
- when :kotlin
63
- Complexity::KotlinCalculator
64
- when :rust
65
- Complexity::RustCalculator
66
- when :swift
67
- Complexity::SwiftCalculator
68
- end
69
- end
70
32
  end
71
33
  end
72
34
  end
@@ -3,14 +3,15 @@
3
3
  module ChurnVsComplexity
4
4
  module Diff
5
5
  class Checker
6
- def initialize(engine_builder:, serializer:, reference:)
6
+ def initialize(engine_builder:, serializer:, reference:, worktree_factory: nil)
7
7
  @engine_builder = engine_builder
8
8
  @serializer = serializer
9
9
  @reference = reference
10
+ @worktree_factory = worktree_factory
10
11
  end
11
12
 
12
13
  def check(folder:)
13
- worktree = setup_worktree(folder:)
14
+ worktree = create_worktree(folder:)
14
15
 
15
16
  begin
16
17
  before_result = @engine_builder.call.check(folder: worktree.folder)
@@ -23,9 +24,15 @@ module ChurnVsComplexity
23
24
 
24
25
  private
25
26
 
27
+ def create_worktree(folder:)
28
+ return @worktree_factory.call(folder:, reference: @reference) if @worktree_factory
29
+
30
+ setup_worktree(folder:)
31
+ end
32
+
26
33
  def setup_worktree(folder:)
27
34
  git_strategy = GitStrategy.new(folder:)
28
- worktree_number = "diff_#{Thread.current.object_id}"
35
+ worktree_number = "diff_#{Process.pid}_#{Thread.current.object_id}"
29
36
  worktree = Timetravel::Worktree.new(root_folder: folder, git_strategy:, number: worktree_number)
30
37
  worktree.prepare
31
38
  worktree.checkout(@reference)
@@ -3,30 +3,28 @@
3
3
  module ChurnVsComplexity
4
4
  module Diff
5
5
  class Config
6
- def initialize(
7
- language:,
8
- reference:,
9
- serializer: :json,
10
- since: nil,
11
- excluded: [],
12
- complexity_validator: ComplexityValidator,
13
- since_validator: Normal::SinceValidator,
14
- **options
15
- )
16
- @language = language
6
+ SUPPORTED_SERIALIZERS = %i[json].freeze
7
+
8
+ def self.from_options(options)
9
+ new(
10
+ scope: AnalysisScope.from_options(options),
11
+ reference: options[:reference],
12
+ serializer: options[:serializer] || :json,
13
+ )
14
+ end
15
+
16
+ def initialize(scope:, reference:, serializer: :json, validators: ValidatorSet.new)
17
+ @scope = scope
17
18
  @reference = reference
18
19
  @serializer = serializer
19
- @since = since
20
- @excluded = excluded
21
- @complexity_validator = complexity_validator
22
- @since_validator = since_validator
23
- @options = options
20
+ @validators = validators
24
21
  end
25
22
 
26
23
  def validate!
27
- LanguageValidator.validate!(@language)
28
- @since_validator.validate!(since: @since, relative_period: nil)
29
- @complexity_validator.validate!(@language)
24
+ LanguageValidator.validate!(@scope.language)
25
+ validate_serializer!
26
+ validate_period!
27
+ @validators.complexity.validate!(@scope.language)
30
28
  end
31
29
 
32
30
  def checker
@@ -40,7 +38,18 @@ module ChurnVsComplexity
40
38
  private
41
39
 
42
40
  def build_engine
43
- Normal::Config.new(language: @language, serializer: :none, since: @since, excluded: @excluded).checker
41
+ Normal::Config.new(scope: @scope, serializer: :none).checker
42
+ end
43
+
44
+ def validate_serializer!
45
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
46
+
47
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
48
+ end
49
+
50
+ def validate_period!
51
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
52
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
44
53
  end
45
54
 
46
55
  def diff_serializer
@@ -3,26 +3,11 @@
3
3
  module ChurnVsComplexity
4
4
  module FileSelector
5
5
  def self.extensions(language)
6
- case language
7
- when :java
8
- ['.java']
9
- when :ruby
10
- ['.rb']
11
- when :javascript
12
- ['.js', '.jsx', '.ts', '.tsx']
13
- when :python
14
- ['.py']
15
- when :go
16
- ['.go']
17
- when :kotlin
18
- ['.kt', '.kts']
19
- when :rust
20
- ['.rs']
21
- when :swift
22
- ['.swift']
23
- else
24
- raise Error, "Unsupported language: #{language}"
25
- end
6
+ LanguageRegistry.fetch(language).extensions
7
+ end
8
+
9
+ def self.for(language)
10
+ LanguageRegistry.file_selector_for(language)
26
11
  end
27
12
 
28
13
  module Any
@@ -33,27 +18,17 @@ module ChurnVsComplexity
33
18
  end
34
19
 
35
20
  class Excluding
36
- def initialize(extensions, excluded, convert_to_absolute_path = false)
21
+ def initialize(extensions, excluded)
37
22
  @extensions = extensions
38
23
  @excluded = excluded
39
- @convert_to_absolute_path = convert_to_absolute_path
40
24
  end
41
25
 
42
26
  def select_files(folder)
43
- were_excluded = []
44
- were_included = []
45
- candidates(folder).each do |f|
46
- if has_excluded_pattern?(f)
47
- were_excluded << f
48
- elsif has_correct_extension?(f) && File.file?(f)
49
- were_included << f
50
- end
51
- end
52
- if @convert_to_absolute_path
53
- were_excluded.map! { |f| File.absolute_path(f) }
54
- were_included.map! { |f| File.absolute_path(f) }
55
- end
56
- { explicitly_excluded: were_excluded, included: were_included }
27
+ were_excluded, were_included = partition_candidates(folder)
28
+ {
29
+ explicitly_excluded: were_excluded,
30
+ included: were_included,
31
+ }
57
32
  end
58
33
 
59
34
  protected
@@ -64,18 +39,35 @@ module ChurnVsComplexity
64
39
 
65
40
  private
66
41
 
67
- def has_correct_extension?(file_path)
42
+ def partition_candidates(folder)
43
+ excluded = []
44
+ included = []
45
+ candidates(folder).each do |file|
46
+ if excluded_pattern?(file)
47
+ excluded << file
48
+ elsif include_candidate?(file)
49
+ included << file
50
+ end
51
+ end
52
+ [excluded, included]
53
+ end
54
+
55
+ def include_candidate?(file)
56
+ correct_extension?(file) && File.file?(file)
57
+ end
58
+
59
+ def correct_extension?(file_path)
68
60
  @extensions.any? { |e| file_path.end_with?(e) }
69
61
  end
70
62
 
71
- def has_excluded_pattern?(file_path)
63
+ def excluded_pattern?(file_path)
72
64
  @excluded.any? { |e| file_path.include?(e) }
73
65
  end
74
66
  end
75
67
 
76
68
  class Predefined < Excluding
77
- def initialize(included:, extensions:, excluded:, convert_to_absolute_path: false)
78
- super(extensions, excluded, convert_to_absolute_path)
69
+ def initialize(included:, extensions:, excluded:)
70
+ super(extensions, excluded)
79
71
  @included = included
80
72
  end
81
73
 
@@ -84,87 +76,44 @@ module ChurnVsComplexity
84
76
  def candidates(*) = @included
85
77
  end
86
78
 
87
- module Java
88
- def self.excluding(excluded)
89
- Excluding.new(FileSelector.extensions(:java), excluded)
79
+ class Factory
80
+ def initialize(extensions:, default_excludes: [])
81
+ @extensions = extensions.freeze
82
+ @default_excludes = default_excludes.freeze
90
83
  end
91
84
 
92
- def self.predefined(included:, excluded:)
93
- Predefined.new(included:, extensions: FileSelector.extensions(:java), excluded:)
94
- end
95
- end
96
-
97
- module Ruby
98
- def self.excluding(excluded)
99
- Excluding.new(FileSelector.extensions(:ruby), excluded)
85
+ def excluding(excluded)
86
+ Excluding.new(@extensions, @default_excludes + excluded)
100
87
  end
101
88
 
102
- def self.predefined(included:, excluded:)
103
- Predefined.new(included:, extensions: FileSelector.extensions(:ruby), excluded:)
89
+ def predefined(included:, excluded:)
90
+ Predefined.new(
91
+ included:,
92
+ extensions: @extensions,
93
+ excluded: @default_excludes + excluded,
94
+ )
104
95
  end
105
96
  end
106
97
 
107
- module JavaScript
108
- def self.excluding(excluded)
109
- Excluding.new(FileSelector.extensions(:javascript), excluded, true)
110
- end
111
-
112
- def self.predefined(included:, excluded:)
113
- Predefined.new(included:, extensions: FileSelector.extensions(:javascript), excluded:,
114
- convert_to_absolute_path: true,)
115
- end
116
- end
117
-
118
- module Python
119
- DEFAULT_EXCLUDES = %w[venv .venv env .env __pycache__ .tox site-packages].freeze
120
-
121
- def self.excluding(excluded)
122
- Excluding.new(FileSelector.extensions(:python), DEFAULT_EXCLUDES + excluded)
123
- end
124
-
125
- def self.predefined(included:, excluded:)
126
- Predefined.new(included:, extensions: FileSelector.extensions(:python), excluded: DEFAULT_EXCLUDES + excluded)
127
- end
128
- end
129
-
130
- module Go
131
- def self.excluding(excluded)
132
- Excluding.new(FileSelector.extensions(:go), excluded)
133
- end
134
-
135
- def self.predefined(included:, excluded:)
136
- Predefined.new(included:, extensions: FileSelector.extensions(:go), excluded:)
137
- end
138
- end
139
-
140
- module Kotlin
141
- def self.excluding(excluded)
142
- Excluding.new(FileSelector.extensions(:kotlin), excluded)
143
- end
144
-
145
- def self.predefined(included:, excluded:)
146
- Predefined.new(included:, extensions: FileSelector.extensions(:kotlin), excluded:)
147
- end
148
- end
149
-
150
- module Rust
151
- def self.excluding(excluded)
152
- Excluding.new(FileSelector.extensions(:rust), excluded)
153
- end
154
-
155
- def self.predefined(included:, excluded:)
156
- Predefined.new(included:, extensions: FileSelector.extensions(:rust), excluded:)
157
- end
158
- end
159
-
160
- module Swift
161
- def self.excluding(excluded)
162
- Excluding.new(FileSelector.extensions(:swift), excluded)
163
- end
164
-
165
- def self.predefined(included:, excluded:)
166
- Predefined.new(included:, extensions: FileSelector.extensions(:swift), excluded:)
167
- end
98
+ {
99
+ Java: :java,
100
+ Ruby: :ruby,
101
+ JavaScript: :javascript,
102
+ Python: :python,
103
+ Go: :go,
104
+ Kotlin: :kotlin,
105
+ Rust: :rust,
106
+ Swift: :swift,
107
+ }.each do |const_name, language|
108
+ const_set(
109
+ const_name,
110
+ Module.new do
111
+ define_singleton_method(:excluding) { |excluded| FileSelector.for(language).excluding(excluded) }
112
+ define_singleton_method(:predefined) do |included:, excluded:|
113
+ FileSelector.for(language).predefined(included:, excluded:)
114
+ end
115
+ end,
116
+ )
168
117
  end
169
118
  end
170
119
  end
@@ -4,63 +4,73 @@ module ChurnVsComplexity
4
4
  module Focus
5
5
  class Config
6
6
  DEFAULT_BASELINE_PATH = '.focus-baseline.json'
7
+ SUPPORTED_SERIALIZERS = %i[json].freeze
8
+ Session = Data.define(:subcommand, :baseline_path)
7
9
 
8
- def initialize(
9
- language:,
10
- subcommand:,
11
- serializer: :json,
12
- since: nil,
13
- excluded: [],
14
- baseline_path: DEFAULT_BASELINE_PATH,
15
- complexity_validator: ComplexityValidator,
16
- since_validator: Normal::SinceValidator,
17
- **options
18
- )
19
- @language = language
20
- @subcommand = subcommand.to_sym
10
+ def self.from_options(options)
11
+ session = Session.new(
12
+ subcommand: options[:subcommand].to_sym,
13
+ baseline_path: options[:baseline_path] || DEFAULT_BASELINE_PATH,
14
+ )
15
+ new(
16
+ scope: AnalysisScope.from_options(options),
17
+ session:,
18
+ serializer: options[:serializer] || :json,
19
+ )
20
+ end
21
+
22
+ def initialize(scope:, session:, serializer: :json, validators: ValidatorSet.new)
23
+ @scope = scope
24
+ @session = session
21
25
  @serializer = serializer
22
- @since = since
23
- @excluded = excluded
24
- @baseline_path = baseline_path
25
- @complexity_validator = complexity_validator
26
- @since_validator = since_validator
27
- @options = options
26
+ @validators = validators
28
27
  end
29
28
 
30
29
  def validate!
31
- LanguageValidator.validate!(@language)
32
- @since_validator.validate!(since: @since, relative_period: nil)
33
- @complexity_validator.validate!(@language)
30
+ LanguageValidator.validate!(@scope.language)
31
+ validate_serializer!
32
+ validate_period!
33
+ @validators.complexity.validate!(@scope.language)
34
34
  validate_subcommand!
35
35
  end
36
36
 
37
37
  def checker
38
38
  Checker.new(
39
39
  engine: normal_engine,
40
- subcommand: @subcommand,
40
+ subcommand: @session.subcommand,
41
41
  serializer: focus_serializer,
42
- baseline_path: @baseline_path,
43
- language: @language,
42
+ baseline_path: @session.baseline_path,
43
+ language: @scope.language,
44
44
  )
45
45
  end
46
46
 
47
47
  private
48
48
 
49
49
  def normal_engine
50
- Normal::Config.new(language: @language, serializer: :none, since: @since, excluded: @excluded).checker
50
+ Normal::Config.new(scope: @scope, serializer: :none).checker
51
+ end
52
+
53
+ def validate_serializer!
54
+ return if SUPPORTED_SERIALIZERS.include?(@serializer)
55
+
56
+ raise ValidationError, "Unsupported serializer: #{@serializer}"
57
+ end
58
+
59
+ def validate_period!
60
+ @validators.since.validate!(since: @scope.since, relative_period: @scope.relative_period)
61
+ Normal::RelativePeriodValidator.validate!(relative_period: @scope.relative_period)
51
62
  end
52
63
 
53
64
  def validate_subcommand!
54
- return if %i[start end].include?(@subcommand)
65
+ return if %i[start end].include?(@session.subcommand)
55
66
 
56
- raise ValidationError, "Invalid focus subcommand: #{@subcommand}. Use 'start' or 'end'."
67
+ raise ValidationError, "Invalid focus subcommand: #{@session.subcommand}. Use 'start' or 'end'."
57
68
  end
58
69
 
59
70
  def focus_serializer
60
- case @subcommand
61
- when :start then nil # start doesn't need a serializer
62
- when :end then Serializer::Json
63
- end
71
+ return if @session.subcommand == :start
72
+
73
+ Serializer::Json if @serializer == :json
64
74
  end
65
75
  end
66
76
  end
@@ -11,17 +11,29 @@ module ChurnVsComplexity
11
11
  end
12
12
 
13
13
  def self.build_report(baseline, current)
14
- before_gammas = baseline['files'].to_h { |f| [f['file'], f['gamma_score']] }
15
- after_gammas = current.to_h { |e| [e[:file], e[:gamma_score]] }
14
+ before_gammas = baseline_gammas(baseline)
15
+ after_gammas = current_gammas(current)
16
16
  touched = find_touched_files(before_gammas, after_gammas)
17
17
 
18
18
  JSON.generate(
19
19
  session: session_data(started: baseline['timestamp'], files_modified: touched.size),
20
- impact: impact_data(before: mean(before_gammas.values), after: mean(after_gammas.values)),
20
+ impact: report_impact(before_gammas, after_gammas),
21
21
  files_touched: touched,
22
22
  )
23
23
  end
24
24
 
25
+ def self.baseline_gammas(baseline)
26
+ baseline['files'].to_h { |entry| [entry['file'], entry['gamma_score']] }
27
+ end
28
+
29
+ def self.current_gammas(current)
30
+ current.to_h { |entry| [entry[:file], entry[:gamma_score]] }
31
+ end
32
+
33
+ def self.report_impact(before_gammas, after_gammas)
34
+ impact_data(before: mean(before_gammas.values), after: mean(after_gammas.values))
35
+ end
36
+
25
37
  def self.build_fallback_report(current)
26
38
  after_scores = current.map { |e| e[:gamma_score] }
27
39
 
@@ -89,7 +101,8 @@ module ChurnVsComplexity
89
101
  new_gamma > old_gamma ? 'Complexity increased. Consider adding tests.' : 'Complexity decreased. Good.'
90
102
  end
91
103
 
92
- private_class_method :build_report, :build_fallback_report, :session_data, :impact_data,
104
+ private_class_method :build_report, :baseline_gammas, :current_gammas, :report_impact,
105
+ :build_fallback_report, :session_data, :impact_data,
93
106
  :mean, :direction, :find_touched_files, :build_touched_entry,
94
107
  :test_file_exists?, :recommend
95
108
  end