lib/harker/gemify.rb in technomancy-harker-0.0.3 vs lib/harker/gemify.rb in technomancy-harker-0.5.0
- old
+ new
@@ -1,40 +1,44 @@
require 'harker'
+require 'fileutils'
+require 'erb'
module Harker
+ # Turn an existing Rails app into a gem.
def self.gemify(rails_root)
- project_name = File.basename(rails_root)
+ @project_name = File.basename(rails_root)
- if File.exist?(rails_root + "/bin/#{project_name}", 'w') or
- File.exist?(rails_root + "/lib/#{project_name}.rb", 'w')
+ if File.exist?(rails_root + "/bin/#{@project_name}") or
+ File.exist?(rails_root + "/lib/#{@project_name}.rb")
abort "Can't write gem files without overwriting existing ones.
Try manually gemifying."
end
- # TODO: can we specify the version better?
- hoe = "Hoe.new('#{project_name}', '1.0.0') do |p|
- p.extra_deps << ['rails', '~> 2.3.2']
- p.extra_deps << ['harker', '~> #{Harker::VERSION}']
- p.developer('Your Name', 'you@example.com') # FIXME!
-end"
+ File.open(File.join(rails_root, '/Rakefile'), 'a') do |fp|
+ fp.puts template('hoe')
+ puts "Added hoe block to Rakefile."
+ end
- bin = "#!/usr/bin/env ruby
-require 'rubygems'
-require 'harker'
+ FileUtils.mkdir_p(File.join(rails_root, '/bin'))
+ File.open(File.join(rails_root, "/bin/#{@project_name}"), 'w') do |fp|
+ fp.puts template('bin')
+ puts "Wrote bin launcher."
+ end
-Harker.launch(File.basename($0), ARGV)"
+ File.open(File.join(rails_root, "/lib/#{@project_name}.rb"), 'w') do |fp|
+ fp.puts template('lib')
+ puts "Wrote lib file."
+ end
- lib = "# Allow rubygems to load this app
-require File.dirname(__FILE__) + '/../config/environment'"
+ # Submitted a patch to hoe to make it ignore log files by default,
+ # but folks should still give it a once-over manually anyway.
+ system "cd #{rails_root}; touch Manifest.txt; rake check_manifest | patch"
+ puts "Wrote Manifest.txt."
+ puts "Ensure the manifest doesn't contain files you don't want in the gem."
+ puts "Then try running rake install_gem."
+ end
- File.open(rails_root + '/Rakefile', 'a') { |fp| fp.puts hoe }
- puts "Added hoe block to Rakefile."
- File.open(rails_root + "/bin/#{project_name}", 'w') { fp.puts bin }
- puts "Wrote bin launcher."
- File.open(rails_root + "/lib/#{project_name}.rb", 'w') { fp.puts lib }
- puts "Wrote lib file."
- system "cd #{rails_root}; rake check_manifest | patch"
- puts "Wrote manifest."
-
- puts "Done! Try running rake install_gem."
+ def self.template(name)
+ template_path = File.join File.dirname(__FILE__), 'templates', "#{name}.erb"
+ ERB.new(File.read(template_path)).result(binding)
end
end