Class: Ghundle::Command::Install

Inherits:
Common
  • Object
show all
Defined in:
lib/ghundle/command/install.rb

Overview

Installs the given hook in the local repository. If there is no local hook by the given name, delegates to the the Fetch command to get the hook first.

Instance Attribute Summary

Attributes inherited from Common

#args

Instance Method Summary collapse

Methods inherited from Common

call, #initialize

Constructor Details

This class inherits a constructor from Ghundle::Command::Common

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ghundle/command/install.rb', line 10

def call
  args.each do |hook_name|
    local_source = Source::Local.new(config.hook_path(hook_name))

    if not local_source.exists?
      # try to fetch it instead
      Fetch.call(*args)
      real_hook_name = File.basename(hook_name)
      local_source   = Source::Local.new(config.hook_path(real_hook_name))
    end

    hook = Hook.new(local_source)

    prepare_git_hook(hook)
    install_git_hook(hook)
  end
end

#install_git_hook(hook) ⇒ Object



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
# File 'lib/ghundle/command/install.rb', line 49

def install_git_hook(hook)
  hook..types.each do |hook_type|
    git_hook_file   = ".git/hooks/#{hook_type}"
    hook_invocation = "ghundle run #{hook.name} $*"

    lines         = File.readlines(git_hook_file).map(&:rstrip)
    existing_hook = lines.find { |l| l.include?(hook_invocation) }

    if existing_hook
      say "Hook already installed for #{hook_type}"
      return
    end

    say "Installing hook #{hook} for #{hook_type}"
    end_line_index = lines.rindex { |l| l =~ /^# End of ghundle scripts/ }

    if end_line_index
      lines.insert(end_line_index, hook_invocation)
    else
      lines << '# Start of ghundle scripts'
      lines << hook_invocation
      lines << '# End of ghundle scripts'
    end

    File.open(git_hook_file, 'w') do |f|
      f.write(lines.join("\n"))
    end
  end
end

#prepare_git_hook(hook) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ghundle/command/install.rb', line 28

def prepare_git_hook(hook)
  validate_git_repo

  hook_types = hook..types
  hook_types.each do |hook_type|
    validate_hook_type(hook_type)
  end

  hook_types.each do |hook_type|
    git_hook_file = ".git/hooks/#{hook_type}"

    if not File.exists?(git_hook_file)
      File.open(git_hook_file, 'w') do |f|
        f.puts '#! /bin/sh'
        f.puts ''
      end
      File.chmod(0755, git_hook_file)
    end
  end
end

#validate_git_repoObject



79
80
81
82
83
# File 'lib/ghundle/command/install.rb', line 79

def validate_git_repo
  if not File.directory?('.git/hooks')
    error "Can't find `.git/hooks` directory, are you in a git repository?"
  end
end

#validate_hook_type(type) ⇒ Object

TODO (2013-06-30) Move validations to Metadata?



86
87
88
89
90
# File 'lib/ghundle/command/install.rb', line 86

def validate_hook_type(type)
  if not possible_hook_types.include?(type)
    error "The type of the script needs to be one of: #{possible_hook_types.join(', ')}."
  end
end