Class: Gel::GemfileParser::ParseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/gel/gemfile_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(result, filename) ⇒ ParseContext

Returns a new instance of ParseContext.



18
19
20
21
22
# File 'lib/gel/gemfile_parser.rb', line 18

def initialize(result, filename)
  @result = result

  @stack = []
end

Instance Method Details

#gem(name, *requirements, **options) ⇒ Object



63
64
65
66
# File 'lib/gel/gemfile_parser.rb', line 63

def gem(name, *requirements, **options)
  options = @result.flatten(options, @stack)
  @result.add_gem(name, requirements, options)
end

#gemspec(name: nil, path: ".", development_group: :development) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gel/gemfile_parser.rb', line 68

def gemspec(name: nil, path: ".", development_group: :development)
  dirname = File.expand_path(path, File.dirname(@result.filename))
  gemspecs = Dir[File.join(dirname, "*.gemspec")]
  gemspecs.map! { |file| Gel::GemspecParser.parse(File.read(file), file) }
  gemspecs.select! { |s| s.name == name } if name
  if gemspecs.empty?
    raise "No gemspecs at #{dirname}"
  elsif gemspecs.count > 1
    raise "Multiple gemspecs at #{dirname}"
  else
    spec = gemspecs[0]
    gem spec.name, path: path
    spec.development_dependencies.each do |name, constraints|
      gem name, constraints, group: development_group
    end
  end
end

#git_source(name, &block) ⇒ Object



37
38
39
# File 'lib/gel/gemfile_parser.rb', line 37

def git_source(name, &block)
  @result.git_sources[name] = block
end

#group(*names) ⇒ Object



86
87
88
89
90
91
# File 'lib/gel/gemfile_parser.rb', line 86

def group(*names)
  @stack << { group: names }
  yield
ensure
  @stack.pop
end

#platforms(*names) ⇒ Object



93
94
95
96
97
98
# File 'lib/gel/gemfile_parser.rb', line 93

def platforms(*names)
  @stack << { platforms: names }
  yield
ensure
  @stack.pop
end

#ruby(version, engine: nil, engine_version: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gel/gemfile_parser.rb', line 41

def ruby(version, engine: nil, engine_version: nil)
  req = Gel::Support::GemRequirement.new(version)
  unless req.satisfied_by?(Gel::Support::GemVersion.new(RUBY_VERSION))
    raise Gel::Error::MismatchRubyVersionError.new(
      running: RUBY_VERSION,
      requested: version,
    )
  end
  unless !engine || RUBY_ENGINE == engine
    raise Gel::Error::MismatchRubyEngineError.new(
      running: RUBY_ENGINE,
      engine: engine,
    )
  end
  if engine_version
    raise "Cannot specify :engine_version without :engine" unless engine
    req = Gel::Support::GemRequirement.new(version)
    raise "Running ruby engine version #{RUBY_ENGINE_VERSION} does not match requested #{engine_version.inspect}" unless req.satisfied_by?(Gel::Support::GemVersion.new(RUBY_ENGINE_VERSION))
  end
  @result.ruby << [version, engine: engine, engine_version: engine_version]
end

#source(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gel/gemfile_parser.rb', line 24

def source(uri)
  if block_given?
    begin
      @stack << { source: uri }
      yield
    ensure
      @stack.pop
    end
  else
    @result.sources << uri
  end
end