Class: CommitMsgUrlShortener::HookScript

Inherits:
Object
  • Object
show all
Defined in:
lib/commit-msg-url-shortener/hook_script.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_path) ⇒ HookScript

Returns a new instance of HookScript.



5
6
7
8
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 5

def initialize git_path
  @name = 'commit-msg'
  @hook_path = File.expand_path File.join(git_path, ".git/hooks/#{@name}")
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 3

def name
  @name
end

Instance Method Details

#append_code(code) ⇒ Object



10
11
12
13
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 10

def append_code code
  remove_code
  open('a+b') {|file| file.puts "#{code}"}
end

#createObject



68
69
70
71
72
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 68

def create
  open('wb') {|file| file.puts "#!/bin/sh"}
  @interpreter = 'sh'
  make_executable
end

#exist?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 64

def exist?
  File.exist? @hook_path
end

#get_used_serviceObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 47

def get_used_service
  lines_call_the_gem = []
  service = nil
  return service unless exist?
  open('rb') do |file|
    file.each_line {|line| lines_call_the_gem << line if line.downcase.index(BIN_NAME.downcase)}
    if lines_call_the_gem.size > 1
      raise "It appears that there are multiple calls to #{BIN_NAME} in your #{name} hook."
    end
    unless lines_call_the_gem.empty?
      service = SERVICES.select{|s| lines_call_the_gem.first.include? s.name}.first
      raise "It appears that your are using an unknow service." unless service
    end
  end
  service
end

#interpreterObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 15

def interpreter
  @interpreter ||= begin
    return unless exist?
    interpreter  = nil
    open('rb') do |file|
      shebang_line = ''
      file.each_line{|line| shebang_line = line if line.index '#!'}
      INTERPRETERS.each{|name| interpreter = name if shebang_line.downcase.index name}
    end
    interpreter
  end
end

#remove_codeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/commit-msg-url-shortener/hook_script.rb', line 28

def remove_code
  new_lines = []
  removed_lines = []
  return removed_lines unless exist?
  open('rb') do |file|
    file.each_line do |line| 
      unless line.downcase.index BIN_NAME.downcase
        new_lines << line
      else
        removed_lines << line
      end
    end
  end
  open('wb') do |file|
    new_lines.each{|line| file.puts line}
  end
  removed_lines
end