Skip to content

Commit 26c307d

Browse files
committed
Add Gemfile.lock parsing for dependency parser
Uses Bundler gem for parsing, parses only name for now, we should probably extract URL for repos.
1 parent 169ceca commit 26c307d

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
require 'bundler'
3+
require 'json'
4+
5+
module DependencyParser
6+
module Ruby
7+
class Parse
8+
def initialize(content)
9+
@content = content
10+
end
11+
12+
def call
13+
deps = Bundler::LockfileParser.new(content)
14+
parsed = deps.specs.map do |spec|
15+
{ repos: [{ name: spec.name }], language: "ruby" }
16+
end
17+
JSON.pretty_generate(parsed)
18+
end
19+
20+
private
21+
22+
attr_reader :content
23+
24+
def self.valid_gemfile?(content)
25+
Bundler::LockfileParser.new(content).specs.any?
26+
rescue
27+
false
28+
end
29+
end
30+
end
31+
end
32+
33+
content = ARGF.read
34+
puts DependencyParser::Ruby::Parse.new(content).call if DependencyParser::Ruby::Parse.valid_gemfile?(content)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
require_relative '../../lib/dependency_parser/ruby/parse'
5+
6+
class UserMailerTest < ActiveSupport::TestCase
7+
test "returns nothing on invalid Gemfile.lock" do
8+
deps = DependencyParser::Ruby::Parse.new("invalid").call
9+
assert_equal [], JSON.parse(deps)
10+
end
11+
12+
test "returns the list of gems for a small Gemfle" do
13+
gemfile_lock = <<-LOCKFILE
14+
GEM
15+
remote: https://rubygems.org/
16+
specs:
17+
actioncable (6.1.4.1)
18+
actionpack (= 6.1.4.1)
19+
activesupport (= 6.1.4.1)
20+
nio4r (~> 2.0)
21+
websocket-driver (>= 0.6.1)
22+
23+
PLATFORMS
24+
ruby
25+
26+
DEPENDENCIES
27+
actioncable
28+
29+
RUBY VERSION
30+
ruby 3.0.4p208
31+
32+
BUNDLED WITH
33+
2.3.21
34+
35+
LOCKFILE
36+
37+
deps = DependencyParser::Ruby::Parse.new(gemfile_lock).call
38+
assert_equal [{ "repos" => [{ "name" => "actioncable" }], "language" => "ruby" }], JSON.parse(deps)
39+
end
40+
end

0 commit comments

Comments
 (0)