Sha256: d6a4adbb7f6467bb39ce05dd0b7d9ac511ddf784ec6222efa9f60342363a4785

Contents?: true

Size: 945 Bytes

Versions: 3

Compression:

Stored size: 945 Bytes

Contents

module Natives
  class GemfileViewer
    def initialize(gemfile_path)
      @gemfile_path = gemfile_path
    end

    def packages
      begin
        # After several trial-and-error, this works:
        # 1. cd to the dir containing the gemfile
        # 2. after that run bundle show
        gemfile_dir = File.expand_path(File.dirname(@gemfile_path))
        gemfile_name = File.basename(@gemfile_path)
        output = %x{cd '#{gemfile_dir}' && BUNDLE_GEMFILE=#{gemfile_name} bundle show 2>&1}
        success = ($?.exitstatus == 0)
        unless success
          raise "Failed to list packages in Gemfile: #{@gemfile_path.inspect}\nReason:\n#{output}"
        end
      end
      extract_packages(output)
    end

    protected

    def extract_packages(output)
      lines = output.split("\n")
      lines.shift # remove first line
      lines.map do |line|
        _, name, _ = line.split(" ")
        name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
natives-0.4.1 lib/natives/gemfile_viewer.rb
natives-0.4.0 lib/natives/gemfile_viewer.rb
natives-0.3.0 lib/natives/gemfile_viewer.rb