chusaku 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 243d9f7340595bb5fc64d98e587210773957dc1b41b1203f8736073c683dc5f0
4
+ data.tar.gz: c5ec7b5619878468e84cd959f98c05a954eb6b4bfeb5123df2044c4719407ff6
5
+ SHA512:
6
+ metadata.gz: 1f335d87b266244f0fa2943aadc062d79afedc62280c0466d2e84004af797c00003e70784b32dc2b202a3a1f5c3200396e43abef0109e222784dfd992a163fae
7
+ data.tar.gz: a42ab21ab2f234b2692d3ee595becef92b399e2d2ee63ce16e837abe12a3971f5eed4dcd521debd5de69d7a0b132443076fdf7cb2daf583db33d4a598b3c7ced
@@ -0,0 +1,29 @@
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/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'bin/**/*'
4
+
5
+ Layout/IndentationConsistency:
6
+ EnforcedStyle: rails
7
+
8
+ Metrics/AbcSize:
9
+ Enabled: false
10
+
11
+ Metrics/MethodLength:
12
+ Enabled: false
13
+
14
+ Metrics/PerceivedComplexity:
15
+ Enabled: false
16
+
17
+ Style/ClassVars:
18
+ Enabled: false
19
+
20
+ Style/CommentedKeyword:
21
+ Enabled: false
22
+
23
+ Style/Documentation:
24
+ Enabled: false
25
+
26
+ Style/RegexpLiteral:
27
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in chusaku.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Nishiki Liu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Chusaku
2
+
3
+ | Build |
4
+ |-------|
5
+ |[![CircleCI](https://circleci.com/gh/nshki/chusaku.svg?style=svg&circle-token=e1917972632f242932171de0ca5443148e83151c)](https://circleci.com/gh/nshki/chusaku)|
6
+
7
+ Add comments above your Rails actions that look like:
8
+
9
+ ```ruby
10
+ # @route GET /waterlilies/:id (waterlily)
11
+ def show
12
+ # ...
13
+ end
14
+ ```
15
+
16
+ Based on your `routes.rb` file!
17
+
18
+
19
+ ## Installation
20
+
21
+ Add this line to your Rails application's Gemfile:
22
+
23
+ ```ruby
24
+ group :development do
25
+ # ...
26
+ gem 'chusaku', require: false
27
+ # ...
28
+ end
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ ```
34
+ $ bundle install
35
+ ```
36
+
37
+
38
+ ## Usage
39
+
40
+ From the root of your Rails application, run:
41
+
42
+ ```
43
+ $ bundle exec chusaku
44
+ ```
45
+
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nshki/chusaku.
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/chusaku ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless File.directory?('./app/controllers') && File.exist?('./Rakefile')
4
+ abort 'Please run chusaku from the root of your project.'
5
+ end
6
+
7
+ require 'rubygems'
8
+
9
+ begin
10
+ # Use project's bundle.
11
+ require 'bundler'
12
+ Bundler.setup
13
+
14
+ # Use project's environment.
15
+ require 'rake'
16
+ load './Rakefile'
17
+ Rake::Task[:environment].invoke
18
+ rescue StandardError
19
+ end
20
+
21
+ require 'chusaku'
22
+ Chusaku.annotate
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'chusaku'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/chusaku.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'chusaku/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'chusaku'
9
+ spec.version = Chusaku::VERSION
10
+ spec.authors = ['Nishiki Liu']
11
+ spec.email = ['nishiki.liu@gmail.com']
12
+
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'
17
+
18
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the
19
+ # 'allowed_push_host' to allow pushing to a single host or delete this section
20
+ # to allow pushing to any host.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+
24
+ spec.metadata['homepage_uri'] = spec.homepage
25
+ spec.metadata['source_code_uri'] = spec.homepage
26
+ spec.metadata['changelog_uri'] = spec.homepage
27
+ else
28
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
29
+ 'public gem pushes.'
30
+ end
31
+
32
+ # Specify which files should be added to the gem when it is released.
33
+ # The `git ls-files -z` loads the files in the RubyGem that have been added
34
+ # into git.
35
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
36
+ `git ls-files -z`.split("\x0").reject do |f|
37
+ f.match(%r{^(test|spec|features)/})
38
+ end
39
+ end
40
+ spec.bindir = 'bin'
41
+ spec.executables = 'chusaku'
42
+ spec.require_paths = ['lib']
43
+
44
+ spec.add_development_dependency 'bundler', '~> 2.0'
45
+ spec.add_development_dependency 'minitest', '~> 5.0'
46
+ spec.add_development_dependency 'rake', '~> 10.0'
47
+
48
+ spec.add_dependency 'rails', '> 4.2'
49
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chusaku
4
+ module Parser
5
+ # Parses a file and groups its lines into categories:
6
+ #
7
+ # Example output:
8
+ #
9
+ # [ { type: :code,
10
+ # body: 'class Foo\n',
11
+ # action: nil },
12
+ # { type: :comment,
13
+ # body: ' # Bar\n # Baz\n',
14
+ # action: nil },
15
+ # { type: :action,
16
+ # body: ' def action_name; end\n',
17
+ # action: 'action_name' }
18
+ # { type: :code,
19
+ # body: 'end # vanilla is the best flavor\n',
20
+ # action: nil } ]
21
+ #
22
+ # @param {String} path
23
+ # @param {Array<String>} actions
24
+ # @return {Array<Hash>}
25
+ def self.call(path:, actions:)
26
+ groups = []
27
+ group = {}
28
+
29
+ File.open(path, 'r').each_line do |line|
30
+ parsed_line = parse_line(line: line, actions: actions)
31
+
32
+ if group[:type] != parsed_line[:type]
33
+ # Now looking at a new group. Push the current group onto the array
34
+ # and start a new one.
35
+ groups.push(group) unless group.empty?
36
+ group = parsed_line
37
+ else
38
+ # Same group. Push the current line into the current group.
39
+ group[:body] += line
40
+ end
41
+ end
42
+
43
+ # Push the last group onto the array and return.
44
+ groups.push(group)
45
+ groups
46
+ end
47
+
48
+ # Given a line and actions, returns the line's type:
49
+ #
50
+ # 1. comment - A line that is entirely commented. Lines that have trailing
51
+ # comments do not fall under this category.
52
+ # 2. action - A line that contains an action definition.
53
+ # 3. code - Anything else.
54
+ #
55
+ # And give back a Hash in the form:
56
+ #
57
+ # { type: :action, body: 'def foo', action: 'foo' }
58
+ #
59
+ # @param {String} line
60
+ # @param {Array<String>} actions
61
+ # @return {Hash}
62
+ def self.parse_line(line:, actions:)
63
+ comment_match = /^\s*#.*$/.match(line)
64
+ def_match = /^\s*def\s+(\w*)\s*\w*.*$/.match(line)
65
+
66
+ if !comment_match.nil?
67
+ { type: :comment, body: line, action: nil }
68
+ elsif !def_match.nil? && actions.include?(def_match[1])
69
+ { type: :action, body: line, action: def_match[1] }
70
+ else
71
+ { type: :code, body: line, action: nil }
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chusaku
4
+ module Routes
5
+ # Extract information about the Rails project's routes.
6
+ #
7
+ # Example output:
8
+ #
9
+ # {
10
+ # 'users' => {
11
+ # 'edit' => { verb: 'GET', path: '/users/:id', name: 'edit_user' },
12
+ # 'update' => { verb: 'PUT', path: '/users', name: nil }
13
+ # },
14
+ # 'empanadas' => {
15
+ # 'create' => { verb: 'POST', path: '/empanadas', name: nil }
16
+ # }
17
+ # }
18
+ #
19
+ # @return {Hash}
20
+ def self.call
21
+ routes = {}
22
+
23
+ Rails.application.routes.routes.each do |route|
24
+ defaults = route.defaults
25
+ action = defaults[:action]
26
+
27
+ routes[defaults[:controller]] ||= {}
28
+ routes[defaults[:controller]][action] =
29
+ {
30
+ verb: route.verb,
31
+ path: route.path.spec.to_s.gsub('(.:format)', ''),
32
+ name: route.name
33
+ }
34
+ end
35
+
36
+ routes
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chusaku
4
+ VERSION = '0.1.0'
5
+ end
data/lib/chusaku.rb ADDED
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'chusaku/version'
4
+
5
+ module Chusaku
6
+ # The main method to run Chusaku. Annotate all actions in your Rails project
7
+ # as follows:
8
+ #
9
+ # # @route GET /waterlilies/:id (waterlilies)
10
+ # def show
11
+ # # ...
12
+ # end
13
+ def self.call
14
+ puts 'Chusaku starting...'
15
+ routes = Chusaku::Routes.call
16
+ controllers = 'app/controllers/**/*_controller.rb'
17
+
18
+ # Loop over all controller file paths.
19
+ Dir.glob(Rails.root.join(controllers)).each do |path|
20
+ controller = /controllers\/(.*)_controller\.rb/.match(path)[1]
21
+ actions = routes[controller]
22
+ next if actions.nil?
23
+
24
+ # Parse the file and iterate over the parsed content, two entries at a
25
+ # time.
26
+ parsed_file = Chusaku::Parser.call(path: path, actions: actions.keys)
27
+ parsed_file.each_cons(2) do |prev, curr|
28
+ # Remove all @route comments in the previous group.
29
+ if prev[:type] == :comment
30
+ prev[:body] = prev[:body].gsub(/^\s*#\s*@route.*$\n/, '')
31
+ end
32
+
33
+ # Only proceed if we are currently looking at an action.
34
+ next unless curr[:type] == :action
35
+
36
+ # Insert annotation comment.
37
+ action = curr[:action]
38
+ annotation = annotate(routes[controller][action])
39
+ whitespace = /^(\s*).*$/.match(curr[:body])[1]
40
+ comment = "#{whitespace}# #{annotation}\n"
41
+ curr[:body] = comment + curr[:body]
42
+ end
43
+
44
+ # Write to file.
45
+ parsed_content = parsed_file.map { |pf| pf[:body] }
46
+ write(path, parsed_content.join)
47
+ puts "Annotated #{controller}"
48
+ end
49
+
50
+ puts 'Chusaku finished!'
51
+ end
52
+
53
+ # Write given content to a file. If we're using an overridden version of File,
54
+ # then use its method instead for testing purposes.
55
+ #
56
+ # @param {String} path
57
+ # @param {String} content
58
+ # @return {void}
59
+ def self.write(path, content)
60
+ File.open(path, 'r+') do |file|
61
+ if file.respond_to?(:test_write)
62
+ file.test_write(content, path)
63
+ else
64
+ file.write(content)
65
+ end
66
+ end
67
+ end
68
+
69
+ # Given a hash describing an action, generate an annotation in the form:
70
+ #
71
+ # @route GET /waterlilies/:id (waterlilies)
72
+ #
73
+ # @param {Hash} action_info
74
+ # @return {String}
75
+ def self.annotate(action_info)
76
+ verb = action_info[:verb]
77
+ path = action_info[:path]
78
+ name = action_info[:name]
79
+ annotation = "@route #{verb} #{path}"
80
+ annotation += " (#{name})" unless name.nil?
81
+ annotation
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chusaku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nishiki Liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ description: Annotate your Rails controllers with route info.
70
+ email:
71
+ - nishiki.liu@gmail.com
72
+ executables:
73
+ - chusaku
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".circleci/config.yml"
78
+ - ".gitignore"
79
+ - ".rubocop.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/chusaku
85
+ - bin/console
86
+ - bin/setup
87
+ - chusaku.gemspec
88
+ - lib/chusaku.rb
89
+ - lib/chusaku/parser.rb
90
+ - lib/chusaku/routes.rb
91
+ - lib/chusaku/version.rb
92
+ homepage: https://github.com/nshki/chusaku
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ allowed_push_host: https://rubygems.org
97
+ homepage_uri: https://github.com/nshki/chusaku
98
+ source_code_uri: https://github.com/nshki/chusaku
99
+ changelog_uri: https://github.com/nshki/chusaku
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.3
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Annotate your Rails controllers with route info.
119
+ test_files: []