X Tutup
#!/usr/bin/env ruby #/ Usage: script/bootstrap [] #/ Bootstraps the gem environment. #/ #/ Options are passed through to the bundle-install command. In most cases you #/ won't need these. They're used primarily in production environments. #/ --local use gems in vendor/cache instead of rubygems.org #/ --without= do not install gems in the groups specified # # ============================================================================= # Uses bundler to install all gems specified in the Gemfile under vendor/gems, # records the load path in config/loadpath, and generates bundler-free binstubs # under bin. # # The basic idea is to use bundler to install necessary gems but not # rely on it to manage the load path at runtime because it's slow. Requiring # 'bundler/setup' takes ~500ms user CPU time in production and ~1500ms in # development/test. This makes it unusable in scenarios that require a fast # boot (e.g., script/gerve, proxymachine daemons, ernie/smoke, etc.). It's also # a problem in development where it slows tools like rake command line # completion to a crawl and adds at least a second to single-file test runs. # # There's very little reason to use bundler at runtime since everything # is known at install time. We simply save off the result of the work done by # bundle/setup and use it until bundle-install is run again. # show usage message with --help if ARGV.include?('--help') system "grep '^#/' <'#{__FILE__}' |cut -c4-" exit 2 end # go into the project root because it makes everything easier root = File.expand_path('../..', __FILE__) Dir.chdir(root) # point bundler to the right stuff ENV['BUNDLE_GEMFILE'] = "#{root}/Gemfile" ENV['BUNDLE_PATH'] = "#{root}/vendor/gems" # bring in rubygems and make sure bundler is installed. require 'rubygems' begin require 'bundler' rescue LoadError => boom warn "Bundler not found. Install it with `gem install bundler' and try again." exit 0 end # record the Gemfile checksum so we can tell if the Gemfile has changed # since our loadpath was last generated. this is used in config/basic.rb # to verify the environment is bootstrapped and up-to-date. checksum = `cksum Gemfile`.to_i installed = File.read('.bundle/checksum').to_i rescue nil # run a quick check to see if everything's installed and up-to-date so we can # skip the install and loadpath generation step if possible. if checksum == installed && system('bundle check 1>/dev/null 2>&1') puts "Gem environment up-to-date." else # run bundle-install to install any missing gems argv = ['--no-color', 'install', '--path', 'vendor/gems'] + ARGV system("bundle", *argv) || begin warn "bundle executable not found. Ensure bundler is installed (`gem " + "install bundler`) and that the gem bin path is in your PATH" exit($?.exitstatus) end # load the Gemfile bundle = Bundler.setup # extract load paths for each gem and write to the config/loadpath file. load_paths = [] bundle.gems.each do |gem| next if gem.name == 'bundler' gem.load_paths.each do |path| if path[0, root.size] == root path = path[(root.size + 1), path.size] load_paths << path else warn "external load path directory detected: #{path}" end end end # move the loadpath and checksum files into place if everything was installed # okay and the load path file was written successfully. File.open('.bundle/loadpath+', 'wb') { |fd| fd.write(load_paths.join("\n")) } File.rename('.bundle/loadpath+', '.bundle/loadpath') File.open('.bundle/checksum', 'wb') { |fd| fd.puts(checksum) } # write binstubs for all executables. we can't use bundler's --binstubs option # because the generated executables require 'bundler/setup'. the binstubs # generated here require only config/basic.rb, which sets up the loadpath # manually using the .bundle/loadpath file. Dir.mkdir "bin" unless File.directory?("bin") template = DATA.read lineno = File.read(__FILE__).count("\n") - template.count("\n") bundle.gems.each do |spec| spec.executables.each do |executable| script = eval('%Q{' + template + '}', binding, __FILE__, lineno) File.open("bin/#{executable}+", 'wb') { |fd| fd.write(script) } File.chmod 0755, "bin/#{executable}+" File.rename("bin/#{executable}+", "bin/#{executable}") end end end __END__ #!/usr/bin/env #{RbConfig::CONFIG['ruby_install_name']} # # This file was generated by script/bootstrap. root = File.expand_path('../..', __FILE__) require "#\{root\}/config/load" gem_path = "#{spec.full_gem_path.sub(root, "#\{root\}")}" load File.join(gem_path, '#{spec.bindir}', '#{executable}')
X Tutup