module NATURECOMMITS

Constants

CONFIG_FILE

Specify and load config

LOLBASEDIR
VERSION

Public Instance Methods

capture(capture_delay=0, is_test=false, test_msg=nil, test_sha=nil) click to toggle source
# 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?() click to toggle source
# File lib/naturecommits.rb, line 38
def is_linux?
  RUBY_PLATFORM.downcase.include?("linux")
end
is_mac?() click to toggle source
# File lib/naturecommits.rb, line 34
def is_mac?
  RUBY_PLATFORM.downcase.include?("darwin")
end
is_windows?() click to toggle source
# File lib/naturecommits.rb, line 42
def is_windows?
  if RUBY_PLATFORM =~ /(win|w)32$/
    true
  end
end
loldir(dir='.') click to toggle source
# File lib/naturecommits.rb, line 53
def loldir(dir='.')
  loldir, commit_sha, commit_msg = parse_git
  return loldir
end
most_recent(dir='.') click to toggle source
# 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='.') click to toggle source
# 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) click to toggle source

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

# 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