Class: Perkins::Runner

Inherits:
Object show all
Defined in:
lib/perkins/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



5
6
7
# File 'lib/perkins/runner.rb', line 5

def branch
  @branch
end

#build_timeObject (readonly)

Returns the value of attribute build_time.



6
7
8
# File 'lib/perkins/runner.rb', line 6

def build_time
  @build_time
end

#commandObject

Returns the value of attribute command.



5
6
7
# File 'lib/perkins/runner.rb', line 5

def command
  @command
end

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/perkins/runner.rb', line 5

def config
  @config
end

#current_buildObject (readonly)

Returns the value of attribute current_build.



6
7
8
# File 'lib/perkins/runner.rb', line 6

def current_build
  @current_build
end

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/perkins/runner.rb', line 6

def duration
  @duration
end

#repoObject

Returns the value of attribute repo.



5
6
7
# File 'lib/perkins/runner.rb', line 5

def repo
  @repo
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/perkins/runner.rb', line 6

def response
  @response
end

#shaObject

Returns the value of attribute sha.



5
6
7
# File 'lib/perkins/runner.rb', line 5

def sha
  @sha
end

#statusObject (readonly)

Returns the value of attribute status.



6
7
8
# File 'lib/perkins/runner.rb', line 6

def status
  @status
end

Instance Method Details

#config_command_with_empty_value?(result, process_status) ⇒ Boolean

Returns:

  • (Boolean)


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

def config_command_with_empty_value?(result, process_status)
  process_status.exitstatus.to_i == 1 && result.empty?
end

#exec(cmd) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/perkins/runner.rb', line 8

def exec(cmd)
  result = run_script(cmd)
  puts result
  process_status = $?

  if successful_command?(process_status) || config_command_with_empty_value?(result,process_status)
    @response = result
    @status = true
    return result
  else
    @response = result
    @status = false
  end
end

#get_buildsObject



110
111
112
# File 'lib/perkins/runner.rb', line 110

def get_builds
  repo.build_reports
end

#git_update(branch) ⇒ Object



86
87
88
89
90
# File 'lib/perkins/runner.rb', line 86

def git_update(branch)
  puts "fetch repo & reset to sha #{sha}".green
  repo.git.fetch()
  repo.git.reset_hard(sha)
end

#run(sha) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/perkins/runner.rb', line 35

def run(sha)
  self.sha = sha
  start_build
  script = Perkins::Build::script(config, repo)
  sh = script.compile
  repo.git.chdir do
    git_update(sha)
    set_build_stats do
      puts "perform build".green
      self.exec(sh)
    end
  end
  store_report
  stop_build
end

#run_script(source) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perkins/runner.rb', line 23

def run_script(source)
  script = File.expand_path(
    "~/.perkins/.build/#{repo.name}/travis-build-#{sha}" #<< stages.join('-')
  )
  FileUtils.mkdir_p(File.dirname(script))
  File.open(script, 'w') { |f| f.write(source) }
  FileUtils.chmod(0755, script)
  Bundler.with_clean_env{
    `bash #{script} 2>&1`.chomp
  }
end

#running?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/perkins/runner.rb', line 70

def running?
  @running
end

#set_build_stats(&block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/perkins/runner.rb', line 61

def set_build_stats(&block)
  up = Time.now
  #call the command itself
  block.call
  down = Time.now
  @build_time = down
  @duration = down - up
end

#start_buildObject



51
52
53
54
# File 'lib/perkins/runner.rb', line 51

def start_build
  @running = true
  @repo.update_column(:build_status, "started")
end

#stop_buildObject



56
57
58
59
# File 'lib/perkins/runner.rb', line 56

def stop_build
  @running = false
  @repo.update_column(:build_status, "stopped")
end

#store_reportObject



105
106
107
108
# File 'lib/perkins/runner.rb', line 105

def store_report
  r = Perkins::BuildReport.new(self.to_report)
  repo.build_reports << r
end

#successful_command?(process_status) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/perkins/runner.rb', line 74

def successful_command?(process_status)
  process_status.exitstatus.to_i == 0
end

#to_reportObject

TODO: add a serialized commit in order to avoid Perkins::Commit initialization in every instantiation



94
95
96
97
98
99
100
101
102
103
# File 'lib/perkins/runner.rb', line 94

def to_report
  {
    build_time: self.build_time,
    duration: self.duration,
    response: self.response,
    status: self.status,
    sha: self.sha,
    branch: self.branch
  }
end

#working_dirObject



82
83
84
# File 'lib/perkins/runner.rb', line 82

def working_dir
  repo.git.dir.path
end