|
| 1 | +require "language_pack/shell_helpers" |
| 2 | + |
| 3 | +module LanguagePack::Helpers |
| 4 | + class BundleList |
| 5 | + class CmdError < BuildpackError |
| 6 | + def initialize(command:, output:) |
| 7 | + super(<<~EOF) |
| 8 | + Error detecting dependencies |
| 9 | +
|
| 10 | + The Ruby buildpack requires information about your application’s dependencies to |
| 11 | + complete the build. Without this information, the Ruby buildpack cannot continue. |
| 12 | +
|
| 13 | + Command failed: `#{command}` |
| 14 | +
|
| 15 | + #{output} |
| 16 | + EOF |
| 17 | + end |
| 18 | + end |
| 19 | + |
| 20 | + # Runs `bundle list` and builds a BundleList object |
| 21 | + # from the output |
| 22 | + # |
| 23 | + # Announces the run, optionally streams results to the user. |
| 24 | + # This is useful for the case where bundler doesn't print |
| 25 | + # any version info. |
| 26 | + # |
| 27 | + # Example: |
| 28 | + # |
| 29 | + # BundleList::Command.new( |
| 30 | + # stream_to_user: stream_to_user |
| 31 | + # ).call |
| 32 | + class HumanCommand |
| 33 | + include LanguagePack::ShellHelpers |
| 34 | + private attr_reader :stream_to_user, :io |
| 35 | + |
| 36 | + def initialize(stream_to_user:, io: self) |
| 37 | + @io = io |
| 38 | + @stream_to_user = stream_to_user |
| 39 | + end |
| 40 | + |
| 41 | + def call |
| 42 | + command = "bundle list" |
| 43 | + io.puts "Running: #{command}" |
| 44 | + |
| 45 | + output = if stream_to_user |
| 46 | + io.pipe(command, user_env: true) |
| 47 | + else |
| 48 | + io.run(command, user_env: true) |
| 49 | + end |
| 50 | + |
| 51 | + if $?.success? |
| 52 | + BundleList.new( |
| 53 | + output: output |
| 54 | + ) |
| 55 | + else |
| 56 | + raise CmdError.new(output: output, command: command) |
| 57 | + end |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + def initialize(output: ) |
| 62 | + @raw = output |
| 63 | + @gems = {} |
| 64 | + @raw.scan(/\* (?<name>\S+) \((?<version>[a-zA-Z0-9\.]+)(?<git_sha> [a-zA-Z0-9]+)?\)/) do |
| 65 | + captures = Regexp.last_match.named_captures |
| 66 | + @gems[captures["name"]] = captures["version"] |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + def has_gem?(name) |
| 71 | + @gems[name] |
| 72 | + end |
| 73 | + |
| 74 | + def length |
| 75 | + @gems.length |
| 76 | + end |
| 77 | + |
| 78 | + def gem_version(name) |
| 79 | + if version = @gems[name] |
| 80 | + Gem::Version.new(version) |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | +end |
0 commit comments