Skip to content

Commit

Permalink
Conditional gem dependency installation within a gemspec (closes #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
zuazo committed Apr 28, 2015
1 parent da7ff7f commit ce4644d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
9 changes: 6 additions & 3 deletions chef-encrypted-attributes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ Gem::Specification.new do |s|
s.add_dependency 'chef', chef_version

# Support old deprecated Ruby versions:
s.add_dependency 'mixlib-shellout', '< 1.6.1' if RUBY_VERSION < '1.9.3'
s.extensions << 'ext/mkrf_conf.rb'
if RUBY_VERSION < '1.9.3'
s.add_development_dependency 'mixlib-shellout', '< 1.6.1'
end
if RUBY_VERSION < '2'
s.add_dependency 'highline', '< 1.7'
s.add_dependency 'ohai', '< 8'
s.add_development_dependency 'highline', '< 1.7'
s.add_development_dependency 'ohai', '< 8'
end

s.add_development_dependency 'rake', '~> 10.0'
Expand Down
52 changes: 52 additions & 0 deletions ext/mkrf_conf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# encoding: UTF-8
#
# Author:: Xabier de Zuazo (<xabier@onddo.com>)
# Copyright:: Copyright (c) 2015 Onddo Labs, SL. (www.onddo.com)
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Conditional gem dependency installation within a gemspec.
#
# Based on:
# * http://www.programmersparadox.com/2012/05/21
# /gemspec-loading-dependent-gems-based-on-the-users-system/
# * https://www.tiredpixel.com/2014/01/05
# /curses-conditional-ruby-gem-installation-within-a-gemspec/

require 'rubygems/dependency_installer'

di = Gem::DependencyInstaller.new

begin
if RUBY_VERSION < '1.9.3'
puts "Installing mixlib-shellout < 1.6.1 because Ruby #{RUBY_VERSION}"
di.install 'mixlib-shellout', '< 1.6.1'
end
if RUBY_VERSION < '2'
puts "Installing highline < 1.7 because Ruby #{RUBY_VERSION}"
s.add_dependency 'highline', '< 1.7'
puts "Installing ohai < 8 because Ruby #{RUBY_VERSION}"
s.add_dependency 'ohai', '< 8'
end
rescue => e
warn "#{$PROGRAM_NAME}: #{e}"
exit!
end

puts 'Writing fake Rakefile'
# Write fake Rakefile for rake since Makefile isn't used
File.open(File.join(File.dirname(__FILE__), 'Rakefile'), 'w') do |f|
f.write("task :default\n")
end

0 comments on commit ce4644d

Please sign in to comment.