Class: Mundler::MRuby

Inherits:
Object
  • Object
show all
Defined in:
lib/mundler/mruby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ MRuby

Returns a new instance of MRuby.



3
4
5
6
7
8
9
# File 'lib/mundler/mruby.rb', line 3

def initialize(config)
  @config = config
  @path = File.join(ENV["HOME"], ".mundler", config.hex)

  # Protect just incase, as we're doing an rm_rf
  raise "Something went wrong" if config.hex.length == 0
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/mundler/mruby.rb', line 11

def path
  @path
end

Instance Method Details

#clone_repositoryObject



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
# File 'lib/mundler/mruby.rb', line 17

def clone_repository
  success_indicator = File.join(@path, ".mundler_cloned_successfully")
  return if File.file?(success_indicator)

  mruby_url = @config.mruby[:url]

  version = (
    @config.mruby[:tag] ||
    @config.mruby[:branch] ||
    @config.mruby[:version]
  )

  FileUtils.rm_rf(@path)
  FileUtils.mkdir_p(@path)
  FileUtils.cd(@path)

  Mundler.with_clean_env do
    system(
      {
        # The {mundler gem path}/cached_git directory contains a binary called
        # `git` that will run instead of normal git, making a cache of a
        # clone.
        "PATH" => ([cached_git_dir] + ENV["PATH"].split(":")).join(":")
      },
      "git clone #{mruby_url} . >/dev/null 2>&1"
    ) || raise(Mundler::CloneError, "Failed to clone #{mruby_url}")
  end

  if version
    system("git reset --hard #{version} >/dev/null 2>&1") ||
      system("git reset --hard origin/#{version} >/dev/null 2>&1") ||
      exit_out("Failed to set mruby version to \"#{version}\".")
  end

  FileUtils.touch(success_indicator)
end

#compile(build_config) ⇒ Object



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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mundler/mruby.rb', line 64

def compile(build_config)
  logfile = Tempfile.new(['mundler_build', '.log'])

  version = (
    @config.mruby[:tag] ||
    @config.mruby[:branch] ||
    @config.mruby[:version]
  )

  @config.gemboxes.each do |gembox|
    puts "Using #{gembox} gembox"
  end
  @config.gems.each do |gem|
    if gem[:platforms].empty?
      puts "Using #{gem[:name]} gem"
    else
      puts "Using #{gem[:name]} gem for platforms: #{gem[:platforms]}"
    end
  end

  puts "Using mruby (#{version})"


  success_indicator = File.join(@path, ".mundler_built_successfully")
  if File.file?(success_indicator)
    return
  end

  FileUtils.cd(@path)

  cleaned = false
  covered = []
  output_thread = Thread.new do
    loop do
      if cleaned
        Dir.glob(File.join(Dir.pwd, "build", "*", "*", "*")).each do |file|
          pathname = Pathname.new(file)
          directory = pathname.directory? ? pathname.to_s : Pathname.new(file).dirname.to_s
          next if covered.include?(directory)
          covered << directory
          print "\e[34m.\e[0m"
        end
      end
      sleep(0.3)
    end
  end

  Mundler.with_clean_env do
    rake = `which rake`.chomp
    system(
      {
        "MRUBY_CONFIG" => build_config,
        # The {mundler gem path}/cached_git directory contains a binary called
        # `git` that will run instead of normal git, making a cache of a
        # clone.
        "PATH" => ([cached_git_dir] + ENV["PATH"].split(":")).join(":")
      },
      "#{rake} clean >#{logfile.path} 2>&1 && #{rake} deep_clean >#{logfile.path} 2>&1"
    ) || begin
      $stderr.print "\e[31mF\e[0m"
      $stderr.puts "\n\n"
      $stderr.puts File.read(build_config)
      $stderr.puts File.read(logfile)

      raise Mundler::CompilationError
    end

    cleaned = true

    rake = `which rake`.chomp
    system(
      {
        "MRUBY_CONFIG" => build_config,
        # The {mundler gem path}/cached_git directory contains a binary called
        # `git` that will run instead of normal git, making a cache of a
        # clone.
        "PATH" => ([cached_git_dir] + ENV["PATH"].split(":")).join(":")
      },
      "#{rake} >#{logfile.path} 2>&1"
    ) || begin
      $stderr.print "\e[31mF\e[0m"
      $stderr.puts "\n\n"
      $stderr.puts File.read(build_config)
      $stderr.puts File.read(logfile)

      raise Mundler::CompilationError
    end
  end

  puts "\n\n"

  FileUtils.touch(success_indicator)
ensure
  output_thread.kill if output_thread
  logfile.close
  logfile.delete
end

#delete_repositoryObject



13
14
15
# File 'lib/mundler/mruby.rb', line 13

def delete_repository
  FileUtils.rm_rf(@path)
end

#exec(args) ⇒ Object

Raises:



54
55
56
57
58
59
60
61
62
# File 'lib/mundler/mruby.rb', line 54

def exec(args)
  raise NotInstalledError unless installed?

  bin_dir = File.join(@path, "bin")
  path = bin_dir + ":" + ENV['PATH']
  Process.spawn({ "PATH" => path }, *args)
  Process.wait
  exit($?.exitstatus) if $?.exitstatus
end


162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mundler/mruby.rb', line 162

def print_summary
  FileUtils.cd(@path)
  puts "Libraries:"
  Dir.glob(File.join(Dir.pwd, "**", "*.a")).each { |a| puts "* " + a.split("build/").last }

  binaries = Dir.glob(File.join(Dir.pwd, "build", "host", "bin", "*"))
  if binaries.count > 0
    puts ""
    puts "Binaries:"
    binaries.each { |a| puts "* " + a.split("/").last }
  end
end