Check for gems with local :path before deploy - ruby-on-rails

For some Rails applications, I'd like to have a safe-guard when I deploy to check if I have some gems configured to be looked up at a local path.
A little bit of context may help to understand.
When I'm in development mode, I want to have a gem in "local mode". In my Gemfile it is configured like this : gem 'my_gem', '~> 0.9', :path => './path/to/my_gem'.
In production, I want to be like this : gem 'my_gem', '~> 0.9', :git => 'git#git.example.com:my_gem.git'.
I've tried to make a shell script (or function) to read the Gemfile.lock and exit with an error if the gem is in "local mode".
My deployment scripts could use this to abort if I've forgotten to switch back to the proper mode.
Any help will be appreciated.
Thanks

Use
group :development do
gem 'my_gem_for_development', '~> 0.9', :require => './path/to/my_gem/lib/my_gem.rb' , :path => './path/to/my_gem/lib'
end
group :production do
gem 'my_gem', '~> 0.9', :git => 'git#git.example.com:my_gem.git'
end

Is this a gem that you're developing? Why not just write the gem to look at the rails env and change settings based on that. Then you can one canonical version of the gem and you won't have to worry about checking to see which gem version you're using. Otherwise, bor1s' solution will work just fine.

Related

Gemfile require gem in all environments except one

I have rails application with a lot of environments: test, dev, production, staging, testing2 and so on.
I want gem 'puma' to be installed for all environments except test. How can I make it?
The simplest way is to make whitelist:
gem 'puma', group: [:development, :staging, :dev, :production, :testing2]
But this seems too bad, because it'll be better just to make:
gem 'puma', except: [:test]
Is there any way to do it?
Sorry, there is no way to do this.

Conditions on gem requirement

I would like to prevent the updating of a gem on my windows (rmagick), so it sticks to 2.12.0 mswin32. Still, my coworker needs to have the gem on his Darwin install...
So, I tried to do something like this in the Gemfile:
if RUBY_PLATFORM =~ /darwin/i
gem 'rmagick', '~> 2.12.0'
else
gem 'rmagick', '=2.12.0.mswin32'
end
but bundle install complaints.
What is the right way of handling this properly?
You can't use conditionals on gemspec because gemspec is serialized
into YAML, which doesn't contain executable code.
I faced a related problem in the Gemfile of a local Rails project (not a
gem).
Currently, the Gemfile contains:
group :test do
...
# on Mac os X
gem 'rb-fsevent' if RUBY_PLATFORM.include?("x86_64-darwin")
gem 'ruby_gntp' if RUBY_PLATFORM.include?("x86_64-darwin")
# on Linux
gem 'rb-inotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
gem 'libnotify' unless RUBY_PLATFORM.include?("x86_64-darwin")
end
This works (although it is ugly) for developing on Mac and Linux
systems.
But, we stopped checking in the Gemfile.lock since it changes every time
a developer with a different platform checks in the code.
So, a solution for multi-platform Gemfiles should also solve the
problem for Gemfile.lock.
The other solutions is building multiple .gemspec files for each target OS and change both platform and dependencies for each platform:
gemspec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
end
# here build the normal gem
# Now for linux:
gemspec.platform = "linux"
gemspec.add_dependency ...
# build the newer gemspec
...
You should use the platforms option Bundler provides:
If a gem should only be used in a particular platform or set of
platforms, you can specify them. Platforms are essentially identical
to groups, except that you do not need to use the --without
install-time flag to exclude groups of gems for other platforms.
So in your specific case that would look something like this:
gem 'rmagick', '~> 2.12.0', :platforms => :ruby
gem 'rmagick', '=2.12.0.mswin32', :platforms => :mswin

How to edit a gem at Heroku

I'm using 'rails3-jquery-autocomplete' gem, but it doesn't have multi column search, but there is a fork that does it ( more details at: https://github.com/crowdint/rails3-jquery-autocomplete/pull/95).
Now I need to deploy to Heroku but it will install the official gem. How can I edit it? Or if it's not possible, how can I import a gem to the application?
Thanks.
you can use the fork by specifying the :git path in your Gemfile declaration
something like
gem "_GEM_NAME_", :git => "git://git/repository.git"
your other option would be to just vendor the gem inside your into a directory like vendor/gems/_gem_name and then you could use the :path option as well
gem "_GEM_NAME_", :path => "vendor/gems/_gem_name"

How can I use the latest version of the Sunspot gem with Solr Cell?

I've been trying (in vain) to get the latest version of the Sunspot gem (currently 2.0.0.pre.111215, incorporating Solr 3.5) working with Solr Cell.
Currently I am using the older version of Sunspot in combination with Solr Cell provided by the following plugin - https://github.com/chebyte/sunspot_cell.
My Gemfile for this configuration is as follows;
gem 'sunspot', '1.2.1'
gem 'sunspot_rails'
Unfortunately this older combination of Solr/Solr cell does not work with many newer PDF files. The recommended solution by Apache is to upgrade to the latest version of both.
The Sunspot Solr Cell bolt-on doesn't seem to be very well supported. The most recently updated version has been switched from a Plugin to a Gem, but I still can't get it to work with the latest version of the Sunspot Gem. https://github.com/zheileman/sunspot_cell
Does anyone know the correct Gemfile configuration to get the elements to play nicely together? The closest I have is this.
gem 'sunspot_cell', :git => 'git://github.com/zheileman/sunspot_cell.git'
gem 'sunspot', :git => "git://github.com/sunspot/sunspot.git"
gem 'sunspot_rails', :git => "git://github.com/sunspot/sunspot.git", :require => "sunspot_rails"
group :development, :test do
gem 'sunspot_solr', :git => "git://github.com/sunspot/sunspot.git"
end
When I run any rake task I get the following error.
uninitialized constant Sunspot::RSolr
If I comment the sunspot_cell gem out temporarily, I can get Rake tasks to run but actual searching fails.
I've tried manually using the Solr jar files in this gem instead of the ones bundled in sunspot_solr but also without success.
After a bit of trial and error, the answer to this turned out to be easier than I had hoped. The gems were apparently specified in the wrong order. I didn't realise it made any difference. I changed the order so that sunspot_cell was last and it burst into like. Magic! (almost).
gem 'sunspot', :git => "git://github.com/sunspot/sunspot.git"
gem 'sunspot_rails', :git => "git://github.com/sunspot/sunspot.git", :require => "sunspot_rails"
gem 'sunspot_cell', :git => 'git://github.com/zheileman/sunspot_cell.git'
Glad to know you already fixed it.
This is my current setup. A pretty big mess of gems, I know =)
gem 'rsolr', :git => 'git://github.com/mwmitchell/rsolr', :branch => "38b5b69a0d63cdf85560806c06f3187ea4339f5a" # 1.0.6 plus the timeout patch
gem 'sunspot'
gem 'sunspot_solr'
gem 'sunspot_rails'
gem 'sunspot_index_queue'
gem 'sunspot_cell', :git => 'git://github.com/zheileman/sunspot_cell.git', :branch => "bc8ac18de1410b3e29af89c4d028acc6deba1e1c"

how to add gem dependency with :path and :branch

I am working on a rails gem that has dependency on the following
gem 'authlogic', :git => 'git://github.com/odorcicd/authlogic.git', :branch => 'rails3'
How can I add this in the gem spec as a dependency? Specifically I need to specifiy the path and branch in the dependency.
You definitely want to use Bundler then. You would put exactly what you have into the Gemfile file. Go checkout the link to Bundler I left below.
-- older info --
For jeweler you would add something like this:
gem.add_dependency 'authlogic', '> 1.0', '<= 2.0'
But you might be better off using Bundler. It's not just for rails: http://gembundler.com/
You need generate the gem. Publish it and after use this gem deploy.

Resources