Class: Gem::Compiler

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

Defined Under Namespace

Classes: CompilerError

Constant Summary collapse

VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gemfile, _options = {}) ⇒ Compiler

Returns a new instance of Compiler.



20
21
22
23
24
# File 'lib/rubygems/compiler.rb', line 20

def initialize(gemfile, _options = {})
  @gemfile    = gemfile
  @output_dir = _options.delete(:output)
  @options    = _options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/rubygems/compiler.rb', line 18

def options
  @options
end

#target_dirObject (readonly)

Returns the value of attribute target_dir.



18
19
20
# File 'lib/rubygems/compiler.rb', line 18

def target_dir
  @target_dir
end

Instance Method Details

#compileObject



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
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
# File 'lib/rubygems/compiler.rb', line 26

def compile
  unpack

  # build extensions
  installer.build_extensions

  # determine build artifacts from require_paths
  dlext    = RbConfig::CONFIG["DLEXT"]
  lib_dirs = installer.spec.require_paths.join(",")

  artifacts = Dir.glob("#{target_dir}/{#{lib_dirs}}/**/*.#{dlext}")

  # build a new gemspec from the original one
  gemspec = installer.spec.dup

  # add discovered artifacts
  artifacts.each do |path|
    # path needs to be relative to target_dir
    file = path.gsub("#{target_dir}/", "")

    debug "Adding '#{file}' to gemspec"
    gemspec.files.push file
  end

  # clear out extensions from gemspec
  gemspec.extensions.clear

  # adjust platform
  gemspec.platform = Gem::Platform::CURRENT

  # build new gem
  output_gem = nil

  Dir.chdir target_dir do
    output_gem = if defined?(Gem::Builder)
      Gem::Builder.new(gemspec).build
    else
      Gem::Package.build(gemspec)
    end
  end

  unless output_gem
    raise CompilerError,
          "There was a problem building the gem."
  end

  # move the built gem to the original output directory
  FileUtils.mv File.join(target_dir, output_gem), @output_dir

  cleanup

  # return the path of the gem
  output_gem
end