Class: Checker::Installator

Inherits:
Object
  • Object
show all
Defined in:
lib/checker/installator.rb

Class Method Summary collapse

Class Method Details

.check_hook!Object



13
14
15
16
17
18
# File 'lib/checker/installator.rb', line 13

def self.check_hook!
  unless Dir.exist?(hooks_dir)
    puts "Git Hooks dir not found. Are you sure you are inside project with git?"
    exit 1
  end
end

.hooks_dirObject



9
10
11
# File 'lib/checker/installator.rb', line 9

def self.hooks_dir
  "#{Dir.pwd}/.git/hooks"
end

.install!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/checker/installator.rb', line 32

def self.install!
  check_hook!

  pre_commit = "#{hooks_dir}/prepare-commit-msg"
  if File.exist?(pre_commit)
    puts "Appending checker script to existing prepare-commit-msg hook..."
    begin
      open(pre_commit, 'a') do |f| 
        f.puts(self.template)
        f.chmod(0755)
      end
    rescue Exception => e
      puts "Couldn't append checker script: #{e.message}"
      exit 1
    end
    exit 0
  else
    tmp = self.template
    str = "#!/bin/bash \n #{tmp}"
    begin
      open(pre_commit, "w") do |f| 
        f.puts(str)
        f.chmod(0755)
      end
    rescue Exception => e
      puts "Couldn't write checker script: #{e.message}"
      exit 1
    end
    puts "Script installed!"
    exit 0
  end
end

.reinstall!Object



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

def self.reinstall!
  check_hook!

  pre_commit = "#{hooks_dir}/prepare-commit-msg"
  if File.exists?(pre_commit)
    puts "Removing current git precommit hook..."
    File.delete(pre_commit)
  end

  install!
end

.templateObject



3
4
5
6
7
# File 'lib/checker/installator.rb', line 3

def self.template
  dir = File.expand_path('../../..', __FILE__)
  temp = File.read(File.join(dir, "/templates/checker-prepare-commit-msg"))
  ERB.new(temp).result
end