Created
July 25, 2011 14:57
-
-
Save jamis/1104315 to your computer and use it in GitHub Desktop.
Capistrano Transcriber: a wrapper script for Capistrano that writes session transcripts to a timestamped file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'capistrano/cli' | |
class TranscriptLogger | |
module TranscriptLoggerMetadata | |
attr_accessor :tx_logger_metadata | |
def self.apply_to(object, options={}) | |
object.extend(self) unless object.respond_to?(:tx_logger_metadata) | |
object.tx_logger_metadata ||= {} | |
object.tx_logger_metadata.update(options) | |
object | |
end | |
end | |
def initialize(file1, file2) | |
@file1 = prepare_stream(file1) | |
@file2 = prepare_stream(file2) | |
end | |
def close | |
@file1.close if @file1.tx_logger_metadata[:needs_close] | |
@file2.close if @file2.tx_logger_metadata[:needs_close] | |
end | |
def puts(line) | |
@file1.puts(line) | |
@file2.puts(line) | |
end | |
private | |
def prepare_stream(file) | |
if file.nil? | |
TranscriptLoggerMetadata.apply_to($stderr, :needs_close => false) | |
elsif file.respond_to?(:puts) | |
TranscriptLoggerMetadata.apply_to(file, :needs_close => false) | |
else | |
TranscriptLoggerMetadata.apply_to(File.open(file.to_str, "a"), :needs_close => true) | |
end | |
end | |
end | |
module TranscriptLoggerOptions | |
def option_parser | |
parser = super | |
parser.separator "" | |
parser.separator "Transcript Logging Options:" | |
parser.on("--[no-]transcript", "write logging to a timestanped file, as well as to whatever -l is set to (default TRUE)") do |tx| | |
options[:tx] = tx | |
end | |
parser | |
end | |
def parse_options! | |
super | |
options[:tx] = true unless options.key?(:tx) | |
if options[:tx] | |
transcript = Time.now.strftime("cap-%Y%m%d%H%M%S.log") | |
options[:output] = TranscriptLogger.new(options[:output], transcript) | |
end | |
options.delete :tx | |
end | |
end | |
Capistrano::CLI.send :include, TranscriptLoggerOptions | |
Capistrano::CLI.execute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment