Skip to content

Instantly share code, notes, and snippets.

@iberianpig
Last active July 26, 2023 10:43
Show Gist options
  • Save iberianpig/6599889cb3f39707ded6d8569add71a5 to your computer and use it in GitHub Desktop.
Save iberianpig/6599889cb3f39707ded6d8569add71a5 to your computer and use it in GitHub Desktop.
Gemfile.local outside of version control
custom_gemfile = <<~GEMFILE
gem "solargraph", require: false
gem 'solargraph-rails', require: false
# gem 'rails', path: '~/.ghq/github.com/rails/rails'
# gem 'activerecord', path: '~/.ghq/github.com/rails/rails/activerecord'
gem 'actionpack', path: '~/.ghq/github.com/rails/rails/actionpack'
gem 'activesupport', path: '~/.ghq/github.com/rails/rails/activesupport'
gem 'debug'
GEMFILE
ignore_gem = [
# 'awesome_print'
]
custom_lines = {} # { gem_name => gem_line }
custom_gemfile.each_line do |line|
pattern = /^\s*gem ['"](?<name>.+?)['"]/
matched = line.match(pattern)
custom_lines[matched[:name]] = line if matched
end
new_content = ''
replaced_gems = []
File.open('Gemfile').each_line do |original_line|
next if ignore_gem.any? { |original_gem| original_line =~ /^\s*gem ['"](?<name>#{original_gem})['"]/ }
replace_gem, replace_line = custom_lines.find { |name, _| original_line =~ /^\s*gem ['"](?<name>#{name})['"]/ }
if replace_gem
# Replace gem line with custom gem line
new_content << new_line
replaced_gems << replace_gem
else
# Keep original gem line
new_content << original_line
end
end
# Add custom gem that are not in Gemfile
(custom_lines.keys - replaced_gems).each do |name|
new_content << custom_lines[name]
end
FileUtils.cp 'Gemfile.lock', 'Gemfile.local.lock' if File.exist?('Gemfile.lock')
instance_eval new_content
@iberianpig
Copy link
Author

.envrc

export BUNDLE_GEMFILE=Gemfile.local

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment