Sha256: 37175e3e6483f2985b9e2c33626923d8312e3f28f65155dce73ecb81f8faf7ad

Contents?: true

Size: 1.95 KB

Versions: 8

Compression:

Stored size: 1.95 KB

Contents

require 'erb'

module Blazing
  class Hook

    include Blazing::Logger

    attr_accessor :target

    def initialize(target)
      @target = target
      @config = target.config
      @options = target.options
      @shell = Blazing::Shell.new
    end

    def setup
      prepare_hook
      deploy_hook
    end

    def rake_command
      rake_config = @config.instance_variable_get("@rake") || {}
      rails_env = "RAILS_ENV=#{@options[:rails_env]}" if @options[:rails_env]

      if rake_config[:task]
        "#{rake_config[:env]} #{rails_env} bundle exec rake #{rake_config[:task]}"
      end
    end

    private

    def load_template(template_name)
      ::ERB.new(File.read(find_template(template_name))).result(binding)
    end

    def find_template(template_name)
      "#{Blazing::TEMPLATE_ROOT}/#{template_name}.erb"
    end

    def prepare_hook
      info "Generating and uploading post-receive hook for #{@target.name}"
      hook = generate_hook
      write hook
    end

    def deploy_hook
      debug "Copying hook for #{@target.name} to #{@target.location}"
      copy_hook
      set_hook_permissions
    end

    def generate_hook
      load_template 'hook/base'
    end

    def write(hook)
      File.open(Blazing::TMP_HOOK, "wb") do |f|
        f.puts hook
      end
    end

    def set_hook_permissions
      if @target.host
        @shell.run "ssh #{@target.user}@#{@target.host} '#{make_hook_executable}'"
      else
        @shell.run "#{make_hook_executable}"
      end
    end

    def copy_hook
      debug "Making hook executable"
      # TODO: handle missing user?
      if @target.host
        @shell.run "scp #{Blazing::TMP_HOOK} #{@target.user}@#{@target.host}:#{@target.path}/.git/hooks/post-receive"
      else
        @shell.run "cp #{Blazing::TMP_HOOK} #{@target.path}/.git/hooks/post-receive"
      end
    end

    def make_hook_executable
      debug "Making hook executable"
      "chmod +x #{@target.path}/.git/hooks/post-receive"
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
blazing-0.4.2 lib/blazing/hook.rb
blazing-0.4.1 lib/blazing/hook.rb
blazing-0.4.0 lib/blazing/hook.rb
blazing-0.4.0.beta3 lib/blazing/hook.rb
blazing-0.4.0.beta2 lib/blazing/hook.rb
blazing-0.4.0.beta1 lib/blazing/hook.rb
blazing-0.3.0 lib/blazing/hook.rb
blazing-0.2.14 lib/blazing/hook.rb