Class: Checker::Installator

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

Class Method Summary collapse

Class Method Details

.install!Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/checker/installator.rb', line 8

def self.install!
  hooks_dir = "#{Dir.pwd}/.git/hooks"

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

  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

.templateObject



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

def self.template
  dir = File.dirname(__FILE__) + "/../.."
  open(dir + "/templates/checker-prepare-commit-msg").read
end