Skip to content

Instantly share code, notes, and snippets.

@apainintheneck
Created December 15, 2024 19:29
Show Gist options
  • Select an option

  • Save apainintheneck/539331dc5c8b6aaae312cd2c6b4353f4 to your computer and use it in GitHub Desktop.

Select an option

Save apainintheneck/539331dc5c8b6aaae312cd2c6b4353f4 to your computer and use it in GitHub Desktop.
A simple CLI for interactively creating a new gem with `bundler`
#!/usr/bin/env ruby
#
# $ ./newgem example
# Add binary executable? no
# Add code of conduct file? no
# Add MIT license? Yes
# Add native extension? (none)
# Add test library? rspec
# Add linter library? standard
# Add continuous integration config? (none)
# $ bundler gem example --no-exe --no-coc --mit --no-ext --test=rspec --linter=standard --no-ci
# Creating gem 'example'...
# MIT License enabled in config
# Changelog enabled in config
# Standard enabled in config
# Initializing git repo in /Users/kevinrobell/Desktop/example
# create example/Gemfile
# create example/lib/example.rb
# create example/lib/example/version.rb
# create example/sig/example.rbs
# create example/example.gemspec
# create example/Rakefile
# create example/README.md
# create example/bin/console
# create example/bin/setup
# create example/.gitignore
# create example/.rspec
# create example/spec/spec_helper.rb
# create example/spec/example_spec.rb
# create example/LICENSE.txt
# create example/CHANGELOG.md
# create example/.standard.yml
# Gem 'example' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
USAGE = <<~USAGE
usage: newgem GEM_NAME
Interactive CLI to create a new gem with `bundler`.
USAGE
case ARGV.size
when 0
puts USAGE
exit 0
when 1
GEM_NAME = ARGV.first
if GEM_NAME.empty?
puts USAGE
puts
puts "Error: empty gem name"
exit 1
end
else
puts USAGE
exit 1
end
require "English"
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "tty-prompt"
end
flags = []
prompt = TTY::Prompt.new
if prompt.yes?("Add binary executable?")
flags << "--exe"
else
flags << "--no-exe"
end
if prompt.yes?("Add code of conduct file?")
flags << "--coc"
else
flags << "--no-coc"
end
if prompt.yes?("Add MIT license?")
flags << "--mit"
else
flags << "--no-mit"
end
case prompt.select("Add native extension?", %w[c rust (none)])
when "c"
flags << "--ext=c"
when "rust"
flags << "--ext=rust"
else
flags << "--no-ext"
end
case prompt.select("Add test library?", %w[rspec minitest test-unit (none)])
when "rspec"
flags << "--test=rspec"
when "minitest"
flags << "--test=minitest"
when "test-unit"
flags << "--test=test-unit"
else
flags << "--no-test"
end
case prompt.select("Add linter library?", %w[rubocop standard (none)])
when "rubocop"
flags << "--linter=rubocop"
when "standard"
flags << "--linter=standard"
else
flags << "--no-linter"
end
case prompt.select("Add continuous integration config?", %w[github gitlab circle (none)])
when "github"
flags << "--ci=github"
when "gitlab"
flags << "--ci=gitlab"
when "circle"
flags << "--ci=circle"
else
flags << "--no-ci"
end
command = ["bundler", "gem", GEM_NAME, *flags]
puts "$ #{command.join(" ")}"
system(*command)
exit $CHILD_STATUS.exitstatus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment