Class: CocoaHookup

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoa-hookup.rb

Overview

Largely based on code from Tim Pope’s Hookup Gem(github.com/tpope/hookup)

Defined Under Namespace

Classes: Error, Failure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(*argv) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/cocoa-hookup.rb', line 10

def self.run(*argv)
  new.run(*argv)
rescue Failure => e
  puts e
  exit 1
rescue Error => e
  puts e
  exit
end

Instance Method Details

#git_dirObject



32
33
34
35
36
37
38
# File 'lib/cocoa-hookup.rb', line 32

def git_dir
  unless @git_dir
    @git_dir = %x{git rev-parse --git-dir}.chomp
    raise Error unless $?.success?
  end
  @git_dir
end

#installObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cocoa-hookup.rb', line 63

def install
  append(post_checkout_file, 0777) do |body, f|
    f.puts "#!/bin/bash" unless body
    if body !~ /cocoa-hookup/

      f.puts %(if command -v cocoa-hookup > /dev/null; then)
      f.puts %(  cocoa-hookup post-checkout "$@") if body !~ /cocoa-hookup/
      f.puts "fi"
    end
  end

  puts "CocoaHookup Installed"
end

#post_checkout(*argv) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocoa-hookup.rb', line 44

def post_checkout(*argv)
  if argv.length != 4
    puts "Invalid arguments for 'cocoa-hookup post-checkout'. Usage: 'cocoa-hookup post-checkout old-rev new-rev branching-flag'"
    return
  end

  old = argv[1]
  new = argv[2]
  branch_checkout = argv[3]

  return unless branch_checkout == "1" # Ignore if not a branch checkout

  if %x{git diff --name-only #{old} #{new}} =~ /^Podfile/
    puts "Podfile is different from last branch, updating Cocoapods..."
    system('pod install')
    system("terminal-notifier -title '#{`pwd`}' -message 'Cocoapods Updated' ")
  end
end

#post_checkout_fileObject



40
41
42
# File 'lib/cocoa-hookup.rb', line 40

def post_checkout_file
  File.join(git_dir, 'hooks', 'post-checkout')
end

#run(*argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cocoa-hookup.rb', line 20

def run(*argv)
  if argv.first =~ /install/i
    install
  elsif argv.first =~ /post-checkout/i
    post_checkout(*argv)
  elsif argv.length == 0
    puts "Usage: 'cocoa-hookup install' to install the git hook"
  else
    raise Error, "Invalid arguments for 'cocoa-hookup'"
  end
end