1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00
capistrano/bin/capify

86 lines
2.2 KiB
Text
Raw Normal View History

#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [path]"
opts.on("-h", "--help", "Displays this help info") do
puts opts
exit 0
end
begin
opts.parse!(ARGV)
rescue OptionParser::ParseError => e
warn e.message
puts opts
exit 1
end
end
if ARGV.empty?
abort "Please specify the directory to capify, e.g. `#{File.basename($0)} .'"
elsif !File.exists?(ARGV.first)
abort "`#{ARGV.first}' does not exist."
elsif !File.directory?(ARGV.first)
abort "`#{ARGV.first}' is not a directory."
elsif ARGV.length > 1
abort "Too many arguments; please specify only the directory to capify."
end
def unindent(string)
indentation = string[/\A\s*/]
string.strip.gsub(/^#{indentation}/, "")
end
files = {
"Capfile" => unindent(<<-FILE),
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy'
FILE
"config/deploy.rb" => unindent(<<-FILE),
##
## Generated By Capistrano #{Capistrano::Version}
##
set :application, "set your application name here"
set :repository, "set your repository location here"
# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion
# see a full list by running "gem contents capistrano | grep 'scm/'"
role :web, "your web-server here" # Your HTTP server, Apache/etc
role :app, "your app-server here" # This may be the same as your `Web` server
role :db, "your db-server here", :primary => true # This is where Rails migrations will run
role :db, "your backup db-server here"
FILE
}
base = ARGV.shift
files.each do |file, content|
file = File.join(base, file)
if File.exists?(file)
warn "[skip] `#{file}' already exists"
elsif File.exists?(file.downcase)
warn "[skip] `#{file.downcase}' exists, which could conflict with `#{file}'"
elsif !File.exists?(File.dirname(file))
FileUtils.mkdir(File.dirname(file))
retry
warn "[skip] directory `#{File.dirname(file)}' did not exist, created."
else
puts "[add] writing `#{file}'"
File.open(file, "w") { |f| f.write(content) }
end
end
puts "[done] capified!"