Permalink
Browse files

Spiked jewelerifcation

  • Loading branch information...
1 parent 92dd671 commit 003aec4c20b13ad33b7bc6449fc71a06b2475f56 @technicalpickles committed Oct 19, 2009
Showing with 105 additions and 19 deletions.
  1. +45 −19 lib/jeweler/generator.rb
  2. +54 −0 lib/jeweler/specification.rb
  3. +6 −0 lib/jeweler/templates/Rakefile_from_gemspec.erb
View
@@ -21,6 +21,8 @@ class NoGitHubToken < StandardError
class GitInitFailed < StandardError
end
+ autoload :Specification, 'jeweler/specification'
+
# Generator for creating a jeweler-enabled project
class Generator
require 'jeweler/generator/options'
@@ -40,14 +42,14 @@ class Generator
require 'jeweler/generator/rdoc_mixin'
require 'jeweler/generator/yard_mixin'
- attr_accessor :target_dir, :user_name, :user_email, :summary, :homepage,
+ attr_accessor :target, :target_dir, :user_name, :user_email, :summary, :homepage,
:description, :project_name, :github_username, :github_token,
:repo, :should_create_remote_repo,
:testing_framework, :documentation_framework,
:should_use_cucumber, :should_setup_gemcutter,
:should_setup_rubyforge, :should_use_reek, :should_use_roodi,
:development_dependencies,
- :options
+ :options, :gemspec
def initialize(options = {})
self.options = options
@@ -76,7 +78,7 @@ def initialize(options = {})
raise ArgumentError, "Unsupported documentation framework (#{documentation_framework})"
end
- self.target_dir = options[:directory] || self.project_name
+ self.target = options[:directory] || self.project_name
self.summary = options[:summary] || 'TODO: one-line summary of your gem'
self.description = options[:description] || 'TODO: longer description of your gem'
@@ -99,14 +101,20 @@ def initialize(options = {})
end
def run
- create_files
- create_version_control
- $stdout.puts "Jeweler has prepared your gem in #{target_dir}"
- if should_create_remote_repo
- create_and_push_repo
- $stdout.puts "Jeweler has pushed your repo to #{homepage}"
- enable_gem_for_repo
- #$stdout.puts "Jeweler has enabled gem building for your repo"
+ if target =~ /\.gemspec$/
+ self.target_dir = File.dirname(target)
+ create_rakefile_template
+ else
+ self.target_dir = self.target
+
+ create_scaffold
+
+ create_version_control
+ if should_create_remote_repo
+ create_and_push_repo
+ enable_gem_for_repo
+ #$stdout.puts "Jeweler has enabled gem building for your repo"
+ end
end
end
@@ -151,16 +159,31 @@ def features_steps_dir
end
private
- def create_files
- unless File.exists?(target_dir) || File.directory?(target_dir)
- FileUtils.mkdir target_dir
+ def create_rakefile_template
+ self.gemspec = Gem::Specification.load(target)
+ self.gemspec.extend(Jeweler::Specification)
+
+ rakefile_target = File.join(target_dir, 'Rakefile')
+ if File.exist? rakefile_target
+ $stdout.puts "# Rakefile already exists. Here's what we would have generated:"
+ $stdout.puts ""
+ $stdout.puts render_template('Rakefile_from_gemspec.erb')
else
- raise FileInTheWay, "The directory #{target_dir} already exists, aborting. Maybe move it out of the way before continuing?"
+ output_template_in_target 'Rakefile_from_gemspec.erb', 'Rakefile'
+ $stdout.puts "Jeweler has created #{rakefile_target} based on #{target}"
end
+ end
+ def create_scaffold
+ unless File.exists?(target) || File.directory?(target)
+ FileUtils.mkdir target
+ else
+ raise FileInTheWay, "The directory #{target} already exists, aborting. Maybe move it out of the way before continuing?"
+ end
- output_template_in_target '.gitignore'
output_template_in_target 'Rakefile'
+
+ output_template_in_target '.gitignore'
output_template_in_target 'LICENSE'
output_template_in_target 'README.rdoc'
output_template_in_target '.document'
@@ -192,6 +215,7 @@ def create_files
touch_in_target File.join(features_steps_dir, steps_filename)
end
+ $stdout.puts "Jeweler has prepared your gem in #{target}"
end
def render_template(source)
@@ -216,21 +240,21 @@ def template_dir
end
def mkdir_in_target(directory)
- final_destination = File.join(target_dir, directory)
+ final_destination = File.join(target, directory)
FileUtils.mkdir final_destination
$stdout.puts "\tcreate\t#{directory}"
end
def touch_in_target(destination)
- final_destination = File.join(target_dir, destination)
+ final_destination = File.join(target, destination)
FileUtils.touch final_destination
$stdout.puts "\tcreate\t#{destination}"
end
def create_version_control
- Dir.chdir(target_dir) do
+ Dir.chdir(target) do
begin
@repo = Git.init()
rescue Git::GitExecuteError => e
@@ -267,6 +291,8 @@ def create_and_push_repo
'name' => project_name
# TODO do a HEAD request to see when it's ready
@repo.push('origin')
+
+ $stdout.puts "Jeweler has pushed your repo to #{homepage}"
end
# FIXME This was borked awhile ago, and even more so with gems being disabled
@@ -57,12 +57,66 @@ def set_jeweler_defaults(base_dir, git_base_dir = nil)
# Used by Specification#to_ruby to generate a ruby-respresentation of a Gem::Specification
def ruby_code(obj)
+ require 'rake'
case obj
when Rake::FileList then obj.to_a.inspect
else super
end
end
+ JEWELER_PROVIDED_ATTRIBUTES = [
+ :has_rdoc,
+ :date
+
+ ]
+ def to_jeweler_tasks
+ mark_version
+ result = []
+ result << "require 'jeweler'"
+ result << "Jeweler::Tasks.new do |gemspec|"
+ result << " gemspec.name = #{ruby_code name}"
+ result << " gemspec.version = #{ruby_code version}"
+ unless platform.nil? or platform == Gem::Platform::RUBY then
+ result << " gemspec.platform = #{ruby_code original_platform}"
+ end
+
+ handled = [
+ :dependencies,
+ :name,
+ :platform,
+ :required_rubygems_version,
+ :specification_version,
+ :version,
+ ]
+
+ baseline_gemspec = Gem::Specification.new
+
+ attributes = self.class.attribute_names.sort_by { |attr_name,| attr_name.to_s }
+
+ attributes.each do |attr_name, default|
+ next if handled.include? attr_name
+ current_value = self.send(attr_name)
+ baseline_value = baseline_gemspec.send(attr_name)
+ if current_value != default && current_value != baseline_value
+ result << " gemspec.#{attr_name} = #{ruby_code current_value}"
+ end
+ end
+
+ unless dependencies.empty? then
+ result << ""
+ dependencies.each do |dep|
+ version_reqs_param = dep.requirements_list.inspect
+ dep.instance_variable_set :@type, :runtime if dep.type.nil? # HACK
+ result << " gemspec.add_#{dep.type}_dependency(%q<#{dep.name}>, #{version_reqs_param})"
+ end
+ end
+
+ result << "end"
+ result << nil
+
+ result.join "\n"
+ end
+
private
def blank?(value)
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'rake'
+
+<%= gemspec.to_jeweler_tasks %>
+
+<%= render_template 'other_tasks.erb' %>

0 comments on commit 003aec4

Please sign in to comment.