-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby_scm_twitter.rb
executable file
·128 lines (102 loc) · 3.18 KB
/
ruby_scm_twitter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env ruby
require 'optparse'
require 'rdoc/usage'
require 'ostruct'
require 'date'
require 'twitter'
class Application
VERSION = '0.0.1'
attr_reader :options
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Set defaults
@options = OpenStruct.new
@options.scm = :svn
@options.verbose = false
end
# Parse options, check arguments, then process the command
def run
if parsed_options? && arguments_valid?
puts "Start at #{DateTime.now}\n\n" if @options.verbose
output_options if @options.verbose # [Optional]
process_command
puts "\nFinished at #{DateTime.now}" if @options.verbose
else
output_usage
end
end
protected
def parsed_options?
# Specify options
opts = OptionParser.new
opts.on('-v', '--version') { output_version ; exit 0 }
opts.on('-h', '--help') { output_help }
opts.on('-V', '--verbose') { @options.verbose = true }
opts.on('-u', '--username USER') { |username| @options.username = username }
opts.on('-p', '--password PASSWORD') { |password| @options.password = password }
opts.on('-f', '--file FILE') { |file| @options.file = file }
opts.on('-r', '--revision REVISION') { |revision| @options.revision = revision }
opts.on('-s', '--scm SCM') { |scm| @options.scm = scm.downcase.to_sym }
opts.parse!(@arguments) rescue return false
true
end
def output_options
puts "Options:\n"
@options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
end
# True if required arguments were provided
def arguments_valid?
[:svn].include?(@options.scm)
end
def output_help
output_version
RDoc::usage() #exits app
end
def output_usage
RDoc::usage('usage') # gets usage from comments above
end
def output_version
puts "#{File.basename(__FILE__)} version #{VERSION}"
end
def process_command
case @options.scm
when :svn, 'svn' : Twitter::SCM::SVN.new(@options).run
end
end
end
module Twitter
module SCM
class SVN
attr_reader :options
def initialize(options)
@options = options
end
def log
`svnlook log #{options.file} -r #{options.revision}`.strip
end
def author
`svnlook author #{options.file} -r #{options.revision}`.strip
end
def message
log_length = 140 - (author.length + 2)
"#{author}: #{log.slice(0, log_length - 3).strip}#{'..' if ((log_length - 3) < log.length)}"
end
def run
Twitter::Base.new(@options.user, @options.password).update(message)
end
end
end
end
# Create and run the application
app = Application.new(ARGV, STDIN).run