Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into spec-activerecord

Conflicts:
	lib/gem_template/gems.rb
	spec/spec_helper.rb
  • Loading branch information...
commit 5a0fe41da9fa35976dd3982cfe88b5f69f363da8 2 parents 7ed5d40 + 74e0e65
@winton authored
View
2  LICENSE
@@ -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
View
2  Rakefile
@@ -1,6 +1,6 @@
require File.dirname(__FILE__) + '/lib/gem_template/gems'
-GemTemplate::Gems.require(:rake)
+GemTemplate::Gems.activate %w(rake rspec)
require 'rake'
require 'rake/gempackagetask'
View
5 config/gemsets.yml
@@ -0,0 +1,5 @@
+gem_template:
+ rake: =0.8.7
+ rspec: =1.3.1
+ default:
+ active_wrapper: =0.3.4
View
13 config/gemspec.yml
@@ -0,0 +1,13 @@
+name: gem_template
+version: 0.1.0
+authors:
+ -
+email:
+homepage:
+summary:
+description:
+dependencies:
+ -
+development_dependencies:
+ - active_wrapper
+ - rspec
View
34 gem_template.gemspec
@@ -1,29 +1,21 @@
# -*- encoding: utf-8 -*-
-lib = File.expand_path('../lib/', __FILE__)
-$:.unshift lib unless $:.include?(lib)
-
-require 'gem_template/gems'
-require 'gem_template/version'
-
Gem::Specification.new do |s|
- s.name = "gem_template"
- s.version = GemTemplate::VERSION
- s.platform = Gem::Platform::RUBY
- s.authors = ["Winton Welsh"]
- s.email = ["mail@wintoni.us"]
- s.homepage = "http://github.com/winton/gem_template"
- s.summary = ""
- s.description = ""
+ GemTemplate::Gems.gemspec.hash.each do |key, value|
+ unless %w(dependencies development_dependencies).include?(key)
+ s.send "#{key}=", value
+ end
+ end
- GemTemplate::Gems::TYPES[:gemspec].each do |g|
- s.add_dependency g.to_s, GemTemplate::Gems::VERSIONS[g]
+ GemTemplate::Gems.gemspec.dependencies.each do |g|
+ s.add_dependency g.to_s, GemTemplate::Gems.versions[g]
end
- GemTemplate::Gems::TYPES[:gemspec_dev].each do |g|
- s.add_development_dependency g.to_s, GemTemplate::Gems::VERSIONS[g]
+ GemTemplate::Gems.gemspec.development_dependencies.each do |g|
+ s.add_development_dependency g.to_s, GemTemplate::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 = `git ls-files -- {bin}/*`.split("\n").collect { |f| File.basename(f) }
+ s.files = `git ls-files`.split("\n")
+ s.require_paths = %w(lib)
+ s.test_files = `git ls-files -- {features,test,spec}/*`.split("\n")
end
View
4 lib/gem_template.rb
@@ -1,10 +1,8 @@
require File.dirname(__FILE__) + '/gem_template/gems'
-GemTemplate::Gems.require(:lib)
+GemTemplate::Gems.activate %w()
$:.unshift File.dirname(__FILE__)
-require 'gem_template/version'
-
module GemTemplate
end
View
118 lib/gem_template/gems.rb
@@ -1,41 +1,105 @@
unless defined?(GemTemplate::Gems)
- require 'rubygems'
+ require 'ostruct'
+ require 'yaml'
module GemTemplate
- 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 :configs, :gemset, :gemsets, :gemspec, :testing, :versions, :warn
+
+ Gems.configs = [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ]
+ Gems.testing = false
+ Gems.warn = true
+
+ class Gemspec
+ attr_accessor :hash
+
+ def initialize(hash)
+ @hash = hash
+ @hash.each do |key, value|
+ self.class.send(:define_method, key) { value }
+ end
+ end
+ end
+
+ def activate(*gems)
+ begin
+ require 'rubygems' if !defined?(::Gem) || @testing
+ rescue LoadError
+ puts "rubygems library could not be required" if @warn
+ end
+
+ self.gemset = :default unless defined?(@gemset) && @gemset
+
+ gems.flatten.collect(&:to_sym).each do |name|
+ version = @versions[name]
+ if defined?(gem)
+ gem name.to_s, version
+ else
+ puts "#{name} #{"(#{version})" if version} failed to activate" if @warn
+ end
+ end
+ end
+
+ def gemset=(gemset)
+ if gemset
+ @gemset = gemset.to_sym
+
+ @gemsets = @configs.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?(::String)
+ hash[key] = value
+ elsif value.is_a?(::Hash) && key == @gemset
+ value.each { |k, v| hash[k] = v }
end
+ hash
end
+ else
+ @gemset = nil
+ @gemsets = nil
+ @versions = nil
end
end
- def require(type=nil)
- (TYPES[type] || TYPES.values.flatten.compact).each do |name|
- gem name.to_s, VERSIONS[name]
+ def reload_gemspec
+ data =
+ YAML::load(
+ File.read(
+ "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml"
+ )
+ ) rescue {}
+
+ @gemspec = Gemspec.new(data)
+ end
+
+ Gems.reload_gemspec
+
+ 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 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
View
3  lib/gem_template/version.rb
@@ -1,3 +0,0 @@
-module GemTemplate
- VERSION = "0.1.0" unless defined?(::GemTemplate::VERSION)
-end
View
2  spec/Rakefile
@@ -1,5 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '/../lib/gem_template/gems')
-GemTemplate::Gems.require(:spec_rake)
+GemTemplate::Gems.activate(:active_wrapper)
require 'active_wrapper/tasks'
View
6 spec/fixtures/gemsets.yml
@@ -0,0 +1,6 @@
+gem_template:
+ rake: =0.8.7
+ default:
+ rspec: =1.3.1
+ rspec2:
+ rspec: =2.3.0
View
12 spec/fixtures/gemspec.yml
@@ -0,0 +1,12 @@
+name: name
+version: 0.1.0
+authors:
+ - Author
+email: email@email.com
+homepage: http://github.com/author/name
+summary: Summary
+description: Description
+dependencies:
+ - rake
+development_dependencies:
+ - rspec
View
175 spec/gem_template/gems_spec.rb
@@ -0,0 +1,175 @@
+require 'spec_helper'
+
+describe GemTemplate::Gems do
+
+ describe :activate do
+ before(:each) do
+ GemTemplate::Gems.configs = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ GemTemplate::Gems.gemset = nil
+ GemTemplate::Gems.testing = true
+ end
+
+ it "should warn if unable to require rubygems" do
+ GemTemplate::Gems.stub!(:require)
+ GemTemplate::Gems.should_receive(:require).with('rubygems').and_raise(LoadError)
+ GemTemplate::Gems.stub!(:gem)
+ out = capture_stdout do
+ GemTemplate::Gems.activate :rspec
+ end
+ out.should =~ /rubygems library could not be required/
+ end
+
+ it "should activate gems" do
+ GemTemplate::Gems.stub!(:gem)
+ GemTemplate::Gems.should_receive(:gem).with('rspec', '=1.3.1')
+ GemTemplate::Gems.should_receive(:gem).with('rake', '=0.8.7')
+ GemTemplate::Gems.activate :rspec, 'rake'
+ end
+ end
+
+ describe :gemset= do
+ before(:each) do
+ GemTemplate::Gems.configs = [
+ {
+ :gem_template => {
+ :rake => '>0.8.6',
+ :default => {
+ :externals => '=1.0.2'
+ }
+ }
+ },
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ end
+
+ describe :default do
+ before(:each) do
+ GemTemplate::Gems.gemset = :default
+ end
+
+ it "should set @gemset" do
+ GemTemplate::Gems.gemset.should == :default
+ end
+
+ it "should set @gemsets" do
+ GemTemplate::Gems.gemsets.should == {
+ :gem_template => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => { :rspec => "=2.3.0" }
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ GemTemplate::Gems.versions.should == {
+ :rake => ">0.8.6",
+ :rspec => "=1.3.1",
+ :externals => "=1.0.2"
+ }
+ end
+
+ it "should set everything to nil if gemset given nil value" do
+ GemTemplate::Gems.gemset = nil
+ GemTemplate::Gems.gemset.should == nil
+ GemTemplate::Gems.gemsets.should == nil
+ GemTemplate::Gems.versions.should == nil
+ end
+ end
+
+ describe :rspec2 do
+ before(:each) do
+ GemTemplate::Gems.gemset = "rspec2"
+ end
+
+ it "should set @gemset" do
+ GemTemplate::Gems.gemset.should == :rspec2
+ end
+
+ it "should set @gemsets" do
+ GemTemplate::Gems.gemsets.should == {
+ :gem_template => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => { :rspec => "=2.3.0" }
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ GemTemplate::Gems.versions.should == {
+ :rake => ">0.8.6",
+ :rspec => "=2.3.0"
+ }
+ end
+ end
+
+ describe :nil do
+ before(:each) do
+ GemTemplate::Gems.gemset = nil
+ end
+
+ it "should set everything to nil" do
+ GemTemplate::Gems.gemset.should == nil
+ GemTemplate::Gems.gemsets.should == nil
+ GemTemplate::Gems.versions.should == nil
+ end
+ end
+ end
+
+ describe :reload_gemspec do
+ before(:all) do
+ GemTemplate::Gems.configs = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ GemTemplate::Gems.gemset = :default
+ end
+
+ before(:each) do
+ @gemspec_path = "#{$root}/gem_template.gemspec"
+ @gemspec = File.read(@gemspec_path)
+ yml = File.read("#{$root}/spec/fixtures/gemspec.yml")
+ File.stub!(:read).and_return yml
+ GemTemplate::Gems.reload_gemspec
+ end
+
+ it "should populate @gemspec" do
+ GemTemplate::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"],
+ "development_dependencies" => ["rspec"]
+ }
+ end
+
+ it "should create methods from keys of @gemspec" do
+ GemTemplate::Gems.gemspec.name.should == "name"
+ GemTemplate::Gems.gemspec.version.should == "0.1.0"
+ GemTemplate::Gems.gemspec.authors.should == ["Author"]
+ GemTemplate::Gems.gemspec.email.should == "email@email.com"
+ GemTemplate::Gems.gemspec.homepage.should == "http://github.com/author/name"
+ GemTemplate::Gems.gemspec.summary.should == "Summary"
+ GemTemplate::Gems.gemspec.description.should == "Description"
+ GemTemplate::Gems.gemspec.dependencies.should == ["rake"]
+ GemTemplate::Gems.gemspec.development_dependencies.should == ["rspec"]
+ end
+
+ it "should produce a valid gemspec" do
+ gemspec = eval(@gemspec, binding, @gemspec_path)
+ gemspec.validate.should == true
+ end
+ end
+end
View
13 spec/spec_helper.rb
@@ -1,7 +1,7 @@
$root = File.expand_path('../../', __FILE__)
require "#{$root}/lib/gem_template/gems"
-GemTemplate::Gems.require(:spec)
+GemTemplate::Gems.activate %w(active_wrapper rspec)
require 'active_wrapper'
@@ -11,6 +11,7 @@
Spec::Runner.configure do |config|
end
+<<<<<<< HEAD
$db, $log, $mail = ActiveWrapper.setup(
:base => File.dirname(__FILE__),
:env => 'test'
@@ -22,4 +23,14 @@ def debug(object)
puts "<pre>"
puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
puts "</pre>"
+=======
+def capture_stdout
+ old = $stdout
+ out = StringIO.new
+ $stdout = out
+ yield
+ return out.string
+ensure
+ $stdout = old
+>>>>>>> master
end

0 comments on commit 5a0fe41

Please sign in to comment.
Something went wrong with that request. Please try again.