Class: Gem::Compiler

Inherits:
Object
  • Object
show all
Includes:
UserInteraction
Defined in:
lib/rubygems-compile/compiler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompiler

Returns a new instance of Compiler.



12
13
14
15
16
17
18
19
20
21
# File 'lib/rubygems-compile/compiler.rb', line 12

def initialize
  require 'rbconfig'
  unless MacRuby.const_defined?(:Compiler)
    load File.join(RbConfig::CONFIG['bindir'], 'macrubyc')
  end
  require 'rubygems-compile/analyzer'

  @current_directory = []
  @config            = Gem.configuration
end

Class Method Details

.callObject



9
10
11
12
# File 'lib/rubygems-compile/compiler.rb', line 9

def compile gem
  @instance ||= Gem::Compiler.new
  @instance.compile gem
end

.compile(gem) ⇒ Object



5
6
7
8
# File 'lib/rubygems-compile/compiler.rb', line 5

def compile gem
  @instance ||= Gem::Compiler.new
  @instance.compile gem
end

Instance Method Details

#compile(gem) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rubygems-compile/compiler.rb', line 23

def compile gem
  @spec = gem.is_a?(Gem::Specification) ? gem : gem.spec

  return if trying_to_compile_self?
  say gem_compilation_message if verbose

  gem_files.each do |file|
    message   = compile_file_message(file)
    full_path = File.join(@spec.full_gem_path, file)
    if safe? full_path
      MacRuby::Compiler.compile_file full_path
    else
      message << "\t\t\tSKIPPED: #{@analyzer.warnings.uniq.join(', ')}"
    end
    say message if really_verbose
  end
end

#compile_file_message(file) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubygems-compile/compiler.rb', line 80

def compile_file_message file
  name = File.basename(file)
  dirs = file.chomp(name).split(File::SEPARATOR)
  tabs = "\t" * dirs.count

  dirs.each_with_index do |dir, index|
    unless @current_directory[index] == dir
      @current_directory[index] = dir
      say( "\t" * (index + 1) + dir + File::SEPARATOR) if really_verbose
    end
  end

  "#{tabs}#{name} => #{name}o"
end

#gem_compilation_messageObject



51
52
53
54
# File 'lib/rubygems-compile/compiler.rb', line 51

def gem_compilation_message
  slash = really_verbose ? '/' : ''
  "Compiling #{@spec.full_name}#{slash}"
end

#gem_filesObject



69
70
71
# File 'lib/rubygems-compile/compiler.rb', line 69

def gem_files
  @spec.lib_files.select { |file| File.extname(file) == '.rb' }
end

#really_verboseObject



99
100
101
# File 'lib/rubygems-compile/compiler.rb', line 99

def really_verbose
  @config.really_verbose
end

#safe?(file) ⇒ Boolean

Uses the GemAnalyzer class to determine if a given file might have any potential issues when compiled.

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/rubygems-compile/compiler.rb', line 45

def safe? file
  @analyzer = Gem::Analyzer.new File.read(file)
  @analyzer.parse
  @analyzer.warnings.empty?
end

#trying_to_compile_self?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
# File 'lib/rubygems-compile/compiler.rb', line 103

def trying_to_compile_self?
  if @spec.name == 'rubygems-compile'
    alert 'You cannot compile rubygems-compile' if really_verbose
    true
  end
end

#verboseObject



95
96
97
# File 'lib/rubygems-compile/compiler.rb', line 95

def verbose
  @config.verbose
end