Class: GemBench::GemfileLineTokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_bench/gemfile_line_tokenizer.rb

Overview

Line by line parser of a Gemfile Uses regular expressions, which, I know, GROSS, but also meh You aren’t using this as a runtime gem in your app are you? Let me know how you use it!

Constant Summary collapse

GEM_REGEX =

run against gem lines like: “gem ‘aftership’, # Ruby SDK of AfterShip API.”

/\A\s*gem\s+([^#]*).*\Z/.freeze
OP_QUO_REG_PROC =

HEREDOC support? (?<op_heredoc><<?[A-Z0-9_]+.?+)

lambda { |idx = nil| /((?<op_quo#{idx}>['"]{1})|(?<op_pct_q#{idx}>%[Qq]?[\(\[\{]{1})|(?<op_heredoc><<[~-]?[A-Z0-9_]+\.?[a-z0-9_]+))/x }
CL_QUO_REG_PROC =

No close for the heredoc, as it will be on a different line…

lambda { |idx = nil| /((?<cl_quo#{idx}>['"])|(?<cl_pct_q#{idx}>[\)\]\}]{1}))/x }
GEM_NAME_REGEX =

run against gem lines like: “gem ‘aftership’, # Ruby SDK of AfterShip API.”

/\A\s*gem\s+#{OP_QUO_REG_PROC.call.source}(?<name>[^'")]*)#{CL_QUO_REG_PROC.call.source}?.*\Z/.freeze
VERSION_CONSTRAINT =
/#{OP_QUO_REG_PROC.call.source}(?<version>[^'")]*)#{CL_QUO_REG_PROC.call.source}/.freeze
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC =
lambda { |key|
  /
    \A\s*[^#]*
    (
      # when key is "branch" will find: `branch: "main"`
      (?<key1>#{key}:\s*)
      #{OP_QUO_REG_PROC.call("k1").source}(?<value1>[^'")]*)?#{CL_QUO_REG_PROC.call("k1").source}
    )
    |
    (
      # when key is "branch" will find: `"branch" => "main"`
      (?<key2>#{OP_QUO_REG_PROC.call("k2a").source}#{key}#{CL_QUO_REG_PROC.call("k2a").source}\s*=>\s*)
      #{OP_QUO_REG_PROC.call("k2b").source}(?<value2>[^'")]*)?#{CL_QUO_REG_PROC.call("k2b").source}
    )
    |
    (
      # when key is "branch" will find: `:branch => "main"`
      (?<key3>:#{key}\s*=>\s*)
      #{OP_QUO_REG_PROC.call("k3").source}(?<value3>[^'")]*)?#{CL_QUO_REG_PROC.call("k3").source}
    )
    |
    (
      # when key is "branch" will find: `"branch": "main"`
      (?<key4>#{OP_QUO_REG_PROC.call("k4a").source}#{key}#{CL_QUO_REG_PROC.call("k4a").source}:\s*)
      #{OP_QUO_REG_PROC.call("k4b").source}(?<value4>[^'")]*)?#{CL_QUO_REG_PROC.call("k4b").source}
    )
  /x
}
VERSION_PATH =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("path").freeze
VERSION_GIT =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("git").freeze
VERSION_GITHUB =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("github").freeze
VERSION_GITLAB =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("gitlab").freeze
VERSION_BITBUCKET =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("bitbucket").freeze
VERSION_CODEBERG =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("codeberg").freeze
VERSION_SRCHUT =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("srchut").freeze
VERSION_GIT_REF =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("ref").freeze
VERSION_GIT_TAG =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("tag").freeze
VERSION_GIT_BRANCH =
GEMFILE_HASH_CONFIG_KEY_REGEX_PROC.call("branch").freeze
VALID_VERSION_TYPES =
%i[
  constraint
  git_ref
  git_tag
]
STR_SYNTAX_TYPES =
{
  quoted: true,
  pct_q: true,
  # We could try to support HEREDOC via parsing the lines following in all_lines, but... ugh.
  heredoc: false,
  unknown: false,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(all_lines, line, index) ⇒ GemfileLineTokenizer

Returns a new instance of GemfileLineTokenizer.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 71

def initialize(all_lines, line, index)
  @line = line.strip
  @is_gem = self.line.match(GEM_REGEX)
  if is_gem
    @all_lines = all_lines
    @index = index
    @tokens = self.line.split(",").map(&:strip)
    determine_name
    if name && STR_SYNTAX_TYPES[str_syntax_type]
      determine_relevant_lines
      determine_version
      @parse_success = true
      @valid = VALID_VERSION_TYPES.include?(version_type)
    else
      noop
    end
  else
    noop
  end
end

Instance Attribute Details

#all_linesObject (readonly)

Returns the value of attribute all_lines.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def all_lines
  @all_lines
end

#indexObject (readonly)

Returns the value of attribute index.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def index
  @index
end

#is_gemObject (readonly)

Returns the value of attribute is_gem.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def is_gem
  @is_gem
end

#lineObject (readonly)

Returns the value of attribute line.



64
65
66
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 64

def line
  @line
end

#nameObject (readonly)

Returns the value of attribute name.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def name
  @name
end

#parse_successObject (readonly)

Returns the value of attribute parse_success.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def parse_success
  @parse_success
end

#relevant_linesObject (readonly)

Returns the value of attribute relevant_lines.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def relevant_lines
  @relevant_lines
end

#str_syntax_typeObject (readonly)

version will be a string if it is a normal constraint like ‘~> 1.2.3’ version will be a hash if it is an alternative constraint like: git: “blah/blah”, ref: “shasha”



69
70
71
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 69

def str_syntax_type
  @str_syntax_type
end

#tokensObject (readonly)

Returns the value of attribute tokens.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def tokens
  @tokens
end

#validObject (readonly)

Returns the value of attribute valid.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def valid
  @valid
end

#versionObject (readonly)

version will be a string if it is a normal constraint like ‘~> 1.2.3’ version will be a hash if it is an alternative constraint like: git: “blah/blah”, ref: “shasha”



69
70
71
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 69

def version
  @version
end

#version_typeObject (readonly)

Returns the value of attribute version_type.



65
66
67
# File 'lib/gem_bench/gemfile_line_tokenizer.rb', line 65

def version_type
  @version_type
end