/
cli.rb
89 lines (74 loc) · 2.29 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'digest/sha1'
require 'yaml'
require 'rest_client'
require 'thor'
require 'SocketIO'
require 'claw/builder'
require 'claw/builder/package_formula'
class Claw::Builder::CLI < Thor
desc "build FORMULA", <<-DESC
Build a package using the FORMULA file given.
DESC
def build(formula)
error("Formula file doesn't exist") unless File.exists?(formula)
server = ENV["MAKE_SERVER"] || "http://localhost:8080"
# load the formula
Dir.chdir File.dirname(File.expand_path(formula))
$:.unshift File.expand_path('.')
load File.basename(formula)
klass = File.read(File.basename(formula))[/class (\w+)/, 1]
spec = eval(klass).to_spec
# create build manifest
manifest = spec.dup
manifest[:included_files] = []
spec[:included_files].each do |file|
io = File.open(file[:path], 'rb')
manifest[:included_files] << io
end
# initiate the build
puts ">> Uploading code for build"
res = RestClient.post "http://localhost:8080/build", manifest.to_json, :content_type => :json, :accept => :json
res = JSON.parse(res)
puts ">> Tailing build..."
done_building = false
client = SocketIO.connect(res['url'], :sync => true) do
before_start do
on_event('update') { |data| print data.first['data'] }
on_event('complete') do |data|
done_building = true
if data.first['success']
puts ">> Build complete"
puts " Build available from #{data.first['package_url']}"
if data.first['checksums']
puts " Checksums:"
data.first['checksums'].keys.sort.each do |algo|
puts "#{algo.upcase.rjust(12)}: #{data.first['checksums'][algo]}"
end
end
else
puts ">> Build FAILED!"
if data.first['error_message']
puts " Error: #{data.first['error_message']}"
end
end
end
end
after_start do
emit("subscribe", { 'build' => res['channel'] })
end
end
loop do
break if done_building
sleep 0.5
end
rescue Interrupt
error "Aborted by user"
rescue Errno::EPIPE
error "Could not connect to build server: #{server}"
end
private
def error(message)
puts "!! #{message}"
exit 1
end
end