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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/mason/cli.rb', line 31
def build(app)
app = File.expand_path(app)
raise "no such directory: #{app}" unless File.exists?(app)
type = options[:type]
output = options[:output]
type = File.extname(output)[1..-1] if !type && output
output = "#{app}.#{type}" if !output && type
type ||= "dir"
output ||= "/tmp/mason.out"
output = File.expand_path(output)
raise "no such output format: #{type}" unless %w( dir img tgz ).include?(type)
if stack = options[:stack]
if Mason::Stacks.state(stack) == :up
puts "* using stack #{stack}"
else
print "* booting stack #{stack} (this may take a while)... "
Mason::Stacks.up(stack)
puts "done"
end
buildpacks_dir = File.expand_path("~/.mason/share/#{stack}/buildpacks")
compile_dir = File.expand_path("~/.mason/share/#{stack}/app")
mason_dir = File.expand_path("~/.mason/share/#{stack}/mason")
FileUtils.rm_rf buildpacks_dir
FileUtils.rm_rf compile_dir
FileUtils.rm_rf mason_dir
FileUtils.cp_r(File.expand_path("~/.mason/buildpacks"), buildpacks_dir,
:preserve => true)
FileUtils.cp_r(File.expand_path("../../../", __FILE__), mason_dir,
:preserve => true)
FileUtils.cp_r(app, compile_dir, :preserve => true)
mason_args = %{ /share/app -q -o /share/output -t #{type} }
mason_args += %{ -b "#{options[:buildpack]}" } if options[:buildpack]
Mason::Stacks.run(stack, <<-COMMAND)
gem spec thor 2>&1 >/dev/null || sudo gem install thor
/usr/bin/env ruby -rubygems /share/mason/bin/mason build #{mason_args}
COMMAND
FileUrils.rm_rf output
FileUtils.cp_r(File.expand_path("~/.mason/share/#{stack}/output"), output,
:preserve => true)
puts "* packaging"
puts " = type: #{type}"
puts " = location: #{output}"
else
print "* detecting buildpack... "
buildpack_url = ENV["BUILDPACK_URL"] || options[:buildpack]
buildpack, ret = Mason::Buildpacks.detect(app, buildpack_url)
raise "no valid buildpack detected" unless buildpack
puts "done"
puts " = name: #{buildpack.name}"
puts " = url: #{buildpack.url}"
puts " = display: #{ret}"
puts "* compiling..."
compile_dir = buildpack.compile(app, options[:env_file], options[:cache])
print "* packaging... " unless options[:quiet]
case type.to_sym
when :tgz then
Dir.chdir(compile_dir) do
system %{ tar czf "#{output}" . }
end
when :img then
raise "img not supported yet"
when :dir then
FileUtils.rm_rf output
FileUtils.cp_r compile_dir, output, :preserve => true
else
raise "no such output type: #{type}"
end
unless options[:quiet]
puts "done"
puts " = type: #{type}"
puts " = location: #{output}"
end
end
end
|