#!/usr/bin/env ruby begin require 'rubygems' rescue LoadError # no rubygems to load, so we fail silently end require 'fileutils' require 'active_support' require 'optparse' require 'newgem' templates = File.dirname(__FILE__) + '/../templates/' OPTIONS = { :import_path => nil, :version => '0.0.1', :full_name => nil, :email => nil, :bin_name => nil, :svn_struct => nil, :test_framework => 'test::unit' } parser = OptionParser.new do |opts| opts.banner = < Options are: BANNER opts.separator "" opts.on("-b", "--bin-name=BIN_NAME[,BIN_NAME2]", String, "Sets up executable scripts in the bin folder.", "Default: none") { |OPTIONS[:bin_name]| } opts.on("-i", "--import_path=PATH", String, "Path where your files could be copied from.", "Default: none") { |OPTIONS[:import_path]| } opts.on("-n", "--full_name=PATH", String, "Your name to be inserted into generated files.", "Default: ~/.rubyforge/user-config.yml[user_name]") { |OPTIONS[:full_name]| } opts.on("-e", "--email=PATH", String, "Your email to be inserted into generated files.", "Default: ~/.rubyforge/user-config.yml[email]") { |OPTIONS[:email]| } opts.on("-s", "--svn", "Creates trunk/branches/tags folders beneath folder.", "Generated gem placed in trunk folder.") { |OPTIONS[:svn_struct]| } opts.on("-t", "--test-with=TEST_FRAMEWORK", String, "Select your preferred testing framework.", "Options: test::unit (default), rspec.") { |OPTIONS[:test_framework]| } opts.on("-v", "--version=YOUR_VERSION", String, "Version of the gem you are creating.", "Default: 0.0.1") { |OPTIONS[:version]| } opts.on("-h", "--help", "Show this help message.") { puts opts; exit } opts.parse!(ARGV) end OPTIONS[:version] = OPTIONS[:version].to_s.split(/\./) version_str = OPTIONS[:version].join('.') author = OPTIONS[:full_name] email = OPTIONS[:email] svn_struct = OPTIONS[:svn_struct] unless author && email rubyforge_config = Newgem::Rubyforge.new author ||= rubyforge_config.full_name email ||= rubyforge_config.email end test_framework = OPTIONS[:test_framework] gem_name = project_name = ARGV[0] parser.parse!(["-h"]) unless gem_name module_name = project_name.classify root = project_name + (svn_struct ? '/trunk' : '') lib = root + '/lib' lib_project_name = root + '/lib/' + project_name version = root + "/lib/" + project_name + "/version.rb" readme, setup = %w(README.txt setup.rb). collect {|f| root + '/' + f} template_files = %w(Rakefile History.txt License.txt Manifest.txt scripts/txt2html website/index.txt website/index.html website/template.rhtml) copy_files = %w(website/javascripts/rounded_corners_lite.inc.js website/stylesheets/screen.css) main = root + "/lib/" + project_name + ".rb" examples = root + '/examples' bin = root + '/bin' folders = %w( scripts website website/javascripts website/stylesheets) FileUtils.rm_rf project_name puts "creating: " + project_name Dir.mkdir project_name if svn_struct %w[trunk branches tags].each do |folder| folder_path = File.join(project_name, folder) puts "creating: " + folder_path Dir.mkdir folder_path end end puts "creating: " + readme File.open(readme, 'w') do |file| file << <<-eos README for #{project_name} ===========#{'=' * project_name.length} eos end puts "creating: " + lib FileUtils.mkdir_p lib folders.each do |folder| new_folder = root + '/' + folder puts "creating: " + new_folder FileUtils.mkdir_p new_folder end if OPTIONS[:import_path] require 'fileutils' OPTIONS[:import_path].chomp! if OPTIONS[:import_path][-1] == 47 # / FileUtils.cp_r OPTIONS[:import_path] + '/.', lib, :preserve => true end puts "creating: " + lib_project_name FileUtils.mkdir_p lib_project_name puts "creating: " + main File.open(main, 'w') do |file| file << <<-EOS module #{module_name} end require '#{project_name}/version' EOS end puts "creating: " + version File.open(version, 'w') do |file| file << <<-EOS module #{module_name} #:nodoc: module VERSION #:nodoc: MAJOR = #{OPTIONS[:version][0] || '0'} MINOR = #{OPTIONS[:version][1] || '0'} TINY = #{OPTIONS[:version][2] || '0'} STRING = [MAJOR, MINOR, TINY].join('.') end end EOS end puts "creating: " + bin Dir.mkdir bin if OPTIONS[:bin_name] bin_names_list = OPTIONS[:bin_name].split(',') template = File.open(templates + 'app.rb','r') {|f| f.readlines.join} bin_names_list.each do |bin_name| puts "creating: " + bin + '/' + bin_name File.open(bin + '/' + bin_name, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') end end bin_names = bin_names_list.join(' ') else bin_names = '' bin_names_list = [] end # Setup spec/test::unit environments # Would like to refactor this... if test_framework == 'rspec' test = root + "/spec" test_helper = test + "/spec_helper.rb" test_template = 'spec.rb' unit_test = test + "/" + project_name + "_spec.rb" test_file_prefix = 'spec/NAME_spec' spec_opts = 'spec.opts' unit_test_template = <<-eos require 'spec' eos requirements = <<-eos begin require 'spec/rake/spectask' rescue LoadError puts 'To use rspec for testing you must install rspec gem:' puts '$ sudo gem install rspec' exit end eos test_task = <<-eos desc "Run the specs under spec/models" Spec::Rake::SpecTask.new do |t| t.spec_opts = ['--options', "spec/spec.opts"] t.spec_files = FileList['spec/*_spec.rb'] end desc "Default task is to run specs" task :default => :spec eos else test = root + "/test" test_helper = test + "/test_helper.rb" test_template = 'test.rb' unit_test = test + "/test_" + project_name + ".rb" test_file_prefix = 'test/test_NAME' unit_test_template = <<-eos require 'test/unit' require File.dirname(__FILE__) + '/../lib/#{project_name}' eos requirements = '' test_task = '' end puts "creating: " + test Dir.mkdir test puts "creating: " + test_helper File.open(test_helper, 'w') do |file| file << unit_test_template end puts "creating: " + unit_test template = File.open(templates + test_template,'r') {|f| f.readlines.join} File.open(unit_test, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') end if test_framework == 'rspec' spec_opts_file = test + "/" + spec_opts puts "creating: " + spec_opts_file template = File.open(templates + spec_opts, 'r') { |f| f.readlines.join } File.open(spec_opts_file, 'w') do |file| file << eval('"' + template.gsub(/"/, '\""') + '"') end end puts "creating: " + examples Dir.mkdir examples puts "creating: " + setup template = File.open(templates + 'setup.rb','r') {|f| f.readlines.join} File.open(setup, 'w') do |file| file << template end bin_names_list = bin_names_list.collect {|name| 'bin/' + name }.join("\n") bin_names_list += "\n" if bin_names_list.size > 0 test_names_list = ['helper.rb',"#{project_name}.rb"].sort.collect {|name| test_file_prefix.gsub('NAME', name) }.join("\n") template_files.each do |template_file| puts "creating: " + root + '/' + template_file template = File.open(templates + template_file,'r') {|f| f.readlines.join} File.open(root + '/' + template_file, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') end end copy_files.each do |file| puts "copying: " + root + '/' + file template = File.open(templates + file,'r') {|f| f.readlines.join} File.open(root + '/' + file, 'w') do |file| file << template end end puts "NOW - update #{gem_name}/Rakefile with gem description, etc"