Sha256: 16506982a15112d8824010a30beaf85ca40adc9a60e5a1d1595b3ad9b54e7010

Contents?: true

Size: 1.22 KB

Versions: 11

Compression:

Stored size: 1.22 KB

Contents

require 'fileutils'
require 'pre-commit/configuration'

module PreCommit

  class Installer

    TARGET_GIT_PATH = '.git'
    TARGET_HOOKS_PATH = 'hooks/pre-commit'
    TEMPLATE_DIR = File.expand_path("../../../templates/hooks/", __FILE__)

    attr_reader :key

    def initialize(key = nil)
      @key = key || "default"
    end

    def hook
      templates[key.sub(/^--/, "")]
    end

    def target
      target_git_path =
      if   File.directory?(TARGET_GIT_PATH)
      then TARGET_GIT_PATH
      else File.readlines('.git').first.match(/gitdir: (.*)$/)[1]
      end
      File.join(target_git_path, TARGET_HOOKS_PATH)
    end

    def install
      if
        hook
      then
        FileUtils.mkdir_p(File.dirname(target))
        FileUtils.cp(hook, target)
        FileUtils.chmod(0755, target)
        puts "Installed #{hook} to #{target}"
        true
      else
        warn "Could not find template #{key}"
        false
      end
    end

  private

    def templates
      return @templates if @templates
      pattern = File.join(TEMPLATE_DIR, "*")

      @templates =
      Dir.glob(pattern).inject({}) do |hash, file|
        key = file.match(/\/([^\/]+?)$/)[1]
        hash[key] = file
        hash
      end
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pre-commit-0.29.0 lib/pre-commit/installer.rb
pre-commit-0.28.0 lib/pre-commit/installer.rb
pre-commit-0.27.0 lib/pre-commit/installer.rb
pre-commit-0.26.0 lib/pre-commit/installer.rb
pre-commit-0.25.0 lib/pre-commit/installer.rb
pre-commit-0.24.0 lib/pre-commit/installer.rb
pre-commit-0.23.0 lib/pre-commit/installer.rb
pre-commit-0.22.1 lib/pre-commit/installer.rb
pre-commit-0.22.0 lib/pre-commit/installer.rb
pre-commit-0.21.0 lib/pre-commit/installer.rb
pre-commit-0.20.0 lib/pre-commit/installer.rb