| 1 |
#!/usr/bin/env ruby |
|---|
| 2 |
|
|---|
| 3 |
require 'optparse' |
|---|
| 4 |
|
|---|
| 5 |
OptionParser.new do |opts| |
|---|
| 6 |
opts.banner = "Usage: #{File.basename($0)} [path]" |
|---|
| 7 |
|
|---|
| 8 |
opts.on("-h", "--help", "Displays this help info") do |
|---|
| 9 |
puts opts |
|---|
| 10 |
exit 0 |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
begin |
|---|
| 14 |
opts.parse!(ARGV) |
|---|
| 15 |
rescue OptionParser::ParseError => e |
|---|
| 16 |
warn e.message |
|---|
| 17 |
puts opts |
|---|
| 18 |
exit 1 |
|---|
| 19 |
end |
|---|
| 20 |
end |
|---|
| 21 |
|
|---|
| 22 |
if ARGV.empty? |
|---|
| 23 |
abort "Please specify the directory to capify, e.g. `#{File.basename($0)} .'" |
|---|
| 24 |
elsif !File.exists?(ARGV.first) |
|---|
| 25 |
abort "`#{ARGV.first}' does not exist." |
|---|
| 26 |
elsif !File.directory?(ARGV.first) |
|---|
| 27 |
abort "`#{ARGV.first}' is not a directory." |
|---|
| 28 |
elsif ARGV.length > 1 |
|---|
| 29 |
abort "Too many arguments; please specify only the directory to capify." |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def unindent(string) |
|---|
| 33 |
indentation = string[/\A\s*/] |
|---|
| 34 |
string.strip.gsub(/^#{indentation}/, "") |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
files = { |
|---|
| 38 |
"Capfile" => unindent(<<-FILE), |
|---|
| 39 |
require 'capistrano/version' |
|---|
| 40 |
load 'deploy' if respond_to?(:namespace) # cap2 differentiator |
|---|
| 41 |
load 'config/deploy' |
|---|
| 42 |
FILE |
|---|
| 43 |
|
|---|
| 44 |
"config/deploy.rb" => unindent(<<-FILE), |
|---|
| 45 |
set :application, "set your application name here" |
|---|
| 46 |
set :repository, "set your repository location here" |
|---|
| 47 |
|
|---|
| 48 |
# If you aren't deploying to /u/apps/\#{application} on the target |
|---|
| 49 |
# servers (which is the default), you can specify the actual location |
|---|
| 50 |
# via the :deploy_to variable: |
|---|
| 51 |
# set :deploy_to, "/var/www/\#{application}" |
|---|
| 52 |
|
|---|
| 53 |
# If you aren't using Subversion to manage your source code, specify |
|---|
| 54 |
# your SCM below: |
|---|
| 55 |
# set :scm, :subversion |
|---|
| 56 |
|
|---|
| 57 |
role :app, "your app-server here" |
|---|
| 58 |
role :web, "your web-server here" |
|---|
| 59 |
role :db, "your db-server here", :primary => true |
|---|
| 60 |
FILE |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
base = ARGV.shift |
|---|
| 64 |
files.each do |file, content| |
|---|
| 65 |
file = File.join(base, file) |
|---|
| 66 |
if File.exists?(file) |
|---|
| 67 |
warn "[skip] `#{file}' already exists" |
|---|
| 68 |
elsif File.exists?(file.downcase) |
|---|
| 69 |
warn "[skip] `#{file.downcase}' exists, which could conflict with `#{file}'" |
|---|
| 70 |
elsif !File.exists?(File.dirname(file)) |
|---|
| 71 |
warn "[skip] directory `#{File.dirname(file)}' does not exist" |
|---|
| 72 |
else |
|---|
| 73 |
puts "[add] writing `#{file}'" |
|---|
| 74 |
File.open(file, "w") { |f| f.write(content) } |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
puts "[done] capified!" |
|---|