#!/usr/bin/env ruby
require 'optparse'
require 'fileutils'
require 'blanket/init'
# default options
OPTIONS = {
}
ARGV.options do |o|
script_name = File.basename($0)
o.set_summary_indent(' ')
o.banner = "Usage: #{script_name} [OPTIONS]"
o.define_head "Configure blanket task with source and sink information"
o.separator ""
o.on("-t", "--task=val", String,
"name of the task") { |OPTIONS[:task]| }
o.on("-o", "--source=val", String,
"blanket type of the source") { |OPTIONS[:source]| }
o.on("-i", "--sink=val", String,
"blanket type of the sink") { |OPTIONS[:sink]| }
o.separator ""
o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
o.parse!
end
BAD_ARGS = "You failed to supply all the required arguments. Please use -h and try again.\n"
$stderr << BAD_ARGS unless OPTIONS[:task] && OPTIONS[:source] && OPTIONS[:sink]
task = OPTIONS[:task]
source_class = OPTIONS[:source]
sink_class = OPTIONS[:sink]
@task_dir = FileUtils.pwd + "/" +task
puts "Creating #{@task_dir}"
FileUtils.mkdir_p @task_dir
def write_config_for(sym)
file = @task_dir + "/#{sym.to_s}.yml"
atts = Object.const_get(OPTIONS[sym]).default_attributes
writer = Blanket::Writer.new(atts)
puts "Writing #{sym} configuration #{file}"
writer.write(file)
end
write_config_for(:source)
write_config_for(:sink)
requirements = ["'capistrano/recipes/blanket'"]
[:source, :sink].each do |phase|
phase_class = Object.const_get(OPTIONS[phase])
(requirements << phase_class.additional_requirements) if phase_class.respond_to? :additional_requirements
end
capfile_requires = requirements.inject("") {|statement, r| statement += "require #{r}\r\n" }
def unindent(string)
indentation = string[/\A\s*/]
string.strip.gsub(/^#{indentation}/, "")
end
files = {
"Capfile" => unindent(capfile_requires)
}
files.each do |file, content|
file = File.join(@task_dir, 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))
warn "[skip] directory `#{File.dirname(file)}' does not exist"
else
puts "[add] writing `#{file}'"
File.open(file, "w") { |f| f.write(content) }
end
end
puts "[done] blanketed!"