#!/usr/bin/env ruby 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(/\./) 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" rakefile, changelog, readme, setup = %w(Rakefile CHANGELOG README setup.rb). collect {|f| project_name + '/' + f} test = project_name + "/test" all_tests = test + "/all_tests.rb" main = project_name + "/lib/" + project_name + ".rb" test_helper = test + "/test_helper.rb" unit_test = test + "/" + project_name + "_test.rb" examples = project_name + '/examples' bin = project_name + '/bin' FileUtils.rm_rf project_name puts "creating: " + project_name Dir.mkdir project_name puts "creating: " + changelog FileUtils.touch changelog 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 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 = OPTIONS[:bin_name].split(',') bin_names.each do |bin_name| puts "creating: " + bin + '/' + bin_name FileUtils.touch bin + '/' + bin_name end bin_names = bin_names.join(' ') else bin_names = '' end puts "creating: " + rakefile template = File.open(templates + 'Rakefile','r') {|f| f.readlines.join} File.open(rakefile, 'w') do |file| file << eval('"' + template.gsub(/"/, '\"') + '"') end puts "creating: " + test Dir.mkdir test puts "creating: " + all_tests File.open(all_tests, 'w') do |file| file << "Dir['**/*_test.rb'].each { |test_case| require test_case }" end 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