Skip to content

Commit c90bbaa

Browse files
committed
Add option to restrict gems from Gemfile (deps only)
1 parent 9f14dbf commit c90bbaa

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/yard/cli/server.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,26 @@ def add_gems
133133
end
134134
end
135135

136-
def add_gems_from_gemfile(gemfile = nil)
136+
def add_gems_from_gemfile(gemfile = nil, dependencies_only = false)
137137
require 'bundler'
138138
gemfile ||= "Gemfile"
139139
if File.exist?("#{gemfile}.lock")
140-
Bundler::LockfileParser.new(File.read("#{gemfile}.lock")).specs.each do |spec|
141-
libraries[spec.name] ||= []
142-
libraries[spec.name] |= [YARD::Server::LibraryVersion.new(spec.name, spec.version.to_s, nil, :gem)]
140+
if dependencies_only
141+
parsed_gemfile = Bundler::LockfileParser.new(File.read("#{gemfile}.lock"))
142+
parsed_gemfile.dependencies.each do |dep|
143+
libraries[dep.name] ||= []
144+
specs = parsed_gemfile.specs.select do |spec|
145+
spec.name == dep.name
146+
end
147+
specs.each do |spec|
148+
libraries[spec.name] |= [YARD::Server::LibraryVersion.new(spec.name, spec.version.to_s, nil, :gem)]
149+
end
150+
end
151+
else
152+
Bundler::LockfileParser.new(File.read("#{gemfile}.lock")).specs.each do |spec|
153+
libraries[spec.name] ||= []
154+
libraries[spec.name] |= [YARD::Server::LibraryVersion.new(spec.name, spec.version.to_s, nil, :gem)]
155+
end
143156
end
144157
else
145158
log.warn "Cannot find #{gemfile}.lock, ignoring --gemfile option"
@@ -174,6 +187,9 @@ def optparse(*args)
174187
opts.on('-G', '--gemfile [GEMFILE]', 'Serves documentation for gems from Gemfile') do |gemfile|
175188
add_gems_from_gemfile(gemfile)
176189
end
190+
opts.on('--gemfile-deps [GEMFILE]', 'Serves documentation for gems from Gemfile (Just dependencies)') do |gemfile|
191+
add_gems_from_gemfile(gemfile, true)
192+
end
177193
opts.on('-t', '--template-path PATH',
178194
'The template path to look for templates in. (used with -t).') do |path|
179195
template_paths << path

spec/cli/server_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,28 @@ def mock_file(filename, content = nil)
280280
run '--gemfile', 'different_name'
281281
end
282282

283+
it "accepts --gemfile-deps" do
284+
bundler_required
285+
@no_verify_libraries = true
286+
@options[:single_library] = false
287+
288+
@libraries['gem1'] = [Server::LibraryVersion.new('gem1', '1.0.0', nil, :gem)]
289+
@libraries['gem2'] = [Server::LibraryVersion.new('gem2', '1.0.0', nil, :gem)]
290+
gem1 = double(:gem1, :name => 'gem1', :version => '1.0.0', :full_gem_path => '/path/to/foo')
291+
gem2 = double(:gem2, :name => 'gem2', :version => '1.0.0', :full_gem_path => '/path/to/bar')
292+
gem3 = double(:gem3, :name => 'gem3', :version => '1.0.0', :full_gem_path => '/path/to/bar')
293+
lockfile_parser = double(:new, :specs => [gem1, gem2, gem3], :dependencies => [gem1, gem2])
294+
allow(Bundler::LockfileParser).to receive(:new).and_return(lockfile_parser)
295+
296+
expect(File).to receive(:exist?).at_least(1).times.with("Gemfile.lock").and_return(true)
297+
allow(File).to receive(:read)
298+
299+
run '--gemfile-deps'
300+
301+
expect(File).to receive(:exist?).with("different_name.lock").and_return(true)
302+
run '--gemfile-deps', 'different_name'
303+
end
304+
283305
it "warns if lockfile is not found (with -G)" do
284306
bundler_required
285307
expect(File).to receive(:exist?).with(/\.yardopts$/).at_least(:once).and_return(false)

0 commit comments

Comments
 (0)