| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require "pathname" |
| 4 |
|
| 5 |
module Bundler |
| 6 |
class CLI |
| 7 |
Bundler.require_thor_actions |
| 8 |
include Thor::Actions |
| 9 |
end |
| 10 |
|
| 11 |
class CLI::Gem |
| 12 |
TEST_FRAMEWORK_VERSIONS = { |
| 13 |
"rspec" => "3.0", |
| 14 |
"minitest" => "5.16", |
| 15 |
"test-unit" => "3.0", |
| 16 |
}.freeze |
| 17 |
|
| 18 |
attr_reader :options, :gem_name, :thor, :name, :target, :extension |
| 19 |
|
| 20 |
def initialize(options, gem_name, thor) |
| 21 |
@options = options |
| 22 |
@gem_name = resolve_name(gem_name) |
| 23 |
|
| 24 |
@thor = thor |
| 25 |
thor.behavior = :invoke |
| 26 |
thor.destination_root = nil |
| 27 |
|
| 28 |
@name = @gem_name |
| 29 |
@target = SharedHelpers.pwd.join(gem_name) |
| 30 |
|
| 31 |
@extension = options[:ext] |
| 32 |
|
| 33 |
validate_ext_name if @extension |
| 34 |
validate_rust_builder_rubygems_version if @extension == "rust" |
| 35 |
end |
| 36 |
|
| 37 |
def run |
| 38 |
Bundler.ui.confirm "Creating gem '#{name}'..." |
| 39 |
|
| 40 |
underscored_name = name.tr("-", "_") |
| 41 |
namespaced_path = name.tr("-", "/") |
| 42 |
constant_name = name.gsub(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase } |
| 43 |
constant_array = constant_name.split("::") |
| 44 |
minitest_constant_name = constant_array.clone.tap {|a| a[-1] = "Test#{a[-1]}" }.join("::") # Foo::Bar => Foo::TestBar |
| 45 |
|
| 46 |
use_git = Bundler.git_present? && options[:git] |
| 47 |
|
| 48 |
git_author_name = use_git ? `git config user.name`.chomp : "" |
| 49 |
git_username = use_git ? `git config github.user`.chomp : "" |
| 50 |
git_user_email = use_git ? `git config user.email`.chomp : "" |
| 51 |
|
| 52 |
github_username = if options[:github_username].nil? |
| 53 |
git_username |
| 54 |
elsif options[:github_username] == false |
| 55 |
"" |
| 56 |
else |
| 57 |
options[:github_username] |
| 58 |
end |
| 59 |
|
| 60 |
config = { |
| 61 |
name: name, |
| 62 |
underscored_name: underscored_name, |
| 63 |
namespaced_path: namespaced_path, |
| 64 |
makefile_path: "#{underscored_name}/#{underscored_name}", |
| 65 |
constant_name: constant_name, |
| 66 |
constant_array: constant_array, |
| 67 |
author: git_author_name.empty? ? "TODO: Write your name" : git_author_name, |
| 68 |
email: git_user_email.empty? ? "TODO: Write your email address" : git_user_email, |
| 69 |
test: options[:test], |
| 70 |
ext: extension, |
| 71 |
exe: options[:exe], |
| 72 |
bundler_version: bundler_dependency_version, |
| 73 |
git: use_git, |
| 74 |
github_username: github_username.empty? ? "[USERNAME]" : github_username, |
| 75 |
required_ruby_version: required_ruby_version, |
| 76 |
rust_builder_required_rubygems_version: rust_builder_required_rubygems_version, |
| 77 |
minitest_constant_name: minitest_constant_name, |
| 78 |
} |
| 79 |
ensure_safe_gem_name(name, constant_array) |
| 80 |
|
| 81 |
templates = { |
| 82 |
"Gemfile.tt" => Bundler.preferred_gemfile_name, |
| 83 |
"lib/newgem.rb.tt" => "lib/#{namespaced_path}.rb", |
| 84 |
"lib/newgem/version.rb.tt" => "lib/#{namespaced_path}/version.rb", |
| 85 |
"sig/newgem.rbs.tt" => "sig/#{namespaced_path}.rbs", |
| 86 |
"newgem.gemspec.tt" => "#{name}.gemspec", |
| 87 |
"Rakefile.tt" => "Rakefile", |
| 88 |
"README.md.tt" => "README.md", |
| 89 |
"bin/console.tt" => "bin/console", |
| 90 |
"bin/setup.tt" => "bin/setup", |
| 91 |
} |
| 92 |
|
| 93 |
executables = %w[ |
| 94 |
bin/console |
| 95 |
bin/setup |
| 96 |
] |
| 97 |
|
| 98 |
templates.merge!("gitignore.tt" => ".gitignore") if use_git |
| 99 |
|
| 100 |
if test_framework = ask_and_set_test_framework |
| 101 |
config[:test] = test_framework |
| 102 |
config[:test_framework_version] = TEST_FRAMEWORK_VERSIONS[test_framework] |
| 103 |
|
| 104 |
case test_framework |
| 105 |
when "rspec" |
| 106 |
templates.merge!( |
| 107 |
"rspec.tt" => ".rspec", |
| 108 |
"spec/spec_helper.rb.tt" => "spec/spec_helper.rb", |
| 109 |
"spec/newgem_spec.rb.tt" => "spec/#{namespaced_path}_spec.rb" |
| 110 |
) |
| 111 |
config[:test_task] = :spec |
| 112 |
when "minitest" |
| 113 |
# Generate path for minitest target file (FileList["test/**/test_*.rb"]) |
| 114 |
# foo => test/test_foo.rb |
| 115 |
# foo-bar => test/foo/test_bar.rb |
| 116 |
# foo_bar => test/test_foo_bar.rb |
| 117 |
paths = namespaced_path.rpartition("/") |
| 118 |
paths[2] = "test_#{paths[2]}" |
| 119 |
minitest_namespaced_path = paths.join("") |
| 120 |
|
| 121 |
templates.merge!( |
| 122 |
"test/minitest/test_helper.rb.tt" => "test/test_helper.rb", |
| 123 |
"test/minitest/test_newgem.rb.tt" => "test/#{minitest_namespaced_path}.rb" |
| 124 |
) |
| 125 |
config[:test_task] = :test |
| 126 |
when "test-unit" |
| 127 |
templates.merge!( |
| 128 |
"test/test-unit/test_helper.rb.tt" => "test/test_helper.rb", |
| 129 |
"test/test-unit/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb" |
| 130 |
) |
| 131 |
config[:test_task] = :test |
| 132 |
end |
| 133 |
end |
| 134 |
|
| 135 |
config[:ci] = ask_and_set_ci |
| 136 |
case config[:ci] |
| 137 |
when "github" |
| 138 |
templates.merge!("github/workflows/main.yml.tt" => ".github/workflows/main.yml") |
| 139 |
config[:ci_config_path] = ".github " |
| 140 |
when "gitlab" |
| 141 |
templates.merge!("gitlab-ci.yml.tt" => ".gitlab-ci.yml") |
| 142 |
config[:ci_config_path] = ".gitlab-ci.yml " |
| 143 |
when "circle" |
| 144 |
templates.merge!("circleci/config.yml.tt" => ".circleci/config.yml") |
| 145 |
config[:ci_config_path] = ".circleci " |
| 146 |
end |
| 147 |
|
| 148 |
if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?", |
| 149 |
"This means that any other developer or company will be legally allowed to use your code " \ |
| 150 |
"for free as long as they admit you created it. You can read more about the MIT license " \ |
| 151 |
"at https://choosealicense.com/licenses/mit.") |
| 152 |
config[:mit] = true |
| 153 |
Bundler.ui.info "MIT License enabled in config" |
| 154 |
templates.merge!("LICENSE.txt.tt" => "LICENSE.txt") |
| 155 |
end |
| 156 |
|
| 157 |
if ask_and_set(:coc, "Do you want to include a code of conduct in gems you generate?", |
| 158 |
"Codes of conduct can increase contributions to your project by contributors who " \ |
| 159 |
"prefer collaborative, safe spaces. You can read more about the code of conduct at " \ |
| 160 |
"contributor-covenant.org. Having a code of conduct means agreeing to the responsibility " \ |
| 161 |
"of enforcing it, so be sure that you are prepared to do that. Be sure that your email " \ |
| 162 |
"address is specified as a contact in the generated code of conduct so that people know " \ |
| 163 |
"who to contact in case of a violation. For suggestions about " \ |
| 164 |
"how to enforce codes of conduct, see https://bit.ly/coc-enforcement.") |
| 165 |
config[:coc] = true |
| 166 |
Bundler.ui.info "Code of conduct enabled in config" |
| 167 |
templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md") |
| 168 |
end |
| 169 |
|
| 170 |
if ask_and_set(:changelog, "Do you want to include a changelog?", |
| 171 |
"A changelog is a file which contains a curated, chronologically ordered list of notable " \ |
| 172 |
"changes for each version of a project. To make it easier for users and contributors to" \ |
| 173 |
" see precisely what notable changes have been made between each release (or version) of" \ |
| 174 |
" the project. Whether consumers or developers, the end users of software are" \ |
| 175 |
" human beings who care about what's in the software. When the software changes, people " \ |
| 176 |
"want to know why and how. see https://keepachangelog.com") |
| 177 |
config[:changelog] = true |
| 178 |
Bundler.ui.info "Changelog enabled in config" |
| 179 |
templates.merge!("CHANGELOG.md.tt" => "CHANGELOG.md") |
| 180 |
end |
| 181 |
|
| 182 |
config[:linter] = ask_and_set_linter |
| 183 |
case config[:linter] |
| 184 |
when "rubocop" |
| 185 |
config[:linter_version] = rubocop_version |
| 186 |
Bundler.ui.info "RuboCop enabled in config" |
| 187 |
templates.merge!("rubocop.yml.tt" => ".rubocop.yml") |
| 188 |
when "standard" |
| 189 |
config[:linter_version] = standard_version |
| 190 |
Bundler.ui.info "Standard enabled in config" |
| 191 |
templates.merge!("standard.yml.tt" => ".standard.yml") |
| 192 |
end |
| 193 |
|
| 194 |
if config[:exe] |
| 195 |
templates.merge!("exe/newgem.tt" => "exe/#{name}") |
| 196 |
executables.push("exe/#{name}") |
| 197 |
end |
| 198 |
|
| 199 |
if extension == "c" |
| 200 |
templates.merge!( |
| 201 |
"ext/newgem/extconf-c.rb.tt" => "ext/#{name}/extconf.rb", |
| 202 |
"ext/newgem/newgem.h.tt" => "ext/#{name}/#{underscored_name}.h", |
| 203 |
"ext/newgem/newgem.c.tt" => "ext/#{name}/#{underscored_name}.c" |
| 204 |
) |
| 205 |
end |
| 206 |
|
| 207 |
if extension == "rust" |
| 208 |
templates.merge!( |
| 209 |
"Cargo.toml.tt" => "Cargo.toml", |
| 210 |
"ext/newgem/Cargo.toml.tt" => "ext/#{name}/Cargo.toml", |
| 211 |
"ext/newgem/extconf-rust.rb.tt" => "ext/#{name}/extconf.rb", |
| 212 |
"ext/newgem/src/lib.rs.tt" => "ext/#{name}/src/lib.rs", |
| 213 |
) |
| 214 |
end |
| 215 |
|
| 216 |
if target.exist? && !target.directory? |
| 217 |
Bundler.ui.error "Couldn't create a new gem named `#{gem_name}` because there's an existing file named `#{gem_name}`." |
| 218 |
exit Bundler::BundlerError.all_errors[Bundler::GenericSystemCallError] |
| 219 |
end |
| 220 |
|
| 221 |
if use_git |
| 222 |
Bundler.ui.info "Initializing git repo in #{target}" |
| 223 |
require "shellwords" |
| 224 |
`git init #{target.to_s.shellescape}` |
| 225 |
|
| 226 |
config[:git_default_branch] = File.read("#{target}/.git/HEAD").split("/").last.chomp |
| 227 |
end |
| 228 |
|
| 229 |
templates.each do |src, dst| |
| 230 |
destination = target.join(dst) |
| 231 |
thor.template("newgem/#{src}", destination, config) |
| 232 |
end |
| 233 |
|
| 234 |
executables.each do |file| |
| 235 |
path = target.join(file) |
| 236 |
executable = (path.stat.mode | 0o111) |
| 237 |
path.chmod(executable) |
| 238 |
end |
| 239 |
|
| 240 |
if use_git |
| 241 |
IO.popen(%w[git add .], { chdir: target }, &:read) |
| 242 |
end |
| 243 |
|
| 244 |
# Open gemspec in editor |
| 245 |
open_editor(options["edit"], target.join("#{name}.gemspec")) if options[:edit] |
| 246 |
|
| 247 |
Bundler.ui.info "Gem '#{name}' was successfully created. " \ |
| 248 |
"For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html" |
| 249 |
end |
| 250 |
|
| 251 |
private |
| 252 |
|
| 253 |
def resolve_name(name) |
| 254 |
SharedHelpers.pwd.join(name).basename.to_s |
| 255 |
end |
| 256 |
|
| 257 |
def ask_and_set(key, header, message) |
| 258 |
choice = options[key] |
| 259 |
choice = Bundler.settings["gem.#{key}"] if choice.nil? |
| 260 |
|
| 261 |
if choice.nil? |
| 262 |
Bundler.ui.confirm header |
| 263 |
choice = Bundler.ui.yes? "#{message} y/(n):" |
| 264 |
Bundler.settings.set_global("gem.#{key}", choice) |
| 265 |
end |
| 266 |
|
| 267 |
choice |
| 268 |
end |
| 269 |
|
| 270 |
def validate_ext_name |
| 271 |
return unless gem_name.index("-") |
| 272 |
|
| 273 |
Bundler.ui.error "You have specified a gem name which does not conform to the \n" \ |
| 274 |
"naming guidelines for C extensions. For more information, \n" \ |
| 275 |
"see the 'Extension Naming' section at the following URL:\n" \ |
| 276 |
"https://guides.rubygems.org/gems-with-extensions/\n" |
| 277 |
exit 1 |
| 278 |
end |
| 279 |
|
| 280 |
def ask_and_set_test_framework |
| 281 |
return if skip?(:test) |
| 282 |
test_framework = options[:test] || Bundler.settings["gem.test"] |
| 283 |
|
| 284 |
if test_framework.to_s.empty? |
| 285 |
Bundler.ui.confirm "Do you want to generate tests with your gem?" |
| 286 |
Bundler.ui.info hint_text("test") |
| 287 |
|
| 288 |
result = Bundler.ui.ask "Enter a test framework. rspec/minitest/test-unit/(none):" |
| 289 |
if /rspec|minitest|test-unit/.match?(result) |
| 290 |
test_framework = result |
| 291 |
else |
| 292 |
test_framework = false |
| 293 |
end |
| 294 |
end |
| 295 |
|
| 296 |
if Bundler.settings["gem.test"].nil? |
| 297 |
Bundler.settings.set_global("gem.test", test_framework) |
| 298 |
end |
| 299 |
|
| 300 |
if options[:test] == Bundler.settings["gem.test"] |
| 301 |
Bundler.ui.info "#{options[:test]} is already configured, ignoring --test flag." |
| 302 |
end |
| 303 |
|
| 304 |
test_framework |
| 305 |
end |
| 306 |
|
| 307 |
def skip?(option) |
| 308 |
options.key?(option) && options[option].nil? |
| 309 |
end |
| 310 |
|
| 311 |
def hint_text(setting) |
| 312 |
if Bundler.settings["gem.#{setting}"] == false |
| 313 |
"Your choice will only be applied to this gem." |
| 314 |
else |
| 315 |
"Future `bundle gem` calls will use your choice. " \ |
| 316 |
"This setting can be changed anytime with `bundle config gem.#{setting}`." |
| 317 |
end |
| 318 |
end |
| 319 |
|
| 320 |
def ask_and_set_ci |
| 321 |
return if skip?(:ci) |
| 322 |
ci_template = options[:ci] || Bundler.settings["gem.ci"] |
| 323 |
|
| 324 |
if ci_template.to_s.empty? |
| 325 |
Bundler.ui.confirm "Do you want to set up continuous integration for your gem? " \ |
| 326 |
"Supported services:\n" \ |
| 327 |
"* CircleCI: https://circleci.com/\n" \ |
| 328 |
"* GitHub Actions: https://github.com/features/actions\n" \ |
| 329 |
"* GitLab CI: https://docs.gitlab.com/ee/ci/\n" \ |
| 330 |
"\n" |
| 331 |
Bundler.ui.info hint_text("ci") |
| 332 |
|
| 333 |
result = Bundler.ui.ask "Enter a CI service. github/gitlab/circle/(none):" |
| 334 |
if /github|gitlab|circle/.match?(result) |
| 335 |
ci_template = result |
| 336 |
else |
| 337 |
ci_template = false |
| 338 |
end |
| 339 |
end |
| 340 |
|
| 341 |
if Bundler.settings["gem.ci"].nil? |
| 342 |
Bundler.settings.set_global("gem.ci", ci_template) |
| 343 |
end |
| 344 |
|
| 345 |
if options[:ci] == Bundler.settings["gem.ci"] |
| 346 |
Bundler.ui.info "#{options[:ci]} is already configured, ignoring --ci flag." |
| 347 |
end |
| 348 |
|
| 349 |
ci_template |
| 350 |
end |
| 351 |
|
| 352 |
def ask_and_set_linter |
| 353 |
return if skip?(:linter) |
| 354 |
linter_template = options[:linter] || Bundler.settings["gem.linter"] |
| 355 |
linter_template = deprecated_rubocop_option if linter_template.nil? |
| 356 |
|
| 357 |
if linter_template.to_s.empty? |
| 358 |
Bundler.ui.confirm "Do you want to add a code linter and formatter to your gem? " \ |
| 359 |
"Supported Linters:\n" \ |
| 360 |
"* RuboCop: https://rubocop.org\n" \ |
| 361 |
"* Standard: https://github.com/standardrb/standard\n" \ |
| 362 |
"\n" |
| 363 |
Bundler.ui.info hint_text("linter") |
| 364 |
|
| 365 |
result = Bundler.ui.ask "Enter a linter. rubocop/standard/(none):" |
| 366 |
if /rubocop|standard/.match?(result) |
| 367 |
linter_template = result |
| 368 |
else |
| 369 |
linter_template = false |
| 370 |
end |
| 371 |
end |
| 372 |
|
| 373 |
if Bundler.settings["gem.linter"].nil? |
| 374 |
Bundler.settings.set_global("gem.linter", linter_template) |
| 375 |
end |
| 376 |
|
| 377 |
# Once gem.linter safely set, unset the deprecated gem.rubocop |
| 378 |
unless Bundler.settings["gem.rubocop"].nil? |
| 379 |
Bundler.settings.set_global("gem.rubocop", nil) |
| 380 |
end |
| 381 |
|
| 382 |
if options[:linter] == Bundler.settings["gem.linter"] |
| 383 |
Bundler.ui.info "#{options[:linter]} is already configured, ignoring --linter flag." |
| 384 |
end |
| 385 |
|
| 386 |
linter_template |
| 387 |
end |
| 388 |
|
| 389 |
def deprecated_rubocop_option |
| 390 |
if !options[:rubocop].nil? |
| 391 |
if options[:rubocop] |
| 392 |
Bundler::SharedHelpers.major_deprecation 2, |
| 393 |
"--rubocop is deprecated, use --linter=rubocop", |
| 394 |
removed_message: "--rubocop has been removed, use --linter=rubocop" |
| 395 |
"rubocop" |
| 396 |
else |
| 397 |
Bundler::SharedHelpers.major_deprecation 2, |
| 398 |
"--no-rubocop is deprecated, use --linter", |
| 399 |
removed_message: "--no-rubocop has been removed, use --linter" |
| 400 |
false |
| 401 |
end |
| 402 |
elsif !Bundler.settings["gem.rubocop"].nil? |
| 403 |
Bundler::SharedHelpers.major_deprecation 2, |
| 404 |
"config gem.rubocop is deprecated; we've updated your config to use gem.linter instead", |
| 405 |
removed_message: "config gem.rubocop has been removed; we've updated your config to use gem.linter instead" |
| 406 |
Bundler.settings["gem.rubocop"] ? "rubocop" : false |
| 407 |
end |
| 408 |
end |
| 409 |
|
| 410 |
def bundler_dependency_version |
| 411 |
v = Gem::Version.new(Bundler::VERSION) |
| 412 |
req = v.segments[0..1] |
| 413 |
req << "a" if v.prerelease? |
| 414 |
req.join(".") |
| 415 |
end |
| 416 |
|
| 417 |
def ensure_safe_gem_name(name, constant_array) |
| 418 |
if /^\d/.match?(name) |
| 419 |
Bundler.ui.error "Invalid gem name #{name} Please give a name which does not start with numbers." |
| 420 |
exit 1 |
| 421 |
end |
| 422 |
|
| 423 |
constant_name = constant_array.join("::") |
| 424 |
|
| 425 |
existing_constant = constant_array.inject(Object) do |c, s| |
| 426 |
defined = begin |
| 427 |
c.const_defined?(s) |
| 428 |
rescue NameError |
| 429 |
Bundler.ui.error "Invalid gem name #{name} -- `#{constant_name}` is an invalid constant name" |
| 430 |
exit 1 |
| 431 |
end |
| 432 |
(defined && c.const_get(s)) || break |
| 433 |
end |
| 434 |
|
| 435 |
return unless existing_constant |
| 436 |
Bundler.ui.error "Invalid gem name #{name} constant #{constant_name} is already in use. Please choose another gem name." |
| 437 |
exit 1 |
| 438 |
end |
| 439 |
|
| 440 |
def open_editor(editor, file) |
| 441 |
thor.run(%(#{editor} "#{file}")) |
| 442 |
end |
| 443 |
|
| 444 |
def rust_builder_required_rubygems_version |
| 445 |
"3.3.11" |
| 446 |
end |
| 447 |
|
| 448 |
def required_ruby_version |
| 449 |
"3.0.0" |
| 450 |
end |
| 451 |
|
| 452 |
def rubocop_version |
| 453 |
"1.21" |
| 454 |
end |
| 455 |
|
| 456 |
def standard_version |
| 457 |
"1.3" |
| 458 |
end |
| 459 |
|
| 460 |
def validate_rust_builder_rubygems_version |
| 461 |
if Gem::Version.new(rust_builder_required_rubygems_version) > Gem.rubygems_version |
| 462 |
Bundler.ui.error "Your RubyGems version (#{Gem.rubygems_version}) is too old to build Rust extension. Please update your RubyGems using `gem update --system` or any other way and try again." |
| 463 |
exit 1 |
| 464 |
end |
| 465 |
end |
| 466 |
end |
| 467 |
end |