Class: Mason::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/mason/cli.rb

Defined Under Namespace

Classes: Buildpacks, Stacks

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject

hack thor



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/mason/cli.rb', line 235

def self.run
  args   = ARGV.dup
  parts  = args.first.to_s.split(":")
  method = parts.pop
  ns     = parts.pop

  args[0] = method

  klass = case ns
    when "buildpacks" then Buildpacks
    when "stacks"     then Stacks
    else self
  end

  unless (args & %w( -h --help )).empty?
    klass.task_help(Thor::Shell::Basic.new, args.first)
    return
  end

  klass.start(args)
rescue StandardError => ex
  $stderr.puts "  ! #{ex}"
  $stderr.puts "  " + ex.backtrace.join("\n  ") if ARGV.include?("--debug")
  exit 1
end

Instance Method Details

#build(app) ⇒ Object



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
125
126
127
128
129
# 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

    FileUtils.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, release = buildpack.compile(app, 
                              options[:env_file], options[:cache])

    File.open("#{options[:cache]}/release.yml", "w") do |f|
      YAML.dump(release, f)
    end

    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

#buildpacksObject



139
140
141
142
143
144
145
146
147
148
# File 'lib/mason/cli.rb', line 139

def buildpacks
  buildpacks = Mason::Buildpacks.buildpacks

  puts "* buildpacks (#{Mason::Buildpacks.root(false)})"
  buildpacks.sort.each do |buildpack|
    puts "  = #{buildpack.name}: #{buildpack.url}"
  end

  puts "  - no buildpacks installed, use buildpacks:install" if buildpacks.length.zero?
end

#stacksObject



170
171
172
173
174
175
176
177
178
179
# File 'lib/mason/cli.rb', line 170

def stacks
  stacks = Mason::Stacks.stacks

  puts "* available stacks"
  stacks.keys.each do |name|
    puts "  - #{name} [#{Mason::Stacks.state(name)}]"
  end

  puts "  - no stacks created, use stacks:create" if stacks.length.zero?
end

#vagrant(*args) ⇒ Object



133
134
135
# File 'lib/mason/cli.rb', line 133

def vagrant(*args)
  Mason::Stacks.vagrant(args)
end

#versionObject



17
18
19
# File 'lib/mason/cli.rb', line 17

def version
  puts "mason v#{Mason::VERSION}"
end