ai_refactor 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -2
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +169 -1
  5. data/README.md +169 -43
  6. data/Rakefile +1 -1
  7. data/ai_refactor.gemspec +1 -0
  8. data/examples/.gitignore +1 -0
  9. data/examples/ex1_convert_a_rspec_test_to_minitest.yml +7 -0
  10. data/examples/ex1_input_spec.rb +32 -0
  11. data/examples/rails_helper.rb +21 -0
  12. data/examples/test_helper.rb +14 -0
  13. data/exe/ai_refactor +139 -52
  14. data/lib/ai_refactor/cli.rb +138 -0
  15. data/lib/ai_refactor/command_file_parser.rb +27 -0
  16. data/lib/ai_refactor/context.rb +33 -0
  17. data/lib/ai_refactor/file_processor.rb +34 -17
  18. data/lib/ai_refactor/prompt.rb +84 -0
  19. data/lib/ai_refactor/prompts/diff.md +17 -0
  20. data/lib/ai_refactor/prompts/input.md +1 -0
  21. data/lib/ai_refactor/refactors/base_refactor.rb +183 -0
  22. data/lib/ai_refactor/refactors/custom.rb +43 -0
  23. data/lib/ai_refactor/refactors/minitest/write_test_for_class.md +15 -0
  24. data/lib/ai_refactor/refactors/minitest/write_test_for_class.rb +51 -0
  25. data/lib/ai_refactor/refactors/project/write_changelog_from_history.md +35 -0
  26. data/lib/ai_refactor/refactors/project/write_changelog_from_history.rb +50 -0
  27. data/lib/ai_refactor/refactors/{prompts/rspec_to_minitest_rails.md → rails/minitest/rspec_to_minitest.md} +40 -1
  28. data/lib/ai_refactor/refactors/rails/minitest/rspec_to_minitest.rb +77 -0
  29. data/lib/ai_refactor/refactors/rspec/minitest_to_rspec.rb +13 -0
  30. data/lib/ai_refactor/refactors/ruby/refactor_ruby.md +10 -0
  31. data/lib/ai_refactor/refactors/ruby/refactor_ruby.rb +29 -0
  32. data/lib/ai_refactor/refactors/ruby/write_ruby.md +7 -0
  33. data/lib/ai_refactor/refactors/ruby/write_ruby.rb +33 -0
  34. data/lib/ai_refactor/refactors.rb +13 -5
  35. data/lib/ai_refactor/run_configuration.rb +115 -0
  36. data/lib/ai_refactor/{refactors/tests → test_runners}/minitest_runner.rb +2 -2
  37. data/lib/ai_refactor/{refactors/tests → test_runners}/rspec_runner.rb +1 -1
  38. data/lib/ai_refactor/{refactors/tests → test_runners}/test_run_diff_report.rb +1 -1
  39. data/lib/ai_refactor/{refactors/tests → test_runners}/test_run_result.rb +1 -1
  40. data/lib/ai_refactor/version.rb +1 -1
  41. data/lib/ai_refactor.rb +13 -8
  42. metadata +47 -13
  43. data/lib/ai_refactor/base_refactor.rb +0 -66
  44. data/lib/ai_refactor/refactors/generic.rb +0 -113
  45. data/lib/ai_refactor/refactors/minitest_to_rspec.rb +0 -11
  46. data/lib/ai_refactor/refactors/rspec_to_minitest_rails.rb +0 -103
  47. /data/lib/ai_refactor/refactors/{prompts → rspec}/minitest_to_rspec.md +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aae248c6a2c5342ea8990e2a672b455c48ec79645e8d88eb20f9afc1af9e7cce
4
- data.tar.gz: f86584d15b6467600be12797e4f34bf4ce4545ad6be3bbaef8f9ce62817dd66f
3
+ metadata.gz: 15f32bc208d6025b339dd08061747f790975c9163909a83dac8d3abc381b4fac
4
+ data.tar.gz: cb1e71b84e2aa8b77900a5a33f10090f3ee65d6d6f0fba606359c5a2cb1f2719
5
5
  SHA512:
6
- metadata.gz: a4d1aa38dc497acd25038229da1fe3e46bcbb4dd26cf33770f2bfe1a8a44a48c656023a2b9342a0b81c9fb620ecc4bc28dba215ef2225da8e5e22b13a8d73d92
7
- data.tar.gz: e1a7e38d44b83b81da06dcb684e8ce61532fe37818c1e4c41f14451923cd9ad9c3f78092fba5b0fe65e316c1fe75681bf34b81501d2f99694a6ceb85826cede3
6
+ metadata.gz: 9b16ab1f05053cbe504cdda1f66326ec0ee27977e322df37ad5b00ffc319dd7c5692785f564a04ba4a6dc238015689f6ed7aca901ab4dc7bad84f418ad887c0e
7
+ data.tar.gz: a18db957cfd44fbf817b61c5b198f6bd122d6f8368f098925ff2b4de3f94c88f3f913133685424107e06b6c9338c6b70f1d6e5b0bb98b7865cecbee522b1cdb0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,68 @@
1
- ## [Unreleased]
1
+ # AI Refactor Changelog
2
+
3
+ ## [0.5.0] - 2023-09-21
4
+
5
+ ### Added
6
+
7
+ - Support for new command files, which are YAML files that can be used to define options for a refactor. This makes it
8
+ simpler to create configurations for refactors that will be used repeatedly. They can be committed to source control
9
+ of your project and shared with other developers.
10
+ - Support for configuring the run commands for the test runners
11
+ - Adding real life examples
12
+
13
+ ## [0.4.0] - 2023-08-15
14
+
15
+ ### Added
16
+
17
+ - Support for providing files as context for prompt.
18
+ - Output path configuration made available to all refactors.
19
+ - New refactor to write minitest tests for classes
20
+ - New refactor to write changelog entries.
21
+ - 'review' prompt CLI switch without invoking AI.
22
+ - CLI switch to control output overwrite behaviour.
23
+ - Extra text context for prompts via command line with -x
24
+ - Support for .ai_refactor config file which can provide default options/switches for prompts.
25
+
26
+ ### Changed
27
+
28
+ - Moved to using zeitwerk for loading and dotenv.
29
+ - Simple registry for refactors and change in naming convention.
30
+ - Updated diff prompt option and fixes for new structure.
31
+ - Reorganised refactors.
32
+ - Tweaked rspec to minitest prompt.
33
+ - Fixed check for custom prompt path.
34
+ - Updated docs.
35
+
36
+ ### Fixed
37
+
38
+ - Fixed reference to built in prompt paths.
39
+
40
+ ## [0.3.1] - 2023-05-25
41
+
42
+ ### Added
43
+
44
+ - Added support for outputting to file from generic refactor.
45
+
46
+ ## [0.2.0] - 2023-05-24
47
+
48
+ ### Added
49
+
50
+ - Introduced a generic refactor type which uses the user supplied prompt and outputs to stdout.
51
+ - Added support for outputting to file from generic refactor.
52
+ - Added a prompt for rspec_to_minitest.
53
+
54
+ ### Fixed
55
+
56
+ - Fixed example.
2
57
 
3
58
  ## [0.1.0] - 2023-05-19
4
59
 
5
- - Initial release
60
+ ### Added
61
+
62
+ - First version of CLI.
63
+
64
+ ### Changed
65
+
66
+ - Gem dependencies are not open-ended.
67
+ - Renamed to clean up intention.
68
+ - Updated docs.
data/Gemfile CHANGED
@@ -10,3 +10,12 @@ gem "rake", "~> 13.0"
10
10
  gem "minitest", "~> 5.0"
11
11
 
12
12
  gem "standard", "~> 1.3"
13
+
14
+ gem "dotenv"
15
+
16
+ # for the examples
17
+
18
+ gem "rails"
19
+ gem "rspec"
20
+ gem "rspec-rails"
21
+ gem "shoulda-matchers"
data/Gemfile.lock CHANGED
@@ -1,35 +1,188 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ai_refactor (0.3.1)
4
+ ai_refactor (0.5.0)
5
5
  colorize (< 2.0)
6
6
  open3 (< 2.0)
7
7
  ruby-openai (>= 3.4.0, < 5.0)
8
+ zeitwerk (~> 2.6)
8
9
 
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
13
+ actioncable (7.0.8)
14
+ actionpack (= 7.0.8)
15
+ activesupport (= 7.0.8)
16
+ nio4r (~> 2.0)
17
+ websocket-driver (>= 0.6.1)
18
+ actionmailbox (7.0.8)
19
+ actionpack (= 7.0.8)
20
+ activejob (= 7.0.8)
21
+ activerecord (= 7.0.8)
22
+ activestorage (= 7.0.8)
23
+ activesupport (= 7.0.8)
24
+ mail (>= 2.7.1)
25
+ net-imap
26
+ net-pop
27
+ net-smtp
28
+ actionmailer (7.0.8)
29
+ actionpack (= 7.0.8)
30
+ actionview (= 7.0.8)
31
+ activejob (= 7.0.8)
32
+ activesupport (= 7.0.8)
33
+ mail (~> 2.5, >= 2.5.4)
34
+ net-imap
35
+ net-pop
36
+ net-smtp
37
+ rails-dom-testing (~> 2.0)
38
+ actionpack (7.0.8)
39
+ actionview (= 7.0.8)
40
+ activesupport (= 7.0.8)
41
+ rack (~> 2.0, >= 2.2.4)
42
+ rack-test (>= 0.6.3)
43
+ rails-dom-testing (~> 2.0)
44
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
45
+ actiontext (7.0.8)
46
+ actionpack (= 7.0.8)
47
+ activerecord (= 7.0.8)
48
+ activestorage (= 7.0.8)
49
+ activesupport (= 7.0.8)
50
+ globalid (>= 0.6.0)
51
+ nokogiri (>= 1.8.5)
52
+ actionview (7.0.8)
53
+ activesupport (= 7.0.8)
54
+ builder (~> 3.1)
55
+ erubi (~> 1.4)
56
+ rails-dom-testing (~> 2.0)
57
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
58
+ activejob (7.0.8)
59
+ activesupport (= 7.0.8)
60
+ globalid (>= 0.3.6)
61
+ activemodel (7.0.8)
62
+ activesupport (= 7.0.8)
63
+ activerecord (7.0.8)
64
+ activemodel (= 7.0.8)
65
+ activesupport (= 7.0.8)
66
+ activestorage (7.0.8)
67
+ actionpack (= 7.0.8)
68
+ activejob (= 7.0.8)
69
+ activerecord (= 7.0.8)
70
+ activesupport (= 7.0.8)
71
+ marcel (~> 1.0)
72
+ mini_mime (>= 1.1.0)
73
+ activesupport (7.0.8)
74
+ concurrent-ruby (~> 1.0, >= 1.0.2)
75
+ i18n (>= 1.6, < 2)
76
+ minitest (>= 5.1)
77
+ tzinfo (~> 2.0)
12
78
  ast (2.4.2)
79
+ builder (3.2.4)
13
80
  colorize (0.8.1)
81
+ concurrent-ruby (1.2.2)
82
+ crass (1.0.6)
83
+ date (3.3.3)
84
+ diff-lcs (1.5.0)
85
+ dotenv (2.8.1)
86
+ erubi (1.12.0)
14
87
  faraday (2.7.4)
15
88
  faraday-net_http (>= 2.0, < 3.1)
16
89
  ruby2_keywords (>= 0.0.4)
17
90
  faraday-multipart (1.0.4)
18
91
  multipart-post (~> 2)
19
92
  faraday-net_http (3.0.2)
93
+ globalid (1.2.1)
94
+ activesupport (>= 6.1)
95
+ i18n (1.14.1)
96
+ concurrent-ruby (~> 1.0)
20
97
  json (2.6.3)
21
98
  language_server-protocol (3.17.0.3)
22
99
  lint_roller (1.0.0)
100
+ loofah (2.21.3)
101
+ crass (~> 1.0.2)
102
+ nokogiri (>= 1.12.0)
103
+ mail (2.8.1)
104
+ mini_mime (>= 0.1.1)
105
+ net-imap
106
+ net-pop
107
+ net-smtp
108
+ marcel (1.0.2)
109
+ method_source (1.0.0)
110
+ mini_mime (1.1.5)
23
111
  minitest (5.18.0)
24
112
  multipart-post (2.3.0)
113
+ net-imap (0.3.7)
114
+ date
115
+ net-protocol
116
+ net-pop (0.1.2)
117
+ net-protocol
118
+ net-protocol (0.2.1)
119
+ timeout
120
+ net-smtp (0.3.3)
121
+ net-protocol
122
+ nio4r (2.5.9)
123
+ nokogiri (1.15.4-arm64-darwin)
124
+ racc (~> 1.4)
25
125
  open3 (0.1.2)
26
126
  parallel (1.23.0)
27
127
  parser (3.2.2.1)
28
128
  ast (~> 2.4.1)
129
+ racc (1.7.1)
130
+ rack (2.2.8)
131
+ rack-test (2.1.0)
132
+ rack (>= 1.3)
133
+ rails (7.0.8)
134
+ actioncable (= 7.0.8)
135
+ actionmailbox (= 7.0.8)
136
+ actionmailer (= 7.0.8)
137
+ actionpack (= 7.0.8)
138
+ actiontext (= 7.0.8)
139
+ actionview (= 7.0.8)
140
+ activejob (= 7.0.8)
141
+ activemodel (= 7.0.8)
142
+ activerecord (= 7.0.8)
143
+ activestorage (= 7.0.8)
144
+ activesupport (= 7.0.8)
145
+ bundler (>= 1.15.0)
146
+ railties (= 7.0.8)
147
+ rails-dom-testing (2.2.0)
148
+ activesupport (>= 5.0.0)
149
+ minitest
150
+ nokogiri (>= 1.6)
151
+ rails-html-sanitizer (1.6.0)
152
+ loofah (~> 2.21)
153
+ nokogiri (~> 1.14)
154
+ railties (7.0.8)
155
+ actionpack (= 7.0.8)
156
+ activesupport (= 7.0.8)
157
+ method_source
158
+ rake (>= 12.2)
159
+ thor (~> 1.0)
160
+ zeitwerk (~> 2.5)
29
161
  rainbow (3.1.1)
30
162
  rake (13.0.6)
31
163
  regexp_parser (2.8.0)
32
164
  rexml (3.2.5)
165
+ rspec (3.12.0)
166
+ rspec-core (~> 3.12.0)
167
+ rspec-expectations (~> 3.12.0)
168
+ rspec-mocks (~> 3.12.0)
169
+ rspec-core (3.12.2)
170
+ rspec-support (~> 3.12.0)
171
+ rspec-expectations (3.12.3)
172
+ diff-lcs (>= 1.2.0, < 2.0)
173
+ rspec-support (~> 3.12.0)
174
+ rspec-mocks (3.12.6)
175
+ diff-lcs (>= 1.2.0, < 2.0)
176
+ rspec-support (~> 3.12.0)
177
+ rspec-rails (6.0.3)
178
+ actionpack (>= 6.1)
179
+ activesupport (>= 6.1)
180
+ railties (>= 6.1)
181
+ rspec-core (~> 3.12)
182
+ rspec-expectations (~> 3.12)
183
+ rspec-mocks (~> 3.12)
184
+ rspec-support (~> 3.12)
185
+ rspec-support (3.12.1)
33
186
  rubocop (1.50.2)
34
187
  json (~> 2.3)
35
188
  parallel (~> 1.10)
@@ -50,6 +203,8 @@ GEM
50
203
  faraday-multipart (>= 1)
51
204
  ruby-progressbar (1.13.0)
52
205
  ruby2_keywords (0.0.5)
206
+ shoulda-matchers (5.3.0)
207
+ activesupport (>= 5.2.0)
53
208
  standard (1.28.2)
54
209
  language_server-protocol (~> 3.17.0.2)
55
210
  lint_roller (~> 1.0)
@@ -61,15 +216,28 @@ GEM
61
216
  standard-performance (1.0.1)
62
217
  lint_roller (~> 1.0)
63
218
  rubocop-performance (~> 1.16.0)
219
+ thor (1.2.2)
220
+ timeout (0.4.0)
221
+ tzinfo (2.0.6)
222
+ concurrent-ruby (~> 1.0)
64
223
  unicode-display_width (2.4.2)
224
+ websocket-driver (0.7.6)
225
+ websocket-extensions (>= 0.1.0)
226
+ websocket-extensions (0.1.5)
227
+ zeitwerk (2.6.8)
65
228
 
66
229
  PLATFORMS
67
230
  arm64-darwin-22
68
231
 
69
232
  DEPENDENCIES
70
233
  ai_refactor!
234
+ dotenv
71
235
  minitest (~> 5.0)
236
+ rails
72
237
  rake (~> 13.0)
238
+ rspec
239
+ rspec-rails
240
+ shoulda-matchers
73
241
  standard (~> 1.3)
74
242
 
75
243
  BUNDLED WITH
data/README.md CHANGED
@@ -1,21 +1,48 @@
1
- # AI Refactor
1
+ # AIRefactor for Ruby
2
2
 
3
- AI Refactor is an experimental tool to see how AI (specifically [OpenAI's ChatGPT](https://platform.openai.com/)) can be used to help apply refactoring to code.
3
+ __The goal for AIRefactor is to use LLMs to apply repetitive refactoring tasks to code.__
4
4
 
5
- The goal is **not** that the AI decides what refactoring to do, but rather, given refactoring tasks specified by the human user,
6
- the AI can help identify which code to change and apply the relevant refactor.
5
+ First the human decides what refactoring is needed and builds up a prompt to describe the task, or uses one of AIRefactors provided prompts.
7
6
 
8
- This is based on the assumption that the LLM AIs are pretty good at identifying patterns.
7
+ AIRefactor then helps to apply the refactoring to one or more files.
8
+
9
+ In some cases, the tool can then check the generated code by running tests and comparing test outputs.
10
+
11
+ #### Notes
12
+
13
+ AI Refactor is an experimental tool and under active development as I explore the idea myself. It may not work as expected, or
14
+ change in ways that break existing functionality.
15
+
16
+ The focus of the tool is work with the Ruby programming language ecosystem, but it can be used with any language.
17
+
18
+ AI Refactor currently uses [OpenAI's ChatGPT](https://platform.openai.com/).
19
+
20
+ ## Examples
21
+
22
+ See the [examples](examples/) directory for some examples of using the tool.
9
23
 
10
24
  ## Available refactors
11
25
 
12
- Currently available:
26
+ Write your own prompt:
13
27
 
14
- - `rspec_to_minitest_rails`: convert RSpec specs to minitest tests in Rails apps
15
- - `generic`: provide your own prompt for the AI and run against the input files
28
+ - `ruby/write_ruby`: provide your own prompt for the AI and expect to output Ruby code (no input files required)
29
+ - `ruby/refactor_ruby`: provide your own refactoring prompt for the AI and expect to output Ruby code
30
+ - `custom`: provide your own prompt for the AI and run against the input files. There is no expectation of the output.
16
31
 
32
+ Use a pre-built prompt:
17
33
 
18
- ### `rspec_to_minitest_rails`
34
+ - `minitest/write_test_for_class`: write a minitest test for a given class
35
+ - `rails/minitest/rspec_to_minitest`: convert RSpec specs to minitest tests in Rails apps
36
+
37
+ ### User supplied prompts, eg `custom`, `ruby/write_ruby` and `ruby/refactor_ruby`
38
+
39
+ Applies the refactor specified by prompting the AI with the user supplied prompt. You must supply a prompt file with the `-p` option.
40
+
41
+ The output is written to `stdout`, or to a file with the `--output` option.
42
+
43
+ User supplied prompts are best configured using a command file, see below.
44
+
45
+ ### `rails/minitest/rspec_to_minitest`
19
46
 
20
47
  Converts RSpec tests to minitest tests for Rails test suites (ie generated minitest tests are actually `ActiveSupport::TestCase`s).
21
48
 
@@ -24,44 +51,32 @@ The tool first runs the original RSpec spec file and then runs the generated min
24
51
  The comparison is simply the count of successful and failed tests but this is probably enough to determine if the conversion worked.
25
52
 
26
53
  ```shellq
27
- stephen$ OPENAI_API_KEY=my-key ai_refactor rspec_to_minitest_rails spec/models/my_thing_spec.rb -v
28
- AI Refactor 1 files(s)/dir(s) '["spec/models/my_thing_spec.rb"]' with rspec_to_minitest_rails refactor
54
+ stephen$ OPENAI_API_KEY=my-key ai_refactor rails/minitest/rspec_to_minitest spec/models/my_thing_spec.rb
55
+ AI Refactor 1 files(s)/dir(s) '["spec/models/my_thing_spec.rb"]' with rails/minitest/rspec_to_minitest refactor
29
56
  ====================
30
57
  Processing spec/models/my_thing_spec.rb...
31
- [Run spec spec/models/my_thing_spec.rb... (bundle exec rspec spec/models/my_thing_spec.rb)]
32
- Do you wish to overwrite test/models/my_thing_test.rb? (y/n)
33
- y
34
- [Converting spec/models/my_thing_spec.rb...]
35
- [Generate AI output. Generation attempts left: 3]
36
- [OpenAI finished, with reason 'stop'...]
37
- [Used tokens: 1869]
38
- [Converted spec/models/my_thing_spec.rb to test/models/my_thing_test.rb...]
39
- [Run generated test file test/models/my_thing_test.rb (bundle exec rails test test/models/my_thing_test.rb)...]
40
- [Done converting spec/models/my_thing_spec.rb to test/models/my_thing_test.rb...]
58
+
59
+ Original test run results:
60
+ >> Examples: 41, Failures: 0, Pendings: 0
61
+
62
+ Translated test file results:
63
+ >> Runs: 41, Failures: 0, Skips: 0
64
+
41
65
  No differences found! Conversion worked!
42
66
  Refactor succeeded on spec/models/my_thing_spec.rb
43
67
 
44
68
  Done processing all files!
45
69
  ```
46
70
 
47
- ### `generic` (user supplied prompt)
71
+ ### `minitest/write_test_for_class`
48
72
 
49
- Applies the refactor specified by prompting the AI with the user supplied prompt. You must supply a prompt file with the `-p` option.
73
+ Writes a minitest test for a given class. The output will, by default, be put into a directory named `test` in the current directory,
74
+ in a path that matches the input file path, with a `_test.rb` suffix.
50
75
 
51
- The output is written to `stdout`, or to a file with the `--output` option. If `--output` is used without a value it overwrites the input.
76
+ For example, if the input file is `app/stuff/my_thing.rb` the output will be written to `test/app/stuff/my_thing_test.rb`.
52
77
 
53
- You can also output to a file using a template, `--output-template` to determine the output file name given a template string:
54
-
55
- The template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' & '[EXT]'.
56
-
57
- Eg `--output-template "[DIR]/[NAME]_[REFACTOR][EXT]"`
58
-
59
- eg for the input `my_dir/my_class.rb`
60
- - `[FILE]`: `my_class.rb`
61
- - `[NAME]`: `my_class`
62
- - `[DIR]`: `my_dir`
63
- - `[REFACTOR]`: `generic`
64
- - `[EXT]`: `.rb`
78
+ This refactor can benefit from being passed related files as context, for example, if the class under test inherits from another class,
79
+ then context can be used to provide the parent class.
65
80
 
66
81
  ## Installation
67
82
 
@@ -78,25 +93,136 @@ If bundler is not being used to manage dependencies, install the gem by executin
78
93
  See `ai_refactor --help` for more information.
79
94
 
80
95
  ```
81
- Usage: ai_refactor REFACTOR_TYPE INPUT_FILE_OR_DIR [options]
96
+ Usage: ai_refactor REFACTOR_TYPE_OR_COMMAND_FILE INPUT_FILE_OR_DIR [options]
82
97
 
83
- Where REFACTOR_TYPE is one of: ["rspec_to_minitest_rails", "generic"]
98
+ Where REFACTOR_TYPE_OR_COMMAND_FILE is either the path to a command YML file, or one of the refactor types: ["custom" ... (run ai_refactor --help for full list of refactor types)]
84
99
 
100
+ -o, --output [FILE] Write output to given file instead of stdout. If no path provided will overwrite input file (will prompt to overwrite existing files). Some refactor tasks will write out to a new file by default. This option will override the tasks default behaviour.
101
+ -O, --output-template TEMPLATE Write outputs to files instead of stdout. The template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' & '[EXT]'. Eg `[DIR]/[NAME]_[REFACTOR][EXT]` (will prompt to overwrite existing files)
102
+ -c, --context CONTEXT_FILES Specify one or more files to use as context for the AI. The contents of these files will be prepended to the prompt sent to the AI.
103
+ -x, --extra CONTEXT_TEXT Specify some text to be prepended to the prompt sent to the AI as extra information of note.
104
+ -r, --review-prompt Show the prompt that will be sent to ChatGPT but do not actually call ChatGPT or make changes to files.
85
105
  -p, --prompt PROMPT_FILE Specify path to a text file that contains the ChatGPT 'system' prompt.
86
- -c, --continue [MAX_MESSAGES] If ChatGPT stops generating due to the maximum token count being reached, continue to generate more messages, until a stop condition or MAX_MESSAGES. MAX_MESSAGES defaults to 3
87
- -m, --model MODEL_NAME Specify a ChatGPT model to use (default gpt-3.5-turbo).
106
+ -f, --diffs Request AI generate diffs of changes rather than writing out the whole file.
107
+ -C, --continue [MAX_MESSAGES] If ChatGPT stops generating due to the maximum token count being reached, continue to generate more messages, until a stop condition or MAX_MESSAGES. MAX_MESSAGES defaults to 3
108
+ -m, --model MODEL_NAME Specify a ChatGPT model to use (default gpt-4).
88
109
  --temperature TEMP Specify the temperature parameter for ChatGPT (default 0.7).
89
110
  --max-tokens MAX_TOKENS Specify the max number of tokens of output ChatGPT can generate. Max will depend on the size of the prompt (default 1500)
90
111
  -t, --timeout SECONDS Specify the max wait time for ChatGPT response.
112
+ --overwrite ANSWER Always overwrite existing output files, 'y' for yes, 'n' for no, or 'a' for ask. Default to ask.
113
+ -N, --no Never overwrite existing output files, same as --overwrite=n.
91
114
  -v, --verbose Show extra output and progress info
92
115
  -d, --debug Show debugging output to help diagnose issues
93
116
  -h, --help Prints this help
117
+ ```
118
+
119
+ ### Interactive mode
120
+
121
+ A basic interactive mode exists too, where you are prompted for options.
122
+
123
+ Start interactive mode by not specifying anything for `REFACTOR_TYPE_OR_COMMAND_FILE` (ie no refactor type or command file)
124
+
125
+ ### Command files and Custom prompts
126
+
127
+ Apart from invoking the tool with CLI options, the tool can also be invoked with a command file.
128
+
129
+ This makes it easier to build custom refactor prompts for projects, and run that custom refactor multiple times.
130
+
131
+ The command file is a YAML file that contains configuration options to pass to the tool.
132
+
133
+ The format of the YAML file is:
134
+
135
+ ```yaml
136
+ # Required options:
137
+ refactor: refactor type name, eg 'ruby/write_ruby'
138
+ # Optional options:
139
+ input_file_paths:
140
+ - input files or directories
141
+ output_file_path: output file or directory
142
+ output_template_path: output file template (see docs)
143
+ prompt_file_path: path
144
+ prompt: |
145
+ A custom prompt to send to ChatGPT if the command needs it (otherwise read from file)
146
+ context_file_paths:
147
+ - file1.rb
148
+ - file2.rb
149
+ # Other configuration options:
150
+ context_text: |
151
+ Some extra info to prepend to the prompt
152
+ diff: true/false (default false)
153
+ ai_max_attempts: max times to generate more if AI does not complete generating (default 3)
154
+ ai_model: ChatGPT model name (default gpt-4)
155
+ ai_temperature: ChatGPT temperature (default 0.7)
156
+ ai_max_tokens: ChatGPT max tokens (default 1500)
157
+ ai_timeout: ChatGPT timeout (default 60)
158
+ overwrite: y/n/a (default a)
159
+ verbose: true/false (default false)
160
+ debug: true/false (default false)
161
+ ```
162
+
163
+ The command file can be invoked by passing it as the first argument to the tool:
94
164
 
95
- For refactor type 'generic':
96
- --output [FILE] Write output to file instead of stdout. If no path provided will overwrite input file (will prompt to overwrite existing files)
97
- --output-template TEMPLATE Write outputs to files instead of stdout. The template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' & '[EXT]'. Eg `[DIR]/[NAME]_[REFACTOR][EXT]` (will prompt to overwrite existing files)
165
+ ```shell
166
+ ai_refactor my_command_file.yml
98
167
  ```
99
168
 
169
+ Other options can be passed on the command line and will override the options in the command file.
170
+
171
+ For example, if the command file contains:
172
+
173
+ ```shell
174
+ ai_refactor my_command_file.yml my_input.rb -d --output foo.rb
175
+ ```
176
+
177
+ ### Prompt template substitutions
178
+
179
+ Prompt text can contain the following substitutions:
180
+
181
+ * `__{{input_file_path}}__`: the path to the input file
182
+ * `__{{output_file_path}}__`: the path to the output file
183
+ * `__{{prompt_header}}__`: the place the pre-build prompt will be injected, if used
184
+ * `__{{prompt_footer}}__`: prompt text that will be inserted after the prompt, eg the "make diffs" prompt if `--diffs` is used
185
+ * `__{{context}}__`: the contents of the context files, if any
186
+ * `__{{content}}__`: the contents of input file, if any
187
+
188
+ ## Outputs
189
+
190
+ Some refactor tasks will write out to a new file by default, others to stdout.
191
+
192
+ The `--output` lets you specify a file to write to instead of the Refactors default behaviour.
193
+
194
+ If `--output` is used without a value it overwrites the input with a prompt to overwrite existing files.
195
+
196
+ You can also output to a file using a template, `--output-template` to determine the output file name given a template string:
197
+
198
+ The template is used to create the output name, where the it can have substitutions, '[FILE]', '[NAME]', '[DIR]', '[REFACTOR]' & '[EXT]'.
199
+
200
+ Eg `--output-template "[DIR]/[NAME]_[REFACTOR][EXT]"`
201
+
202
+ eg for the input `my_dir/my_class.rb`
203
+ - `[FILE]`: `my_class.rb`
204
+ - `[NAME]`: `my_class`
205
+ - `[DIR]`: `my_dir`
206
+ - `[REFACTOR]`: `generic`
207
+ - `[EXT]`: `.rb`
208
+
209
+ ## Configuration
210
+
211
+ ### `.ai_refactor` file
212
+
213
+ The tool can be configured using a `.ai_refactor` file in the current directory or in the user's home directory.
214
+
215
+ This file provides default CLI switches to add to any `ai_refactor` command.
216
+
217
+ ## Command history
218
+
219
+ The tool keeps a history of commands run in the `.ai_refactor_history` file in the current working directory.
220
+
221
+ ## Note on performance and ChatGPT version
222
+
223
+ _The quality of results depend very much on the version of ChatGPT being used._
224
+
225
+ I have tested with both 3.5 and 4 and see **significantly** better performance with version 4.
100
226
 
101
227
  ## Development
102
228
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require "rake/testtask"
6
6
  Rake::TestTask.new(:test) do |t|
7
7
  t.libs << "test"
8
8
  t.libs << "lib"
9
- t.test_files = FileList["test/**/test_*.rb"]
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
10
  end
11
11
 
12
12
  require "standard/rake"
data/ai_refactor.gemspec CHANGED
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_dependency "colorize", "< 2.0"
33
33
  spec.add_dependency "open3", "< 2.0"
34
34
  spec.add_dependency "ruby-openai", ">= 3.4.0", "< 5.0"
35
+ spec.add_dependency "zeitwerk", "~> 2.6"
35
36
  end
@@ -0,0 +1 @@
1
+ ex1_input_test.rb
@@ -0,0 +1,7 @@
1
+ refactor: rails/minitest/rspec_to_minitest
2
+ input_file_paths:
3
+ - examples/ex1_input_spec.rb
4
+ # We need to add context here as otherwise to tell the AI to require our local test_helper.rb file so that we can run the tests after
5
+ context_text: "In the output test use `require_relative` to include 'test_helper'."
6
+ # By default, ai_refactor runs "bundle exec rails test" but this isn't going to work here as we are not actually in a Rails app context in the examples
7
+ minitest_run_command: ruby __FILE__
@@ -0,0 +1,32 @@
1
+ require_relative "rails_helper"
2
+
3
+ RSpec.describe MyModel, type: :model do
4
+ subject(:model) { described_class.new }
5
+
6
+ it { is_expected.to validate_presence_of(:name) }
7
+
8
+ it "should allow integer values for age" do
9
+ model.age = 1
10
+ expect(model.age).to eq 1
11
+ end
12
+
13
+ it "should allow string values for name" do
14
+ model.name = "test"
15
+ expect(model.name).to eq "test"
16
+ end
17
+
18
+ it "should be invalid with invalid name" do
19
+ model.name = nil
20
+ expect(model).to be_invalid
21
+ end
22
+
23
+ it "should convert integer values for name" do
24
+ model.name = 1
25
+ expect(model.name).to eq "1"
26
+ end
27
+
28
+ it "should not allow string values for age" do
29
+ model.age = "test"
30
+ expect(model.age).to eq 0
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ require "rails/all"
2
+ require "shoulda-matchers"
3
+
4
+ Shoulda::Matchers.configure do |config|
5
+ config.integrate do |with|
6
+ with.test_framework :rspec
7
+ with.library :rails
8
+ end
9
+ end
10
+
11
+ class MyModel
12
+ include ActiveModel::Model
13
+ include ActiveModel::Attributes
14
+ include ActiveModel::Validations
15
+ include ActiveModel::Validations::Callbacks
16
+
17
+ validates :name, presence: true
18
+
19
+ attribute :name, :string
20
+ attribute :age, :integer
21
+ end