# # parse_dependencies_for # def parse_project_gemfile required_gems, required_fake_gems = [], [] gem_file = "#{project_dir}/lib/#{project[:name]}/gems.rb" if File.exist? gem_file # puts "Parsing gemfile #{gem_file}" code = File.open(gem_file){|f| f.read} stub_class = Class.new stub_class.send(:define_method, :gem){|*args| required_gems << args} stub_class.send(:define_method, :fake_gem){|*args| required_fake_gems << args} stub = stub_class.new stub.instance_eval code, __FILE__, __LINE__ end return required_gems, required_fake_gems end # # Install gems # namespace :gem do desc "Install gem required by project" task :install do gems, fake_gems = parse_project_gemfile gems.each do |name, version| puts "Installing gem #{name} #{version}" %x(gem install #{name}#{" -v #{version}" if version} --ignore-dependencies) end end desc "List all gems required by project" task :list do puts "Gems required for #{project[:official_name]}:" gems, fake_gems = parse_project_gemfile puts(gems + fake_gems) end desc "Install all gem required by project (including fake gems)" task :install_all do gems, fake_gems = parse_project_gemfile (gems + fake_gems).each do |name, version| puts "Installing gem #{name} #{version}" %x(gem install #{name} #{"-v #{version}" if version}) end end end # # Gem Spec, for sample look at Rakefile # require 'rake/clean' require 'rake/gempackagetask' require 'tmpdir' class GemHelper class << self def next_version gem_name %x(gem list --remote #{gem_name}).scan /#{gem_name} \((\d+\.\d+\.\d+)\)/ do |s| v = s.first.split('.').collect{|d| d.to_i} v[-1] = v[-1] + 1 return v.join('.') end return '0.0.1' end def prepare_gem_metadata spec = Gem::Specification.new do |s| gems, fake_gems = parse_project_gemfile gems = gems + fake_gems gems.each{|name_version| s.add_dependency *name_version} options = project.clone options.delete(:name) options.delete(:gem) || raise("this project isn't a gem!") name = options.delete(:official_name) s.name = name s.platform = options.delete(:platform) || Gem::Platform::RUBY s.has_rdoc = options.delete(:has_rdoc) == nil ? false : true s.require_path = options.delete(:lib) || "lib" s.files = options.delete(:files) || (%w{Rakefile readme.md} + Dir.glob("{lib,spec}/**/*")) s.bindir = options.delete(:bin) if options.include? :bin s.version = options.delete(:version) || GemHelper.next_version(name) # s.name = options[:name) # s.author = options[:author) # s.homepage = options[:homepage) options.each{|k, v| s.send "#{k}=", v} end Rake::GemPackageTask.new(spec) do |p| p.need_tar = true if RUBY_PLATFORM !~ /mswin/ p.need_zip = true p.package_dir = package_dir end end def package_dir Dir.tmpdir + "/#{project[:official_name]}_tmp" end end end namespace :gem do task :ensure_gem do GemHelper.prepare_gem_metadata end task push: :ensure_gem do gem_file = Dir.glob("#{GemHelper.package_dir}/#{project[:official_name]}*.gem").first system "gem push #{gem_file}" end task clean: :ensure_gem do GemHelper.prepare_gem_metadata system "rm -r #{GemHelper.package_dir}" end desc "Build and publish project as gem" task release: [:ensure_gem, :gem, :push, :clean] end