Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active October 1, 2015 19:59
Show Gist options
  • Save rosskevin/9d6aad6c97fd5e0581dd to your computer and use it in GitHub Desktop.
Save rosskevin/9d6aad6c97fd5e0581dd to your computer and use it in GitHub Desktop.
Bundle Gemfile hack for working with rails engines both locally and otherwise - symlinks directory when in local developer mode.
#----------------------------------------------------------------------
#
# Load all engines
#
# Sample hash - Add to your Gemfile:
# # Load all engines
# @engines = {
# acme: {git: 'git@bitbucket.org:alienfast/acme.git'}
# }
# eval_gemfile File.expand_path 'bundle_engines.rb', File.dirname(__FILE__)
#----------------------------------------------------------------------
# ENV.each do |key, value|
# puts "#{key}: \t#{value}"
# end unless $already_ran
# if osx, use ../acme
# if ci, use engines/acme && checkout
# else use engines/acme
is_developer_env = (ENV['_system_name'] == 'OSX')
is_docker_env = File.exists?('/.dockerinit')
is_ci_env = (is_docker_env || ENV['USER'] == 'apps')
puts 'Running within Docker.' if is_docker_env
# test ci or override e.g. for local package/deployment to elastic beanstalk.
# if packaging elastic beanstalk in dev env: use
# CI=true bundle install; rake eb:package eb:deploy
(is_developer_env = false; is_ci_env = true) if ENV['CI'] == 'true'
# make the engines dir
pwd = File.dirname(__FILE__)
engines_dir = File.expand_path 'engines', pwd
FileUtils.mkdir engines_dir unless File.exists? engines_dir
@engines.each_key do |engine|
options = @engines[engine]
engine_git = options[:git]
engine_branch = options[:branch]
engine = engine.to_s
# determine the path, always use engines/#{engine} as the path so the gemfile.lock doesn't change from dev to ci to production
engine_path = File.expand_path engine, engines_dir
if is_developer_env
project_engine_path = File.expand_path "../#{engine}", pwd
unless $already_ran
# if in dev env and previously ran as CI, kill the dir
if File.exists?(engine_path) && !File.symlink?(engine_path)
FileUtils.rm_rf engine_path
end
# http://superuser.com/questions/842642/how-to-make-a-symlinked-folder-appear-as-a-normal-folder
system "ln -s #{project_engine_path} #{engine_path}" unless File.symlink? engine_path
puts "Developer environment, using #{project_engine_path}"
end
else
# clone or pull the engine gem if using the bamboo CI environment
if is_ci_env && !$already_ran
puts "CI environment, using #{engine_path}" unless $already_ran
# if running as CI and we were in local dev env, kill the symlink
File.delete engine_path if File.symlink? engine_path
if File.exists?("#{engine_path}/.git")
cmd = "cd #{engine_path}; git pull 2>&1"
else
FileUtils.mkdir_p engine_path
options = ''
options += "-b #{engine_branch}" unless engine_branch.nil?
cmd = "git clone #{options} #{engine_git} #{engine_path} 2>&1"
end
output = system("#{cmd}")
raise "\n\nFailed to execute cmd: \n\t#{cmd}\nresult: #{output}" unless $?.success?
else
puts "Non-development environment, using #{engine_path}" unless $already_ran
end
end
# add the gem
gem engine, path: engine_path
# include the optional Gemfile_shared.rb
shared_gemfile = File.expand_path "#{engine_path}/Gemfile_shared.rb", pwd
eval_gemfile shared_gemfile if File.exists?(shared_gemfile)
end
$already_ran = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment