Class: Bundler::GemBytes::Actions::Gemspec::GemSpecification Private

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds information about the gemspec file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, source_path) ⇒ GemSpecification

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new GemSpecification object

Parameters:

  • source (String)

    The contents of the gemspec file

  • source_path (String)

    The path to the gemspec file



17
18
19
20
21
22
23
24
25
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 17

def initialize(source, source_path)
  @source = source
  @source_path = source_path

  @dependencies = []
  @attributes = []

  @source_buffer, @source_ast = parse(source, source_path)
end

Instance Attribute Details

#attributesArray<Attribute> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The attributes found in the gemspec file

Returns:



74
75
76
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 74

def attributes
  @attributes
end

#dependenciesArray<Dependency> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The dependencies found in the gemspec file

Returns:



70
71
72
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 70

def dependencies
  @dependencies
end

#gemspec_astParser::AST::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The AST node for the Gem::Specification block within the source

Returns:

  • (Parser::AST::Node)


66
67
68
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 66

def gemspec_ast
  @gemspec_ast
end

#gemspec_objectGem::Specification (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The Gem::Specification object

Examples:

data.gemspec_object # => #<Gem::Specification:0x00007f9b1b8b3f10>

Returns:

  • (Gem::Specification)


10
11
12
13
14
15
16
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
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
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
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 10

class GemSpecification
  # Create a new GemSpecification object
  #
  # @param source [String] The contents of the gemspec file
  # @param source_path [String] The path to the gemspec file
  # @api private
  #
  def initialize(source, source_path)
    @source = source
    @source_path = source_path

    @dependencies = []
    @attributes = []

    @source_buffer, @source_ast = parse(source, source_path)
  end

  # The contents of the gemspec file
  # @example
  #   data.source # => "Gem::Specification.new do |spec|\n  spec.name = 'example'\nend"
  # @return [String]
  attr_reader :source

  # The path to the gemspec file (used for error reporting)
  # @example
  #   data.source_path # => "example.gemspec"
  # @return [String]
  attr_reader :soure_path

  # The source buffer used when updating the gemspec file
  # @return [Parser::Source::Buffer]
  # @api private
  attr_reader :source_buffer

  # The parsed AST for the gemspec file source
  # @example
  #   data.source_ast # => (send nil :puts)
  # @return [Parser::AST::Node]
  attr_reader :source_ast

  # The Gem::Specification object
  # @example
  #   data.gemspec_object # => #<Gem::Specification:0x00007f9b1b8b3f10>
  # @return [Gem::Specification]
  def gemspec_object
    @gemspec_object ||= load_gem_specification
  end

  # The name of the Gem::Specification object within the gemspec block
  # @example When the gemspec block starts `Gem::Specification.new do |spec|`
  #   data.gemspec_object_name # => :spec
  # @return [Symbol]
  attr_accessor :gemspec_object_name

  # The AST node for the Gem::Specification block within the source
  # @return [Parser::AST::Node]
  attr_accessor :gemspec_ast

  # The dependencies found in the gemspec file
  # @return [Array<Dependency>]
  attr_reader :dependencies

  # The attributes found in the gemspec file
  # @return [Array<Attribute>]
  attr_reader :attributes

  private

  # Parses the given code into an AST
  # @param source [String] The code to parse
  # @param source_path [String] The path to the file being parsed (used for error messages only)
  # @return [Array<Parser::AST::Node, Parser::Source::Buffer>] The AST and buffer
  # @api private
  def parse(source, source_path)
    source_buffer = Parser::Source::Buffer.new(source_path, source: source)
    processed_source = RuboCop::AST::ProcessedSource.new(source, ruby_version, source_path)
    unless processed_source.valid_syntax?
      raise "Invalid syntax in #{source_path}\n#{processed_source.diagnostics.map(&:render).join("\n")}"
    end

    source_ast = processed_source.ast
    [source_buffer, source_ast]
  end

  # The currently running Ruby version as a float (MAJOR.MINOR only)
  #
  # @return [Float] The Ruby version number, e.g., 3.0
  # @api private
  def ruby_version = RUBY_VERSION.match(/^(?<version>\d+\.\d+)/)['version'].to_f

  # Load the gemspec file into a Gem::Specification object
  # @return [Gem::Specification] The Gem::Specification object
  # @api private
  def load_gem_specification
    # Store the current $LOAD_PATH
    original_load_path = $LOAD_PATH.dup

    # Temporarily add 'lib' to the $LOAD_PATH
    lib_path = File.expand_path('lib', Dir.pwd)
    $LOAD_PATH.unshift(lib_path)

    # Evaluate the gemspec file
    eval(source, binding, '.').tap do # rubocop:disable Security/Eval
      # Restore the original $LOAD_PATH
      $LOAD_PATH.replace(original_load_path)
    end
  end
end

#gemspec_object_nameSymbol

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of the Gem::Specification object within the gemspec block

Examples:

When the gemspec block starts ‘Gem::Specification.new do |spec|`

data.gemspec_object_name # => :spec

Returns:

  • (Symbol)


62
63
64
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 62

def gemspec_object_name
  @gemspec_object_name
end

#sourceString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The contents of the gemspec file

Examples:

data.source # => "Gem::Specification.new do |spec|\n  spec.name = 'example'\nend"

Returns:

  • (String)


31
32
33
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 31

def source
  @source
end

#source_astParser::AST::Node (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parsed AST for the gemspec file source

Examples:

data.source_ast # => (send nil :puts)

Returns:

  • (Parser::AST::Node)


48
49
50
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 48

def source_ast
  @source_ast
end

#source_bufferParser::Source::Buffer (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The source buffer used when updating the gemspec file

Returns:

  • (Parser::Source::Buffer)


42
43
44
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 42

def source_buffer
  @source_buffer
end

#soure_pathString (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The path to the gemspec file (used for error reporting)

Examples:

data.source_path # => "example.gemspec"

Returns:

  • (String)


37
38
39
# File 'lib/bundler/gem_bytes/actions/gemspec/gem_specification.rb', line 37

def soure_path
  @soure_path
end