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

Constant Summary collapse

FAILURE =
"FAILURE"
SUCCESS =
"SUCCESS"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Build.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/heidi/build.rb', line 17

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)

  @i_locked_build = false
end

Instance Attribute Details

#build_rootObject (readonly)

Returns the value of attribute build_root.



14
15
16
# File 'lib/heidi/build.rb', line 14

def build_root
  @build_root
end

#commitObject (readonly)

Returns the value of attribute commit.



14
15
16
# File 'lib/heidi/build.rb', line 14

def commit
  @commit
end

#hooksObject (readonly)

Returns the value of attribute hooks.



14
15
16
# File 'lib/heidi/build.rb', line 14

def hooks
  @hooks
end

#log_rootObject (readonly)

Returns the value of attribute log_root.



14
15
16
# File 'lib/heidi/build.rb', line 14

def log_root
  @log_root
end

#logsObject (readonly)

Returns the value of attribute logs.



14
15
16
# File 'lib/heidi/build.rb', line 14

def logs
  @logs
end

#projectObject (readonly)

Returns the value of attribute project.



14
15
16
# File 'lib/heidi/build.rb', line 14

def project
  @project
end

#rootObject (readonly)

Returns the value of attribute root.



14
15
16
# File 'lib/heidi/build.rb', line 14

def root
  @root
end

#shellObject (readonly)

Returns the value of attribute shell.



14
15
16
# File 'lib/heidi/build.rb', line 14

def shell
  @shell
end

Instance Method Details

#authorObject



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

def author
  project.author(@commit)
end

#cleanObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/heidi/build.rb', line 80

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



40
41
42
# File 'lib/heidi/build.rb', line 40

def date
  project.date(@commit)
end

#failed?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/heidi/build.rb', line 166

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

#load_hooksObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/heidi/build.rb', line 54

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



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/heidi/build.rb', line 118

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


  if block_given?
    yield
    unlock
  end
end

#lock_fileObject



114
115
116
# File 'lib/heidi/build.rb', line 114

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

#locked?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/heidi/build.rb', line 139

def locked?
  File.exists? lock_file
end

#locked_build?Boolean

Returns:

  • (Boolean)


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

def locked_build?
  @i_locked_build == true ? true : false
end

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



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

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

#messageObject



44
45
46
# File 'lib/heidi/build.rb', line 44

def message
  project.message(@commit)
end

#record(what) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/heidi/build.rb', line 147

def record(what)
  flags = File::CREAT|File::TRUNC|File::WRONLY
  file = nil
  case what
  when :failure
    project.build_status = Heidi::FAILED
    file = File.open(File.join(@root, FAILURE), flags)
  when :success
    project.build_status = Heidi::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



174
175
176
177
178
179
180
# File 'lib/heidi/build.rb', line 174

def status
  self.failed? ?
    Heidi::FAILED :
    self.success? ?
      Heidi::PASSED :
      Heidi::DNF
end

#success?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/heidi/build.rb', line 170

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

#tar_ballObject

file handle to tar ball



183
184
185
186
187
188
# File 'lib/heidi/build.rb', line 183

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

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

#timeObject



48
49
50
51
52
# File 'lib/heidi/build.rb', line 48

def time
  Time.parse(date)
rescue
  Time.now
end

#unlockObject



132
133
134
135
136
137
# File 'lib/heidi/build.rb', line 132

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