2018-11-02 19:07:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "pathname"
|
|
|
|
|
|
|
|
module Bundler
|
|
|
|
class CLI
|
|
|
|
Bundler.require_thor_actions
|
|
|
|
include Thor::Actions
|
|
|
|
end
|
|
|
|
|
|
|
|
class CLI::Gem
|
|
|
|
TEST_FRAMEWORK_VERSIONS = {
|
|
|
|
"rspec" => "3.0",
|
2019-04-14 02:01:35 -04:00
|
|
|
"minitest" => "5.0",
|
2020-05-08 01:19:04 -04:00
|
|
|
"test-unit" => "3.0",
|
2018-11-02 19:07:56 -04:00
|
|
|
}.freeze
|
|
|
|
|
|
|
|
attr_reader :options, :gem_name, :thor, :name, :target
|
|
|
|
|
|
|
|
def initialize(options, gem_name, thor)
|
|
|
|
@options = options
|
|
|
|
@gem_name = resolve_name(gem_name)
|
|
|
|
|
|
|
|
@thor = thor
|
|
|
|
thor.behavior = :invoke
|
|
|
|
thor.destination_root = nil
|
|
|
|
|
|
|
|
@name = @gem_name
|
|
|
|
@target = SharedHelpers.pwd.join(gem_name)
|
|
|
|
|
|
|
|
validate_ext_name if options[:ext]
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
Bundler.ui.confirm "Creating gem '#{name}'..."
|
|
|
|
|
|
|
|
underscored_name = name.tr("-", "_")
|
|
|
|
namespaced_path = name.tr("-", "/")
|
|
|
|
constant_name = name.gsub(/-[_-]*(?![_-]|$)/) { "::" }.gsub(/([_-]+|(::)|^)(.|$)/) { $2.to_s + $3.upcase }
|
|
|
|
constant_array = constant_name.split("::")
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
use_git = Bundler.git_present? && options[:git]
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
git_author_name = use_git ? `git config user.name`.chomp : ""
|
2021-04-14 23:47:04 -04:00
|
|
|
git_username = use_git ? `git config github.user`.chomp : ""
|
2021-02-01 10:17:16 -05:00
|
|
|
git_user_email = use_git ? `git config user.email`.chomp : ""
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2021-04-14 23:47:04 -04:00
|
|
|
github_username = if options[:github_username].nil?
|
|
|
|
git_username
|
|
|
|
elsif options[:github_username] == false
|
|
|
|
""
|
|
|
|
else
|
|
|
|
options[:github_username]
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
config = {
|
|
|
|
:name => name,
|
|
|
|
:underscored_name => underscored_name,
|
|
|
|
:namespaced_path => namespaced_path,
|
|
|
|
:makefile_path => "#{underscored_name}/#{underscored_name}",
|
|
|
|
:constant_name => constant_name,
|
|
|
|
:constant_array => constant_array,
|
|
|
|
:author => git_author_name.empty? ? "TODO: Write your name" : git_author_name,
|
|
|
|
:email => git_user_email.empty? ? "TODO: Write your email address" : git_user_email,
|
|
|
|
:test => options[:test],
|
|
|
|
:ext => options[:ext],
|
|
|
|
:exe => options[:exe],
|
|
|
|
:bundler_version => bundler_dependency_version,
|
2021-02-01 10:17:16 -05:00
|
|
|
:git => use_git,
|
2019-04-14 02:01:35 -04:00
|
|
|
:github_username => github_username.empty? ? "[USERNAME]" : github_username,
|
2021-02-01 10:17:16 -05:00
|
|
|
:required_ruby_version => Gem.ruby_version < Gem::Version.new("2.4.a") ? "2.3.0" : "2.4.0",
|
2018-11-02 19:07:56 -04:00
|
|
|
}
|
|
|
|
ensure_safe_gem_name(name, constant_array)
|
|
|
|
|
|
|
|
templates = {
|
2020-05-08 01:19:04 -04:00
|
|
|
"#{Bundler.preferred_gemfile_name}.tt" => Bundler.preferred_gemfile_name,
|
2018-11-02 19:07:56 -04:00
|
|
|
"lib/newgem.rb.tt" => "lib/#{namespaced_path}.rb",
|
|
|
|
"lib/newgem/version.rb.tt" => "lib/#{namespaced_path}/version.rb",
|
|
|
|
"newgem.gemspec.tt" => "#{name}.gemspec",
|
|
|
|
"Rakefile.tt" => "Rakefile",
|
|
|
|
"README.md.tt" => "README.md",
|
|
|
|
"bin/console.tt" => "bin/console",
|
2019-04-14 02:01:35 -04:00
|
|
|
"bin/setup.tt" => "bin/setup",
|
2018-11-02 19:07:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
executables = %w[
|
|
|
|
bin/console
|
|
|
|
bin/setup
|
|
|
|
]
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
templates.merge!("gitignore.tt" => ".gitignore") if use_git
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
if test_framework = ask_and_set_test_framework
|
|
|
|
config[:test] = test_framework
|
|
|
|
config[:test_framework_version] = TEST_FRAMEWORK_VERSIONS[test_framework]
|
|
|
|
|
|
|
|
case test_framework
|
|
|
|
when "rspec"
|
|
|
|
templates.merge!(
|
|
|
|
"rspec.tt" => ".rspec",
|
|
|
|
"spec/spec_helper.rb.tt" => "spec/spec_helper.rb",
|
|
|
|
"spec/newgem_spec.rb.tt" => "spec/#{namespaced_path}_spec.rb"
|
|
|
|
)
|
2020-05-08 01:19:04 -04:00
|
|
|
config[:test_task] = :spec
|
2018-11-02 19:07:56 -04:00
|
|
|
when "minitest"
|
|
|
|
templates.merge!(
|
2020-05-08 01:19:04 -04:00
|
|
|
"test/minitest/test_helper.rb.tt" => "test/test_helper.rb",
|
2021-01-03 20:11:34 -05:00
|
|
|
"test/minitest/test_newgem.rb.tt" => "test/test_#{namespaced_path}.rb"
|
2018-11-02 19:07:56 -04:00
|
|
|
)
|
2020-05-08 01:19:04 -04:00
|
|
|
config[:test_task] = :test
|
|
|
|
when "test-unit"
|
|
|
|
templates.merge!(
|
|
|
|
"test/test-unit/test_helper.rb.tt" => "test/test_helper.rb",
|
|
|
|
"test/test-unit/newgem_test.rb.tt" => "test/#{namespaced_path}_test.rb"
|
|
|
|
)
|
|
|
|
config[:test_task] = :test
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-07 22:41:42 -05:00
|
|
|
config[:ci] = ask_and_set_ci
|
|
|
|
case config[:ci]
|
|
|
|
when "github"
|
2020-06-16 06:29:57 -04:00
|
|
|
templates.merge!("github/workflows/main.yml.tt" => ".github/workflows/main.yml")
|
2020-03-07 22:41:42 -05:00
|
|
|
when "travis"
|
2020-06-16 06:29:57 -04:00
|
|
|
templates.merge!("travis.yml.tt" => ".travis.yml")
|
2020-03-07 22:41:42 -05:00
|
|
|
when "gitlab"
|
2020-06-16 06:29:57 -04:00
|
|
|
templates.merge!("gitlab-ci.yml.tt" => ".gitlab-ci.yml")
|
2020-03-07 22:41:42 -05:00
|
|
|
when "circle"
|
2020-06-16 06:29:57 -04:00
|
|
|
templates.merge!("circleci/config.yml.tt" => ".circleci/config.yml")
|
2020-06-11 08:54:04 -04:00
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
if ask_and_set(:mit, "Do you want to license your code permissively under the MIT license?",
|
|
|
|
"This means that any other developer or company will be legally allowed to use your code " \
|
|
|
|
"for free as long as they admit you created it. You can read more about the MIT license " \
|
|
|
|
"at https://choosealicense.com/licenses/mit.")
|
|
|
|
config[:mit] = true
|
|
|
|
Bundler.ui.info "MIT License enabled in config"
|
|
|
|
templates.merge!("LICENSE.txt.tt" => "LICENSE.txt")
|
|
|
|
end
|
|
|
|
|
|
|
|
if ask_and_set(:coc, "Do you want to include a code of conduct in gems you generate?",
|
|
|
|
"Codes of conduct can increase contributions to your project by contributors who " \
|
|
|
|
"prefer collaborative, safe spaces. You can read more about the code of conduct at " \
|
|
|
|
"contributor-covenant.org. Having a code of conduct means agreeing to the responsibility " \
|
|
|
|
"of enforcing it, so be sure that you are prepared to do that. Be sure that your email " \
|
|
|
|
"address is specified as a contact in the generated code of conduct so that people know " \
|
|
|
|
"who to contact in case of a violation. For suggestions about " \
|
|
|
|
"how to enforce codes of conduct, see https://bit.ly/coc-enforcement.")
|
|
|
|
config[:coc] = true
|
|
|
|
Bundler.ui.info "Code of conduct enabled in config"
|
|
|
|
templates.merge!("CODE_OF_CONDUCT.md.tt" => "CODE_OF_CONDUCT.md")
|
|
|
|
end
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
if ask_and_set(:changelog, "Do you want to include a changelog?",
|
|
|
|
"A changelog is a file which contains a curated, chronologically ordered list of notable " \
|
|
|
|
"changes for each version of a project. To make it easier for users and contributors to" \
|
|
|
|
" see precisely what notable changes have been made between each release (or version) of" \
|
|
|
|
" the project. Whether consumers or developers, the end users of software are" \
|
|
|
|
" human beings who care about what's in the software. When the software changes, people " \
|
|
|
|
"want to know why and how. see https://keepachangelog.com")
|
|
|
|
config[:changelog] = true
|
|
|
|
Bundler.ui.info "Changelog enabled in config"
|
|
|
|
templates.merge!("CHANGELOG.md.tt" => "CHANGELOG.md")
|
|
|
|
end
|
|
|
|
|
2021-04-14 23:47:04 -04:00
|
|
|
config[:linter] = ask_and_set_linter
|
|
|
|
case config[:linter]
|
|
|
|
when "rubocop"
|
|
|
|
config[:linter_version] = Gem.ruby_version < Gem::Version.new("2.4.a") ? "0.81.0" : "1.7"
|
2020-05-08 01:19:04 -04:00
|
|
|
Bundler.ui.info "RuboCop enabled in config"
|
2020-06-16 09:37:21 -04:00
|
|
|
templates.merge!("rubocop.yml.tt" => ".rubocop.yml")
|
2021-04-14 23:47:04 -04:00
|
|
|
when "standard"
|
|
|
|
config[:linter_version] = Gem.ruby_version < Gem::Version.new("2.4.a") ? "0.2.5" : "1.0"
|
|
|
|
Bundler.ui.info "Standard enabled in config"
|
|
|
|
templates.merge!("standard.yml.tt" => ".standard.yml")
|
2020-05-08 01:19:04 -04:00
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
templates.merge!("exe/newgem.tt" => "exe/#{name}") if config[:exe]
|
|
|
|
|
|
|
|
if options[:ext]
|
|
|
|
templates.merge!(
|
|
|
|
"ext/newgem/extconf.rb.tt" => "ext/#{name}/extconf.rb",
|
|
|
|
"ext/newgem/newgem.h.tt" => "ext/#{name}/#{underscored_name}.h",
|
|
|
|
"ext/newgem/newgem.c.tt" => "ext/#{name}/#{underscored_name}.c"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
if File.exist?(target) && !File.directory?(target)
|
|
|
|
Bundler.ui.error "Couldn't create a new gem named `#{gem_name}` because there's an existing file named `#{gem_name}`."
|
|
|
|
exit Bundler::BundlerError.all_errors[Bundler::GenericSystemCallError]
|
|
|
|
end
|
|
|
|
|
|
|
|
if use_git
|
|
|
|
Bundler.ui.info "Initializing git repo in #{target}"
|
|
|
|
`git init #{target}`
|
|
|
|
|
|
|
|
config[:git_default_branch] = File.read("#{target}/.git/HEAD").split("/").last.chomp
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
templates.each do |src, dst|
|
|
|
|
destination = target.join(dst)
|
2021-02-01 10:17:16 -05:00
|
|
|
thor.template("newgem/#{src}", destination, config)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
executables.each do |file|
|
2021-02-01 10:17:16 -05:00
|
|
|
path = target.join(file)
|
|
|
|
executable = (path.stat.mode | 0o111)
|
|
|
|
path.chmod(executable)
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
if use_git
|
2018-11-02 19:07:56 -04:00
|
|
|
Dir.chdir(target) do
|
|
|
|
`git add .`
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Open gemspec in editor
|
|
|
|
open_editor(options["edit"], target.join("#{name}.gemspec")) if options[:edit]
|
|
|
|
|
|
|
|
Bundler.ui.info "Gem '#{name}' was successfully created. " \
|
|
|
|
"For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html"
|
|
|
|
end
|
|
|
|
|
2020-10-15 00:20:25 -04:00
|
|
|
private
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
def resolve_name(name)
|
|
|
|
SharedHelpers.pwd.join(name).basename.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def ask_and_set(key, header, message)
|
|
|
|
choice = options[key]
|
|
|
|
choice = Bundler.settings["gem.#{key}"] if choice.nil?
|
|
|
|
|
|
|
|
if choice.nil?
|
|
|
|
Bundler.ui.confirm header
|
|
|
|
choice = Bundler.ui.yes? "#{message} y/(n):"
|
|
|
|
Bundler.settings.set_global("gem.#{key}", choice)
|
|
|
|
end
|
|
|
|
|
|
|
|
choice
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_ext_name
|
|
|
|
return unless gem_name.index("-")
|
|
|
|
|
|
|
|
Bundler.ui.error "You have specified a gem name which does not conform to the \n" \
|
|
|
|
"naming guidelines for C extensions. For more information, \n" \
|
|
|
|
"see the 'Extension Naming' section at the following URL:\n" \
|
2019-08-21 10:46:46 -04:00
|
|
|
"https://guides.rubygems.org/gems-with-extensions/\n"
|
2018-11-02 19:07:56 -04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def ask_and_set_test_framework
|
|
|
|
test_framework = options[:test] || Bundler.settings["gem.test"]
|
|
|
|
|
2020-04-24 04:00:59 -04:00
|
|
|
if test_framework.to_s.empty?
|
2018-11-02 19:07:56 -04:00
|
|
|
Bundler.ui.confirm "Do you want to generate tests with your gem?"
|
2020-05-30 05:06:57 -04:00
|
|
|
Bundler.ui.info hint_text("test")
|
|
|
|
|
2020-05-31 02:58:16 -04:00
|
|
|
result = Bundler.ui.ask "Enter a test framework. rspec/minitest/test-unit/(none):"
|
2020-05-08 01:19:04 -04:00
|
|
|
if result =~ /rspec|minitest|test-unit/
|
2018-11-02 19:07:56 -04:00
|
|
|
test_framework = result
|
|
|
|
else
|
|
|
|
test_framework = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if Bundler.settings["gem.test"].nil?
|
|
|
|
Bundler.settings.set_global("gem.test", test_framework)
|
|
|
|
end
|
|
|
|
|
2020-06-02 16:09:47 -04:00
|
|
|
if options[:test] == Bundler.settings["gem.test"]
|
|
|
|
Bundler.ui.info "#{options[:test]} is already configured, ignoring --test flag."
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
test_framework
|
|
|
|
end
|
|
|
|
|
2020-05-30 05:06:57 -04:00
|
|
|
def hint_text(setting)
|
|
|
|
if Bundler.settings["gem.#{setting}"] == false
|
2020-05-21 22:27:12 -04:00
|
|
|
"Your choice will only be applied to this gem."
|
|
|
|
else
|
2020-05-22 13:00:00 -04:00
|
|
|
"Future `bundle gem` calls will use your choice. " \
|
2020-05-30 05:06:57 -04:00
|
|
|
"This setting can be changed anytime with `bundle config gem.#{setting}`."
|
2020-05-21 22:27:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-11 08:54:04 -04:00
|
|
|
def ask_and_set_ci
|
|
|
|
ci_template = options[:ci] || Bundler.settings["gem.ci"]
|
|
|
|
|
2020-05-30 05:06:57 -04:00
|
|
|
if ci_template.to_s.empty?
|
2020-05-31 02:58:16 -04:00
|
|
|
Bundler.ui.confirm "Do you want to set up continuous integration for your gem? " \
|
|
|
|
"Supported services:\n" \
|
2020-05-28 05:48:15 -04:00
|
|
|
"* CircleCI: https://circleci.com/\n" \
|
|
|
|
"* GitHub Actions: https://github.com/features/actions\n" \
|
|
|
|
"* GitLab CI: https://docs.gitlab.com/ee/ci/\n" \
|
2020-03-07 22:41:42 -05:00
|
|
|
"* Travis CI: https://travis-ci.org/\n" \
|
|
|
|
"\n"
|
2020-05-30 05:06:57 -04:00
|
|
|
Bundler.ui.info hint_text("ci")
|
2020-03-07 22:41:42 -05:00
|
|
|
|
2020-05-31 02:58:16 -04:00
|
|
|
result = Bundler.ui.ask "Enter a CI service. github/travis/gitlab/circle/(none):"
|
2020-06-11 08:54:04 -04:00
|
|
|
if result =~ /github|travis|gitlab|circle/
|
|
|
|
ci_template = result
|
|
|
|
else
|
|
|
|
ci_template = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if Bundler.settings["gem.ci"].nil?
|
|
|
|
Bundler.settings.set_global("gem.ci", ci_template)
|
|
|
|
end
|
|
|
|
|
2020-06-01 20:59:53 -04:00
|
|
|
if options[:ci] == Bundler.settings["gem.ci"]
|
|
|
|
Bundler.ui.info "#{options[:ci]} is already configured, ignoring --ci flag."
|
|
|
|
end
|
|
|
|
|
2020-06-11 08:54:04 -04:00
|
|
|
ci_template
|
|
|
|
end
|
|
|
|
|
2021-04-14 23:47:04 -04:00
|
|
|
def ask_and_set_linter
|
|
|
|
linter_template = options[:linter] || Bundler.settings["gem.linter"]
|
|
|
|
linter_template = deprecated_rubocop_option if linter_template.nil?
|
|
|
|
|
|
|
|
if linter_template.to_s.empty?
|
|
|
|
Bundler.ui.confirm "Do you want to add a code linter and formatter to your gem? " \
|
|
|
|
"Supported Linters:\n" \
|
|
|
|
"* RuboCop: https://rubocop.org\n" \
|
|
|
|
"* Standard: https://github.com/testdouble/standard\n" \
|
|
|
|
"\n"
|
|
|
|
Bundler.ui.info hint_text("linter")
|
|
|
|
|
|
|
|
result = Bundler.ui.ask "Enter a linter. rubocop/standard/(none):"
|
|
|
|
if result =~ /rubocop|standard/
|
|
|
|
linter_template = result
|
|
|
|
else
|
|
|
|
linter_template = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if Bundler.settings["gem.linter"].nil?
|
|
|
|
Bundler.settings.set_global("gem.linter", linter_template)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Once gem.linter safely set, unset the deprecated gem.rubocop
|
|
|
|
unless Bundler.settings["gem.rubocop"].nil?
|
|
|
|
Bundler.settings.set_global("gem.rubocop", nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:linter] == Bundler.settings["gem.linter"]
|
|
|
|
Bundler.ui.info "#{options[:linter]} is already configured, ignoring --linter flag."
|
|
|
|
end
|
|
|
|
|
|
|
|
linter_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def deprecated_rubocop_option
|
|
|
|
if !options[:rubocop].nil?
|
|
|
|
if options[:rubocop]
|
|
|
|
Bundler::SharedHelpers.major_deprecation 2, "--rubocop is deprecated, use --linter=rubocop"
|
|
|
|
"rubocop"
|
|
|
|
else
|
|
|
|
Bundler::SharedHelpers.major_deprecation 2, "--no-rubocop is deprecated, use --linter"
|
|
|
|
false
|
|
|
|
end
|
|
|
|
elsif !Bundler.settings["gem.rubocop"].nil?
|
|
|
|
Bundler::SharedHelpers.major_deprecation 2,
|
|
|
|
"config gem.rubocop is deprecated; we've updated your config to use gem.linter instead"
|
|
|
|
Bundler.settings["gem.rubocop"] ? "rubocop" : false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def bundler_dependency_version
|
|
|
|
v = Gem::Version.new(Bundler::VERSION)
|
|
|
|
req = v.segments[0..1]
|
|
|
|
req << "a" if v.prerelease?
|
|
|
|
req.join(".")
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_safe_gem_name(name, constant_array)
|
|
|
|
if name =~ /^\d/
|
|
|
|
Bundler.ui.error "Invalid gem name #{name} Please give a name which does not start with numbers."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
constant_name = constant_array.join("::")
|
|
|
|
|
|
|
|
existing_constant = constant_array.inject(Object) do |c, s|
|
|
|
|
defined = begin
|
|
|
|
c.const_defined?(s)
|
|
|
|
rescue NameError
|
|
|
|
Bundler.ui.error "Invalid gem name #{name} -- `#{constant_name}` is an invalid constant name"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
(defined && c.const_get(s)) || break
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless existing_constant
|
|
|
|
Bundler.ui.error "Invalid gem name #{name} constant #{constant_name} is already in use. Please choose another gem name."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def open_editor(editor, file)
|
|
|
|
thor.run(%(#{editor} "#{file}"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|