Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
executable file 153 lines (127 sloc) 4 KB
#!/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
opt :git, "Use Git to track updates to generated files"
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("com.twitter.birdname", "#{namespace}.#{name.downcase}").
gsub("com/twitter/birdname", "#{namespace.gsub('.', '/')}/#{name.downcase}").
gsub("BirdName", name).
gsub("birdname", name.downcase).
gsub("bird_name", name.underscore).
gsub("birdName", name.camelize)
end
def sys(cmd, abort_on_fail=true)
system(cmd + " &> /dev/null") || abort_on_fail && abort("failed: #{cmd}")
end
require "erb"
require "fileutils"
include FileUtils
project_name = ARGV.pop.camelize(true)
is_public = opts[:public]
namespace = opts[:namespace] || "com.twitter"
git = opts[:git]
$overwrite_all = true if git
$ex_post_facto = false
$branch = 'master'
$files = []
if git
if !File.exists?('.git')
if `ls -l` != ''
abort('files in directory, no git repo.')
end
sys('git init')
sys('touch README.md')
sys('git add .')
sys("git commit -m'first commit'")
sys('git checkout -b scala-bootstrapper')
else
if `git status -s` != ''
abort('uncommitted files in directory.')
end
$branch = `git branch`.grep(/^\*/).first.chomp.gsub(/^\* (.+)$/, '\1')
if !sys('git checkout scala-bootstrapper', false)
$ex_post_facto = true
sys('git checkout -b scala-bootstrapper')
end
end
end
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)) }
$files << target_path
end
if File.exists?("src/scripts/startup.sh")
startup = "src/scripts/#{project_name.downcase}.sh"
`mv src/scripts/startup.sh #{startup}`
$files << startup
end
[ "src/scripts/#{project_name.downcase}.sh", "src/scripts/console", "run" ].each do |executable|
`chmod +x #{executable}` if File.exists?(executable)
end
if git
$files.each { |file| sys("git add #{file}") if File.exists?(file) }
sys("git commit -m'scala-bootstrapper'", false) # fails if no change
sys("git checkout #{$branch}")
sys('git merge --no-ff --no-commit scala-bootstrapper')
if $ex_post_facto
# don't commit anything, just make scala-bootstrapper head a parent
sys('rm .git/index')
sys('git checkout HEAD .')
sys('git clean -fdx')
end
sys("git commit -m'merged scala-bootstrapper'", false) # fails if no change
end
if $ex_post_facto
puts <<EOF
Found existing .git directory; scala-bootstrapper branch created but
generated files not merged to #{$branch}. To manually merge changes,
run
git cherry-pick --no-commit scala-bootstrapper
then
git commit
once you have resolved any conflicts.
EOF
end
Jump to Line
Something went wrong with that request. Please try again.