#!/usr/bin/env ruby require File.dirname(__FILE__) + "/../vendor/trollop" opts = Trollop::options do banner "Usage: #{$0} [options] PROJECT_NAME" opt :public, "Use the public twitter maven repo" opt :namespace, "Use something besides com.twitter", :type => :string end if ARGV.length < 1 Trollop::die "PROJECT_NAME is required" exit 1 end class String def camelize(first_letter_in_uppercase = false) if first_letter_in_uppercase gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self[0].chr.downcase + camelize(self)[1..-1] end end def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end end def gsub_birds(haystack, name, namespace) haystack. gsub("BirdName", name). gsub("birdname", name.downcase). gsub("bird_name", name.underscore). gsub("birdName", name.camelize). gsub("com.twitter", namespace). gsub("com/twitter", namespace.gsub(".", "/")) end require "erb" require "fileutils" include FileUtils project_name = ARGV.pop.camelize(true) is_public = opts[:public] namespace = opts[:namespace] || "com.twitter" root = File.expand_path(File.dirname(__FILE__) + "/../lib/template") Dir["#{root}/**/*"].select{|path| File.file?(path)}.each do |path| relative = path.sub("#{root}/", "") content = File.read(path) template = ERB.new(content, nil, nil, "@output") target_path = gsub_birds(relative, project_name, namespace).sub(/\.erb$/, '') if File.exists?(target_path) && !$overwrite_all print "File exists `#{relative}`, replace? ([Y]es, [N]o, [A]ll, [Q]uit)" $stdout.flush case STDIN.gets when /^y/i: # continue when /^n/i: next when /^a/i: $overwrite_all = true when /^q/i: exit(2) else retry end end puts "writing #{target_path}" mkdir_p(File.dirname(target_path)) File.open(target_path, "w") {|f| f.print(gsub_birds(template.result(binding), project_name, namespace)) } end if File.exists?("src/scripts/startup.sh") `mv src/scripts/startup.sh src/scripts/#{project_name.downcase}.sh` end [ "src/scripts/#{project_name.downcase}.sh", "src/scripts/console", "run" ].each do |executable| `chmod +x #{executable}` if File.exists?(executable) end