Class: Hookie::Framework

Inherits:
Object
  • Object
show all
Defined in:
lib/hookie/framework.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook, repo_path) ⇒ Framework

Returns a new instance of Framework.



14
15
16
17
# File 'lib/hookie/framework.rb', line 14

def initialize(hook, repo_path)
  @repo = Grit::Repo.new(repo_path)
  read_changes
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



7
8
9
# File 'lib/hookie/framework.rb', line 7

def changes
  @changes
end

Class Method Details

.hook(hook) ⇒ Object



9
10
11
12
# File 'lib/hookie/framework.rb', line 9

def self.hook(hook)
  hookie = Hookie::Framework.new hook, ARGV[1] || Dir.getwd
  hookie.run_plugins(hook)
end

Instance Method Details

#commit_url(commit) ⇒ Object



62
63
64
65
66
# File 'lib/hookie/framework.rb', line 62

def commit_url(commit)
  if config['hookie.core.web.commit']
    config['hookie.core.web.commit'].gsub("%REPO%", repo_name).gsub("%COMMIT%", commit.id)
  end
end

#configObject



82
83
84
# File 'lib/hookie/framework.rb', line 82

def config
  @repo.config
end

#head_names_for_commit(commit) ⇒ Object



78
79
80
# File 'lib/hookie/framework.rb', line 78

def head_names_for_commit(commit)
  @repo.heads.collect { |head| head.name if head.commit.id == commit.id }.compact
end

#log(plugin, message) ⇒ Object



51
52
53
54
# File 'lib/hookie/framework.rb', line 51

def log(plugin, message)
  timestamp = Time.now.strftime("%Y-%m-%d %H:%M:%S")
  puts "[#{timestamp}] #{plugin}: #{message}"
end

#repo_nameObject



68
69
70
71
72
73
74
75
76
# File 'lib/hookie/framework.rb', line 68

def repo_name
  if config['hookie.core.repo.name']
    config['hookie.core.repo.name']
  elsif @repo.bare
    File.basename(@repo.path, ".git")
  else
    File.basename(File.expand_path(File.join(@repo.path, "..")))
  end
end

#repo_urlObject



56
57
58
59
60
# File 'lib/hookie/framework.rb', line 56

def repo_url
  if config['hookie.core.web.browse']
    config['hookie.core.web.browse'].gsub("%REPO%", repo_name)
  end
end

#run_plugins(hook) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hookie/framework.rb', line 19

def run_plugins(hook)
  # we are only allowed to run if the plugin is in the list of allowed
  # plugins
  unless config['hookie.core.allowedplugins']
    exit 255
  end

  Dir.glob(File.join(File.join(File.dirname(__FILE__),"plugins"), "*_plugin.rb")) do |filename|
    begin
      require filename
    rescue LoadError => e
      puts "Unable to load plugin #{filename}: #{e}"
    end
  end

  Hookie::Plugin.constants.each do |constant|
    clazz = Hookie::Plugin.const_get(constant)
    if clazz < Hookie::BasePlugin
      plugin = clazz.new(self)
      if config['hookie.core.allowedplugins'].include?(plugin.config_key) and
        plugin.respond_to?(hook) and
        plugin.should_run?
          begin
            plugin.send(hook)
          rescue Exception => e
            log plugin, "ERROR: plugin threw an exception: #{e}"
          end
      end
    end
  end
end