diff --git a/Gemfile b/Gemfile index 3b77a68..2b5109a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'https://rubygems.org' +source "https://rubygems.org" # Specify your gem's dependencies in raygun.gemspec gemspec diff --git a/Rakefile b/Rakefile index c702cfc..2995527 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1 @@ -require 'bundler/gem_tasks' +require "bundler/gem_tasks" diff --git a/lib/colorize.rb b/lib/colorize.rb index 714eaf4..f8f1180 100644 --- a/lib/colorize.rb +++ b/lib/colorize.rb @@ -5,41 +5,40 @@ # want to add a gem dependency. class String - # # Colors Hash # COLORS = { - :black => 0, - :red => 1, - :green => 2, - :yellow => 3, - :blue => 4, - :magenta => 5, - :cyan => 6, - :white => 7, - :default => 9, - - :light_black => 10, - :light_red => 11, - :light_green => 12, - :light_yellow => 13, - :light_blue => 14, - :light_magenta => 15, - :light_cyan => 16, - :light_white => 17 + black: 0, + red: 1, + green: 2, + yellow: 3, + blue: 4, + magenta: 5, + cyan: 6, + white: 7, + default: 9, + + light_black: 10, + light_red: 11, + light_green: 12, + light_yellow: 13, + light_blue: 14, + light_magenta: 15, + light_cyan: 16, + light_white: 17 } # # Modes Hash # MODES = { - :default => 0, # Turn off all attributes + default: 0, # Turn off all attributes #:bright => 1, # Set bright mode - :underline => 4, # Set underline mode - :blink => 5, # Set blink mode - :swap => 7, # Exchange foreground and background colors - :hide => 8 # Hide text (foreground color would be the same as background) + underline: 4, # Set underline mode + blink: 5, # Set blink mode + swap: 7, # Exchange foreground and background colors + hide: 8 # Hide text (foreground color would be the same as background) } protected @@ -47,15 +46,13 @@ class String # # Set color values in new string intance # - def set_color_parameters( params ) - if (params.instance_of?(Hash)) + def set_color_parameters(params) + if params.instance_of?(Hash) @color = params[:color] @background = params[:background] @mode = params[:mode] @uncolorized = params[:uncolorized] self - else - nil end end @@ -77,22 +74,22 @@ def set_color_parameters( params ) # puts "This is blue text on red".blue.on_red.blink # puts "This is uncolorized".blue.on_red.uncolorize # - def colorize( params ) + def colorize(params) return self unless STDOUT.isatty begin - require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/ + require "Win32/Console/ANSI" if RUBY_PLATFORM.match?(/win32/) rescue LoadError - raise 'You must gem install win32console to use colorize on Windows' + raise "You must gem install win32console to use colorize on Windows" end color_parameters = {} - if (params.instance_of?(Hash)) + if params.instance_of?(Hash) color_parameters[:color] = COLORS[params[:color]] color_parameters[:background] = COLORS[params[:background]] color_parameters[:mode] = MODES[params[:mode]] - elsif (params.instance_of?(Symbol)) + elsif params.instance_of?(Symbol) color_parameters[:color] = COLORS[params] end @@ -100,14 +97,14 @@ def colorize( params ) color_parameters[:background] ||= @background ||= COLORS[:default] color_parameters[:mode] ||= @mode ||= MODES[:default] - color_parameters[:uncolorized] ||= @uncolorized ||= self.dup + color_parameters[:uncolorized] ||= @uncolorized ||= dup # calculate bright mode color_parameters[:color] += 50 if color_parameters[:color] > 10 color_parameters[:background] += 50 if color_parameters[:background] > 10 - "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters ) + "\033[#{color_parameters[:mode]};#{color_parameters[:color] + 30};#{color_parameters[:background] + 40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters(color_parameters) end # @@ -127,37 +124,36 @@ def colorized? # # Make some color and on_color methods # - COLORS.each_key do | key | + COLORS.each_key do |key| next if key == :default define_method key do - self.colorize( :color => key ) + colorize(color: key) end define_method "on_#{key}" do - self.colorize( :background => key ) + colorize(background: key) end end # # Methods for modes # - MODES.each_key do | key | + MODES.each_key do |key| next if key == :default define_method key do - self.colorize( :mode => key ) + colorize(mode: key) end end class << self - # # Return array of available modes used by colorize method # def modes keys = [] - MODES.each_key do | key | + MODES.each_key do |key| keys << key end keys @@ -168,7 +164,7 @@ def modes # def colors keys = [] - COLORS.each_key do | key | + COLORS.each_key do |key| keys << key end keys @@ -177,16 +173,16 @@ def colors # # Display color matrix with color names. # - def color_matrix( txt = "[X]" ) + def color_matrix(txt = "[X]") size = String.colors.length - String.colors.each do | color | - String.colors.each do | back | - print txt.colorize( :color => color, :background => back ) + String.colors.each do |color| + String.colors.each do |back| + print txt.colorize(color: color, background: back) end puts " < #{color}" end - String.colors.reverse.each_with_index do | back, index | - puts "#{"|".rjust(txt.length)*(size-index)} < #{back}" + String.colors.reverse.each_with_index do |back, index| + puts "#{"|".rjust(txt.length) * (size - index)} < #{back}" end "" end diff --git a/lib/raygun/raygun.rb b/lib/raygun/raygun.rb index 45f6511..124b77a 100644 --- a/lib/raygun/raygun.rb +++ b/lib/raygun/raygun.rb @@ -1,17 +1,17 @@ -require 'optparse' -require 'ostruct' -require 'fileutils' -require 'securerandom' -require 'net/http' -require 'json' -require 'colorize' +require "optparse" +require "ostruct" +require "fileutils" +require "securerandom" +require "net/http" +require "json" +require "colorize" -require_relative 'version' -require_relative 'template_repo' +require_relative "version" +require_relative "template_repo" module Raygun class Runner - CARBONFIVE_REPO = 'carbonfive/raygun-rails' + CARBONFIVE_REPO = "carbonfive/raygun-rails" attr_accessor :target_dir, :app_dir, :app_name, :dash_name, :snake_name, :camel_name, :title_name, :prototype_repo, :current_ruby_version, :current_ruby_patch_level @@ -19,18 +19,18 @@ class Runner def initialize(target_dir, prototype_repo) @target_dir = target_dir @app_dir = File.expand_path(target_dir.strip.to_s) - @app_name = File.basename(app_dir).gsub(/\s+/, '-') - @dash_name = app_name.gsub('_', '-') - @snake_name = app_name.gsub('-', '_') + @app_name = File.basename(app_dir).gsub(/\s+/, "-") + @dash_name = app_name.tr("_", "-") + @snake_name = app_name.tr("-", "_") @camel_name = camelize(snake_name) @title_name = titleize(snake_name) @prototype_repo = prototype_repo @current_ruby_version = RUBY_VERSION - @current_ruby_patch_level = if RUBY_VERSION < '2.1.0' # Ruby adopted semver starting with 2.1.0. - "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" + @current_ruby_patch_level = if RUBY_VERSION < "2.1.0" # Ruby adopted semver starting with 2.1.0. + "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" else - "#{RUBY_VERSION}" + RUBY_VERSION.to_s end end @@ -55,17 +55,17 @@ def fetch_prototype $stdout.flush cached_prototypes_dir = File.join(Dir.home, ".raygun") - @prototype = "#{cached_prototypes_dir}/#{name.gsub('/','--')}-#{sha}.tar.gz" + @prototype = "#{cached_prototypes_dir}/#{name.gsub("/", "--")}-#{sha}.tar.gz" # Do we already have the tarball cached under ~/.raygun? - if File.exists?(@prototype) + if File.exist?(@prototype) puts " Using cached version.".colorize(:yellow) else print " Downloading...".colorize(:yellow) $stdout.flush # Download the tarball and install in the cache. - Dir.mkdir(cached_prototypes_dir, 0755) unless Dir.exists?(cached_prototypes_dir) + Dir.mkdir(cached_prototypes_dir, 0o755) unless Dir.exist?(cached_prototypes_dir) shell "curl -s -L #{tarball_url} -o #{@prototype}" puts " done!".colorize(:yellow) end @@ -73,8 +73,8 @@ def fetch_prototype def check_raygun_version required_raygun_version = - %x{tar xfz #{@prototype} --include "*.raygun-version" -O 2> /dev/null}.chomp || - ::Raygun::VERSION + `tar xfz #{@prototype} --include "*.raygun-version" -O 2> /dev/null`.chomp || + ::Raygun::VERSION if Gem::Version.new(required_raygun_version) > Gem::Version.new(::Raygun::VERSION) puts "" @@ -82,7 +82,7 @@ def check_raygun_version print " This version of the raygun gem (".colorize(:light_red) print "#{::Raygun::VERSION})".colorize(:white) print " is too old to generate this application (needs ".colorize(:light_red) - print "#{required_raygun_version}".colorize(:white) + print required_raygun_version.to_s.colorize(:white) puts " or newer).".colorize(:light_red) puts "" print "Please update the gem by running ".colorize(:light_red) @@ -101,7 +101,7 @@ def copy_prototype # Github includes an extra directory layer in the tag tarball. extraneous_dir = Dir.glob("#{app_dir}/*").first dirs_to_move = Dir.glob("#{extraneous_dir}/*", File::FNM_DOTMATCH) - .reject { |d| %w{. ..}.include?(File.basename(d)) } + .reject { |d| %w[. ..].include?(File.basename(d)) } FileUtils.mv dirs_to_move, app_dir FileUtils.remove_dir extraneous_dir @@ -110,15 +110,15 @@ def copy_prototype def rename_new_app Dir.chdir(app_dir) do { - 'AppPrototype' => camel_name, - 'app-prototype' => dash_name, - 'app_prototype' => snake_name, - 'App Prototype' => title_name + "AppPrototype" => camel_name, + "app-prototype" => dash_name, + "app_prototype" => snake_name, + "App Prototype" => title_name }.each do |proto_name, new_name| shell "find . -type f -print | xargs #{sed_i} 's/#{proto_name}/#{new_name}/g'" end - %w(d f).each do |find_type| + %w[d f].each do |find_type| shell "find . -depth -type #{find_type} -name '*app_prototype*' -exec bash -c 'mv $0 ${0/app_prototype/#{snake_name}}' {} \\;" end end @@ -155,12 +155,12 @@ def initialize_git end def print_plan - puts ' ____ '.colorize(:light_yellow) + puts " ____ ".colorize(:light_yellow) puts ' / __ \____ ___ ______ ___ ______ '.colorize(:light_yellow) puts ' / /_/ / __ `/ / / / __ `/ / / / __ \ '.colorize(:light_yellow) - puts ' / _, _/ /_/ / /_/ / /_/ / /_/ / / / / '.colorize(:light_yellow) + puts " / _, _/ /_/ / /_/ / /_/ / /_/ / / / / ".colorize(:light_yellow) puts ' /_/ |_|\__,_/\__, /\__, /\__,_/_/ /_/ '.colorize(:light_yellow) - puts ' /____//____/ '.colorize(:light_yellow) + puts " /____//____/ ".colorize(:light_yellow) puts puts "Raygun will create new app in directory:".colorize(:yellow) + " #{target_dir}".colorize(:yellow) + "...".colorize(:yellow) puts @@ -210,29 +210,29 @@ def print_next_steps_for_custom_repo def camelize(string) result = string.sub(/^[a-z\d]*/) { $&.capitalize } - result.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" } + result.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" } end def titleize(underscored_string) - result = underscored_string.gsub(/_/, ' ') - result.gsub(/\b('?[a-z])/) { $1.capitalize } + result = underscored_string.tr("_", " ") + result.gsub(/\b('?[a-z])/) { Regexp.last_match(1).capitalize } end # Distinguish BSD vs GNU sed with the --version flag (only present in GNU sed). def sed_i @sed_format ||= begin - %x{sed --version &> /dev/null} - $?.success? ? "sed -i" : "sed -i ''" + `sed --version &> /dev/null` + $CHILD_STATUS.success? ? "sed -i" : "sed -i ''" end end # Run a shell command and raise an exception if it fails. def shell(command) - %x{#{command}} - raise "#{command} failed with status #{$?.exitstatus}." unless $?.success? + `#{command}` + raise "#{command} failed with status #{$CHILD_STATUS.exitstatus}." unless $CHILD_STATUS.success? end - def self.parse(args) + def self.parse(_args) raygun = nil options = OpenStruct.new @@ -242,14 +242,14 @@ def self.parse(args) parser = OptionParser.new do |opts| opts.banner = "Usage: raygun [options] NEW_APP_DIRECTORY" - opts.on('-h', '--help', "Show raygun usage") do + opts.on("-h", "--help", "Show raygun usage") do usage_and_exit(opts) end - opts.on('-p', '--prototype [github_repo]', "Prototype github repo (e.g. carbonfive/raygun-rails).") do |prototype| + opts.on("-p", "--prototype [github_repo]", "Prototype github repo (e.g. carbonfive/raygun-rails).") do |prototype| options.prototype_repo = prototype end - opts.on('-v', '--version', 'Print the version number') do + opts.on("-v", "--version", "Print the version number") do puts Raygun::VERSION exit 1 end @@ -262,7 +262,6 @@ def self.parse(args) raise OptionParser::InvalidOption if options.target_dir.nil? raygun = Raygun::Runner.new(options.target_dir, options.prototype_repo) - rescue OptionParser::InvalidOption usage_and_exit(parser) end diff --git a/lib/raygun/template_repo.rb b/lib/raygun/template_repo.rb index 1dd617b..fbe4938 100644 --- a/lib/raygun/template_repo.rb +++ b/lib/raygun/template_repo.rb @@ -3,7 +3,7 @@ class TemplateRepo attr_reader :name, :branch, :tarball, :sha def initialize(repo) - @name, @branch = repo.split('#').map(&:strip) + @name, @branch = repo.split("#").map(&:strip) fetch end @@ -11,6 +11,7 @@ def initialize(repo) def fetch return if @branch && @sha + @branch ? fetch_branches : fetch_tags end @@ -19,12 +20,12 @@ def handle_github_error(response) print "Whoops - need to try again!".colorize(:red) puts "" print "We could not find (".colorize(:light_red) - print "#{name}".colorize(:white) + print name.to_s.colorize(:white) print "##{branch}".colorize(:white) if @branch print ") on github.".colorize(:light_red) puts "" print "The response from github was a (".colorize(:light_red) - print "#{response.code}".colorize(:white) + print response.code.to_s.colorize(:white) puts ") which I'm sure you can fix right up!".colorize(:light_red) puts "" exit 1 @@ -35,7 +36,7 @@ def handle_missing_tag_error print "Whoops - need to try again!".colorize(:red) puts "" print "We could not find any tags in the repo (".colorize(:light_red) - print "#{name}".colorize(:white) + print name.to_s.colorize(:white) print ") on github.".colorize(:light_red) puts "" print "Raygun uses the 'largest' tag in a repository, where tags are sorted alphanumerically.".colorize(:light_red) @@ -50,8 +51,8 @@ def fetch_branches response = http_get("https://api.github.com/repos/#{name}/branches/#{branch}") handle_github_error(response) unless response.code == "200" result = JSON.parse(response.body) - @sha = result['commit']['sha'] - @tarball = result['_links']['html'].gsub(%r(/tree/#{branch}), "/archive/#{branch}.tar.gz") + @sha = result["commit"]["sha"] + @tarball = result["_links"]["html"].gsub(%r{/tree/#{branch}}, "/archive/#{branch}.tar.gz") end def fetch_tags @@ -60,8 +61,8 @@ def fetch_tags result = JSON.parse(response.body).first handle_missing_tag_error unless result - @sha = result['commit']['sha'] - @tarball = result['tarball_url'] + @sha = result["commit"]["sha"] + @tarball = result["tarball_url"] end def http_get(url) @@ -70,7 +71,7 @@ def http_get(url) http.use_ssl = true request = Net::HTTP::Get.new(URI.encode(url)) - response = http.request(request) + response = http.request(request) end end end diff --git a/raygun.gemspec b/raygun.gemspec index a317632..7c63440 100644 --- a/raygun.gemspec +++ b/raygun.gemspec @@ -1,22 +1,20 @@ -# -*- encoding: utf-8 -*- - -File.expand_path('../lib', __FILE__).tap do |lib| +File.expand_path("lib", __dir__).tap do |lib| $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) end -require 'raygun/version' +require "raygun/version" Gem::Specification.new do |gem| gem.name = "raygun" gem.version = Raygun::VERSION gem.authors = ["Christian Nelson", "Jonah Williams", "Jason Wadsworth"] gem.email = ["christian@carbonfive.com"] - gem.description = %q{Carbon Five Rails application generator} - gem.summary = %q{Generates and customizes Rails applications with Carbon Five best practices baked in.} + gem.description = "Carbon Five Rails application generator" + gem.summary = "Generates and customizes Rails applications with Carbon Five best practices baked in." gem.homepage = "https://github.com/carbonfive/raygun" - gem.license = 'MIT' + gem.license = "MIT" - gem.files = `git ls-files`.split($/) + gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"]