class GemBootstrap::CLI

@api private

Constants

GEM_NAME_NOT_AVAILABLE
MISSING_GH_USERNAME
RUBYGEMS_URI

Public Class Methods

new(io: Io.new) click to toggle source

@param [Io] io

# File lib/gem-bootstrap/cli.rb, line 20
def initialize(io: Io.new)
  @io = io
  @shell = ShellHelper.new(io: io)
  @gh = GitHubHelper.new
end

Public Instance Methods

run(args) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 26
def run(args)
  catch(:exit) do
    options = OptionParser.new.parse(args)
    config = build_config(options)
    generate_src(config)

    throw :exit, 0 if options.source?

    bundle_install(config)
    ensure_gem_name_available(config)
    setup_github_repo(config)

    0
  end
end

Private Instance Methods

apply_github_username(options) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 61
def apply_github_username(options)
  options[:github_username] ||= github_username
end
build_config(options) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 44
def build_config(options)
  display_help(options, exit_code: 0) if options.help?
  display_help(options, exit_code: 1) if invalid_options?(options)
  apply_github_username(options)
  Configuration.build(options)
end
bundle_install(config) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 117
def bundle_install(config)
  Dir.chdir(config.gem_name) do
    sh('gem install bundler')
    sh('bundle install')
    sh('rake test')
  end
end
display_help(options, exit_code: 0) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 56
def display_help(options, exit_code: 0)
  @io.puts(options)
  throw :exit, exit_code
end
ensure_gem_name_available(config) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 86
def ensure_gem_name_available(config)
  # check to see if the gem name is available on RubyGems.org
  uri = URI.parse(format(RUBYGEMS_URI, config.gem_name))
  if Net::HTTP.get_response(uri).code.to_i != 404
    error_with(format(GEM_NAME_NOT_AVAILABLE, config.gem_name))
  end
end
error_with(msg) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 71
def error_with(msg)
  @io.warn(msg)
  throw :exit, 1
end
generate_src(config) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 80
def generate_src(config)
  mkdir(config.gem_name)
  builder = Builder.new(config: config, io: @io)
  builder.generate_src(dir: config.gem_name)
end
git_init_commit_push(config) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 105
def git_init_commit_push(config)
  Dir.chdir(config.gem_name) do
    sh('git init .')
    sh("git config user.name #{config.author_name.inspect}")
    sh("git config user.email #{config.author_email.inspect}")
    sh('git add .')
    sh('git commit -m "Add basic test, build, and release scripts"')
    sh("git remote add origin git@github.com:#{config.github_repo}.git")
    sh('git push -u origin master')
  end
end
github_username() click to toggle source
# File lib/gem-bootstrap/cli.rb, line 65
def github_username
  @gh.github_username
rescue StandardError
  error_with(MISSING_GH_USERNAME)
end
invalid_options?(options) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 51
def invalid_options?(options)
  # TODO : there must be a single argument and it must be a valid gem-name
  options.arguments.size != 1
end
mkdir(dir) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 76
def mkdir(dir)
  FileUtils.mkdir(dir) unless File.exist?(dir)
end
setup_github_repo(config) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 94
def setup_github_repo(config)
  @gh.create_repo(
    name: config.gem_name,
    description: config.project_summary,
    homepage: config.github_url
  )
  git_init_commit_push(config)
rescue StandardError => e
  error_with("Failed to create github repository: #{e.message}")
end
sh(command) click to toggle source
# File lib/gem-bootstrap/cli.rb, line 125
def sh(command)
  @shell.exec(command)
end