Class: ConventionalCommits::Configuration::HooksInstaller

Inherits:
Object
  • Object
show all
Defined in:
lib/hooks/hooks_installer.rb

Instance Method Summary collapse

Instance Method Details

#cli_command(cmd) ⇒ Object



63
64
65
# File 'lib/hooks/hooks_installer.rb', line 63

def cli_command(cmd)
  "bundle exec conventional_commits #{cmd}"
end

#common_hooksObject



56
57
58
59
60
61
# File 'lib/hooks/hooks_installer.rb', line 56

def common_hooks
  initial_lines +
    [
      cli_command("validate_branch_name")
    ]
end

#create_file_and_make_executable(path:, commands:) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/hooks/hooks_installer.rb', line 31

def create_file_and_make_executable(path:, commands:)
  File.create_directory(path: hook_directory)
  full_path = "#{hook_directory}/#{path}"
  filtered_commands = filter_existed_values_in_script(path: full_path, commands:)
  data = filtered_commands.join("\n")
  File.create_or_add_to_file_data(full_path, data)
  FileUtils.chmod("+x", full_path)
end

#filter_existed_values_in_script(path:, commands:) ⇒ Object



40
41
42
43
44
45
# File 'lib/hooks/hooks_installer.rb', line 40

def filter_existed_values_in_script(path:, commands:)
  return commands unless File.exist?(path)

  data = File.read_file(path) || ""
  commands.reject { |cmd| data.include?(cmd) }
end

#hook_directoryObject



52
53
54
# File 'lib/hooks/hooks_installer.rb', line 52

def hook_directory
  ".git/hooks"
end

#initial_linesObject



47
48
49
50
# File 'lib/hooks/hooks_installer.rb', line 47

def initial_lines
  [start_line,
   ruby_eval]
end

#install_allObject



25
26
27
28
29
# File 'lib/hooks/hooks_installer.rb', line 25

def install_all
  install_pre_commit
  install_commit_msg
  install_prepare_commit_msg
end

#install_commit_msgObject



20
21
22
23
# File 'lib/hooks/hooks_installer.rb', line 20

def install_commit_msg
  cmd = (common_hooks + [cli_command(validate_commit_msg)])
  create_file_and_make_executable(path: "commit-msg", commands: cmd)
end

#install_pre_commitObject



15
16
17
18
# File 'lib/hooks/hooks_installer.rb', line 15

def install_pre_commit
  cmd = common_hooks
  create_file_and_make_executable(path: "pre-commit", commands: cmd)
end

#install_prepare_commit_msgObject



8
9
10
11
12
13
# File 'lib/hooks/hooks_installer.rb', line 8

def install_prepare_commit_msg
  cmd = (common_hooks + [
    cli_command(prepare_commit_cli)
  ])
  create_file_and_make_executable(path: "prepare-commit-msg", commands: cmd)
end

#prepare_commit_cliObject



75
76
77
# File 'lib/hooks/hooks_installer.rb', line 75

def prepare_commit_cli
  "prepare_commit_msg --msg_path $1"
end

#ruby_evalObject



67
68
69
# File 'lib/hooks/hooks_installer.rb', line 67

def ruby_eval
  "eval \"$(rbenv init -)\""
end

#start_lineObject



71
72
73
# File 'lib/hooks/hooks_installer.rb', line 71

def start_line
  "#!/bin/sh"
end

#validate_commit_msgObject



79
80
81
# File 'lib/hooks/hooks_installer.rb', line 79

def validate_commit_msg
  "validate_commit_msg --msg_path $1"
end