How can Bundler/Gemfile be configured to use different gem sources during development?
I have a Sinatra application that requires another gem I'm developing locally. I'm having trouble configuring Bundler to use my local gem code during development but my vendored gem code in production.
Ideally I could do something like this, but Bundler doesn't allow you to specify the same gem twice:
# Doesn't work:
group :development do
gem 'awesome', :path => "~/code/awesome"
end
group :production do
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end
In the meantime I've resorted to manually vendoring the gem & updating the gem source in the Gemfile every single time I deploy, which is quite a hassle. My workflow is this:
- Point to my local gem during development (
gem 'awesome',开发者_StackOverflow :path => "~/code/awesome"
) - When ready to deploy, unpack gem into
vendor/gems
- Update Gemfile to point to vendored gem (
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
) - Run
bundle install
(to update Gemfile.lock) - Deploy code
- Return to step 1.
What a hassle! I'd like to do something cleaner than simply writing Rake tasks to automate my current setup.
What's the best workflow for this scenario?
There is a new feature that allows to do that, by simply specyfing local.gem_name
config option, like:
bundle config local.rack ~/path/to/local/rack
This only works if the gem has a git repo and branch specified in the Gemfile.
See thr Bundler docs for more details: http://bundler.io/v1.3/bundle_config.html
Apparently, you can use regular Ruby in your Gemfile. According to this article you can set an environment variable (or any other variable, I've found), to let you pick which version of a gem you want to use.
## based on an ENV variable
if ENV['RACK_ENV'] == "development"
gem 'awesome', :path => "~/code/awesome"
else
gem 'awesome', :path => "vendor/gems/awesome-0.0.1"
end
Maybe that'll work. If you need to vendor your in-progress gem maybe you could make a tiny little script that'll set the ENV, vendor it, and reset the ENV. Eh?
If you are using doccker to build your containers, you can always set the path as an environment variable in the dockerfile, and use this environment variable in Gemfile. Please find an example of Dockerfile and Gemfile below.
Dockerfile
ARG tenant
ENV mgm=3
ENV GEMBOX_URL='abc.com:9292'
WORKDIR /app
COPY Gemfile* ./
RUN bundle install --without development test
COPY . .
ENTRYPOINT ["entrypoint.sh"]
CMD ["crond", "-f"]
Gemfile
source 'https://rubygems.org/'
source ENV['GEMBOX_URL']
gem 'jwt'
gem 'activerecord-import'
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary-edge', '~> 0.12.5.0'
gem 'zgear', '~> 0.6.4.1', source: ENV['GEMBOX_URL']
gem "piston", '~> 1.3.1', source: ENV['GEMBOX_URL']
gem 'communication_connector', '~> 0.1.4', source: ENV['GEMBOX_URL']
gem 'health_check', source: ENV['GEMBOX_URL']
Here is a suggestion which I didn't get to fully work (used for a spree theme and I got problems with some stylesheets from the theme):
group :production do
gem 'gemname', '~> 0.1.6', :git => 'git://github.com/foouser/gemname.git'
end
group :development do
gem 'gemnamedev', :path => '~/path/gemname' # use local version
end
Duplicate your gemname.gemspec file and call it gemnamedev.gemspec, and change s.name inside it to "gemnamedev".
精彩评论