#!/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' templates = File.dirname(__FILE__) + '/../templates/' OPTIONS = { :import_path => nil, :version => '0.0.1', :bin_name => nil } 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("-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('.') gem_name = project_name = ARGV[0] parser.parse!(["-h"]) unless gem_name module_name = project_name.classify lib = project_name + '/lib' lib_project_name = project_name + '/lib/' + project_name version = project_name + "/lib/" + project_name + "/version.rb" readme, setup = %w(README.txt setup.rb). collect {|f| project_name + '/' + f} template_files = %w(Rakefile Manifest.txt History.txt scripts/txt2html website/index.txt website/template.rhtml) copy_files = %w(website/javascripts/rounded_corners_lite.inc.js website/stylesheets/screen.css) test = project_name + "/test" main = project_name + "/lib/" + project_name + ".rb" test_helper = test + "/test_helper.rb" unit_test = test + "/test_" + project_name + ".rb" examples = project_name + '/examples' bin = project_name + '/bin' folders = %w( scripts website website/javascripts website/stylesheets) # Rakefile variables author = ENV['USERNAME'] || ENV['USER'] || 'you' FileUtils.rm_rf project_name puts "creating: " + project_name Dir.mkdir project_name puts "creating: " + readme File.open(readme, 'w') do |file| file << <<-eos README for #{project_name} ===========#{'=' * project_name.length} eos end puts "creating: " + lib Dir.mkdir lib folders.each do |folder| new_folder = project_name + '/' + folder puts "creating: " + new_folder Dir.mkdir 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 Dir.mkdir lib_project_name puts "creating: " + main File.open(main, 'w') do |file| file << "Dir[File.join(File.dirname(__FILE__), '#{project_name}/**/*.rb')].sort.each { |lib| require lib }" 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 puts "creating: " + test Dir.mkdir test puts "creating: " + test_helper File.open(test_helper, 'w') do |file| file << <<-eos require 'test/unit' require File.dirname(__FILE__) + '/../lib/#{project_name}' eos end puts "creating: " + unit_test template = File.open(templates + 'test.rb','r') {|f| f.readlines.join} File.open(unit_test, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') 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/test_' + name }.join("\n") template_files.each do |template_file| puts "creating: " + project_name + '/' + template_file template = File.open(templates + template_file,'r') {|f| f.readlines.join} File.open(project_name + '/' + template_file, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') end end copy_files.each do |file| puts "copying: " + project_name + '/' + file template = File.open(templates + file,'r') {|f| f.readlines.join} File.open(project_name + '/' + file, 'w') do |file| file << template end end puts "NOW - update #{gem_name}/Rakefile with gem description, etc"