Class: Heidi::Project

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ Project

Returns a new instance of Project.



10
11
12
13
14
15
# File 'lib/heidi/project.rb', line 10

def initialize(root)
  @root        = root
  @lock_file   = File.join(root, ".lock")
  @cached_root = File.join(root, "cached")
  @git         = Heidi::Git.new(@cached_root)
end

Instance Attribute Details

#cached_rootObject (readonly)

Returns the value of attribute cached_root.



8
9
10
# File 'lib/heidi/project.rb', line 8

def cached_root
  @cached_root
end

#lock_fileObject (readonly)

Returns the value of attribute lock_file.



8
9
10
# File 'lib/heidi/project.rb', line 8

def lock_file
  @lock_file
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/heidi/project.rb', line 8

def root
  @root
end

Class Method Details

.find(commit) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/heidi/project.rb', line 28

def @builds.find(commit)
  return nil unless commit.is_a?(String)
  return nil unless commit.length >= 5

  self.select do |build|
    build.commit == commit || build.commit =~ Regexp.new("^#{commit}")
  end.first
end

Instance Method Details

#author(commit = self.commit) ⇒ Object



53
54
55
# File 'lib/heidi/project.rb', line 53

def author(commit=self.commit)
  @git.log(1, "%cN <%cE>", commit)
end

#build_statusObject



82
83
84
# File 'lib/heidi/project.rb', line 82

def build_status
  @git["build.status"]
end

#build_status=(status) ⇒ Object



85
86
87
# File 'lib/heidi/project.rb', line 85

def build_status=(status)
  @git["build.status"] = status
end

#buildsObject



17
18
19
# File 'lib/heidi/project.rb', line 17

def builds
  @builds || load_builds
end

#commitObject



49
50
51
# File 'lib/heidi/project.rb', line 49

def commit
  @git.commit[0..8]
end

#current_buildObject



75
76
77
# File 'lib/heidi/project.rb', line 75

def current_build
  @git["build.current"]
end

#date(commit = self.commit) ⇒ Object



57
58
59
# File 'lib/heidi/project.rb', line 57

def date(commit=self.commit)
  @git.log(1, "%ci", commit)
end

#fetchObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/heidi/project.rb', line 115

def fetch
  if integration_branch && @git.branch != integration_branch
    if @git.branches.include? integration_branch
      @git.switch(integration_branch)
      @git.merge "origin/#{integration_branch}"

    else
      @git.checkout(integration_branch, "origin/#{integration_branch}")

    end
  end

  @git.pull

  # when the head has changed, update some stuff
  if last_commit != self.commit
    record_last_commit
  end
end

#integrate(forced = false) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/heidi/project.rb', line 94

def integrate(forced=false)
  return true if !forced && self.current_build == self.commit
  return "locked" if locked?

  status = "unknown"

  self.lock do
    record_current_build
    res = Heidi::Integrator.new(self).integrate
    if res == true
      status = nil
    elsif res.is_a? String
      status = res
    else
      status = "failed"
    end
  end

  return status
end

#integration_branchObject



89
90
91
92
# File 'lib/heidi/project.rb', line 89

def integration_branch
  name = @git["build.branch"]
  name == "" ? nil : name
end

#last_commitObject



61
62
63
# File 'lib/heidi/project.rb', line 61

def last_commit
  @git["commit"]
end

#latest_buildObject



68
69
70
# File 'lib/heidi/project.rb', line 68

def latest_build
  @git["build.latest"]
end

#load_buildsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/heidi/project.rb', line 21

def load_builds
  @builds = []
  Dir[File.join(root, "logs", "*")].each do |build|
    next unless File.directory? build
    @builds << Heidi::Build.new(self, File.basename(build))
  end

  def @builds.find(commit)
    return nil unless commit.is_a?(String)
    return nil unless commit.length >= 5

    self.select do |build|
      build.commit == commit || build.commit =~ Regexp.new("^#{commit}")
    end.first
  end

  return @builds
end

#lock(&block) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/heidi/project.rb', line 135

def lock(&block)
  File.open(lock_file, File::TRUNC|File::CREAT|File::WRONLY) do |f|
    f.puts Time.now.strftime("%c")
  end

  if block_given?
    yield
    self.unlock
  end
end

#locked?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/heidi/project.rb', line 163

def locked?
  File.exists? lock_file
end

#logObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/heidi/project.rb', line 146

def log
  log = @git.graph(120)

  lines = []
  log.out.lines.each do |line|
    color_less = line.gsub(/\e\[[^m]+m/, '')
    commit = color_less.scan(/^[\| \*]+ ([a-z0-9]+)/).flatten.first
    lines << { :line => line, :build => builds.find(commit) }
  end

  return lines
end

#nameObject



45
46
47
# File 'lib/heidi/project.rb', line 45

def name
  @name ||= @git[:name]
end

#name=(name) ⇒ Object



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

def name=(name)
  @name = name
  @git["name"] = name
end

#record_current_buildObject



78
79
80
# File 'lib/heidi/project.rb', line 78

def record_current_build
  @git["build.current"] = self.commit
end

#record_last_commitObject



64
65
66
# File 'lib/heidi/project.rb', line 64

def record_last_commit
  @git["commit"] = self.commit
end

#record_latest_buildObject



71
72
73
# File 'lib/heidi/project.rb', line 71

def record_latest_build
  @git["build.latest"] = self.commit
end

#unlockObject



159
160
161
# File 'lib/heidi/project.rb', line 159

def unlock
  File.unlink lock_file
end