Module: NATURECOMMITS

Defined in:
lib/naturecommits.rb,
lib/naturecommits/version.rb

Constant Summary collapse

LOLBASEDIR =
File.join $home, ".naturecommits"
VERSION =
"0.0.6"

Instance Method Summary collapse

Instance Method Details

#capture(capture_delay = 0, is_test = false, test_msg = nil, test_sha = nil) ⇒ Object



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/naturecommits.rb', line 69

def capture(capture_delay=0, is_test=false, test_msg=nil, test_sha=nil)
  #
  # Read the git repo information from the current working directory
  #
  if not is_test
    loldir, commit_sha, commit_msg = parse_git
  else
    commit_msg = test_msg
    commit_sha = test_sha
    loldir = File.join LOLBASEDIR, "test"
  end
  
  #
  # lolspeak translate the message
  #
  if (ENV['naturecommits_TRANZLATE'] == '1' || false)
      commit_msg = commit_msg.tranzlate
  end

  #
  # Create a directory to hold the lolimages
  #
  if not File.directory? loldir
    FileUtils.mkdir_p loldir
  end

  #
  # SMILE FOR THE CAMERA! 3...2...1...
  # We're just assuming the captured image is 640x480 for now, we may
  # need updates to the imagesnap program to manually set this (or resize)
  # if this changes on future mac isights.
  #
  puts "*** Preserving this moment in history. ***"
  snapshot_loc = File.join(loldir, "tmp_snapshot.jpg")
  if is_mac?
    naturecommits_RT = File.join(File.dirname(__FILE__), '..')
    imagesnap_bin = File.join(naturecommits_RT, "ext", "imagesnap", "imagesnap")
    system("#{imagesnap_bin} -q #{snapshot_loc} -w #{capture_delay}")
  elsif is_linux?
    tmpdir = File.expand_path("#{loldir}/tmpdir#{rand(1000)}/")
    FileUtils.mkdir_p( tmpdir )
    # There's no way to give a capture delay in mplayer, but a number of frame
    # I've found that 6 is a good value for me.
    frames = if capture_delay != 0 then capture_delay else 6 end

    # mplayer's output is ugly and useless, let's throw it away
    _, r, _ = Open3.popen3("mplayer -vo jpeg:outdir=#{tmpdir} -frames #{frames} tv://")
    # looks like we still need to read the output for something to happen
    r.read
    FileUtils.mv(tmpdir + "/%08d.jpg" % frames, snapshot_loc)
    FileUtils.rm_rf( tmpdir )
  elsif is_windows?
    naturecommits_RT = File.join(File.dirname(__FILE__), '..')
    commandcam_exe = File.join(naturecommits_RT, "ext", "CommandCam", "CommandCam.exe")
    # DirectShow takes a while to show... at least for me anyway
    delaycmd = " /delay 3000"
    if capture_delay > 0
      # CommandCam delay is in milliseconds
      delaycmd = " /delay #{capture_delay * 1000}"
    end
    _, r, _ = Open3.popen3("#{commandcam_exe} /filename #{snapshot_loc}#{delaycmd}")
    # looks like we still need to read the output for something to happen
    r.read
  end


  #
  # Process the image with ImageMagick to add loltext
  #

  # read in the image, and resize it via the canvas
  canvas = ImageList.new("#{snapshot_loc}")
  if (canvas.columns > 640 || canvas.rows > 480)
    canvas.resize_to_fill!(640,480)
  end

  # create a draw object for annotation
  draw = Magick::Draw.new
  #if is_mac?
  #  draw.font = "/Library/Fonts/Impact.ttf"
  #else
  #  draw.font = "/usr/share/fonts/TTF/impact.ttf"
  #end
  naturecommits_RT = File.join(File.dirname(__FILE__), '..')
  draw.font = File.join(naturecommits_RT, "fonts", "Impact.ttf")

  draw.fill = 'white'
  draw.stroke = 'black'

  # convenience method for word wrapping
  # based on https://github.com/cmdrkeene/memegen/blob/master/lib/meme_generator.rb
  def word_wrap(text, col = 27)
    wrapped = text.gsub(/(.{1,#{col + 4}})(\s+|\Z)/, "\\1\n")
    wrapped.chomp!
  end

  draw.annotate(canvas, 0, 0, 0, 0, commit_sha) do
    self.gravity = NorthEastGravity
    self.pointsize = 32
    self.stroke_width = 2
  end

  draw.annotate(canvas, 0, 0, 0, 0, word_wrap(commit_msg)) do
    self.gravity = SouthWestGravity
    self.pointsize = 48
    self.interline_spacing = -(48 / 5) if self.respond_to?(:interline_spacing)
    self.stroke_width = 2
  end

  #
  # Squash the images and write the files
  #
  #canvas.flatten_images.write("#{loldir}/#{commit_sha}.jpg")
  canvas.write(File.join loldir, "#{$USER}_#{commit_sha}.jpg")
  FileUtils.rm(snapshot_loc)
  
  #
  # Add to Amazon S3
  #
  local_file = "#{loldir}/#{$USER}_#{commit_sha}.jpg"

  # bucket & mime-type
  bucket = $BUCKET
  mime_type = 'image/jpeg'

  AWS::S3::DEFAULT_HOST.replace "s3-eu-west-1.amazonaws.com"
  
  AWS::S3::Base.establish_connection!(
    :access_key_id     => $ACCESS,
    :secret_access_key => $SECRET
  )

  base_name = File.basename(local_file)

  puts "*** Uploading #{local_file} as '#{base_name}' to '#{bucket}' ***"

  AWS::S3::S3Object.store(
    base_name,
    File.open(local_file),
    bucket,
    :content_type => mime_type
  )

  puts "*** Uploaded! ***"

  #if in test mode, open image for inspection
  if is_test
    Launchy.open(File.join loldir, "#{$USER}_#{commit_sha}.jpg")
  end
end

#is_linux?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/naturecommits.rb', line 38

def is_linux?
  RUBY_PLATFORM.downcase.include?("linux")
end

#is_mac?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/naturecommits.rb', line 34

def is_mac?
  RUBY_PLATFORM.downcase.include?("darwin")
end

#is_windows?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/naturecommits.rb', line 42

def is_windows?
  if RUBY_PLATFORM =~ /(win|w)32$/
    true
  end
end

#loldir(dir = '.') ⇒ Object



53
54
55
56
# File 'lib/naturecommits.rb', line 53

def loldir(dir='.')
  loldir, commit_sha, commit_msg = parse_git
  return loldir
end

#most_recent(dir = '.') ⇒ Object



48
49
50
51
# File 'lib/naturecommits.rb', line 48

def most_recent(dir='.')
  loldir, commit_sha, commit_msg = parse_git
  Dir.glob(File.join loldir, "*").max_by {|f| File.mtime(f)}
end

#parse_git(dir = '.') ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/naturecommits.rb', line 58

def parse_git(dir='.')
  g = Git.open('.')
  commit = g.log.first
  commit_msg = commit.message.split("\n").first
  commit_sha = commit.sha[0..10]
  basename = File.basename(g.dir.to_s)
  basename.sub!(/^\./, 'dot') #no invisible directories in output, thanks!
  loldir = File.join LOLBASEDIR, basename
  return loldir, commit_sha, commit_msg
end

#word_wrap(text, col = 27) ⇒ Object

convenience method for word wrapping based on github.com/cmdrkeene/memegen/blob/master/lib/meme_generator.rb



160
161
162
163
# File 'lib/naturecommits.rb', line 160

def word_wrap(text, col = 27)
  wrapped = text.gsub(/(.{1,#{col + 4}})(\s+|\Z)/, "\\1\n")
  wrapped.chomp!
end