Class: FunCi::Setup::HookWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/fun_ci/setup/hook_writer.rb

Constant Summary collapse

ALLOWED_HOOKS =
%w[pre-commit pre-push].freeze
MARKER =
"# fun-ci-managed-hook"
HOOK_COMMANDS =
{
  "pre-commit" => "fun-ci trigger --no-validate",
  "pre-push" => "fun-ci trigger"
}.freeze
HOOK_TEMPLATE =
<<~SH
  #!/bin/sh
  #{MARKER}
  COMMIT=$(git rev-parse HEAD 2>/dev/null || echo "0000000000000000000000000000000000000000")
  BRANCH=$(git branch --show-current 2>/dev/null || echo "unknown")
  %<command>s "$COMMIT" "$BRANCH"
SH

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root:, hook_type:, stdout:) ⇒ HookWriter

Returns a new instance of HookWriter.



28
29
30
31
32
# File 'lib/fun_ci/setup/hook_writer.rb', line 28

def initialize(project_root:, hook_type:, stdout:)
  @project_root = project_root
  @hook_type = hook_type
  @stdout = stdout
end

Class Method Details

.run(project_root:, hook_type:, stdout: $stdout) ⇒ Object



24
25
26
# File 'lib/fun_ci/setup/hook_writer.rb', line 24

def self.run(project_root:, hook_type:, stdout: $stdout)
  new(project_root: project_root, hook_type: hook_type, stdout: stdout).run
end

Instance Method Details

#runObject



34
35
36
37
38
39
40
41
42
# File 'lib/fun_ci/setup/hook_writer.rb', line 34

def run
  return reject("Not a git repository: no .git/ found.") unless git_repo?
  return reject("Unknown hook type: #{@hook_type}") unless ALLOWED_HOOKS.include?(@hook_type)
  return skip("Hook #{@hook_type} already exists from another tool, so it was left alone.") if foreign_hook?

  write_hook
  @stdout.puts "Installed #{@hook_type} hook."
  0
end