Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'spec-activerecord' of github.com:winton/gem_template

Conflicts:
	Rakefile
	gem_template.gemspec
	lib/gem_template.rb
	lib/mover/gems.rb
	spec/Rakefile
	spec/spec_helper.rb
  • Loading branch information...
commit 03bbbfbcf16d501038704d8a1a77a690c9bd5682 2 parents 2d6115f + c9ebbd4
Winton Welsh authored
2  LICENSE
View
@@ -1,4 +1,4 @@
-Copyright (c) 2010 Winton Welsh
+Copyright (c) 2010
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
85 Rakefile
View
@@ -1,9 +1,8 @@
require File.dirname(__FILE__) + '/lib/mover/gems'
-Mover::Gems.require(:rake)
+Mover::Gems.activate %w(rake rspec)
require 'rake'
-require 'rake/gempackagetask'
require 'spec/rake/spectask'
def gemspec
@@ -13,13 +12,6 @@ def gemspec
end
end
-if defined?(Rake::GemPackageTask)
- Rake::GemPackageTask.new(gemspec) do |pkg|
- pkg.gem_spec = gemspec
- end
- task :gem => :gemspec
-end
-
if defined?(Spec::Rake::SpecTask)
desc "Run specs"
Spec::Rake::SpecTask.new do |t|
@@ -28,33 +20,60 @@ if defined?(Spec::Rake::SpecTask)
t.warning = true
end
task :spec
+ task :default => :spec
+end
+
+desc "Build gem(s)"
+task :gem do
+ old_gemset = ENV['GEMSET']
+ root = File.expand_path('../', __FILE__)
+ pkg = "#{root}/pkg"
+ system "rm -Rf #{pkg}"
+ Mover::Gems.gemset_names.each do |gemset|
+ ENV['GEMSET'] = gemset.to_s
+ system "cd #{root} && gem build mover.gemspec"
+ system "mkdir -p #{pkg} && mv *.gem pkg"
+ end
+ ENV['GEMSET'] = old_gemset
+end
+
+namespace :gem do
+ desc "Install gem(s)"
+ task :install do
+ Rake::Task['gem'].invoke
+ Dir["#{File.dirname(__FILE__)}/pkg/*.gem"].each do |pkg|
+ system "gem install #{pkg} --no-ri --no-rdoc"
+ end
+ end
+
+ desc "Push gem(s)"
+ task :push do
+ Rake::Task['gem'].invoke
+ Dir["#{File.dirname(__FILE__)}/pkg/*.gem"].each do |pkg|
+ system "gem push #{pkg}"
+ end
+ end
end
namespace :gems do
- desc "Install gems (DEV=1|0 DOCS=1|0 SUDO=1|0)"
+ desc "Install gem dependencies (DEV=0 DOCS=0 GEMSPEC=default SUDO=0)"
task :install do
- file = File.dirname(__FILE__) + '/gems'
- sudo = (ENV['SUDO'] ||= '0').to_i
- docs = (ENV['DOCS'] ||= '0').to_i
- sudo = sudo == 1 ? 'sudo' : ''
- docs = docs == 1 ? '' : '--no-ri --no-rdoc'
- gems = []
+ dev = ENV['DEV'] == '1'
+ docs = ENV['DOCS'] == '1' ? '' : '--no-ri --no-rdoc'
+ gemset = ENV['GEMSET']
+ sudo = ENV['SUDO'] == '1' ? 'sudo' : ''
- if File.exists?(file)
- File.open(file, 'r') do |f|
- gems = f.readlines.collect do |line|
- line.split(' ')
- end
- end
+ Mover::Gems.gemset = gemset if gemset
+
+ if dev
+ gems = Mover::Gems.gemspec.development_dependencies
else
- gems = Mover::Gems::TYPES[:gemspec]
- gems = Mover::Gems::TYPES[:gemspec_dev] if ENV['DEV'] == '1'
- gems.collect! do |g|
- [ g.to_s, Mover::Gems::VERSIONS[g] ]
- end
+ gems = Mover::Gems.gemspec.dependencies
end
- gems.each do |(name, version)|
+ gems.each do |name|
+ name = name.to_s
+ version = Mover::Gems.versions[name]
if Gem.source_index.find_name(name, version).empty?
version = version ? "-v #{version}" : ''
system "#{sudo} gem install #{name} #{version} #{docs}"
@@ -65,15 +84,7 @@ namespace :gems do
end
end
-desc "Install gem locally"
-task :install => :package do
- sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
-end
-
desc "Validate the gemspec"
task :gemspec do
gemspec.validate
-end
-
-task :package => :gemspec
-task :default => :spec
+end
5 config/gemsets.yml
View
@@ -0,0 +1,5 @@
+mover:
+ rake: >=0.8.7
+ rspec: ~>1.0
+ default:
+ active_wrapper: =0.4.3
13 config/gemspec.yml
View
@@ -0,0 +1,13 @@
+name: mover
+version: 0.3.5
+authors:
+ - Winton Welsh
+email: mail@wintoni.us
+homepage: http://github.com/winton/mover
+summary: Move ActiveRecord records across tables like it ain't no thang
+description: Move ActiveRecord records across tables like it ain't no thang
+dependencies: null
+development_dependencies:
+ - active_wrapper
+ - rake
+ - rspec
4 lib/mover.rb
View
@@ -1,11 +1,7 @@
require File.dirname(__FILE__) + '/mover/gems'
-Mover::Gems.require(:lib)
-
$:.unshift File.dirname(__FILE__)
-require 'mover/version'
-
module Mover
def self.included(base)
164 lib/mover/gems.rb
View
@@ -1,41 +1,151 @@
unless defined?(Mover::Gems)
- require 'rubygems'
+ require 'yaml'
module Mover
- class Gems
-
- VERSIONS = {
- :active_wrapper => '=0.3.4',
- :rake => '=0.8.7',
- :rspec => '=1.3.1'
- }
-
- TYPES = {
- :gemspec => [],
- :gemspec_dev => [ :active_wrapper, :rspec ],
- :lib => [],
- :rake => [ :rake, :rspec ],
- :spec => [ :active_wrapper, :rspec ],
- :spec_rake => [ :active_wrapper ]
- }
-
+ module Gems
class <<self
- def lockfile
- file = File.expand_path('../../../gems', __FILE__)
- unless File.exists?(file)
- File.open(file, 'w') do |f|
- Gem.loaded_specs.each do |key, value|
- f.puts "#{key} #{value.version.version}"
+ attr_accessor :config
+ attr_reader :gemset, :gemsets, :versions
+
+ class SimpleStruct
+ attr_reader :hash
+
+ def initialize(hash)
+ @hash = hash
+ @hash.each do |key, value|
+ self.class.send(:define_method, key) { @hash[key] }
+ self.class.send(:define_method, "#{key}=") { |v| @hash[key] = v }
+ end
+ end
+ end
+
+ Gems.config = SimpleStruct.new(
+ :gemsets => [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ],
+ :gemspec => "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml",
+ :warn => true
+ )
+
+ def activate(*gems)
+ begin
+ require 'rubygems' unless defined?(::Gem)
+ rescue LoadError
+ puts "rubygems library could not be required" if @config.warn
+ end
+
+ self.gemset ||= gemset_from_loaded_specs
+
+ gems.flatten.collect(&:to_sym).each do |name|
+ version = @versions[name]
+ vendor = File.expand_path("../../../vendor/#{name}/lib", __FILE__)
+ if File.exists?(vendor)
+ $:.unshift vendor
+ elsif defined?(gem)
+ gem name.to_s, version
+ else
+ puts "#{name} #{"(#{version})" if version} failed to activate" if @config.warn
+ end
+ end
+ end
+
+ def dependencies
+ dependency_filter(@gemspec.dependencies, @gemset)
+ end
+
+ def development_dependencies
+ dependency_filter(@gemspec.development_dependencies, @gemset)
+ end
+
+ def gemset=(gemset)
+ if gemset
+ @gemset = gemset.to_sym
+
+ @gemsets = @config.gemsets.reverse.collect { |config|
+ if config.is_a?(::String)
+ YAML::load(File.read(config)) rescue {}
+ elsif config.is_a?(::Hash)
+ config
+ end
+ }.inject({}) do |hash, config|
+ deep_merge(hash, symbolize_keys(config))
+ end
+
+ @versions = (@gemsets[gemspec.name.to_sym] || {}).inject({}) do |hash, (key, value)|
+ if !value.is_a?(::Hash) && value
+ hash[key] = value
+ elsif key == @gemset
+ (value || {}).each { |k, v| hash[k] = v }
end
+ hash
end
+ else
+ @gemset = nil
+ @gemsets = nil
+ @versions = nil
+ end
+ end
+
+ def gemset_names
+ (
+ [ :default ] +
+ @gemsets[gemspec.name.to_sym].inject([]) { |array, (key, value)|
+ array.push(key) if value.is_a?(::Hash) || value.nil?
+ array
+ }
+ ).uniq
+ end
+
+ def gemspec(reload=false)
+ if @gemspec && !reload
+ @gemspec
+ else
+ data = YAML::load(File.read(@config.gemspec)) rescue {}
+ @gemspec = SimpleStruct.new(data)
+ end
+ end
+
+ private
+
+ def deep_merge(first, second)
+ merger = lambda do |key, v1, v2|
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
end
+ first.merge(second, &merger)
end
- def require(type=nil)
- (TYPES[type] || TYPES.values.flatten.compact).each do |name|
- gem name.to_s, VERSIONS[name]
+ def dependency_filter(dependencies, match)
+ (dependencies || []).inject([]) { |array, value|
+ if value.is_a?(::Hash)
+ array += value[match.to_s] if value[match.to_s]
+ else
+ array << value
+ end
+ array
+ }.uniq.collect(&:to_sym)
+ end
+
+ def gemset_from_loaded_specs
+ if defined?(Gem)
+ Gem.loaded_specs.each do |name, spec|
+ if name == gemspec.name
+ return :default
+ elsif name[0..gemspec.name.length] == "#{gemspec.name}-"
+ return name[gemspec.name.length+1..-1].to_sym
+ end
+ end
+ :default
+ else
+ :none
+ end
+ end
+
+ def symbolize_keys(hash)
+ return {} unless hash.is_a?(::Hash)
+ hash.inject({}) do |options, (key, value)|
+ value = symbolize_keys(value) if value.is_a?(::Hash)
+ options[(key.to_sym rescue key) || key] = value
+ options
end
end
end
3  lib/mover/version.rb
View
@@ -1,3 +0,0 @@
-module Mover
- VERSION = "0.3.5" unless defined?(::Mover::VERSION)
-end
39 mover.gemspec
View
@@ -1,29 +1,32 @@
# -*- encoding: utf-8 -*-
-lib = File.expand_path('../lib/', __FILE__)
+root = File.expand_path('../', __FILE__)
+lib = "#{root}/lib"
$:.unshift lib unless $:.include?(lib)
-
+
require 'mover/gems'
-require 'mover/version'
+Mover::Gems.gemset ||= ENV['GEMSET'] || :default
Gem::Specification.new do |s|
- s.name = "mover"
- s.version = Mover::VERSION
- s.platform = Gem::Platform::RUBY
- s.authors = ["Winton Welsh"]
- s.email = ["mail@wintoni.us"]
- s.homepage = "http://github.com/winton/mover"
- s.summary = "Move ActiveRecord records across tables like it ain't no thang"
- s.description = "Move ActiveRecord records across tables like it ain't no thang"
+ Mover::Gems.gemspec.hash.each do |key, value|
+ if key == 'name' && Mover::Gems.gemset != :default
+ s.name = "#{value}-#{Mover::Gems.gemset}"
+ elsif key == 'summary' && Mover::Gems.gemset == :solo
+ s.summary = value + " (no dependencies)"
+ elsif !%w(dependencies development_dependencies).include?(key)
+ s.send "#{key}=", value
+ end
+ end
- Mover::Gems::TYPES[:gemspec].each do |g|
- s.add_dependency g.to_s, Mover::Gems::VERSIONS[g]
+ Mover::Gems.dependencies.each do |g|
+ s.add_dependency g.to_s, Mover::Gems.versions[g]
end
- Mover::Gems::TYPES[:gemspec_dev].each do |g|
- s.add_development_dependency g.to_s, Mover::Gems::VERSIONS[g]
+ Mover::Gems.development_dependencies.each do |g|
+ s.add_development_dependency g.to_s, Mover::Gems.versions[g]
end
- s.files = Dir.glob("{bin,lib}/**/*") + %w(LICENSE README.md)
- s.executables = Dir.glob("{bin}/*").collect { |f| File.basename(f) }
- s.require_path = 'lib'
+ s.executables = `cd #{root} && git ls-files -- {bin}/*`.split("\n").collect { |f| File.basename(f) }
+ s.files = `cd #{root} && git ls-files`.split("\n")
+ s.require_paths = %w(lib)
+ s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
end
2  spec/Rakefile
View
@@ -1,5 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../lib/mover/gems')
-Mover::Gems.require(:spec_rake)
+Mover::Gems.activate(:active_wrapper)
require 'active_wrapper/tasks'
9 spec/fixtures/gemsets.yml
View
@@ -0,0 +1,9 @@
+name:
+ rake: =0.8.7
+ default:
+ mysql: =2.8.1
+ rspec: =1.3.1
+ rspec2:
+ mysql2: =0.2.6
+ rspec: =2.3.0
+ solo: null
15 spec/fixtures/gemspec.yml
View
@@ -0,0 +1,15 @@
+name: name
+version: 0.1.0
+authors:
+ - Author
+email: email@email.com
+homepage: http://github.com/author/name
+summary: Summary
+description: Description
+dependencies:
+ - rake
+ - default:
+ - mysql
+ - rspec2:
+ - mysql2
+development_dependencies: null
249 spec/mover/gems_spec.rb
View
@@ -0,0 +1,249 @@
+require 'spec_helper'
+
+describe Mover::Gems do
+
+ before(:each) do
+ @old_config = Mover::Gems.config
+
+ Mover::Gems.config.gemspec = "#{$root}/spec/fixtures/gemspec.yml"
+ Mover::Gems.config.gemsets = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ Mover::Gems.config.warn = true
+
+ Mover::Gems.gemspec true
+ Mover::Gems.gemset = nil
+ end
+
+ after(:each) do
+ Mover::Gems.config = @old_config
+ end
+
+ describe :activate do
+ it "should activate gems" do
+ Mover::Gems.stub!(:gem)
+ Mover::Gems.should_receive(:gem).with('rspec', '=1.3.1')
+ Mover::Gems.should_receive(:gem).with('rake', '=0.8.7')
+ Mover::Gems.activate :rspec, 'rake'
+ end
+ end
+
+ describe :gemset= do
+ before(:each) do
+ Mover::Gems.config.gemsets = [
+ {
+ :name => {
+ :rake => '>0.8.6',
+ :default => {
+ :externals => '=1.0.2'
+ }
+ }
+ },
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ end
+
+ describe :default do
+ before(:each) do
+ Mover::Gems.gemset = :default
+ end
+
+ it "should set @gemset" do
+ Mover::Gems.gemset.should == :default
+ end
+
+ it "should set @gemsets" do
+ Mover::Gems.gemsets.should == {
+ :name => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :mysql => "=2.8.1",
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => {
+ :mysql2 => "=0.2.6",
+ :rspec => "=2.3.0"
+ },
+ :solo => nil
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ Mover::Gems.versions.should == {
+ :externals => "=1.0.2",
+ :mysql => "=2.8.1",
+ :rake => ">0.8.6",
+ :rspec => "=1.3.1"
+ }
+ end
+
+ it "should return proper values for Gems.dependencies" do
+ Mover::Gems.dependencies.should == [ :rake, :mysql ]
+ Mover::Gems.development_dependencies.should == []
+ end
+
+ it "should return proper values for Gems.gemset_names" do
+ Mover::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ end
+ end
+
+ describe :rspec2 do
+ before(:each) do
+ Mover::Gems.gemset = "rspec2"
+ end
+
+ it "should set @gemset" do
+ Mover::Gems.gemset.should == :rspec2
+ end
+
+ it "should set @gemsets" do
+ Mover::Gems.gemsets.should == {
+ :name => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :mysql => "=2.8.1",
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => {
+ :mysql2=>"=0.2.6",
+ :rspec => "=2.3.0"
+ },
+ :solo => nil
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ Mover::Gems.versions.should == {
+ :mysql2 => "=0.2.6",
+ :rake => ">0.8.6",
+ :rspec => "=2.3.0"
+ }
+ end
+
+ it "should return proper values for Gems.dependencies" do
+ Mover::Gems.dependencies.should == [ :rake, :mysql2 ]
+ Mover::Gems.development_dependencies.should == []
+ end
+
+ it "should return proper values for Gems.gemset_names" do
+ Mover::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ end
+ end
+
+ describe :solo do
+ before(:each) do
+ Mover::Gems.gemset = :solo
+ end
+
+ it "should set @gemset" do
+ Mover::Gems.gemset.should == :solo
+ end
+
+ it "should set @gemsets" do
+ Mover::Gems.gemsets.should == {
+ :name => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :mysql => "=2.8.1",
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => {
+ :mysql2=>"=0.2.6",
+ :rspec => "=2.3.0"
+ },
+ :solo => nil
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ Mover::Gems.versions.should == {:rake=>">0.8.6"}
+ end
+
+ it "should return proper values for Gems.dependencies" do
+ Mover::Gems.dependencies.should == [:rake]
+ Mover::Gems.development_dependencies.should == []
+ end
+
+ it "should return proper values for Gems.gemset_names" do
+ Mover::Gems.gemset_names.should == [ :default, :rspec2, :solo ]
+ end
+ end
+
+ describe :nil do
+ before(:each) do
+ Mover::Gems.gemset = nil
+ end
+
+ it "should set everything to nil" do
+ Mover::Gems.gemset.should == nil
+ Mover::Gems.gemsets.should == nil
+ Mover::Gems.versions.should == nil
+ end
+ end
+ end
+
+ describe :gemset_from_loaded_specs do
+ before(:each) do
+ Gem.stub!(:loaded_specs)
+ end
+
+ it "should return the correct gemset for name gem" do
+ Gem.should_receive(:loaded_specs).and_return({ "name" => nil })
+ Mover::Gems.send(:gemset_from_loaded_specs).should == :default
+ end
+
+ it "should return the correct gemset for name-rspec gem" do
+ Gem.should_receive(:loaded_specs).and_return({ "name-rspec2" => nil })
+ Mover::Gems.send(:gemset_from_loaded_specs).should == :rspec2
+ end
+ end
+
+ describe :reload_gemspec do
+ it "should populate @gemspec" do
+ Mover::Gems.gemspec.hash.should == {
+ "name" => "name",
+ "version" => "0.1.0",
+ "authors" => ["Author"],
+ "email" => "email@email.com",
+ "homepage" => "http://github.com/author/name",
+ "summary" => "Summary",
+ "description" => "Description",
+ "dependencies" => [
+ "rake",
+ { "default" => [ "mysql" ] },
+ { "rspec2" => [ "mysql2" ] }
+ ],
+ "development_dependencies" => nil
+ }
+ end
+
+ it "should create methods from keys of @gemspec" do
+ Mover::Gems.gemspec.name.should == "name"
+ Mover::Gems.gemspec.version.should == "0.1.0"
+ Mover::Gems.gemspec.authors.should == ["Author"]
+ Mover::Gems.gemspec.email.should == "email@email.com"
+ Mover::Gems.gemspec.homepage.should == "http://github.com/author/name"
+ Mover::Gems.gemspec.summary.should == "Summary"
+ Mover::Gems.gemspec.description.should == "Description"
+ Mover::Gems.gemspec.dependencies.should == [
+ "rake",
+ { "default" => ["mysql"] },
+ { "rspec2" => [ "mysql2" ] }
+ ]
+ Mover::Gems.gemspec.development_dependencies.should == nil
+ end
+
+ it "should produce a valid gemspec" do
+ Mover::Gems.gemset = :default
+ gemspec = File.expand_path("../../../mover.gemspec", __FILE__)
+ gemspec = eval(File.read(gemspec), binding, gemspec)
+ gemspec.validate.should == true
+ end
+ end
+end
12 spec/spec_helper.rb
View
@@ -1,13 +1,14 @@
+require 'pp'
+
$root = File.expand_path('../../', __FILE__)
require "#{$root}/lib/mover/gems"
-Mover::Gems.require(:spec)
+Mover::Gems.activate %w(active_wrapper rspec)
require 'active_wrapper'
require 'fileutils'
require "#{$root}/lib/mover"
-require 'pp'
require "#{$root}/spec/fixtures/article"
require "#{$root}/spec/fixtures/article_archive"
@@ -54,11 +55,4 @@ def create_records(klass, values={})
record.update_attributes(values)
record
end
-end
-
-# For use with rspec textmate bundle
-def debug(object)
- puts "<pre>"
- puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
- puts "</pre>"
end
Please sign in to comment.
Something went wrong with that request. Please try again.