Module: MrubyApp::CLI

Defined in:
lib/mruby_app/cli.rb

Class Method Summary collapse

Class Method Details

.startObject



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
# File 'lib/mruby_app/cli.rb', line 3

def self.start
  has_mundler = File.file?(File.join(Dir.pwd, "Mundlefile"))

  mrbc = "mrbc"
  mrbc = "mundle exec mrbc" if has_mundler

  mruby = "mruby"
  mruby = "mundle exec mruby" if has_mundler

  file = Tempfile.new('foo')
  output = file.path

  app_files = Dir.glob(File.join("app", "**", "*.rb"))
  additional = Dir.glob(File.join(__dir__, "mruby", "*.rb"))
  additional.map! { |f| File.expand_path(f) }

  compile = (%W(#{mrbc} -g -o#{output}) + app_files + additional).join(" ")
  chmod = "chmod +x #{output}"
  execute = "#{mruby} -d #{output}"

  # TODO: build
  # build = (%W(#{mrbc} -Bapp -g -o./build.c) + app_files + additional).join(" ")
  # system(build)

  system(compile) || raise("Failed to compiled")
  system(chmod) || raise("Failed to chmod")
  begin
    system(execute)
  rescue Interrupt
  end
ensure
  if file
    file.close
    file.unlink
  end
end