Class: Builder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Builder

Returns a new instance of Builder.



83
84
85
# File 'lib/buildboard.rb', line 83

def initialize(params)
  @params = params
end

Class Method Details

.build_dirObject



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

def build_dir
  File.join(ENV['HOME'], '.buildboard')
end

.buildsObject



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

def builds
  Dir[File.join(build_dir, '**', '*-*-*')].map{|build|
    match = build.match(/.*\/(.*)-(.*)-(.*)/)
    { :started => Time.at(match[1].to_i),
      :sha => match[2],
      :status => match[3],
      :integrity_status => (match[3] == 'failure' ? 'failed' : 'success')
    }
  }
end

.builds_for(projectname) ⇒ Object



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

def builds_for(projectname)
  Dir[File.join(build_dir, sanitize_filename(projectname), '*-*-*')].reverse[0..9].map{|build|
    Build.load_file(build)
  }
end

.find(projectname, filename) ⇒ Object



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

def find(projectname, filename)
  Build.load_file(File.join(build_dir, sanitize_filename(projectname), sanitize_filename(filename)))
end

.projectsObject



36
37
38
39
40
41
42
# File 'lib/buildboard.rb', line 36

def projects
  Dir[File.join(build_dir, '*')].map{|project|
    project_name = project.gsub("#{build_dir}/", '')
    last_build = Dir[File.join(project, '*')].last
    Build.load_file(last_build)
  }.sort{|a, b| b.finished_at <=> a.finished_at }
end

.sanitize_filename(filename) ⇒ Object

Borrowed from the Rails security guide: guides.rubyonrails.org/security.html#file-uploads



56
57
58
59
60
61
62
63
64
65
# File 'lib/buildboard.rb', line 56

def sanitize_filename(filename)
  name = filename.strip
  # NOTE: File.basename doesn't work right with Windows paths on Unix
  # get only the filename, not the whole path
  name.gsub! /^.*(\\|\/)/, ''
  # Finally, replace all non alphanumeric, underscore
  # or periods with underscore
  name.gsub! /[^\w\.\-]/, '_'
  name
end

Instance Method Details

#build_fileObject



91
92
93
94
# File 'lib/buildboard.rb', line 91

def build_file
  started = Time.parse(@params[:started_at]).to_i
  File.join(project_dir, "#{started}-#{@params[:sha][0..7]}-#{@params[:status]}")
end

#dumpObject



96
97
98
99
100
101
# File 'lib/buildboard.rb', line 96

def dump
  FileUtils.mkdir_p(project_dir)
  File.open(build_file, "w") do |file|
    YAML::dump(@params, file)
  end
end

#project_dirObject



87
88
89
# File 'lib/buildboard.rb', line 87

def project_dir
  File.join(self.class.build_dir, @params[:project])
end