Class: Heidi::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/heidi/build.rb

Overview

An integration is called a build. The collections of builds is the log of the project. A single build lives in $project/ A build is tied to a commit

Defined Under Namespace

Classes: Logs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, commit = project.commit) ⇒ Build

Returns a new instance of Build.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/heidi/build.rb', line 14

def initialize(project, commit=project.commit)
  @project = project
  @commit  = commit

  @root       = File.join(project.root, "logs", commit)
  @log_root   = File.join(@root, "logs")
  @build_root = File.join(@root, "build")

  if !File.exists? @root
    SimpleShell.new(project.root).mkdir %W(-p #{@root})
  end
  @shell = SimpleShell.new(@root)

  @shell.mkdir %W(-p #{@log_root}) unless File.exists?(@log_root)
  @logs = Logs.new(@log_root)
end

Instance Attribute Details

#build_rootObject (readonly)

Returns the value of attribute build_root.



11
12
13
# File 'lib/heidi/build.rb', line 11

def build_root
  @build_root
end

#commitObject (readonly)

Returns the value of attribute commit.



11
12
13
# File 'lib/heidi/build.rb', line 11

def commit
  @commit
end

#hooksObject (readonly)

Returns the value of attribute hooks.



11
12
13
# File 'lib/heidi/build.rb', line 11

def hooks
  @hooks
end

#log_rootObject (readonly)

Returns the value of attribute log_root.



11
12
13
# File 'lib/heidi/build.rb', line 11

def log_root
  @log_root
end

#logsObject (readonly)

Returns the value of attribute logs.



11
12
13
# File 'lib/heidi/build.rb', line 11

def logs
  @logs
end

#projectObject (readonly)

Returns the value of attribute project.



11
12
13
# File 'lib/heidi/build.rb', line 11

def project
  @project
end

#rootObject (readonly)

Returns the value of attribute root.



11
12
13
# File 'lib/heidi/build.rb', line 11

def root
  @root
end

#shellObject (readonly)

Returns the value of attribute shell.



11
12
13
# File 'lib/heidi/build.rb', line 11

def shell
  @shell
end

Instance Method Details

#authorObject



31
32
33
# File 'lib/heidi/build.rb', line 31

def author
  project.author(@commit)
end

#cleanObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/heidi/build.rb', line 65

def clean
  1.downto(0) do |i|
    if File.exists? "#{@log_root}.#{i}"
      if i - 1 < 0
        shell.mv %W(#{@log_root}.#{i} #{@log_root}.#{i+1})
      else
        shell.rm %W(-rf #{@log_root}.#{i})
      end
    end
  end

  if File.exists? "#{@log_root}"
    shell.mv %W(#{@log_root} #{@log_root}.0)
  end

  %w(build/ SUCCESS FAILURE).each do |inode|
    shell.rm("-r", "-f", inode) if File.exists? File.join(@root, inode)
  end

  # re-instate the logs
  @shell.mkdir %W(-p #{@log_root})
end

#dateObject



35
36
37
# File 'lib/heidi/build.rb', line 35

def date
  project.date(@commit)
end

#failed?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/heidi/build.rb', line 144

def failed?
  File.exists?(File.join(@root, "FAILURE"))
end

#load_hooksObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/heidi/build.rb', line 39

def load_hooks
  log :info, "Loading hooks"
  @hooks  = {
    :before  => [],
    :build   => [],
    :tests   => [],
    :success => [],
    :failure => [],
  }

  @hooks.keys.each do |key|
    log :debug, "Loading #{key} hooks"

    Dir[File.join(project.root, "hooks", key.to_s, "*")].sort.each do |hook|
      next if File.directory? hook
      next unless File.executable? hook

      log :debug, "Loaded hook: #{hook}"

      @hooks[key] << Heidi::Hook.new(self, hook)
    end
  end

  log :info, "Hooks loaded"
end

#lock(&block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/heidi/build.rb', line 103

def lock(&block)
  log(:info, "Locking build")
  File.open(lock_file, File::CREAT|File::TRUNC|File::WRONLY) do |f|
    f.puts Time.now.ctime
  end

  if block_given?
    yield
    unlock
  end
end

#lock_fileObject



99
100
101
# File 'lib/heidi/build.rb', line 99

def lock_file
  File.join(@root, ".lock")
end

#locked?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/heidi/build.rb', line 121

def locked?
  File.exists? lock_file
end

#log(type, msg, raw = false) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/heidi/build.rb', line 88

def log(type, msg, raw=false)
  name = case type
  when :error
    "heidi.errors"
  else
    "heidi.#{type}"
  end

  logs[name].send(raw == false ? :write : :raw, msg)
end

#record(what) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/heidi/build.rb', line 125

def record(what)
  flags = File::CREAT|File::TRUNC|File::WRONLY
  file = nil
  case what
  when :failure
    project.build_status = "failed"
    file = File.open(File.join(@root, "FAILURE"), flags)
  when :success
    project.build_status = "passed"
    project.record_latest_build
    file = File.open(File.join(@root, "SUCCESS"), flags)
  end

  unless file.nil?
    file.puts Time.now.ctime
    file.close
  end
end

#statusObject



152
153
154
155
156
157
158
# File 'lib/heidi/build.rb', line 152

def status
  self.failed? ?
    "failed" :
    self.success? ?
      "passed" :
      "DNF"
end

#success?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/heidi/build.rb', line 148

def success?
  File.exists?(File.join(@root, "SUCCESS"))
end

#tar_ballObject

file handle to tar ball



161
162
163
164
165
166
# File 'lib/heidi/build.rb', line 161

def tar_ball
  ball = File.join(@root, "#{commit}.tar.bz2")

  return nil if !File.exists?(ball)
  return File.open(ball, File::RDONLY)
end

#unlockObject



115
116
117
118
119
# File 'lib/heidi/build.rb', line 115

def unlock
  return unless locked?
  log(:info, "Unlocking build")
  File.unlink lock_file
end