Class: Jekyll::PreCommit::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-pre-commit/runner.rb

Instance Method Summary collapse

Instance Method Details

#run(site, staged_files) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'lib/jekyll-pre-commit/runner.rb', line 5

def run(site, staged_files)
  result = { :ok => true, :messages => [] }
  if !site.config["pre-commit"] || site.config["pre-commit"].empty?
    # Bail if there are no pre-commit checks enabled
    result[:messages].push("No pre-commit checks enabled")
    return result
  end

  staged_posts = Array.new
  not_staged_posts = Array.new

  site.posts.docs.each do |p|
    if (staged_files.include? p.path.gsub(Dir.pwd + "/", ""))
      staged_posts.push(p)
    else
      not_staged_posts.push(p)
    end
  end

  if staged_posts.empty?
    # If no posts are being committed. Safe to commit.
    result[:messages].push("No posts staged")
    return result
  end

  site.config["pre-commit"].each do |c|
    begin
      o = Object.const_get("Jekyll::PreCommit::Checks::" + c["check"]).new
    rescue
      result[:ok] = false
      result[:messages].push("The check #{c["check"]} does not exist! Please fix your configuration.")
      break
    end
    r = o.check(staged_posts, not_staged_posts, site, c)
    if !r[:ok]
      result[:ok] = false
    end
    if r[:message] != ""
      result[:messages].push(r[:message])
    end
  end

  return result
end