Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Server CLI now reads .yardopts files, and works with alternate yardoc…

… db paths.
  • Loading branch information...
commit 679ecb9a3bd3828318b0740b7e7c67c271ae566e 1 parent 9e5a174
@japgolly japgolly authored
Showing with 127 additions and 26 deletions.
  1. +57 −10 lib/yard/cli/server.rb
  2. +70 −16 spec/cli/server_spec.rb
View
67 lib/yard/cli/server.rb
@@ -1,3 +1,5 @@
+require_relative 'yardoc'
+
module YARD
module CLI
# A local documentation server
@@ -71,18 +73,56 @@ def select_adapter
def add_libraries(args)
(0...args.size).step(2) do |index|
- library, yardoc = args[index], args[index + 1]
- yardoc ||= '.yardoc'
- yardoc = File.expand_path(yardoc)
- if File.exist?(yardoc)
+ library, dir = args[index], args[index + 1]
+
+ libver = nil
+ if dir
+ # Provided dir contains a .yardopts file
+ libver = create_library_version_if_yardopts_exist(library, dir)
+
+ # Use provided dir directly
+ #libver ||= File.exist?(dir) && YARD::Server::LibraryVersion.new(library, nil, dir)
+ libver ||= (
+ raise "Yardoc db not found at #{dir}" unless File.exist?(dir)
+ YARD::Server::LibraryVersion.new(library, nil, dir)
+ )
+ else
+ # Check if this dir contains a .yardopts file
+ libver = create_library_version_if_yardopts_exist(library, '.')
+
+ # Check default location
+ libver ||= File.exist?('.yardoc') && YARD::Server::LibraryVersion.new(library, nil, '.yardoc')
+ end
+
+ # Register library
+ if libver
+ libver.yardoc_file = File.expand_path(libver.yardoc_file) if libver.yardoc_file
+ libver.source_path = File.expand_path(libver.source_path) if libver.source_path
libraries[library] ||= []
- libraries[library] |= [YARD::Server::LibraryVersion.new(library, nil, yardoc)]
+ libraries[library] |= [libver]
else
- log.warn "Cannot find yardoc db for #{library}: #{yardoc}"
+ log.warn "Cannot find yardoc db for #{library}: #{dir}"
end
end
end
+ # @param [String] library The library name.
+ # @param [nil, String] dir The argument provided on the CLI after the library name. Is supposed to point to either
+ # a project directory with a Yard options file, or a yardoc db.
+ # @return [nil, LibraryVersion]
+ def create_library_version_if_yardopts_exist(library, dir)
+ if dir and File.exists? File.join(dir, Yardoc::DEFAULT_YARDOPTS_FILE)
+
+ # Found yardopts, create libver
+ YARD::Registry.yardoc_file = nil
+ CLI::Yardoc.new.parse_arguments
+ libver = YARD::Server::LibraryVersion.new(library, nil, YARD::Registry.yardoc_file)
+ libver.source_path = dir
+
+ libver
+ end
+ end
+
def add_gems
require 'rubygems'
Gem.source_index.find_name('').each do |spec|
@@ -166,13 +206,20 @@ def optparse(*args)
parse_options(opts, args)
if args.empty? && libraries.empty?
- if !File.exist? File.expand_path('.yardoc')
+ # No args - try to use current dir
+ add_libraries([File.basename(Dir.pwd), nil])
+
+ # Generate doc for first time
+ libver = libraries.empty? ? nil : libraries.values.first.first
+ if libver and !File.exist?(libver.yardoc_file)
log.enter_level(Logger::INFO) do
- log.info "No .yardoc file found in current directory, parsing source before starting server..."
+ yardoc_file = libver.yardoc_file.sub /^#{Regexp.quote Dir.pwd}[\\\/]+/, ''
+ log.info "No yardoc db found in #{yardoc_file}, parsing source before starting server..."
+ end
+ Dir.chdir(libver.source_path) do
+ Yardoc.run('-n')
end
- Yardoc.run('-n')
end
- add_libraries([File.basename(Dir.pwd), File.expand_path('.yardoc')])
else
add_libraries(args)
options[:single_library] = false if libraries.size > 1
View
86 spec/cli/server_spec.rb
@@ -1,10 +1,10 @@
require File.dirname(__FILE__) + '/../spec_helper'
+require 'tmpdir'
class Server::WebrickAdapter; def start; end end
describe YARD::CLI::Server do
before do
- @no_verify_libraries = false
@no_adapter_mock = false
@libraries = {}
@options = {:single_library => true, :caching => false}
@@ -31,34 +31,87 @@ def run(*args)
library = Server::LibraryVersion.new(File.basename(Dir.pwd), nil, File.expand_path('.yardoc'))
@libraries = {library.name => [library]}
end
- unless @no_verify_libraries
- @libraries.values.each {|libs| libs.each {|lib| File.should_receive(:exist?).at_least(1).times.with(File.expand_path(lib.yardoc_file)).and_return(true) } }
- end
unless @no_adapter_mock
@cli.stub!(:adapter).and_return(@adapter)
@adapter.should_receive(:new).with(@libraries, @options, @server_options).and_return(@adapter)
@adapter.should_receive(:start)
end
+
@cli.run(*args.flatten)
+ assert_libraries @libraries, @cli.libraries
+
@cli = YARD::CLI::Server.new
end
- it "should default to current dir if no library is specified" do
- Dir.should_receive(:pwd).and_return('/path/to/foo')
- @libraries['foo'] = [Server::LibraryVersion.new('foo', nil, File.expand_path('.yardoc'))]
- run
+ def assert_libraries(expected_libs, actual_libs)
+ actual_libs.should == expected_libs
+ expected_libs.each { |name, libs|
+ libs.each_with_index { |expected,i|
+ actual = actual_libs[name][i]
+ [:source, :source_path, :yardoc_file].each {|m|
+ actual.send(m).should == expected.send(m)
+ }
+ }
+ }
end
- it "should use .yardoc as yardoc file is library list is odd" do
- @libraries['a'] = [Server::LibraryVersion.new('a', nil, File.expand_path('.yardoc'))]
- run 'a'
+ context '.yardopts file exists' do
+ around :each do |ex|
+ Dir.mktmpdir {|dir|
+ Dir.chdir(dir) {
+ Dir.mkdir 'blah'
+ Dir.mkdir 'hehe'
+ @name= File.basename(Dir.pwd)
+ ex.call
+ }
+ }
+ end
+
+ it "should use .yardoc as the yardoc db if .yardopts doesn't specify an alternate path" do
+ File.write '.yardopts', '--protected'
+ @libraries[@name] = [Server::LibraryVersion.new(@name, nil, File.expand_path('.yardoc'))]
+ @libraries.values[0][0].source_path = Dir.pwd
+ run
+ end
+
+ it "should use the yardoc db location specified by .yardopts" do
+ File.write '.yardopts', '--db hehe'
+ @libraries[@name] = [Server::LibraryVersion.new(@name, nil, File.expand_path('hehe'))]
+ @libraries.values[0][0].source_path = Dir.pwd
+ run
+ end
+
+ it "should parse .yardopts when the library list is odd" do
+ File.write '.yardopts', '--db hehe'
+ @libraries['a'] = [Server::LibraryVersion.new('a', nil, File.expand_path('hehe'))]
+ @libraries.values[0][0].source_path = Dir.pwd
+ run 'a'
+ end
end
- it "should force multi library if more than one library is listed" do
- @options[:single_library] = false
- @libraries['a'] = [Server::LibraryVersion.new('a', nil, File.expand_path('b'))]
- @libraries['c'] = [Server::LibraryVersion.new('c', nil, File.expand_path('.yardoc'))]
- run %w(a b c)
+ context ".yardots file doesn't exist" do
+ before :each do
+ File.should_receive(:exists?).at_least(:once).with(/^(.[\\\/])?\.yardopts$/).and_return(false)
+ end
+
+ it "should default to .yardoc if no library is specified" do
+ Dir.should_receive(:pwd).and_return('/path/to/foo')
+ @libraries['foo'] = [Server::LibraryVersion.new('foo', nil, File.expand_path('.yardoc'))]
+ run
+ end
+
+ it "should use .yardoc as yardoc file if library list is odd" do
+ @libraries['a'] = [Server::LibraryVersion.new('a', nil, File.expand_path('.yardoc'))]
+ run 'a'
+ end
+
+ it "should force multi library if more than one library is listed" do
+ File.should_receive(:exist?).at_least(:once).with(/^(b|\.yardoc)$/).at_least(:twice).and_return(true)
+ @options[:single_library] = false
+ @libraries['a'] = [Server::LibraryVersion.new('a', nil, File.expand_path('b'))]
+ @libraries['c'] = [Server::LibraryVersion.new('c', nil, File.expand_path('.yardoc'))]
+ run %w(a b c)
+ end
end
it "should accept -m, --multi-library" do
@@ -183,6 +236,7 @@ def run(*args)
it "should warn if lockfile is not found (with -G)" do
bundler_required
+ File.should_receive(:exists?).with(/\.yardopts$/).at_least(:once).and_return(false)
File.should_receive(:exists?).with('somefile.lock').and_return(false)
log.should_receive(:warn).with(/Cannot find somefile.lock/)
run '-G', 'somefile'
Please sign in to comment.
Something went wrong with that request. Please try again.