diff --git a/Rakefile b/Rakefile index 0c5ad51ad..ea2231292 100644 --- a/Rakefile +++ b/Rakefile @@ -41,8 +41,7 @@ gemspec = Gem::Specification.new do |s| s.summary = %q{Send your application errors to our hosted service and reclaim your inbox.} s.files = FileList['[A-Z]*', 'generators/**/*.*', 'lib/**/*.rb', - 'test/**/*.rb', 'rails/**/*.rb', 'recipes/**/*.rb', - 'tasks/**/*.rake'] + 'test/**/*.rb', 'rails/**/*.rb', 'tasks/**/*.rake'] s.require_path = 'lib' s.test_files = Dir[*['test/**/*_test.rb']] @@ -73,7 +72,7 @@ task :gemspec do end LOCAL_GEM_ROOT = File.join(GEM_ROOT, 'tmp', 'local_gems').freeze -LOCAL_GEMS = %w(rails sham_rack) +LOCAL_GEMS = %w(rails sham_rack capistrano) task :vendor_test_gems do LOCAL_GEMS.each do |gem_name| diff --git a/features/rails.feature b/features/rails.feature index c5cf5c12c..bafbb4fcf 100644 --- a/features/rails.feature +++ b/features/rails.feature @@ -32,3 +32,12 @@ Feature: Install the Gem in a Rails application And I configure my application to require the "hoptoad_notifier" gem And I run "script/generate hoptoad" Then I should see "Must pass --api-key or create config/initializers/hoptoad.rb" + + Scenario: Configure and deploy + When I generate a new Rails application + And I run "capify ." + And I configure the Hoptoad shim + And I configure my application to require the "hoptoad_notifier" gem + And I run "script/generate hoptoad -k myapikey" + And I run "cap -T" + Then I should see "deploy:notify_hoptoad" diff --git a/features/support/terminal.rb b/features/support/terminal.rb index 51325c6bd..eae979145 100644 --- a/features/support/terminal.rb +++ b/features/support/terminal.rb @@ -30,8 +30,11 @@ def run(command) def build_and_install_gem(gemspec) pkg_dir = File.join(TEMP_DIR, 'pkg') FileUtils.mkdir_p(pkg_dir) - `gem build #{gemspec} 2>&1` + output = `gem build #{gemspec} 2>&1` gem_file = Dir.glob("*.gem").first + unless gem_file + raise "Gem didn't build:\n#{output}" + end target = File.join(pkg_dir, gem_file) FileUtils.mv(gem_file, target) install_gem_to(BUILT_GEM_ROOT, target) diff --git a/generators/hoptoad/hoptoad_generator.rb b/generators/hoptoad/hoptoad_generator.rb index 4186524d9..74e19e3ca 100644 --- a/generators/hoptoad/hoptoad_generator.rb +++ b/generators/hoptoad/hoptoad_generator.rb @@ -14,9 +14,9 @@ def manifest record do |m| m.directory 'lib/tasks' m.file 'hoptoad_notifier_tasks.rake', 'lib/tasks/hoptoad_notifier_tasks.rake' - # if File.exists?('config/deploy.rb') - # m.insert_into 'config/deploy.rb', "require 'hoptoad_notifier/recipes/hoptoad'" - # end + if File.exists?('config/deploy.rb') + m.append_to 'config/deploy.rb', "require 'hoptoad_notifier/capistrano'" + end unless options[:api_key].nil? m.template 'initializer.rb', 'config/initializers/hoptoad.rb', :assigns => {:api_key => options[:api_key]} diff --git a/generators/hoptoad/lib/insert_commands.rb b/generators/hoptoad/lib/insert_commands.rb index cc76c56df..9136fb052 100644 --- a/generators/hoptoad/lib/insert_commands.rb +++ b/generators/hoptoad/lib/insert_commands.rb @@ -7,27 +7,28 @@ def file_contains?(relative_destination, line) end Rails::Generator::Commands::Create.class_eval do - def insert_into(file, line) - logger.insert "#{line} into #{file}" + def append_to(file, line) + logger.insert "#{line} appended to #{file}" unless options[:pretend] || file_contains?(file, line) - gsub_file file, /^(class|module) .+$/ do |match| - "#{match}\n #{line}" + File.open(file, "a") do |file| + file.puts + file.puts line end end end end Rails::Generator::Commands::Destroy.class_eval do - def insert_into(file, line) - logger.remove "#{line} from #{file}" + def append_to(file, line) + logger.remove "#{line} removed from #{file}" unless options[:pretend] - gsub_file file, "\n #{line}", '' + gsub_file file, "\n#{line}", '' end end end Rails::Generator::Commands::List.class_eval do - def insert_into(file, line) - logger.insert "#{line} into #{file}" + def append_to(file, line) + logger.insert "#{line} appended to #{file}" end end diff --git a/lib/hoptoad_notifier/capistrano.rb b/lib/hoptoad_notifier/capistrano.rb new file mode 100644 index 000000000..538419e53 --- /dev/null +++ b/lib/hoptoad_notifier/capistrano.rb @@ -0,0 +1,20 @@ +# Defines deploy:notify_hoptoad which will send information about the deploy to Hoptoad. + +Capistrano::Configuration.instance(:must_exist).load do + after "deploy", "deploy:notify_hoptoad" + after "deploy:migrations", "deploy:notify_hoptoad" + + namespace :deploy do + desc "Notify Hoptoad of the deployment" + task :notify_hoptoad, :except => { :no_release => true } do + rails_env = fetch(:hoptoad_env, fetch(:rails_env, "production")) + local_user = ENV['USER'] || ENV['USERNAME'] + executable = RUBY_PLATFORM.downcase.include?('mswin') ? 'rake.bat' : 'rake' + notify_command = "#{executable} hoptoad:deploy TO=#{rails_env} REVISION=#{current_revision} REPO=#{repository} USER=#{local_user}" + notify_command << " API_KEY=#{ENV['API_KEY']}" if ENV['API_KEY'] + puts "Notifying Hoptoad of Deploy (#{notify_command})" + `#{notify_command}` + puts "Hoptoad Notification Complete." + end + end +end diff --git a/recipes/hoptoad.rb b/recipes/hoptoad.rb deleted file mode 100644 index 52151c15a..000000000 --- a/recipes/hoptoad.rb +++ /dev/null @@ -1,24 +0,0 @@ -# When Hoptoad is installed as a plugin this is loaded automatically. -# -# When Hoptoad installed as a gem, you need to add -# require 'hoptoad_notifier/recipes/hoptoad' -# to your deploy.rb -# -# Defines deploy:notify_hoptoad which will send information about the deploy to Hoptoad. -# -after "deploy", "deploy:notify_hoptoad" -after "deploy:migrations", "deploy:notify_hoptoad" - -namespace :deploy do - desc "Notify Hoptoad of the deployment" - task :notify_hoptoad, :except => { :no_release => true } do - rails_env = fetch(:hoptoad_env, fetch(:rails_env, "production")) - local_user = ENV['USER'] || ENV['USERNAME'] - executable = RUBY_PLATFORM.downcase.include?('mswin') ? 'rake.bat' : 'rake' - notify_command = "#{executable} hoptoad:deploy TO=#{rails_env} REVISION=#{current_revision} REPO=#{repository} USER=#{local_user}" - notify_command << " API_KEY=#{ENV['API_KEY']}" if ENV['API_KEY'] - puts "Notifying Hoptoad of Deploy (#{notify_command})" - `#{notify_command}` - puts "Hoptoad Notification Complete." - end -end