diff --git a/lib-injection/auto_inject.rb b/lib-injection/auto_inject.rb index 831eee9e376..5a18c5d9db6 100644 --- a/lib-injection/auto_inject.rb +++ b/lib-injection/auto_inject.rb @@ -1,67 +1,63 @@ if !ENV['skip_autoinject'] if system 'skip_autoinject=true bundle show ddtrace' - puts 'ddtrace already installed... skipping auto-injection' + puts '[DATADOG LIB INJECTION] ddtrace already installed... skipping auto-injection' else - version = "" - sha = "" + require 'bundler' + # require 'shellwords' + + if Bundler.frozen_bundle? + puts "[DATADOG LIB INJECTION] Cannot inject with frozen Bundler" + else + version = "" + sha = "" - condition = - if !version.empty? + condition = if !version.empty? # For public release - "--version '#{version.gsub(/^v/, '')}'" + "--version #{version.gsub(/^v/, '')}" elsif !sha.empty? # For internal testing - "--github 'datadog/dd-trace-rb' --ref '#{sha}'" + "--github datadog/dd-trace-rb --ref #{sha}" else - puts "NO VERSION" + puts "[DATADOG LIB INJECTION] NO VERSION" end - puts "ddtrace is not installed... Perform auto-injection... for dd-trace-rb" - - require 'bundler' - require 'fileutils' - - gemfile = Bundler::SharedHelpers.default_gemfile - lockfile = Bundler::SharedHelpers.default_lockfile - - puts gemfile - puts lockfile - - if gemfile.basename.to_s == 'gems.rb' - datadog_gemfile = gemfile.dirname + "datadog-Gemfile" - datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" - else - datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") - datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") - end + puts "[DATADOG LIB INJECTION] ddtrace is not installed... Perform lib injection for dd-trace-rb." - puts datadog_gemfile - puts datadog_lockfile + gemfile = Bundler::SharedHelpers.default_gemfile + lockfile = Bundler::SharedHelpers.default_lockfile - # Copies for trial - FileUtils.cp gemfile, datadog_gemfile - FileUtils.cp lockfile, datadog_lockfile - - if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" - puts 'ddtrace added to bundle...' + if gemfile.basename.to_s == 'gems.rb' + datadog_gemfile = gemfile.dirname + "datadog-Gemfile" + datadog_lockfile = lockfile.dirname + "datadog-Gemfile.lock" + else + datadog_gemfile = gemfile.dirname + ("datadog-#{gemfile.basename}") + datadog_lockfile = lockfile.dirname + ("datadog-#{lockfile.basename}") + end - # Trial success, replace the original - FileUtils.cp datadog_gemfile, gemfile - FileUtils.cp datadog_lockfile, lockfile - else - puts 'Something went wrong when adding ddtrace to bundle...' + require 'fileutils' + + begin + # Copies for trial + FileUtils.cp gemfile, datadog_gemfile + FileUtils.cp lockfile, datadog_lockfile + + if system "skip_autoinject=true BUNDLE_GEMFILE=#{datadog_gemfile} bundle add ddtrace #{condition} --require ddtrace/auto_instrument" + puts '[DATADOG LIB INJECTION] ddtrace added to bundle...' + + # Trial success, replace the original + FileUtils.cp datadog_gemfile, gemfile + FileUtils.cp datadog_lockfile, lockfile + else + puts '[DATADOG LIB INJECTION] Something went wrong when adding ddtrace to bundle...' + end + + rescue => e + puts "[DATADOG LIB INJECTION] #{e}" + ensure + # Remove the copies + FileUtils.rm datadog_gemfile + FileUtils.rm datadog_lockfile + end end - - # Remove the copies - FileUtils.rm datadog_gemfile - FileUtils.rm datadog_lockfile - end - - begin - require 'ddtrace' - rescue LoadError => e - puts e - else - puts 'ddtrace loaded...' end end