#!/usr/bin/env ruby begin require 'lolcommits' rescue LoadError require 'rubygems' require 'lolcommits' end include Lolcommits require "launchy" require "choice" # # CHECK FOR FURTHER DEPENDENCIES # # which replacement http://stackoverflow.com/q/2108727 def command?(name) `which #{name}` $?.success? end if Configuration.is_mac? unless File.executable? File.join(Configuration::LOLCOMMITS_ROOT, "ext", "imagesnap", "imagesnap") puts "Couldn't properly execute imagesnap for some reason, please file a bug?!" exit 1 end elsif Configuration.is_linux? if not command?('mplayer') puts "Couldn't find mplayer in your PATH!" exit 1 end end unless File.readable? File.join(Configuration::LOLCOMMITS_ROOT, "fonts", "Impact.ttf") puts "Couldn't properly read Impact font from gem package, please file a bug?!" exit 1 end def die_if_not_git_repo! begin g=Git.open('.') rescue ArgumentError # ruby-git throws an argument error if path isnt for a valid git repo... puts "Erm? Can't do that since we're not in a valid git repository!" exit 1 end end # # NO ARGUMENTS SPECIFIED, HELP THE USER OUT # def do_noargs #TODO: make this a contextual helper to know status of whether lolcommits is enabled puts "Do what exactly?" puts "Try: lolcommits --enable (when in a git repository)" puts "Or: lolcommits --help" end HOOK_PATH = File.join ".git", "hooks", "post-commit" HOOK_DIR = File.join ".git", "hooks" # # IF --ENABLE, DO ENABLE # def do_enable if not File.directory?(".git") puts "You don't appear to be in the base directory of a git project." exit 1 end #its possible a hooks dir doesnt exist, so create it if so if not File.directory?(HOOK_DIR) Dir.mkdir(HOOK_DIR) end if File.exists? HOOK_PATH puts "A post-commit hook already exists for this project." #TODO: disambiguate between OUR post-commit hook and something else exit 1 end doc = "#!/bin/sh\nlolcommits --capture\n" File.open(HOOK_PATH, 'w') {|f| f.write(doc) } FileUtils.chmod 0755, HOOK_PATH puts "installed lolcommmit hook as:" puts " -> #{File.expand_path(HOOK_PATH)}" puts "(to remove later, you can use: lolcommits --disable)" # we dont symlink, but rather install a small stub that calls the one from path # that way, as gem version changes, script updates even if new file thus breaking symlink end # # IF --DISABLE, DO DISABLE # def do_disable if File.exists? HOOK_PATH #TODO: check if hook file has been modified before removing FileUtils.rm HOOK_PATH puts "removed #{HOOK_PATH}" else puts "lolcommits is not enabled for this directory, so there is nothing to uninstall." end end def configuration if Choice.choices[:test] Configuration.new(:loldir => Configuration.loldir_for('test')) else Configuration.new end end # # IF --CAPTURE, DO CAPTURE # def do_capture capture_delay = Choice.choices[:delay] || ENV['LOLCOMMITS_DELAY'] || 0 capture_device = Choice.choices[:device] || ENV['LOLCOMMITS_DEVICE'] || nil if Choice.choices[:test] puts "*** Capturing in test mode." runner = Lolcommits::Runner.new(:capture_delay => capture_delay, :capture_device => capture_device, :message => Choice.choices[:msg], :sha => Choice.choices[:sha], :config => configuration ) runner.run Launchy.open(runner.main_image) else runner = Lolcommits::Runner.new(:capture_delay => capture_delay, :capture_device => capture_device, :config => configuration ) runner.run end end def do_configure configuration.do_configure! Choice.choices[:plugin] end def do_last die_if_not_git_repo! lolimage = configuration.most_recent if lolimage.nil? puts "No lolcommits have been captured for this repository yet." exit 1 end Launchy.open lolimage end # # Command line parsing fun # Choice.options do option :enable do long "--enable" short '-e' action { do_enable } desc "install lolcommits for this repo" end option :disable do long "--disable" short '-d' action { do_disable } desc "uninstall lolcommits for this repo" end option :capture do long "--capture" short '-c' desc "capture lolcommit based on last git commit" end option :last do long "--last" short "-l" desc "view the most recent lolcommit" end option :browse do long "--browse" short "-b" desc "browse this repo's lolcommits" end option :configure do long "--config" desc "configure a plugin" end option :show_config do short "-sc" long "--show-config" desc "display configuration file" end option :plugin do desc "pass plugin name for --config" long "--plugin" short "-p" default nil end option :plugins do desc "list all available plugins" long "--plugins" end option :test do long "--test" desc "Run in test mode" end option :sha do desc "pass SHA manually [TEST-MODE]" long "--sha" short '-s' default "test-#{rand(10 ** 10)}" end option :msg do desc "pass commit msg manually [TEST-MODE]" long "--msg" short '-m' default "this is a test message i didnt really commit something" end option :delay do long "--delay=SECONDS" desc "delay taking of the snapshot by n seconds" cast Integer short '-w' end option :device do long "--device=DEVICE" desc "the device name used to take the snapshot (only mac)" end end # # Handle actions manually since choice seems weird # if not (Choice.choices[:enable] || Choice.choices[:disable]) if Choice.choices[:capture] do_capture() elsif Choice.choices[:configure] do_configure() elsif Choice.choices[:show_config] puts configuration.user_configuration.to_yaml elsif Choice.choices[:plugins] configuration.puts_plugins() elsif Choice.choices[:last] do_last() elsif Choice.choices[:browse] die_if_not_git_repo! Launchy.open configuration.loldir else do_noargs() end end