|
| 1 | +module LanguagePack |
| 2 | + module Helpers |
| 3 | + # Centralize logic for extracting information from the `Gemfile.lock` format |
| 4 | + # |
| 5 | + # - Extracts Ruby version from `RUBY VERSION` |
| 6 | + # - Extracts Bundler version from `BUNDLED WITH` |
| 7 | + # |
| 8 | + # Example: |
| 9 | + # |
| 10 | + # gemfile_lock = GemfileLock.new(contents: <<~EOF) |
| 11 | + # RUBY VERSION |
| 12 | + # ruby 3.3.5p100 |
| 13 | + # BUNDLED WITH |
| 14 | + # 2.3.4 |
| 15 | + # EOF |
| 16 | + # |
| 17 | + # expect(gemfile_lock.bundler.version).to eq("2.3.4") |
| 18 | + # expect(gemfile_lock.ruby.ruby_version).to eq("3.3.5") |
| 19 | + class GemfileLock |
| 20 | + attr_reader :ruby, :bundler |
| 21 | + |
| 22 | + def initialize(contents: , report: HerokuBuildReport::GLOBAL) |
| 23 | + @ruby = RubyVersionParse.new(contents: contents, report: report) |
| 24 | + @bundler = BundlerVersionParse.new(contents: contents, report: report) |
| 25 | + end |
| 26 | + |
| 27 | + # Holds information about the RUBY VERSION of the parsed Gemfile.lock |
| 28 | + class RubyVersionParse |
| 29 | + # Ruby version from Gemfile.lock i.e. `3.3.8` |
| 30 | + # Either 3 numbers or nil |
| 31 | + attr_reader :ruby_version, |
| 32 | + # Contains pre-release info |
| 33 | + # - String: i.e. "rc2" is a prerelease |
| 34 | + # - nil: No pre-release (or no version at all) |
| 35 | + :pre, |
| 36 | + # Either :ruby or :jruby |
| 37 | + :engine, |
| 38 | + # `engine_version` is the JRuby version or for Ruby, it is the same as `ruby_version` |
| 39 | + # i.e. `<major>.<minor>.<patch>` |
| 40 | + :engine_version |
| 41 | + |
| 42 | + def initialize(contents: , report: HerokuBuildReport::GLOBAL) |
| 43 | + if match = contents.match(/^RUBY VERSION(\r?\n) ruby (?<version>\d+\.\d+\.\d+)((\-|\.)(?<pre>\S*))?/m) |
| 44 | + @pre = match[:pre] |
| 45 | + @empty = false |
| 46 | + @ruby_version = match[:version] |
| 47 | + else |
| 48 | + if contents.match?(/RUBY VERSION/) |
| 49 | + report.capture("gemfile_lock.ruby_version.failed_parse" => true) |
| 50 | + if match = contents.match(/(?<contents>RUBY VERSION(\r?\n).*)$/) |
| 51 | + report.capture("gemfile_lock.ruby_version.failed_contents" => match[:contents]) |
| 52 | + end |
| 53 | + end |
| 54 | + @pre = nil |
| 55 | + @empty = true |
| 56 | + @ruby_version = nil |
| 57 | + end |
| 58 | + |
| 59 | + if jruby = contents.to_s.match(/^RUBY VERSION(\r?\n) ruby [^\(]*\(jruby (?<version>(\d+|\.)+)\)/m) |
| 60 | + @engine = :jruby |
| 61 | + @engine_version = jruby[:version] |
| 62 | + else |
| 63 | + @engine = :ruby |
| 64 | + @engine_version = ruby_version |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def empty? |
| 69 | + @empty |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + class BundlerVersionParse |
| 74 | + # Bundler value from `Gemfile.lock` (String or nil) i.e. `2.5.23` |
| 75 | + attr_reader :version |
| 76 | + |
| 77 | + def initialize(contents: , report: HerokuBuildReport::GLOBAL) |
| 78 | + if match = contents.match(/^BUNDLED WITH(\r?\n) (?<version>(?<major>\d+)\.(?<minor>\d+)\.\d+)/m) |
| 79 | + @empty = false |
| 80 | + @version = match[:version] |
| 81 | + else |
| 82 | + if contents.match?(/BUNDLED WITH/) |
| 83 | + report.capture("gemfile_lock.bundler_version.failed_parse" => true) |
| 84 | + if match = contents.match(/(?<contents>BUNDLED WITH(\r?\n).*)$/) |
| 85 | + report.capture("gemfile_lock.bundler_version.failed_contents" => match[:contents]) |
| 86 | + end |
| 87 | + end |
| 88 | + @empty = true |
| 89 | + @version = nil |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def empty? |
| 94 | + @empty |
| 95 | + end |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments