rubocop-extension-generator 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bcee5e332852d452e3588610c68ae4fef761575b0a2570188e46757b2c03e8fd
4
+ data.tar.gz: 96ac7fdd367d30a449c07f9279ac23e1254514f1e9921f649a84a5e849de3e65
5
+ SHA512:
6
+ metadata.gz: 8a0b31e2b319110c112285c2b00da343bcee1623d54dc17d12a75210264fa9ad60aaab6a9fc8776f87258a650df898f0074c8253dab6c013b256938917909508
7
+ data.tar.gz: 8d77af43d7a34596070985d985b5017f2dee1ac35e9e7bfab0a380bd70e868430c93b5e4daf159de41dd559d710dc1b87e3ffff79e7b70b064599a366fbbf22a
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rubocop-extension-generator.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Masataka Pocke Kuwabara
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # rubocop-extension-generator
2
+
3
+ A generator of RuboCop's custom cops gem.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rubocop-extension-generator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rubocop-extension-generator
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ $ rubocop-extension-generator rubocop-foobar
25
+ Creating gem 'rubocop-foobar'...
26
+ create rubocop-foobar/Gemfile
27
+ create rubocop-foobar/lib/rubocop/foobar.rb
28
+ create rubocop-foobar/lib/rubocop/foobar/version.rb
29
+ create rubocop-foobar/rubocop-foobar.gemspec
30
+ create rubocop-foobar/Rakefile
31
+ create rubocop-foobar/README.md
32
+ create rubocop-foobar/bin/console
33
+ create rubocop-foobar/bin/setup
34
+ create rubocop-foobar/.gitignore
35
+ Initializing git repo in /tmp/tmp.Gu7G94wX00/rubocop-foobar
36
+ Gem 'rubocop-foobar' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
37
+ create rubocop-foobar/lib/rubocop-foobar.rb
38
+ create rubocop-foobar/lib/rubocop/foobar/inject.rb
39
+ create rubocop-foobar/lib/rubocop/cop/foobar_cops.rb
40
+ create rubocop-foobar/config/default.yml
41
+ create rubocop-foobar/spec/spec_helper.rb
42
+ create rubocop-foobar/.rspec
43
+ update lib/rubocop/foobar.rb
44
+ update lib/rubocop/foobar.rb
45
+ update lib/rubocop/foobar/version.rb
46
+ update rubocop-foobar.gemspec
47
+ update rubocop-foobar.gemspec
48
+ update Rakefile
49
+ update Gemfile
50
+
51
+ It's done! You can start developing a new extension of RuboCop in rubocop-foobar.
52
+ For the next step, you can use the cop generator.
53
+
54
+ $ bundle exec rake 'new_cop[Foobar/SuperCoolCopName]'
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ 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).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubocop-hq/rubocop-extension-generator.
66
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rubocop/extension/generator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -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
@@ -0,0 +1,5 @@
1
+ #!ruby
2
+
3
+ require 'rubocop/extension/generator'
4
+
5
+ RuboCop::Extension::Generator::CLI.run(ARGV)
@@ -0,0 +1,17 @@
1
+ require "rubocop/extension/generator/version"
2
+ require "rubocop/extension/generator/cli"
3
+ require "rubocop/extension/generator/generator"
4
+
5
+ require 'active_support'
6
+ require 'active_support/core_ext/string/inflections'
7
+
8
+ require 'optparse'
9
+ require 'pathname'
10
+ require 'fileutils'
11
+
12
+ module RuboCop
13
+ module Extension
14
+ module Generator
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module RuboCop
2
+ module Extension
3
+ module Generator
4
+ class CLI
5
+ BANNER = <<~TEXT
6
+ Usage: rubocop-extension-generator NAME
7
+ TEXT
8
+
9
+ def self.run(argv)
10
+ new(argv).run
11
+ end
12
+
13
+ def initialize(argv)
14
+ @argv = argv
15
+ end
16
+
17
+ def run
18
+ # For --help
19
+ opt = OptionParser.new(BANNER)
20
+ args = opt.parse(@argv)
21
+
22
+ name = args.first
23
+ raise "It must be named `rubocop-*`. For example: rubocop-rspec" unless name.match?(/\Arubocop-\w+\z/)
24
+
25
+ Generator.new(name).generate
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,182 @@
1
+ module RuboCop
2
+ module Extension
3
+ module Generator
4
+ class Generator
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def generate
10
+ system('bundle', 'gem', name, exception: true)
11
+
12
+ put "lib/#{name}.rb", <<~RUBY
13
+ # frozen_string_literal: true
14
+
15
+ require 'rubocop'
16
+
17
+ require_relative '#{dirname}'
18
+ require_relative '#{dirname}/version'
19
+ require_relative '#{dirname}/inject'
20
+
21
+ RuboCop::#{classname}::Inject.defaults!
22
+
23
+ require_relative '#{cops_file_name.sub(/\.rb$/, '').sub(/^lib\//, '')}'
24
+ RUBY
25
+
26
+ put "lib/#{dirname}/inject.rb", <<~RUBY
27
+ # frozen_string_literal: true
28
+
29
+ # The original code is from https://github.com/rubocop-hq/rubocop-rspec/blob/master/lib/rubocop/rspec/inject.rb
30
+ # See https://github.com/rubocop-hq/rubocop-rspec/blob/master/MIT-LICENSE.md
31
+ module RuboCop
32
+ module #{classname}
33
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
34
+ # bit of our configuration.
35
+ module Inject
36
+ def self.defaults!
37
+ path = CONFIG_DEFAULT.to_s
38
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
39
+ config = Config.new(hash, path)
40
+ puts "configuration from \#{path}" if ConfigLoader.debug?
41
+ config = ConfigLoader.merge_with_default(config, path)
42
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ RUBY
48
+
49
+ put cops_file_name, <<~RUBY
50
+ # frozen_string_literal: true
51
+ RUBY
52
+
53
+ put "config/default.yml", <<~YAML
54
+ # Write it!
55
+ YAML
56
+
57
+ put 'spec/spec_helper.rb', <<~RUBY
58
+ # frozen_string_literal: true
59
+
60
+ require '#{name}'
61
+ require 'rubocop/rspec/support'
62
+
63
+ RSpec.configure do |config|
64
+ config.include RuboCop::RSpec::ExpectOffense
65
+
66
+ config.disable_monkey_patching!
67
+ config.raise_errors_for_deprecations!
68
+ config.raise_on_warning = true
69
+ config.fail_if_no_examples = true
70
+
71
+ config.order = :random
72
+ Kernel.srand config.seed
73
+ end
74
+ RUBY
75
+
76
+ put '.rspec', <<~TEXT
77
+ --format documentation
78
+ --color
79
+ --require spec_helper
80
+ TEXT
81
+
82
+ patch "lib/#{dirname}.rb", /^ end\nend/, <<~RUBY
83
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
84
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
85
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
86
+
87
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
88
+ end
89
+ end
90
+ RUBY
91
+
92
+ patch "lib/#{dirname}.rb", 'module Rubocop', 'module RuboCop'
93
+ patch "lib/#{dirname}/version.rb", 'module Rubocop', 'module RuboCop'
94
+ patch "#{name}.gemspec", 'Rubocop', 'RuboCop'
95
+
96
+ patch "#{name}.gemspec", /^end/, <<~RUBY
97
+
98
+ spec.add_runtime_dependency 'rubocop'
99
+ end
100
+ RUBY
101
+
102
+ patch "Rakefile", /\z/, <<~RUBY
103
+
104
+ require 'rspec/core/rake_task'
105
+
106
+ RSpec::Core::RakeTask.new(:spec) do |spec|
107
+ spec.pattern = FileList['spec/**/*_spec.rb']
108
+ end
109
+
110
+ desc 'Generate a new cop with a template'
111
+ task :new_cop, [:cop] do |_task, args|
112
+ require 'rubocop'
113
+
114
+ cop_name = args.fetch(:cop) do
115
+ warn 'usage: bundle exec rake new_cop[Department/Name]'
116
+ exit!
117
+ end
118
+
119
+ github_user = `git config github.user`.chop
120
+ github_user = 'your_id' if github_user.empty?
121
+
122
+ generator = RuboCop::Cop::Generator.new(cop_name, github_user)
123
+
124
+ generator.write_source
125
+ generator.write_spec
126
+ generator.inject_require(root_file_path: '#{cops_file_name}')
127
+ generator.inject_config(config_file_path: 'config/default.yml')
128
+
129
+ puts generator.todo
130
+ end
131
+ RUBY
132
+
133
+ patch 'Gemfile', /\z/, <<~RUBY
134
+ gem 'rspec'
135
+ RUBY
136
+
137
+ puts
138
+ puts <<~MESSAGE
139
+ It's done! You can start developing a new extension of RuboCop in #{root_path}.
140
+ For the next step, you can use the cop generator.
141
+
142
+ $ bundle exec rake 'new_cop[#{classname}/SuperCoolCopName]'
143
+ MESSAGE
144
+ end
145
+
146
+ private def put(path, content)
147
+ path = root_path / path
148
+ puts "create #{path}"
149
+ FileUtils.mkdir_p(path.dirname) unless path.dirname.directory?
150
+ path.write(content)
151
+ end
152
+
153
+ private def patch(path, pattern, replacement)
154
+ puts "update #{path}"
155
+ path = root_path / path
156
+ file = path.read
157
+ raise "Cannot apply patch for #{path} because #{pattern} is missing" unless file.match?(pattern)
158
+ path.write file.sub(pattern, replacement)
159
+ end
160
+
161
+ private def root_path
162
+ @root_path ||= Pathname(name)
163
+ end
164
+
165
+ private def dirname
166
+ @dirname ||= name.sub('-', '/')
167
+ end
168
+
169
+ private def classname
170
+ @classname ||= name.split('-').last.camelcase
171
+ end
172
+
173
+ private def cops_file_name
174
+ @cops_file_name ||= "lib/rubocop/cop/#{name.split('-').last}_cops.rb"
175
+ end
176
+
177
+ attr_reader :name
178
+ private :name
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,7 @@
1
+ module RuboCop
2
+ module Extension
3
+ module Generator
4
+ VERSION = "0.2.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ require_relative 'lib/rubocop/extension/generator/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rubocop-extension-generator"
5
+ spec.version = RuboCop::Extension::Generator::VERSION
6
+ spec.authors = ["Masataka Pocke Kuwabara"]
7
+ spec.email = ["kuwabara@pocke.me"]
8
+
9
+ spec.summary = %q{A generator of RuboCop's custom cops gem}
10
+ spec.description = %q{A generator of RuboCop's custom cops gem}
11
+ spec.homepage = "https://github.com/rubocop-hq/rubocop-extension-generator"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
13
+ spec.licenses = ['MIT']
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_runtime_dependency 'bundler'
31
+ spec.add_runtime_dependency 'activesupport'
32
+ end
@@ -0,0 +1,28 @@
1
+ # Smoke testing for this gem
2
+
3
+ require 'tmpdir'
4
+ require 'pathname'
5
+
6
+ exe_path = File.expand_path('../exe/rubocop-extension-generator', __dir__)
7
+ load_path = File.expand_path('../lib', __dir__)
8
+
9
+ Dir.mktmpdir('-rubocop-extension-generator-smoke') do |base_dir|
10
+ base_dir = Pathname(base_dir)
11
+ gem_name = 'rubocop-smoke'
12
+ gem_dir = base_dir / gem_name
13
+
14
+ system({ 'RUBYLIB' => load_path }, 'ruby', exe_path, gem_name, exception: true, chdir: base_dir)
15
+
16
+ gemspec_path = gem_dir / "#{gem_name}.gemspec"
17
+ gemspec = gemspec_path.read
18
+ gemspec[/spec\.summary.+/] = 'spec.summary = "a gem for smoke testing"'
19
+ gemspec.gsub!(/^.+spec\.description.+$/, '')
20
+ gemspec.gsub!(/^.+spec\.homepage.+$/, '')
21
+ gemspec.gsub!(/^.+spec\.metadata.+$/, '')
22
+
23
+ gemspec_path.write gemspec
24
+
25
+ system('bundle', 'install', exception: true, chdir: gem_dir)
26
+ system('bundle', 'exec', 'rake', 'new_cop[Smoke/Foo]', exception: true, chdir: gem_dir)
27
+ system('bundle', 'exec', 'rake', 'spec', exception: true, chdir: gem_dir)
28
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-extension-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Masataka Pocke Kuwabara
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-08 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A generator of RuboCop's custom cops gem
42
+ email:
43
+ - kuwabara@pocke.me
44
+ executables:
45
+ - rubocop-extension-generator
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - exe/rubocop-extension-generator
57
+ - lib/rubocop/extension/generator.rb
58
+ - lib/rubocop/extension/generator/cli.rb
59
+ - lib/rubocop/extension/generator/generator.rb
60
+ - lib/rubocop/extension/generator/version.rb
61
+ - rubocop-extension-generator.gemspec
62
+ - smoke/smoke.rb
63
+ homepage: https://github.com/rubocop-hq/rubocop-extension-generator
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ homepage_uri: https://github.com/rubocop-hq/rubocop-extension-generator
69
+ source_code_uri: https://github.com/rubocop-hq/rubocop-extension-generator
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A generator of RuboCop's custom cops gem
89
+ test_files: []