Class: VagrantGitHooks::Util::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-git-hooks/util/installer.rb

Constant Summary collapse

BACKUP_SUFFIX =
".bak"

Instance Method Summary collapse

Constructor Details

#initialize(hooks_dir:, manifest:, ui: nil) ⇒ Installer



13
14
15
16
17
# File 'lib/vagrant-git-hooks/util/installer.rb', line 13

def initialize(hooks_dir:, manifest:, ui: nil)
  @hooks_dir = hooks_dir.to_s
  @manifest  = manifest
  @ui        = ui
end

Instance Method Details

#install(entries, fail_on_error: true, dry_run: false) ⇒ Hash

Installs or updates managed Git hooks.

Existing foreign hooks are backed up once before replacement. Managed hooks are updated in place and tracked in the manifest by SHA-256 digest.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-git-hooks/util/installer.rb', line 28

def install(entries, fail_on_error: true, dry_run: false)
  FileUtils.mkdir_p(@hooks_dir) unless dry_run
  data = @manifest.load
  data["hooks_dir"] = @hooks_dir
  managed = data["managed"]
  result  = { installed: [], updated: [], backed_up: [], dry_run: dry_run }

  entries.each do |entry|
    name   = entry[:name].to_s
    script = build(entry, fail_on_error)
    path   = File.join(@hooks_dir, name)
    ours   = File.exist?(path) && HookBuilder.managed?(safe_read(path))

    backup = backup_if_foreign(path, name, ours, managed, result, dry_run)
    write_hook(path, script) unless dry_run

    (ours ? result[:updated] : result[:installed]) << name
    managed[name] = {
      "backup" => backup,
      "sha256" => Digest::SHA256.hexdigest(script),
      "installed_at" => now
    }
  end

  @manifest.save(data) unless dry_run
  result
end

#status(configured_names = []) ⇒ Array<Hash>

Reports whether hooks are installed, modified, foreign, or missing.



88
89
90
91
92
93
94
95
96
97
# File 'lib/vagrant-git-hooks/util/installer.rb', line 88

def status(configured_names = [])
  data    = @manifest.load
  base    = data["hooks_dir"] || @hooks_dir
  managed = data["managed"]
  names   = (managed.keys + configured_names.map(&:to_s)).uniq.sort

  names.map do |name|
    { name: name, state: state_of(File.join(base, name), managed[name]), backup: managed.dig(name, "backup") }
  end
end

#uninstall(dry_run: false) ⇒ Hash

Removes managed hooks and restores backups recorded in the manifest.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/vagrant-git-hooks/util/installer.rb', line 60

def uninstall(dry_run: false)
  data    = @manifest.load
  base    = data["hooks_dir"] || @hooks_dir
  result  = { removed: [], restored: [], dry_run: dry_run }

  data["managed"].each do |name, info|
    path = File.join(base, name)

    if File.exist?(path) && HookBuilder.managed?(safe_read(path))
      File.delete(path) unless dry_run
      result[:removed] << name
    end

    backup = info["backup"]
    next unless backup && File.exist?(backup)

    restore(backup, path) unless dry_run
    result[:restored] << name
  end

  @manifest.clear unless dry_run
  result
end