Class: Raygun::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/raygun/raygun.rb

Constant Summary collapse

CARBONFIVE_REPO =
'carbonfive/raygun-rails'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_dir, prototype_repo, gitlab_endpoint, ref, embed_as) ⇒ Runner

Returns a new instance of Runner.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/raygun/raygun.rb', line 19

def initialize(target_dir, prototype_repo, gitlab_endpoint, ref, embed_as)
  @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('-', '_')
  @camel_name     = camelize(snake_name)
  @title_name     = titleize(snake_name)
  @prototype_repo = prototype_repo
  @gitlab_endpoint = gitlab_endpoint
  @ref = ref
  @embed_as = embed_as
  if embed_as
    FileUtils.mkdir_p(app_dir) unless File.exist?(app_dir)
    @app_dir = File.join(app_dir, embed_as)
  end
  
  @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}"
                              else
                                "#{RUBY_VERSION}"
                              end
end

Instance Attribute Details

#app_dirObject

Returns the value of attribute app_dir.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def app_dir
  @app_dir
end

#app_nameObject

Returns the value of attribute app_name.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def app_name
  @app_name
end

#camel_nameObject

Returns the value of attribute camel_name.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def camel_name
  @camel_name
end

#current_ruby_patch_levelObject

Returns the value of attribute current_ruby_patch_level.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def current_ruby_patch_level
  @current_ruby_patch_level
end

#current_ruby_versionObject

Returns the value of attribute current_ruby_version.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def current_ruby_version
  @current_ruby_version
end

#dash_nameObject

Returns the value of attribute dash_name.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def dash_name
  @dash_name
end

#embed_asObject

Returns the value of attribute embed_as.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def embed_as
  @embed_as
end

#gitlab_endpointObject

Returns the value of attribute gitlab_endpoint.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def gitlab_endpoint
  @gitlab_endpoint
end

#prototype_repoObject

Returns the value of attribute prototype_repo.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def prototype_repo
  @prototype_repo
end

#refObject

Returns the value of attribute ref.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def ref
  @ref
end

#snake_nameObject

Returns the value of attribute snake_name.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def snake_name
  @snake_name
end

#target_dirObject

Returns the value of attribute target_dir.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def target_dir
  @target_dir
end

#title_nameObject

Returns the value of attribute title_name.



16
17
18
# File 'lib/raygun/raygun.rb', line 16

def title_name
  @title_name
end

Instance Method Details

#cached_prototypes_dirObject



80
# File 'lib/raygun/raygun.rb', line 80

def cached_prototypes_dir; File.join(Dir.home, ".raygun"); end

#check_raygun_versionObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/raygun/raygun.rb', line 141

def check_raygun_version
  required_raygun_version =
    %x{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  ""
    print "Hold up!".colorize(:red)
    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)
    puts  " or newer).".colorize(:light_red)
    puts  ""
    print "Please update the gem by running ".colorize(:light_red)
    print "gem update raygun".colorize(:white)
    puts  ", and try again. Thanks!".colorize(:light_red)
    puts  ""
    exit 1
  end
end

#check_targetObject



44
45
46
47
48
49
# File 'lib/raygun/raygun.rb', line 44

def check_target
  unless Dir["#{@app_dir}/*"].empty?
    puts "Misfire! The target directory isn't empty... aim elsewhere."
    exit 1
  end
end

#clean_up_unwanted_filesObject



198
199
200
# File 'lib/raygun/raygun.rb', line 198

def clean_up_unwanted_files
  FileUtils.rm "#{app_dir}/.raygun-version", force: true
end

#configure_new_appObject



190
191
192
193
194
195
196
# File 'lib/raygun/raygun.rb', line 190

def configure_new_app
  clean_up_unwanted_files

  update_ruby_version

  initialize_git
end

#copy_prototypeObject



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/raygun/raygun.rb', line 163

def copy_prototype
  FileUtils.mkdir_p(app_dir)

  shell "tar xfz #{@prototype} -C #{app_dir}"

  # 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)) }

  FileUtils.mv         dirs_to_move, app_dir
  FileUtils.remove_dir extraneous_dir
end

#fetch_prototypeObject



72
73
74
75
76
77
78
# File 'lib/raygun/raygun.rb', line 72

def fetch_prototype
  if gitlab?
    fetch_prototype_from_gitlab
  else
    fetch_prototype_from_github
  end
end

#fetch_prototype_from_githubObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/raygun/raygun.rb', line 112

def fetch_prototype_from_github
  print "Checking for the latest application prototype...".colorize(:yellow)
  $stdout.flush

  # Check if we can connect, or fail gracefully and use the latest cached version.
  latest_tag_obj = fetch_latest_tag(prototype_repo)
  latest_tag     = latest_tag_obj['name']
  tarball_url    = latest_tag_obj['tarball_url']

  print " #{latest_tag}.".colorize(:white)
  $stdout.flush

  @prototype = "#{cached_prototypes_dir}/#{prototype_repo.sub('/', '--')}-#{latest_tag}.tar.gz"

  # Do we already have the tarball cached under ~/.raygun?
  if File.exists?(@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)

    shell "curl -s -L #{tarball_url} -o #{@prototype}"
    puts " done!".colorize(:yellow)
  end
end

#fetch_prototype_from_gitlabObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/raygun/raygun.rb', line 82

def fetch_prototype_from_gitlab
  print "Checking for the latest application prototype on gitlab...".colorize(:yellow)
  $stdout.flush
  
  latest_tag = ref || fetch_latest_tag_gitlab(prototype_repo)
  
  gitlab_cached_prototypes_dir = File.join(cached_prototypes_dir, gitlab_client.domain)
  @prototype = File.join(gitlab_cached_prototypes_dir, "#{prototype_repo.sub('/', '--')}-#{latest_tag}.tar.gz")
  
  

  if File.exists?(@prototype)
    puts " Using cached version.".colorize(:yellow)
  else
    print " Downloading...".colorize(:yellow)
    $stdout.flush

    # Download the tarball and install in the cache.
    Dir.mkdir(gitlab_cached_prototypes_dir, 0755) unless Dir.exists?(gitlab_cached_prototypes_dir)
    tarball_url = URI([gitlab_endpoint, prototype_repo, "repository", "archive.tar.gz"].join('/'))
    tarball_url.query = URI.encode_www_form(private_token: gitlab_client.token, ref: latest_tag)

    shell "curl -s -L '#{tarball_url.to_s}' -o #{@prototype}"
    puts " done!".colorize(:yellow)
  end
  
  $stdout.flush
end

#gitlab?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/raygun/raygun.rb', line 52

def gitlab?
  !!@gitlab_endpoint
end

#gitlab_clientObject



56
57
58
# File 'lib/raygun/raygun.rb', line 56

def gitlab_client
  @gitlab_client ||= GitlabClient.new(gitlab_endpoint)
end

#gitlab_project_idObject



60
61
62
# File 'lib/raygun/raygun.rb', line 60

def gitlab_project_id
  @gitlab_project_id ||= parse_gitlab_project_id
end

#initialize_gitObject



212
213
214
215
216
217
218
219
220
# File 'lib/raygun/raygun.rb', line 212

def initialize_git
  dir = app_dir
  dir = File.join(dir, '..') if embed_as
  Dir.chdir(dir) do
    shell "git init" unless (embed_as && File.exist?('.git'))
    shell "git add -A ."
    shell "git commit -m 'Raygun-zapped skeleton.'"
  end
end

#parse_gitlab_project_idObject



64
65
66
67
68
69
70
# File 'lib/raygun/raygun.rb', line 64

def parse_gitlab_project_id
  if prototype_repo.to_i == prototype_repo
    prototype_repo
  else
    
  end
end


247
248
249
250
251
252
253
# File 'lib/raygun/raygun.rb', line 247

def print_next_steps
  if @prototype_repo == CARBONFIVE_REPO
    print_next_steps_carbon_five
  else
    print_next_steps_for_custom_repo
  end
end


255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/raygun/raygun.rb', line 255

def print_next_steps_carbon_five
  puts ""
  puts "Zap! Your application is ready. Next steps...".colorize(:yellow)
  puts ""
  puts "# Install updated dependencies".colorize(:light_green)
  puts "$".colorize(:blue) + " cd #{target_dir}".colorize(:light_blue)
  puts "$".colorize(:blue) + " gem install bundler".colorize(:light_blue)
  puts "$".colorize(:blue) + " bundle".colorize(:light_blue)
  puts ""
  puts "# Prepare the database: schema and reference / sample data".colorize(:light_green)
  puts "$".colorize(:blue) + " rake db:setup db:sample_data".colorize(:light_blue)
  puts ""
  puts "# Run the specs (they should all pass)".colorize(:light_green)
  puts "$".colorize(:blue) + " rake".colorize(:light_blue)
  puts ""
  puts "# Run the app and check things out".colorize(:light_green)
  puts "$".colorize(:blue) + " foreman start".colorize(:light_blue)
  puts "$".colorize(:blue) + " open http://localhost:3000".colorize(:light_blue)
  puts ""
  puts "Enjoy your Carbon Five flavored Rails application!".colorize(:yellow)
end


277
278
279
280
281
282
# File 'lib/raygun/raygun.rb', line 277

def print_next_steps_for_custom_repo
  puts ""
  puts "Zap! Your application is ready.".colorize(:yellow)
  puts ""
  puts "Enjoy your raygun generated application!".colorize(:yellow)
end


222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/raygun/raygun.rb', line 222

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
  puts "Raygun will create new app in directory:".colorize(:yellow) + " #{target_dir}".colorize(:yellow) + "...".colorize(:yellow)
  puts
  puts "-".colorize(:blue) +   " Application Name:".colorize(:light_blue) + " #{title_name}".colorize(:light_green)
  if gitlab?
    puts "-".colorize(:blue) + " Gitlab Endpoint: ".colorize(:light_blue) + " #{gitlab_endpoint}".colorize(:light_green)
  end
  puts "-".colorize(:blue) +   " Project Template:".colorize(:light_blue) + " #{prototype_repo}".colorize(:light_green)
  if ref
    puts "-".colorize(:blue) + " Branch/Tag:      ".colorize(:light_blue) + " #{ref}".colorize(:light_green)
  end
  if embed_as
    puts "-".colorize(:blue) + " Embed as:        ".colorize(:light_blue) + " #{embed_as}".colorize(:light_green)
  end
  puts "-".colorize(:blue) +   " Ruby Version:    ".colorize(:light_blue) + " #{@current_ruby_patch_level}".colorize(:light_green)
  puts
end

#rename_new_appObject



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/raygun/raygun.rb', line 177

def rename_new_app
  Dir.chdir(app_dir) do
    {
      '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
  end
end

#update_ruby_versionObject



202
203
204
205
206
207
208
209
210
# File 'lib/raygun/raygun.rb', line 202

def update_ruby_version
  prototype_ruby_patch_level = File.read(File.expand_path("#{app_dir}/.ruby-version", __FILE__)).strip
  prototype_ruby_version     = prototype_ruby_patch_level.match(/(\d\.\d\.\d).*/)[1]

  Dir.chdir(app_dir) do
    shell "#{sed_i} 's/#{prototype_ruby_patch_level}/#{@current_ruby_patch_level}/g' .ruby-version README.md"
    shell "#{sed_i} 's/#{prototype_ruby_version}/#{@current_ruby_version}/g' Gemfile"
  end
end