raygun 1.0.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1 +1,9 @@
1
- require 'bundler/gem_tasks'
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ RuboCop::RakeTask.new
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task default: %i[rubocop spec]
data/bin/raygun CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- File.expand_path('../../lib', __FILE__).tap do |lib|
3
+ File.expand_path("../lib", __dir__).tap do |lib|
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  end
6
6
 
7
- require 'raygun/raygun'
7
+ require "raygun/raygun"
8
8
 
9
9
  raygun = Raygun::Runner.parse(ARGV)
10
10
 
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/lib/colorize.rb CHANGED
@@ -5,41 +5,40 @@
5
5
  # want to add a gem dependency.
6
6
 
7
7
  class String
8
-
9
8
  #
10
9
  # Colors Hash
11
10
  #
12
11
  COLORS = {
13
- :black => 0,
14
- :red => 1,
15
- :green => 2,
16
- :yellow => 3,
17
- :blue => 4,
18
- :magenta => 5,
19
- :cyan => 6,
20
- :white => 7,
21
- :default => 9,
22
-
23
- :light_black => 10,
24
- :light_red => 11,
25
- :light_green => 12,
26
- :light_yellow => 13,
27
- :light_blue => 14,
28
- :light_magenta => 15,
29
- :light_cyan => 16,
30
- :light_white => 17
12
+ black: 0,
13
+ red: 1,
14
+ green: 2,
15
+ yellow: 3,
16
+ blue: 4,
17
+ magenta: 5,
18
+ cyan: 6,
19
+ white: 7,
20
+ default: 9,
21
+
22
+ light_black: 10,
23
+ light_red: 11,
24
+ light_green: 12,
25
+ light_yellow: 13,
26
+ light_blue: 14,
27
+ light_magenta: 15,
28
+ light_cyan: 16,
29
+ light_white: 17
31
30
  }
32
31
 
33
32
  #
34
33
  # Modes Hash
35
34
  #
36
35
  MODES = {
37
- :default => 0, # Turn off all attributes
36
+ default: 0, # Turn off all attributes
38
37
  #:bright => 1, # Set bright mode
39
- :underline => 4, # Set underline mode
40
- :blink => 5, # Set blink mode
41
- :swap => 7, # Exchange foreground and background colors
42
- :hide => 8 # Hide text (foreground color would be the same as background)
38
+ underline: 4, # Set underline mode
39
+ blink: 5, # Set blink mode
40
+ swap: 7, # Exchange foreground and background colors
41
+ hide: 8 # Hide text (foreground color would be the same as background)
43
42
  }
44
43
 
45
44
  protected
@@ -47,16 +46,14 @@ class String
47
46
  #
48
47
  # Set color values in new string intance
49
48
  #
50
- def set_color_parameters( params )
51
- if (params.instance_of?(Hash))
52
- @color = params[:color]
53
- @background = params[:background]
54
- @mode = params[:mode]
55
- @uncolorized = params[:uncolorized]
56
- self
57
- else
58
- nil
59
- end
49
+ def color_parameters(params)
50
+ return unless params.instance_of?(Hash)
51
+
52
+ @color = params[:color]
53
+ @background = params[:background]
54
+ @mode = params[:mode]
55
+ @uncolorized = params[:uncolorized]
56
+ self
60
57
  end
61
58
 
62
59
  public
@@ -77,22 +74,23 @@ class String
77
74
  # puts "This is blue text on red".blue.on_red.blink
78
75
  # puts "This is uncolorized".blue.on_red.uncolorize
79
76
  #
80
- def colorize( params )
77
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
78
+ def colorize(params)
81
79
  return self unless STDOUT.isatty
82
80
 
83
81
  begin
84
- require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
82
+ require "Win32/Console/ANSI" if RUBY_PLATFORM.match?(/win32/)
85
83
  rescue LoadError
86
- raise 'You must gem install win32console to use colorize on Windows'
84
+ raise "You must gem install win32console to use colorize on Windows"
87
85
  end
88
86
 
89
87
  color_parameters = {}
90
88
 
91
- if (params.instance_of?(Hash))
89
+ if params.instance_of?(Hash)
92
90
  color_parameters[:color] = COLORS[params[:color]]
93
91
  color_parameters[:background] = COLORS[params[:background]]
94
92
  color_parameters[:mode] = MODES[params[:mode]]
95
- elsif (params.instance_of?(Symbol))
93
+ elsif params.instance_of?(Symbol)
96
94
  color_parameters[:color] = COLORS[params]
97
95
  end
98
96
 
@@ -100,15 +98,18 @@ class String
100
98
  color_parameters[:background] ||= @background ||= COLORS[:default]
101
99
  color_parameters[:mode] ||= @mode ||= MODES[:default]
102
100
 
103
- color_parameters[:uncolorized] ||= @uncolorized ||= self.dup
101
+ color_parameters[:uncolorized] ||= @uncolorized ||= dup
104
102
 
105
103
  # calculate bright mode
106
104
  color_parameters[:color] += 50 if color_parameters[:color] > 10
107
105
 
108
106
  color_parameters[:background] += 50 if color_parameters[:background] > 10
109
107
 
110
- "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )
108
+ "\033[#{color_parameters[:mode]};#{color_parameters[:color] + 30};"\
109
+ "#{color_parameters[:background] + 40}m#{color_parameters[:uncolorized]}\033[0m"\
110
+ .color_parameters(color_parameters)
111
111
  end
112
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
112
113
 
113
114
  #
114
115
  # Return uncolorized string
@@ -127,37 +128,36 @@ class String
127
128
  #
128
129
  # Make some color and on_color methods
129
130
  #
130
- COLORS.each_key do | key |
131
+ COLORS.each_key do |key|
131
132
  next if key == :default
132
133
 
133
134
  define_method key do
134
- self.colorize( :color => key )
135
+ colorize(color: key)
135
136
  end
136
137
 
137
138
  define_method "on_#{key}" do
138
- self.colorize( :background => key )
139
+ colorize(background: key)
139
140
  end
140
141
  end
141
142
 
142
143
  #
143
144
  # Methods for modes
144
145
  #
145
- MODES.each_key do | key |
146
+ MODES.each_key do |key|
146
147
  next if key == :default
147
148
 
148
149
  define_method key do
149
- self.colorize( :mode => key )
150
+ colorize(mode: key)
150
151
  end
151
152
  end
152
153
 
153
154
  class << self
154
-
155
155
  #
156
156
  # Return array of available modes used by colorize method
157
157
  #
158
158
  def modes
159
159
  keys = []
160
- MODES.each_key do | key |
160
+ MODES.each_key do |key|
161
161
  keys << key
162
162
  end
163
163
  keys
@@ -168,7 +168,7 @@ class String
168
168
  #
169
169
  def colors
170
170
  keys = []
171
- COLORS.each_key do | key |
171
+ COLORS.each_key do |key|
172
172
  keys << key
173
173
  end
174
174
  keys
@@ -177,16 +177,16 @@ class String
177
177
  #
178
178
  # Display color matrix with color names.
179
179
  #
180
- def color_matrix( txt = "[X]" )
180
+ def color_matrix(txt = "[X]")
181
181
  size = String.colors.length
182
- String.colors.each do | color |
183
- String.colors.each do | back |
184
- print txt.colorize( :color => color, :background => back )
182
+ String.colors.each do |color|
183
+ String.colors.each do |back|
184
+ print txt.colorize(color: color, background: back)
185
185
  end
186
186
  puts " < #{color}"
187
187
  end
188
- String.colors.reverse.each_with_index do | back, index |
189
- puts "#{"|".rjust(txt.length)*(size-index)} < #{back}"
188
+ String.colors.reverse.each_with_index do |back, index|
189
+ puts "#{"|".rjust(txt.length) * (size - index)} < #{back}"
190
190
  end
191
191
  ""
192
192
  end
@@ -0,0 +1,65 @@
1
+ require "colorize"
2
+ require "open-uri"
3
+
4
+ module Raygun
5
+ class C5Conventions
6
+ REPO = "carbonfive/c5-conventions".freeze
7
+
8
+ SOURCES = [
9
+ "https://raw.githubusercontent.com/#{REPO}/main/rubocop/.rubocop.yml",
10
+ "https://raw.githubusercontent.com/#{REPO}/main/rubocop/.rubocop-common.yml",
11
+ "https://raw.githubusercontent.com/#{REPO}/main/rubocop/.rubocop-performance.yml",
12
+ "https://raw.githubusercontent.com/#{REPO}/main/rubocop/.rubocop-rails.yml",
13
+ "https://raw.githubusercontent.com/#{REPO}/main/rubocop/.rubocop-rspec.yml"
14
+ ].freeze
15
+
16
+ ATTRIBUTION_HEADER = <<~TEXT
17
+ # Sourced from #{REPO} @ %<sha>s
18
+ #
19
+ # If you make changes to this file, consider opening a PR to backport them to the c5-conventions repo:
20
+ # https://github.com/#{REPO}
21
+ TEXT
22
+
23
+ # Install the latest copy of c5-conventions rubocop config in the given directory.
24
+ # If the latest files can't be downloaded from GitHub, print a warning, don't raise an exception.
25
+ # Any existing .rubocop.yml will be overwritten.
26
+ def self.install(target:, sources: SOURCES)
27
+ new(target: target, sources: sources).install
28
+ rescue Errno::ENOENT, OpenURI::HTTPError => e
29
+ puts ""
30
+ puts "Failed to install the CarbonFive conventions from the #{REPO} GitHub repo".colorize(:light_red)
31
+ puts "Error: #{e}".colorize(:light_red)
32
+ puts "You'll have to manage your own `.rubocop.yml` setup".colorize(:light_red)
33
+ end
34
+
35
+ def initialize(sources:, target:)
36
+ @sources = sources
37
+ @target = target
38
+ end
39
+
40
+ def install
41
+ sources.each do |url_str|
42
+ uri = URI(url_str)
43
+ contents = [attribution_header, uri.open.read].join("\n")
44
+ filename = File.basename(uri.path)
45
+
46
+ IO.write(File.join(target, filename), contents)
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ attr_reader :sources, :target
53
+
54
+ def attribution_header
55
+ format(ATTRIBUTION_HEADER, sha: latest_commit_sha)
56
+ end
57
+
58
+ def latest_commit_sha
59
+ @latest_commit_sha ||= begin
60
+ stdout = `git ls-remote https://github.com/#{REPO} main`
61
+ stdout[/^(\h{7})/, 1] || "main"
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/raygun/raygun.rb CHANGED
@@ -1,43 +1,46 @@
1
- require 'optparse'
2
- require 'ostruct'
3
- require 'fileutils'
4
- require 'securerandom'
5
- require 'net/http'
6
- require 'json'
7
- require 'colorize'
8
-
9
- require_relative 'version'
1
+ require "optparse"
2
+ require "ostruct"
3
+ require "fileutils"
4
+ require "securerandom"
5
+ require "net/http"
6
+ require "json"
7
+ require "colorize"
8
+
9
+ require_relative "version"
10
+ require_relative "c5_conventions"
11
+ require_relative "template_repo"
10
12
 
11
13
  module Raygun
12
14
  class Runner
13
- CARBONFIVE_REPO = 'carbonfive/raygun-rails'
15
+ CARBONFIVE_REPO = "carbonfive/raygun-rails"
14
16
 
15
- attr_accessor :target_dir, :app_dir, :app_name, :dash_name, :snake_name, :camel_name, :title_name, :prototype_repo,
17
+ attr_accessor :target_dir, :app_dir, :app_name, :dash_name, :snake_name,
18
+ :camel_name, :title_name, :prototype_repo,
16
19
  :current_ruby_version, :current_ruby_patch_level
17
20
 
18
21
  def initialize(target_dir, prototype_repo)
19
22
  @target_dir = target_dir
20
23
  @app_dir = File.expand_path(target_dir.strip.to_s)
21
- @app_name = File.basename(app_dir).gsub(/\s+/, '-')
22
- @dash_name = app_name.gsub('_', '-')
23
- @snake_name = app_name.gsub('-', '_')
24
+ @app_name = File.basename(app_dir).gsub(/\s+/, "-")
25
+ @dash_name = app_name.tr("_", "-")
26
+ @snake_name = app_name.tr("-", "_")
24
27
  @camel_name = camelize(snake_name)
25
28
  @title_name = titleize(snake_name)
26
29
  @prototype_repo = prototype_repo
27
30
 
28
31
  @current_ruby_version = RUBY_VERSION
29
- @current_ruby_patch_level = if RUBY_VERSION < '2.1.0' # Ruby adopted semver starting with 2.1.0.
30
- "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
32
+ @current_ruby_patch_level = if RUBY_VERSION < "2.1.0" # Ruby adopted semver starting with 2.1.0.
33
+ "#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
31
34
  else
32
- "#{RUBY_VERSION}"
35
+ RUBY_VERSION.to_s
33
36
  end
34
37
  end
35
38
 
36
39
  def check_target
37
- unless Dir["#{@app_dir}/*"].empty?
38
- puts "Misfire! The target directory isn't empty... aim elsewhere."
39
- exit 1
40
- end
40
+ return if Dir["#{@app_dir}/*"].empty?
41
+
42
+ puts "Misfire! The target directory isn't empty... aim elsewhere.".colorize(:light_red)
43
+ exit 1
41
44
  end
42
45
 
43
46
  def fetch_prototype
@@ -45,26 +48,26 @@ module Raygun
45
48
  $stdout.flush
46
49
 
47
50
  # Check if we can connect, or fail gracefully and use the latest cached version.
48
- latest_tag_obj = fetch_latest_tag(prototype_repo)
49
- latest_tag = latest_tag_obj['name']
50
- tarball_url = latest_tag_obj['tarball_url']
51
+ repo = TemplateRepo.new(prototype_repo)
52
+ name = repo.name
53
+ tarball_url = repo.tarball
54
+ sha = repo.sha
51
55
 
52
- print " #{latest_tag}.".colorize(:white)
56
+ print " #{name}.".colorize(:white)
53
57
  $stdout.flush
54
58
 
55
59
  cached_prototypes_dir = File.join(Dir.home, ".raygun")
56
- @prototype = "#{cached_prototypes_dir}/#{prototype_repo.sub('/', '--')}-#{latest_tag}.tar.gz"
60
+ @prototype = "#{cached_prototypes_dir}/#{name.gsub("/", "--")}-#{sha}.tar.gz"
57
61
 
58
62
  # Do we already have the tarball cached under ~/.raygun?
59
- if File.exists?(@prototype)
63
+ if File.exist?(@prototype)
60
64
  puts " Using cached version.".colorize(:yellow)
61
65
  else
62
66
  print " Downloading...".colorize(:yellow)
63
67
  $stdout.flush
64
68
 
65
69
  # Download the tarball and install in the cache.
66
- Dir.mkdir(cached_prototypes_dir, 0755) unless Dir.exists?(cached_prototypes_dir)
67
-
70
+ Dir.mkdir(cached_prototypes_dir, 0o755) unless Dir.exist?(cached_prototypes_dir)
68
71
  shell "curl -s -L #{tarball_url} -o #{@prototype}"
69
72
  puts " done!".colorize(:yellow)
70
73
  end
@@ -72,24 +75,23 @@ module Raygun
72
75
 
73
76
  def check_raygun_version
74
77
  required_raygun_version =
75
- %x{tar xfz #{@prototype} --include "*.raygun-version" -O 2> /dev/null}.chomp ||
76
- ::Raygun::VERSION
77
-
78
- if Gem::Version.new(required_raygun_version) > Gem::Version.new(::Raygun::VERSION)
79
- puts ""
80
- print "Hold up!".colorize(:red)
81
- print " This version of the raygun gem (".colorize(:light_red)
82
- print "#{::Raygun::VERSION})".colorize(:white)
83
- print " is too old to generate this application (needs ".colorize(:light_red)
84
- print "#{required_raygun_version}".colorize(:white)
85
- puts " or newer).".colorize(:light_red)
86
- puts ""
87
- print "Please update the gem by running ".colorize(:light_red)
88
- print "gem update raygun".colorize(:white)
89
- puts ", and try again. Thanks!".colorize(:light_red)
90
- puts ""
91
- exit 1
92
- end
78
+ `tar xfz #{@prototype} --include "*.raygun-version" -O 2> /dev/null`.chomp ||
79
+ ::Raygun::VERSION
80
+ return unless Gem::Version.new(required_raygun_version) > Gem::Version.new(::Raygun::VERSION)
81
+
82
+ puts ""
83
+ print "Hold up!".colorize(:red)
84
+ print " This version of the raygun gem (".colorize(:light_red)
85
+ print "#{::Raygun::VERSION})".colorize(:white)
86
+ print " is too old to generate this application (needs ".colorize(:light_red)
87
+ print required_raygun_version.to_s.colorize(:white)
88
+ puts " or newer).".colorize(:light_red)
89
+ puts ""
90
+ print "Please update the gem by running ".colorize(:light_red)
91
+ print "gem update raygun".colorize(:white)
92
+ puts ", and try again. Thanks!".colorize(:light_red)
93
+ puts ""
94
+ exit 1
93
95
  end
94
96
 
95
97
  def copy_prototype
@@ -100,25 +102,28 @@ module Raygun
100
102
  # Github includes an extra directory layer in the tag tarball.
101
103
  extraneous_dir = Dir.glob("#{app_dir}/*").first
102
104
  dirs_to_move = Dir.glob("#{extraneous_dir}/*", File::FNM_DOTMATCH)
103
- .reject { |d| %w{. ..}.include?(File.basename(d)) }
105
+ .reject { |d| %w[. ..].include?(File.basename(d)) }
104
106
 
105
107
  FileUtils.mv dirs_to_move, app_dir
106
108
  FileUtils.remove_dir extraneous_dir
109
+
110
+ C5Conventions.install(target: app_dir) if @prototype_repo == CARBONFIVE_REPO
107
111
  end
108
112
 
109
113
  def rename_new_app
110
114
  Dir.chdir(app_dir) do
111
115
  {
112
- 'AppPrototype' => camel_name,
113
- 'app-prototype' => dash_name,
114
- 'app_prototype' => snake_name,
115
- 'App Prototype' => title_name
116
+ "AppPrototype" => camel_name,
117
+ "app-prototype" => dash_name,
118
+ "app_prototype" => snake_name,
119
+ "App Prototype" => title_name
116
120
  }.each do |proto_name, new_name|
117
121
  shell "find . -type f -print | xargs #{sed_i} 's/#{proto_name}/#{new_name}/g'"
118
122
  end
119
123
 
120
- %w(d f).each do |find_type|
121
- shell "find . -depth -type #{find_type} -name '*app_prototype*' -exec bash -c 'mv $0 ${0/app_prototype/#{snake_name}}' {} \\;"
124
+ %w[d f].each do |find_type|
125
+ shell "find . -depth -type #{find_type} -name '*app_prototype*' " \
126
+ "-exec bash -c 'mv $0 ${0/app_prototype/#{snake_name}}' {} \\;"
122
127
  end
123
128
  end
124
129
  end
@@ -148,26 +153,33 @@ module Raygun
148
153
  def initialize_git
149
154
  Dir.chdir(app_dir) do
150
155
  shell "git init"
156
+ shell "git checkout -q -b main"
151
157
  shell "git add -A ."
152
158
  shell "git commit -m 'Raygun-zapped skeleton.'"
153
159
  end
154
160
  end
155
161
 
162
+ # rubocop:disable Metrics/AbcSize
156
163
  def print_plan
157
- puts ' ____ '.colorize(:light_yellow)
164
+ puts " ____ ".colorize(:light_yellow)
158
165
  puts ' / __ \____ ___ ______ ___ ______ '.colorize(:light_yellow)
159
166
  puts ' / /_/ / __ `/ / / / __ `/ / / / __ \ '.colorize(:light_yellow)
160
- puts ' / _, _/ /_/ / /_/ / /_/ / /_/ / / / / '.colorize(:light_yellow)
167
+ puts " / _, _/ /_/ / /_/ / /_/ / /_/ / / / / ".colorize(:light_yellow)
161
168
  puts ' /_/ |_|\__,_/\__, /\__, /\__,_/_/ /_/ '.colorize(:light_yellow)
162
- puts ' /____//____/ '.colorize(:light_yellow)
169
+ puts " /____//____/ ".colorize(:light_yellow)
163
170
  puts
164
- puts "Raygun will create new app in directory:".colorize(:yellow) + " #{target_dir}".colorize(:yellow) + "...".colorize(:yellow)
171
+ puts "Raygun will create new app in directory:".colorize(:yellow) +
172
+ " #{target_dir}".colorize(:yellow) + "...".colorize(:yellow)
165
173
  puts
166
- puts "-".colorize(:blue) + " Application Name:".colorize(:light_blue) + " #{title_name}".colorize(:light_reen)
167
- puts "-".colorize(:blue) + " Project Template:".colorize(:light_blue) + " #{prototype_repo}".colorize(:light_reen)
168
- puts "-".colorize(:blue) + " Ruby Version: ".colorize(:light_blue) + " #{@current_ruby_patch_level}".colorize(:light_reen)
174
+ puts "-".colorize(:blue) + " Application Name:".colorize(:light_blue) +
175
+ " #{title_name}".colorize(:light_reen)
176
+ puts "-".colorize(:blue) + " Project Template:".colorize(:light_blue) +
177
+ " #{prototype_repo}".colorize(:light_reen)
178
+ puts "-".colorize(:blue) + " Ruby Version: ".colorize(:light_blue) +
179
+ " #{@current_ruby_patch_level}".colorize(:light_reen)
169
180
  puts
170
181
  end
182
+ # rubocop:enable Metrics/AbcSize
171
183
 
172
184
  def print_next_steps
173
185
  if @prototype_repo == CARBONFIVE_REPO
@@ -176,25 +188,30 @@ module Raygun
176
188
  print_next_steps_for_custom_repo
177
189
  end
178
190
  end
179
-
191
+
192
+ # rubocop:disable Metrics/AbcSize
180
193
  def print_next_steps_carbon_five
181
194
  puts ""
182
195
  puts "Zap! Your application is ready. Next steps...".colorize(:yellow)
183
196
  puts ""
184
197
  puts "# Install updated dependencies and prepare the database".colorize(:light_green)
185
198
  puts "$".colorize(:blue) + " cd #{target_dir}".colorize(:light_blue)
186
- puts "$".colorize(:blue) + " ./bin/setup".colorize(:light_blue)
199
+ puts "$".colorize(:blue) + " bin/setup".colorize(:light_blue)
187
200
  puts ""
188
201
  puts "# Run the specs (they should all pass)".colorize(:light_green)
189
- puts "$".colorize(:blue) + " rake".colorize(:light_blue)
202
+ puts "$".colorize(:blue) + " bin/rake".colorize(:light_blue)
190
203
  puts ""
191
204
  puts "# Run the app and check things out".colorize(:light_green)
192
- puts "$".colorize(:blue) + " foreman start".colorize(:light_blue)
205
+ puts "$".colorize(:blue) + " yarn start".colorize(:light_blue)
193
206
  puts "$".colorize(:blue) + " open http://localhost:3000".colorize(:light_blue)
194
207
  puts ""
208
+ puts "# For next steps like adding Bootstrap or React, check out the raygun README".colorize(:light_green)
209
+ puts "$".colorize(:blue) + " open https://github.com/carbonfive/raygun/#next-steps".colorize(:light_blue)
210
+ puts ""
195
211
  puts "Enjoy your Carbon Five flavored Rails application!".colorize(:yellow)
196
212
  end
197
-
213
+ # rubocop:enable Metrics/AbcSize
214
+
198
215
  def print_next_steps_for_custom_repo
199
216
  puts ""
200
217
  puts "Zap! Your application is ready.".colorize(:yellow)
@@ -204,111 +221,80 @@ module Raygun
204
221
 
205
222
  protected
206
223
 
207
- # Fetch the tags for the repo (e.g. 'carbonfive/raygun-rails') and return the latest as JSON.
208
- def fetch_latest_tag(repo)
209
- url = "https://api.github.com/repos/#{repo}/tags"
210
- uri = URI.parse(url)
211
- http = Net::HTTP.new(uri.host, uri.port)
212
- http.use_ssl = true
213
- request = Net::HTTP::Get.new(URI.encode(url))
214
-
215
- response = http.request(request)
216
-
217
- unless response.code == "200"
218
- puts ""
219
- print "Whoops - need to try again!".colorize(:red)
220
- puts ""
221
- print "We could not find (".colorize(:light_red)
222
- print "#{repo}".colorize(:white)
223
- print ") on github.".colorize(:light_red)
224
- puts ""
225
- print "The response from github was a (".colorize(:light_red)
226
- print "#{response.code}".colorize(:white)
227
- puts ") which I'm sure you can fix right up!".colorize(:light_red)
228
- puts ""
229
- exit 1
230
- end
231
-
232
- result = JSON.parse(response.body).first
233
- unless result
234
- puts ""
235
- print "Whoops - need to try again!".colorize(:red)
236
- puts ""
237
- print "We could not find any tags in the repo (".colorize(:light_red)
238
- print "#{repo}".colorize(:white)
239
- print ") on github.".colorize(:light_red)
240
- puts ""
241
- print "Raygun uses the 'largest' tag in a repository, where tags are sorted alphanumerically.".colorize(:light_red)
242
- puts ""
243
- print "E.g., tag 'v.0.10.0' > 'v.0.9.9' and 'x' > 'a'.".colorize(:light_red)
244
- print ""
245
- puts ""
246
- exit 1
247
- end
248
-
249
- result
250
- end
251
-
252
224
  def camelize(string)
253
225
  result = string.sub(/^[a-z\d]*/) { $&.capitalize }
254
- result.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }
226
+ result.gsub(%r{(?:_|(/))([a-z\d]*)}) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
255
227
  end
256
228
 
257
229
  def titleize(underscored_string)
258
- result = underscored_string.gsub(/_/, ' ')
259
- result.gsub(/\b('?[a-z])/) { $1.capitalize }
230
+ result = underscored_string.tr("_", " ")
231
+ result.gsub(/\b('?[a-z])/) { Regexp.last_match(1).capitalize }
260
232
  end
261
233
 
262
234
  # Distinguish BSD vs GNU sed with the --version flag (only present in GNU sed).
263
235
  def sed_i
264
- @sed_format ||= begin
265
- %x{sed --version &> /dev/null}
266
- $?.success? ? "sed -i" : "sed -i ''"
267
- end
236
+ @sed_i ||= begin
237
+ `sed --version &> /dev/null`
238
+ $?.success? ? "sed -i" : "sed -i ''"
239
+ end
268
240
  end
269
241
 
270
242
  # Run a shell command and raise an exception if it fails.
271
243
  def shell(command)
272
- %x{#{command}}
244
+ output = `#{command}`
273
245
  raise "#{command} failed with status #{$?.exitstatus}." unless $?.success?
274
- end
275
-
276
- def self.parse(args)
277
- raygun = nil
278
246
 
279
- options = OpenStruct.new
280
- options.target_dir = nil
281
- options.prototype_repo = CARBONFIVE_REPO
282
-
283
- parser = OptionParser.new do |opts|
284
- opts.banner = "Usage: raygun [options] NEW_APP_DIRECTORY"
247
+ output
248
+ end
285
249
 
286
- opts.on('-h', '--help', "Show raygun usage") do
287
- usage_and_exit(opts)
250
+ class << self
251
+ # rubocop:disable Metrics/MethodLength
252
+ def parse(_args)
253
+ raygun = nil
254
+
255
+ options = OpenStruct.new
256
+ options.target_dir = nil
257
+ options.prototype_repo = CARBONFIVE_REPO
258
+
259
+ parser = OptionParser.new do |opts|
260
+ opts.banner = "Usage: raygun [options] NEW_APP_DIRECTORY"
261
+
262
+ opts.on("-h", "--help", "Show raygun usage") do
263
+ usage_and_exit(opts)
264
+ end
265
+ opts.on(
266
+ "-p",
267
+ "--prototype [github_repo]",
268
+ "Prototype github repo (e.g. carbonfive/raygun-rails)."
269
+ ) do |prototype|
270
+ options.prototype_repo = prototype
271
+ end
272
+
273
+ opts.on("-v", "--version", "Print the version number") do
274
+ puts Raygun::VERSION
275
+ exit 1
276
+ end
288
277
  end
289
- opts.on('-p', '--prototype [github_repo]', "Prototype github repo (e.g. carbonfive/raygun-rails).") do |prototype|
290
- options.prototype_repo = prototype
291
- end
292
- end
293
278
 
294
- begin
295
- parser.parse!
296
- options.target_dir = ARGV.first
279
+ begin
280
+ parser.parse!
281
+ options.target_dir = ARGV.first
297
282
 
298
- raise OptionParser::InvalidOption if options.target_dir.nil?
283
+ raise OptionParser::InvalidOption if options.target_dir.nil?
299
284
 
300
- raygun = Raygun::Runner.new(options.target_dir, options.prototype_repo)
285
+ raygun = Raygun::Runner.new(options.target_dir, options.prototype_repo)
286
+ rescue OptionParser::InvalidOption
287
+ usage_and_exit(parser)
288
+ end
301
289
 
302
- rescue OptionParser::InvalidOption
303
- usage_and_exit(parser)
290
+ raygun
304
291
  end
292
+ # rubocop:enable Metrics/MethodLength
305
293
 
306
- raygun
307
- end
308
-
309
- def self.usage_and_exit(parser)
310
- puts parser
311
- exit 1
294
+ def usage_and_exit(parser)
295
+ puts parser
296
+ exit 1
297
+ end
312
298
  end
313
299
  end
314
300
  end