class GemPolish::CLI::Polisher

Constants

BADGE_NAMES
TEMPLATE_DIR

Public Class Methods

new(options, thor) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 5
def initialize(options, thor)
  @options = options
  @thor = thor
  @defaults = set_defaults
end

Public Instance Methods

git_user() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 49
def git_user
  user = @options[:git_user] || read_from_git_config
  user.empty? ? "TODO: Write your name" : user
end
insert_badges() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 22
def insert_badges
  if badges
    @thor.insert_into_file(readme, after: /^#\s.*\n/, force: false) { badge_links }
  end
end
insert_coveralls() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 28
def insert_coveralls
  if parse_opt(:coveralls)
    @thor.prepend_file(spec_helper, template(:coveralls) + "\n")
    add_dev_dependency('simplecov', '0.7')
    @thor.append_file(gemfile, %{gem 'coveralls', require: false})
  end
end
insert_description() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 15
def insert_description
  if description
    @thor.gsub_file(gemspec, /TODO:.*summary.*(?=})/, description)
    @thor.gsub_file(readme, /TODO:.*gem description/, description)
  end
end
insert_rspec_conf() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 36
def insert_rspec_conf
  if parse_opt(:rspec_conf)
    @thor.append_file(spec_helper, "\n" + template(:rspec_configuration))
  end
end
insert_travis() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 42
def insert_travis
  if t = parse_opt(:travis)
    @thor.say_status :rewrite, relative_destination(travis)
    File.write(travis, YAML.dump(travis_content(t)))
  end
end
set_defaults() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 11
def set_defaults
  defaults_disabled? ? {} : read_from_conf_file
end

Private Instance Methods

add_dev_dependency(gem, version = nil) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 108
def add_dev_dependency(gem, version = nil)
  total_size = File.size(gemspec)
  pos_before_end = total_size - 4
  insertion = %{  spec.add_development_dependency "#{gem}"}
  return if File.read(gemspec).match(/#{insertion}/)
  insertion << %{, "~> #{version}"} if version

  @thor.say_status :append, relative_destination(gemspec)
  File.open(gemspec, 'r+') do |file|
    file.seek(pos_before_end, IO::SEEK_SET)
    file.puts(insertion)
    file.puts('end')
  end
end
badges() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 68
def badges
  @badges ||= parse_opt(:badges)
end
conf_file() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 138
def conf_file
  "#{ENV['HOME']}/.gem_polish.yml"
end
conf_file_present?() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 98
def conf_file_present?
  File.exist?(conf_file)
end
defaults_disabled?() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 94
def defaults_disabled?
  @options[:no_default]
end
description() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 56
def description
  @description ||= @options[:description]
end
gem_name() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 142
def gem_name
  File.basename(Dir.pwd)
end
gemfile() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 158
def gemfile
  "Gemfile"
end
gemspec() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 154
def gemspec
  "#{gem_name}.gemspec"
end
load_conf_file() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 102
def load_conf_file
  YAML.load(File.read(conf_file)).each_with_object({}) do |(k, v), h|
    h[k.to_sym] = v
  end
end
parse_opt(opt) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 86
def parse_opt(opt)
  @options[opt] || @defaults[opt]
end
read_from_conf_file() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 90
def read_from_conf_file
  conf_file_present? ? load_conf_file : {}
end
read_from_git_config() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 81
def read_from_git_config
  # can it return nil?
  `git config user.name`.to_s.chomp
end
readme() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 162
def readme
  "README.md"
end
relative_destination(dest) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 128
def relative_destination(dest)
  d = File.expand_path(dest, @thor.destination_root)
  @thor.relative_to_original_destination_root(d)
end
spec_helper() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 150
def spec_helper
  "spec/spec_helper.rb"
end
template(name) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 134
def template(name)
  File.read("#{TEMPLATE_DIR}/#{name}.template")
end
travis() click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 146
def travis
  ".travis.yml"
end
travis_content(opts) click to toggle source
# File lib/gem_polish/cli/polisher.rb, line 123
def travis_content(opts)
  c = { 'language' => 'ruby'}
  c.merge((opts.is_a?(Hash) ? opts : { 'rvm' => opts }))
end