Class: CapsuleCD::Ruby::RubyHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/capsulecd/ruby/ruby_helper.rb

Class Method Summary collapse

Class Method Details

.execute_in_childObject

since the Gem::Specification class is basically eval’ing the gemspec file, and the gemspec file is doing a require to load the version.rb file, the version.rb file is cached in memory. We’re going to try to get around that issue by parsing the gemspec file in a forked process. stackoverflow.com/questions/1076257/returning-data-from-forked-processes



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 38

def self.execute_in_child
  read, write = IO.pipe

  pid = fork do
    read.close
    result = yield
    Marshal.dump(result, write)
    exit!(0) # skips exit handlers.
  end

  write.close
  result = read.read
  Process.wait(pid)
  raise "child failed" if result.empty?
  Marshal.load(result)
end

.get_gemspec_data(repo_path) ⇒ Object



27
28
29
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 27

def self.get_gemspec_data(repo_path)
  self.load_gemspec_data(self.get_gemspec_path(repo_path))
end

.get_gemspec_path(repo_path) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 19

def self.get_gemspec_path(repo_path)
  gemspecs = Dir.glob(repo_path + '/*.gemspec')
  if gemspecs.empty?
    fail CapsuleCD::Error::BuildPackageInvalid, '*.gemspec file is required to process Ruby gem'
  end
  gemspecs.first
end

.load_gemspec_data(gemspec_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 55

def self.load_gemspec_data(gemspec_path)
  gemspec_data = nil
  Bundler.with_clean_env do
    gemspec_data = self.execute_in_child do
      Gem::Specification::load(gemspec_path)
    end
  end
  if !gemspec_data
    fail CapsuleCD::Error::BuildPackageInvalid, '*.gemspec could not be parsed.'
  end
  return gemspec_data
end

.read_version_file(repo_path, gem_name, version_filename = 'version.rb') ⇒ Object



11
12
13
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 11

def self.read_version_file(repo_path, gem_name, version_filename = 'version.rb')
  File.read(self.version_filepath(repo_path, gem_name, version_filename))
end

.version_filepath(repo_path, gem_name, version_filename = 'version.rb') ⇒ Object



7
8
9
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 7

def self.version_filepath(repo_path, gem_name, version_filename = 'version.rb')
  "#{repo_path}/lib/#{gem_name}/#{version_filename}"
end

.write_version_file(repo_path, gem_name, metadata_str, version_filename = 'version.rb') ⇒ Object



15
16
17
# File 'lib/capsulecd/ruby/ruby_helper.rb', line 15

def self.write_version_file(repo_path, gem_name, , version_filename = 'version.rb')
  File.open(self.version_filepath(repo_path, gem_name, version_filename), 'w') { |file| file.write() }
end