chusaku 0.4.0 → 0.6.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76adb49d62bcaeab5291f4b09b12318ea5e80dc7c4b77ff7bbfee697732a953f
4
- data.tar.gz: 8434d642b81b5d4cb67b7f9852adef2da679c24f7c9ea287aba903653b9703ee
3
+ metadata.gz: 714c60f9b5f8e9ebd4db391daa894dfa96a1ec95b7773900988df7b9a24677da
4
+ data.tar.gz: 1d7adffa9c39bb7dd7cd2f6d6b86fd7844bfbe77a207988eebd1469d5a74d0de
5
5
  SHA512:
6
- metadata.gz: 7b5880ed576e31a7f80a970389dc559e81ec932005716122de67a5f2c25586f82e674e6bdf1aba100d13a5bc7cba41df458b6e439c825684fa51ef11f77c6337
7
- data.tar.gz: fa2ed22e8e87762f4577f4e45765f22c1e2d8139eb8d02f18682f7859ac543bfad8977b545645717a5f6acb2fd75f1eac02de6697982de6eebc02cedd595c68c
6
+ metadata.gz: 0bc70bbe88b4cde24d236ac000bf13533fdad892413dc03cf8b8c4eca9f6eb7f21bd8f5a6df382521898a7baddc07f74e1444067ec0f2febd828603c79cfe020
7
+ data.tar.gz: 9a51c9476b1585c1f382f3845a202b6ea4eeed70e30d2cb450de4d01ed59c084ec2d7c1b11930bd6f62a9264129d21b545e0270bf9ea52d2cb92724d520cf924
@@ -0,0 +1,14 @@
1
+ name: Linting
2
+ on: [pull_request]
3
+ jobs:
4
+ standard:
5
+ runs-on: ubuntu-latest
6
+ steps:
7
+ - uses: actions/checkout@v2
8
+ - name: Set up Ruby
9
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
10
+ with:
11
+ ruby-version: 3.0.0
12
+ bundler-cache: true
13
+ - name: Run Standard
14
+ run: bundle exec standardrb --fail-level A
@@ -0,0 +1,17 @@
1
+ name: Testing
2
+ on: [pull_request]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby-version: ["2.6", "2.7", "3.0"]
9
+ steps:
10
+ - uses: actions/checkout@v2
11
+ - name: Set up Ruby
12
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
13
+ with:
14
+ ruby-version: ${{ matrix.ruby-version }}
15
+ bundler-cache: true
16
+ - name: Run tests
17
+ run: bundle exec rake
data/.rubocop.yml CHANGED
@@ -1,27 +1,7 @@
1
- require:
2
- - rubocop-performance
1
+ require: standard
3
2
 
4
- AllCops:
5
- Exclude:
6
- - 'bin/**/*'
7
- - 'test/mock/app/**/*'
8
- - 'test/mock/examples/**/*'
9
-
10
- Layout/LineLength:
11
- Max: 120
12
-
13
- Metrics/AbcSize:
14
- Exclude:
15
- - 'test/**/*'
3
+ inherit_gem:
4
+ standard: config/base.yml
16
5
 
17
- Metrics/MethodLength:
18
- Max: 25
19
- Exclude:
20
- - 'test/**/*'
21
-
22
- Metrics/ModuleLength:
23
- Max: 250
24
-
25
- Style/ClassAndModuleChildren:
26
- Exclude:
27
- - 'test/**/*'
6
+ AllCops:
7
+ DisabledByDefault: true
data/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
4
2
 
5
3
  # Specify your gem's dependencies in chusaku.gemspec
6
4
  gemspec
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Chusaku
2
2
 
3
- | Gem | CI | Quality |
4
- |-----|----|---------|
5
- |[![Gem Version](https://badge.fury.io/rb/chusaku.svg)](https://badge.fury.io/rb/chusaku)|[![CircleCI](https://circleci.com/gh/nshki/chusaku.svg?style=svg&circle-token=e1917972632f242932171de0ca5443148e83151c)](https://circleci.com/gh/nshki/chusaku)|[![Maintainability](https://api.codeclimate.com/v1/badges/e21235bd513aadf0407b/maintainability)](https://codeclimate.com/github/nshki/chusaku/maintainability)
3
+ [![Gem Version](https://badge.fury.io/rb/chusaku.svg)](https://badge.fury.io/rb/chusaku)
6
4
 
7
5
  Add comments above your Rails actions that look like:
8
6
 
@@ -29,7 +27,7 @@ Add this line to your Rails application's Gemfile:
29
27
  ```ruby
30
28
  group :development do
31
29
  # ...
32
- gem 'chusaku', require: false
30
+ gem "chusaku", require: false
33
31
  # ...
34
32
  end
35
33
  ```
@@ -54,9 +52,10 @@ Chusaku has some flags available for use as well:
54
52
  ```
55
53
  $ bundle exec chusaku --help
56
54
  Usage: chusaku [options]
55
+ --dry-run Run without file modifications
57
56
  --exit-with-error-on-annotation
58
57
  Fail if any file was annotated
59
- --dry-run Run without file modifications
58
+ --verbose Print all annotations
60
59
  -v, --version Show Chusaku version number and quit
61
60
  -h, --help Show this help message and quit
62
61
  ```
data/Rakefile CHANGED
@@ -1,12 +1,10 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rake/testtask'
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
5
3
 
6
4
  Rake::TestTask.new(:test) do |t|
7
- t.libs << 'test'
8
- t.libs << 'lib'
9
- t.test_files = FileList['test/**/*_test.rb']
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
10
8
  end
11
9
 
12
10
  task default: :test
data/bin/chusaku CHANGED
@@ -1,19 +1,19 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'rubygems'
3
+ require "rubygems"
4
4
 
5
5
  begin
6
6
  # Use project's bundle.
7
- require 'bundler'
7
+ require "bundler"
8
8
  Bundler.setup
9
9
 
10
10
  # Use project's environment.
11
- require 'rake'
12
- load './Rakefile'
11
+ require "rake"
12
+ load "./Rakefile"
13
13
  Rake::Task[:environment].invoke
14
- rescue StandardError
14
+ rescue
15
15
  end
16
16
 
17
- require 'chusaku/cli'
17
+ require "chusaku/cli"
18
18
 
19
19
  exit Chusaku::CLI.new.call
data/bin/console CHANGED
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
2
 
4
- require 'bundler/setup'
5
- require 'chusaku'
3
+ require "bundler/setup"
4
+ require "chusaku"
6
5
 
7
6
  # You can add fixtures and/or initialization code here to make experimenting
8
7
  # with your gem easier. You can also use a different console, if you like.
@@ -11,5 +10,5 @@ require 'chusaku'
11
10
  # require "pry"
12
11
  # Pry.start
13
12
 
14
- require 'irb'
13
+ require "irb"
15
14
  IRB.start(__FILE__)
data/chusaku.gemspec CHANGED
@@ -1,32 +1,30 @@
1
- # frozen_string_literal: true
2
-
3
- lib = File.expand_path('lib', __dir__)
1
+ lib = File.expand_path("lib", __dir__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'chusaku/version'
3
+ require "chusaku/version"
6
4
 
7
5
  Gem::Specification.new do |spec|
8
- spec.name = 'chusaku'
6
+ spec.name = "chusaku"
9
7
  spec.version = Chusaku::VERSION
10
- spec.authors = ['Nishiki Liu']
11
- spec.email = ['nishiki@hey.com']
8
+ spec.authors = ["Nishiki Liu"]
9
+ spec.email = ["hello@nshki.com"]
12
10
 
13
- spec.summary = 'Annotate your Rails controllers with route info.'
14
- spec.description = 'Annotate your Rails controllers with route info.'
15
- spec.homepage = 'https://github.com/nshki/chusaku'
16
- spec.license = 'MIT'
11
+ spec.summary = "Annotate your Rails controllers with route info."
12
+ spec.description = "Annotate your Rails controllers with route info."
13
+ spec.homepage = "https://github.com/nshki/chusaku"
14
+ spec.license = "MIT"
17
15
 
18
16
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the
19
17
  # 'allowed_push_host' to allow pushing to a single host or delete this section
20
18
  # to allow pushing to any host.
21
19
  if spec.respond_to?(:metadata)
22
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
23
21
 
24
- spec.metadata['homepage_uri'] = spec.homepage
25
- spec.metadata['source_code_uri'] = spec.homepage
26
- spec.metadata['changelog_uri'] = spec.homepage
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+ spec.metadata["changelog_uri"] = spec.homepage
27
25
  else
28
- raise 'RubyGems 2.0 or newer is required to protect against ' \
29
- 'public gem pushes.'
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
30
28
  end
31
29
 
32
30
  # Specify which files should be added to the gem when it is released.
@@ -37,15 +35,14 @@ Gem::Specification.new do |spec|
37
35
  f.match(%r{^(test|spec|features)/})
38
36
  end
39
37
  end
40
- spec.bindir = 'bin'
41
- spec.executables = 'chusaku'
42
- spec.require_paths = ['lib']
38
+ spec.bindir = "bin"
39
+ spec.executables = "chusaku"
40
+ spec.require_paths = ["lib"]
43
41
 
44
- spec.add_development_dependency 'bundler', '~> 2.0'
45
- spec.add_development_dependency 'minitest', '~> 5.0'
46
- spec.add_development_dependency 'rake', '~> 12.3'
47
- spec.add_development_dependency 'rubocop', '~> 0.77'
48
- spec.add_development_dependency 'rubocop-performance', '~> 1.5'
42
+ spec.add_development_dependency "bundler", "~> 2.2"
43
+ spec.add_development_dependency "minitest", "~> 5.14"
44
+ spec.add_development_dependency "rake", "~> 13.0"
45
+ spec.add_development_dependency "standardrb", "~> 1.0"
49
46
 
50
- spec.add_dependency 'rails', '> 2.0'
47
+ spec.add_dependency "railties", ">= 3.0"
51
48
  end
data/lib/chusaku/cli.rb CHANGED
@@ -1,7 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- require 'optparse'
4
- require 'chusaku'
1
+ require "optparse"
2
+ require "chusaku"
5
3
 
6
4
  module Chusaku
7
5
  # Enables flags for the `chusaku` executable.
@@ -30,7 +28,7 @@ module Chusaku
30
28
  check_for_rails_project
31
29
  Chusaku.call(options)
32
30
  rescue NotARailsProject
33
- warn('Please run chusaku from the root of your project.')
31
+ warn("Please run chusaku from the root of your project.")
34
32
  STATUS_ERROR
35
33
  rescue Finished
36
34
  STATUS_SUCCESS
@@ -43,8 +41,8 @@ module Chusaku
43
41
  # @raise [Chusaku::CLI::NotARailsProject] Exception if not Rails project
44
42
  # @return [void]
45
43
  def check_for_rails_project
46
- has_controllers = File.directory?('./app/controllers')
47
- has_rakefile = File.exist?('./Rakefile')
44
+ has_controllers = File.directory?("./app/controllers")
45
+ has_rakefile = File.exist?("./Rakefile")
48
46
  raise NotARailsProject unless has_controllers && has_rakefile
49
47
  end
50
48
 
@@ -53,37 +51,42 @@ module Chusaku
53
51
  # @return [OptionParser] Preconfigured OptionParser instance
54
52
  def optparser
55
53
  OptionParser.new do |opts|
56
- opts.banner = 'Usage: chusaku [options]'
57
- add_error_on_annotation_flag(opts)
54
+ opts.banner = "Usage: chusaku [options]"
58
55
  add_dry_run_flag(opts)
56
+ add_error_on_annotation_flag(opts)
57
+ add_verbose_flag(opts)
59
58
  add_version_flag(opts)
60
59
  add_help_flag(opts)
61
60
  end
62
61
  end
63
62
 
63
+ # Adds `--dry-run` flag.
64
+ #
65
+ # @param opts [OptionParser] OptionParser instance
66
+ # @return [void]
67
+ def add_dry_run_flag(opts)
68
+ opts.on("--dry-run", "Run without file modifications") do
69
+ @options[:dry] = true
70
+ end
71
+ end
72
+
64
73
  # Adds `--exit-with-error-on-annotation` flag.
65
74
  #
66
75
  # @param opts [OptionParser] OptionParser instance
67
76
  # @return [void]
68
77
  def add_error_on_annotation_flag(opts)
69
- opts.on(
70
- '--exit-with-error-on-annotation',
71
- 'Fail if any file was annotated'
72
- ) do
78
+ opts.on("--exit-with-error-on-annotation", "Fail if any file was annotated") do
73
79
  @options[:error_on_annotation] = true
74
80
  end
75
81
  end
76
82
 
77
- # Adds `--dry-run` flag.
83
+ # Adds `--verbose` flag.
78
84
  #
79
85
  # @param opts [OptionParser] OptionParser instance
80
86
  # @return [void]
81
- def add_dry_run_flag(opts)
82
- opts.on(
83
- '--dry-run',
84
- 'Run without file modifications'
85
- ) do
86
- @options[:dry] = true
87
+ def add_verbose_flag(opts)
88
+ opts.on("--verbose", "Print all annotations") do
89
+ @options[:verbose] = true
87
90
  end
88
91
  end
89
92
 
@@ -92,11 +95,7 @@ module Chusaku
92
95
  # @param opts [OptionParser] OptionParser instance
93
96
  # @return [void]
94
97
  def add_version_flag(opts)
95
- opts.on(
96
- '-v',
97
- '--version',
98
- 'Show Chusaku version number and quit'
99
- ) do
98
+ opts.on("-v", "--version", "Show Chusaku version number and quit") do
100
99
  puts(Chusaku::VERSION)
101
100
  raise Finished
102
101
  end
@@ -107,11 +106,7 @@ module Chusaku
107
106
  # @param opts [OptionParser] OptionParser instance
108
107
  # @return [void]
109
108
  def add_help_flag(opts)
110
- opts.on(
111
- '-h',
112
- '--help',
113
- 'Show this help message and quit'
114
- ) do
109
+ opts.on("-h", "--help", "Show this help message and quit") do
115
110
  puts(opts)
116
111
  raise Finished
117
112
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Chusaku
4
2
  # Handles parsing a file and groups its lines into categories.
5
3
  module Parser
@@ -13,22 +11,26 @@ module Chusaku
13
11
  # {
14
12
  # type: :code,
15
13
  # body: 'class Foo\n',
16
- # action: nil
14
+ # action: nil,
15
+ # line_number: 1
17
16
  # },
18
17
  # {
19
18
  # type: :comment,
20
19
  # body: ' # Bar\n # Baz\n',
21
- # action: nil
20
+ # action: nil,
21
+ # line_number: 2
22
22
  # },
23
23
  # {
24
24
  # type: :action,
25
25
  # body: ' def action_name; end\n',
26
- # action: 'action_name'
26
+ # action: 'action_name',
27
+ # line_number: 4
27
28
  # }
28
29
  # {
29
30
  # type: :code,
30
31
  # body: 'end # vanilla is the best flavor\n',
31
- # action: nil
32
+ # action: nil,
33
+ # line_number: 5
32
34
  # }
33
35
  # ]
34
36
  # }
@@ -41,23 +43,23 @@ module Chusaku
41
43
  group = {}
42
44
  content = IO.read(path)
43
45
 
44
- content.each_line do |line|
46
+ content.each_line.with_index do |line, index|
45
47
  parsed_line = parse_line(line: line, actions: actions)
46
48
 
47
- if group[:type] != parsed_line[:type]
49
+ if group[:type] == parsed_line[:type]
50
+ # Same group. Push the current line into the current group.
51
+ group[:body] += line
52
+ else
48
53
  # Now looking at a new group. Push the current group onto the array
49
54
  # and start a new one.
50
55
  groups.push(group) unless group.empty?
51
- group = parsed_line
52
- else
53
- # Same group. Push the current line into the current group.
54
- group[:body] += line
56
+ group = parsed_line.merge(line_number: index + 1)
55
57
  end
56
58
  end
57
59
 
58
60
  # Push the last group onto the array and return.
59
61
  groups.push(group)
60
- { content: content, groups: groups }
62
+ {content: content, groups: groups}
61
63
  end
62
64
 
63
65
  # Given a line and actions, returns the line's type.
@@ -81,11 +83,11 @@ module Chusaku
81
83
  def_match = /^\s*def\s+(\w*)\s*\w*.*$/.match(line)
82
84
 
83
85
  if !comment_match.nil?
84
- { type: :comment, body: line, action: nil }
86
+ {type: :comment, body: line, action: nil}
85
87
  elsif !def_match.nil? && actions.include?(def_match[1])
86
- { type: :action, body: line, action: def_match[1] }
88
+ {type: :action, body: line, action: def_match[1]}
87
89
  else
88
- { type: :code, body: line, action: nil }
90
+ {type: :code, body: line, action: nil}
89
91
  end
90
92
  end
91
93
  end
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Chusaku
4
2
  # Handles extracting information about the Rails project's routes.
5
3
  class Routes
@@ -9,18 +7,18 @@ module Chusaku
9
7
  # Example output:
10
8
  #
11
9
  # {
12
- # 'users' => {
13
- # 'edit' => [
14
- # { verb: 'GET', path: '/users/:id', name: 'edit_user' }
10
+ # "users" => {
11
+ # "edit" => [
12
+ # {verb: "GET", path: "/users/:id", name: "edit_user"}
15
13
  # ],
16
- # 'update' => [
17
- # { verb: 'PATCH', path: '/users', name: 'edit_user' },
18
- # { verb: 'PUT', path: '/users', name: 'edit_user' }
14
+ # "update" => [
15
+ # {verb: "PATCH", path: "/users", name: "edit_user"},
16
+ # {verb: "PUT", path: "/users", name: "edit_user"}
19
17
  # ]
20
18
  # },
21
- # 'empanadas' => {
22
- # 'create' => [
23
- # { verb: 'POST', path: '/empanadas', name: nil }
19
+ # "empanadas" => {
20
+ # "create" => [
21
+ # {verb: "POST", path: "/empanadas", name: nil}
24
22
  # ]
25
23
  # }
26
24
  # }
@@ -86,7 +84,7 @@ module Chusaku
86
84
  def format(route:, verb:, defaults:)
87
85
  {
88
86
  verb: verb,
89
- path: route.path.spec.to_s.gsub('(.:format)', ''),
87
+ path: route.path.spec.to_s.gsub("(.:format)", ""),
90
88
  name: route.name,
91
89
  defaults: defaults
92
90
  }
@@ -1,5 +1,3 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Chusaku
4
- VERSION = '0.4.0'
2
+ VERSION = "0.6.1"
5
3
  end
data/lib/chusaku.rb CHANGED
@@ -1,8 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- require 'chusaku/version'
4
- require 'chusaku/parser'
5
- require 'chusaku/routes'
1
+ require "chusaku/version"
2
+ require "chusaku/parser"
3
+ require "chusaku/routes"
6
4
 
7
5
  # Handles core functionality of annotating projects.
8
6
  module Chusaku
@@ -20,11 +18,12 @@ module Chusaku
20
18
  def call(flags = {})
21
19
  @flags = flags
22
20
  @routes = Chusaku::Routes.call
23
- @annotated_paths = []
24
- controllers_pattern = 'app/controllers/**/*_controller.rb'
21
+ @changes = []
22
+ @changed_files = []
23
+ controllers_pattern = "app/controllers/**/*_controller.rb"
25
24
 
26
25
  Dir.glob(Rails.root.join(controllers_pattern)).each do |path|
27
- controller = %r{controllers\/(.*)_controller\.rb}.match(path)[1]
26
+ controller = %r{controllers/(.*)_controller\.rb}.match(path)[1]
28
27
  actions = @routes[controller]
29
28
  next if actions.nil?
30
29
 
@@ -45,18 +44,46 @@ module Chusaku
45
44
  def annotate_file(path:, controller:, actions:)
46
45
  parsed_file = Chusaku::Parser.call(path: path, actions: actions)
47
46
  parsed_file[:groups].each_cons(2) do |prev, curr|
48
- clean_group(prev)
47
+ record_change(group: prev, type: :clean, path: path)
49
48
  next unless curr[:type] == :action
50
49
 
51
50
  route_data = @routes[controller][curr[:action]]
52
51
  next unless route_data.any?
53
52
 
54
- annotate_group(group: curr, route_data: route_data)
53
+ record_change(group: curr, type: :annotate, route_data: route_data, path: path)
55
54
  end
56
55
 
57
56
  write_to_file(path: path, parsed_file: parsed_file)
58
57
  end
59
58
 
59
+ # Clean or annotate a group and track the group as changed if applicable.
60
+ #
61
+ # @param group [Hash] { type => Symbol, body => String }
62
+ # @param type [Symbol] [:clean, :annotate]
63
+ # @param path [String] File path
64
+ # @param route_data [Array<Hash>] [{
65
+ # verb: String,
66
+ # path: String,
67
+ # name: String }]
68
+ # @return [void]
69
+ def record_change(group:, type:, path:, route_data: [])
70
+ old_body = group[:body]
71
+
72
+ case type
73
+ when :clean
74
+ clean_group(group)
75
+ when :annotate
76
+ annotate_group(group: group, route_data: route_data)
77
+ end
78
+ return if old_body == group[:body]
79
+
80
+ @changes.push \
81
+ old_body: old_body,
82
+ new_body: group[:body],
83
+ path: path,
84
+ line_number: group[:line_number]
85
+ end
86
+
60
87
  # Given a parsed group, clean out its contents.
61
88
  #
62
89
  # @param group [Hash] { type => Symbol, body => String }
@@ -64,9 +91,9 @@ module Chusaku
64
91
  def clean_group(group)
65
92
  return unless group[:type] == :comment
66
93
 
67
- group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, '')
94
+ group[:body] = group[:body].gsub(/^\s*#\s*@route.*$\n/, "")
68
95
  group[:body] =
69
- group[:body].gsub(%r{^\s*# (GET|POST|PATCH\/PUT|DELETE) \/\S+$\n}, '')
96
+ group[:body].gsub(%r{^\s*# (GET|POST|PATCH/PUT|DELETE) /\S+$\n}, "")
70
97
  end
71
98
 
72
99
  # Add an annotation to the given group given by Chusaku::Parser that looks
@@ -97,8 +124,8 @@ module Chusaku
97
124
  if defaults&.any?
98
125
  defaults_str =
99
126
  defaults
100
- .map { |key, value| "#{key}: #{value.inspect}" }
101
- .join(', ')
127
+ .map { |key, value| "#{key}: #{value.inspect}" }
128
+ .join(", ")
102
129
  annotation += " {#{defaults_str}}"
103
130
  end
104
131
  annotation += " (#{name})" unless name.nil?
@@ -112,10 +139,10 @@ module Chusaku
112
139
  # @return [void]
113
140
  def write_to_file(path:, parsed_file:)
114
141
  new_content = new_content_for(parsed_file)
115
- return unless parsed_file[:content] != new_content
142
+ return if parsed_file[:content] == new_content
116
143
 
117
144
  !@flags.include?(:dry) && perform_write(path: path, content: new_content)
118
- @annotated_paths.push(path)
145
+ @changed_files.push(path)
119
146
  end
120
147
 
121
148
  # Extracts the new file content for the given parsed file.
@@ -147,20 +174,53 @@ module Chusaku
147
174
  #
148
175
  # @return [String] 'r' or 'w'
149
176
  def file_mode
150
- File.instance_methods.include?(:test_write) ? 'r' : 'w'
177
+ File.instance_methods.include?(:test_write) ? "r" : "w"
151
178
  end
152
179
 
153
180
  # Output results to user.
154
181
  #
155
182
  # @return [Integer] 0 for success, 1 for error
156
183
  def output_results
157
- if @annotated_paths.any?
158
- puts("Annotated #{@annotated_paths.join(', ')}")
159
- @flags.include?(:error_on_annotation) ? 1 : 0
160
- else
161
- puts('Nothing to annotate')
162
- 0
163
- end
184
+ puts(output_copy)
185
+ exit_code = 0
186
+ exit_code = 1 if @changed_files.any? && @flags.include?(:error_on_annotation)
187
+ exit_code
188
+ end
189
+
190
+ # Determines the copy to be used in the program output.
191
+ #
192
+ # @return [String] Copy to be outputted to user
193
+ def output_copy
194
+ return "Nothing to annotate." if @changed_files.empty?
195
+
196
+ copy = changes_copy
197
+ copy += "\nChusaku has finished running."
198
+ copy += "\nThis was a dry run so no files were changed." if @flags.include?(:dry)
199
+ copy += "\nExited with status code 1." if @flags.include?(:error_on_annotation)
200
+ copy
201
+ end
202
+
203
+ # Returns the copy for recorded changes if `--verbose` flag is passed.
204
+ #
205
+ # @return [String] Copy of recorded changes
206
+ def changes_copy
207
+ return "" unless @flags.include?(:verbose)
208
+
209
+ @changes.map do |change|
210
+ <<~CHANGE_OUTPUT
211
+ [#{change[:path]}:#{change[:line_number]}]
212
+
213
+ Before:
214
+ ```ruby
215
+ #{change[:old_body].chomp}
216
+ ```
217
+
218
+ After:
219
+ ```ruby
220
+ #{change[:new_body].chomp}
221
+ ```
222
+ CHANGE_OUTPUT
223
+ end.join("\n")
164
224
  end
165
225
  end
166
226
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chusaku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nishiki Liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-10 00:00:00.000000000 Z
11
+ date: 2021-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,94 +16,80 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.0'
19
+ version: '2.2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.0'
26
+ version: '2.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.0'
33
+ version: '5.14'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.0'
40
+ version: '5.14'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '12.3'
47
+ version: '13.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '12.3'
54
+ version: '13.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rubocop
56
+ name: standardrb
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.77'
61
+ version: '1.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.77'
68
+ version: '1.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rubocop-performance
70
+ name: railties
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '1.5'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '1.5'
83
- - !ruby/object:Gem::Dependency
84
- name: rails
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">"
73
+ - - ">="
88
74
  - !ruby/object:Gem::Version
89
- version: '2.0'
75
+ version: '3.0'
90
76
  type: :runtime
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
- - - ">"
80
+ - - ">="
95
81
  - !ruby/object:Gem::Version
96
- version: '2.0'
82
+ version: '3.0'
97
83
  description: Annotate your Rails controllers with route info.
98
84
  email:
99
- - nishiki@hey.com
85
+ - hello@nshki.com
100
86
  executables:
101
87
  - chusaku
102
88
  extensions: []
103
89
  extra_rdoc_files: []
104
90
  files:
105
- - ".circleci/config.yml"
106
- - ".codeclimate.yml"
91
+ - ".github/workflows/linting.yml"
92
+ - ".github/workflows/testing.yml"
107
93
  - ".gitignore"
108
94
  - ".rubocop.yml"
109
95
  - Gemfile
@@ -142,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
128
  - !ruby/object:Gem::Version
143
129
  version: '0'
144
130
  requirements: []
145
- rubygems_version: 3.1.4
131
+ rubygems_version: 3.1.6
146
132
  signing_key:
147
133
  specification_version: 4
148
134
  summary: Annotate your Rails controllers with route info.
data/.circleci/config.yml DELETED
@@ -1,29 +0,0 @@
1
- version: 2
2
- jobs:
3
- build:
4
- working_directory: ~/chusaku
5
- docker:
6
- - image: circleci/ruby
7
- steps:
8
- - checkout
9
- - run:
10
- name: Install Bundler
11
- command: |
12
- gem install bundler
13
- - restore_cache:
14
- keys:
15
- - dep-v1-{{ .Branch }}-{{ checksum "chusaku.gemspec" }}
16
- - dep-v1-{{ .Branch }}
17
- - dep-v1-
18
- - run:
19
- name: Install dependencies
20
- command: |
21
- bundle install --path vendor/bundle
22
- - save_cache:
23
- key: dep-v1-{{ .Branch }}-{{ checksum "chusaku.gemspec" }}
24
- paths:
25
- - vendor/bundle
26
- - run:
27
- name: Run tests
28
- command: |
29
- bundle exec rake test
data/.codeclimate.yml DELETED
@@ -1,4 +0,0 @@
1
- plugins:
2
- rubocop:
3
- enabled: true
4
- channel: rubocop-0-77