Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add yardoc --asset to copy over files/directories to output dir after…

… generation
  • Loading branch information...
commit 62d56c9e1307b4bca1ba188e8eefa4be1abfdd93 1 parent 5d04b68
Loren Segal authored
1  .yardopts
View
@@ -1,6 +1,7 @@
--protected
--no-private
--exclude /server/templates/
+--asset docs/images:images
-
docs/WhatsNew.md
docs/GettingStarted.md
1  Rakefile
View
@@ -44,5 +44,4 @@ end
YARD::Rake::YardocTask.new do |t|
t.options += ['--title', "YARD #{YARD::VERSION} Documentation"]
- t.after = lambda { `cp -R docs/images/ doc/images/` }
end
40 lib/yard/cli/yardoc.rb
View
@@ -146,6 +146,10 @@ class Yardoc < Command
# @return [Boolean] whether to print statistics after parsing
# @since 0.6.0
attr_accessor :statistics
+
+ # @return [Array<String>] a list of assets to copy after generation
+ # @since 0.6.0
+ attr_accessor :assets
# Creates a new instance of the commandline utility
def initialize
@@ -163,6 +167,7 @@ def initialize
:verifier => Verifier.new
)
@visibilities = [:public]
+ @assets = {}
@excluded = []
@files = []
@hidden_tags = []
@@ -202,6 +207,7 @@ def run(*args)
if generate
run_generate(checksums)
+ copy_assets
elsif list
print_list
end
@@ -287,6 +293,19 @@ def run_generate(checksums)
end
Templates::Engine.generate(objects, options)
end
+
+ # Copies any assets to the output directory
+ # @return [void]
+ # @since 0.6.0
+ def copy_assets
+ return unless options[:serializer]
+ outpath = options[:serializer].basepath
+ assets.each do |from, to|
+ to = File.join(outpath, to)
+ log.debug "Copying asset '#{from}' to '#{to}'"
+ FileUtils.cp_r(from, to)
+ end
+ end
# Prints a list of all objects
# @return [void]
@@ -495,6 +514,22 @@ def output_options(opts)
add_extra_files(*files.split(","))
end
+ opts.on('--asset FROM[:TO]', 'A file or directory to copy over to output directory after generating') do |asset|
+ re = /^(?:\.\.\/|\/)/
+ from, to = *asset.split(':').map {|f| File.cleanpath(f) }
+ to ||= from
+ if from =~ re || to =~ re
+ log.warn "Invalid file '#{asset}'"
+ else
+ assets[from] = to
+ end
+ end
+
+ opts.on('-o', '--output-dir PATH',
+ 'The output directory. (defaults to ./doc)') do |dir|
+ options[:serializer].basepath = dir
+ end
+
opts.on('-m', '--markup MARKUP',
'Markup style used in documentation, like textile, markdown or rdoc. (defaults to rdoc)') do |markup|
options[:markup] = markup.to_sym
@@ -505,11 +540,6 @@ def output_options(opts)
options[:markup_provider] = markup_provider.to_sym
end
- opts.on('-o', '--output-dir PATH',
- 'The output directory. (defaults to ./doc)') do |dir|
- options[:serializer].basepath = dir
- end
-
opts.on('--charset ENC', 'Character set to use for HTML output (default is system locale)') do |encoding|
begin
Encoding.default_external, Encoding.default_internal = encoding, encoding
38 spec/cli/yardoc_spec.rb
View
@@ -200,6 +200,44 @@ def self.should_accept(*args, &block)
CLI::Stats.should_not_receive(:new)
@yardoc.run *%w( --no-stats )
end
+
+ describe '--asset' do
+ before do
+ @yardoc.generate = true
+ @yardoc.stub!(:run_generate)
+ end
+
+ it "should copy assets to output directory" do
+ FileUtils.should_receive(:cp_r).with('a', 'doc/a')
+ @yardoc.run *%w( --asset a )
+ @yardoc.assets.should == {'a' => 'a'}
+ end
+
+ it "should allow multiple --asset options" do
+ FileUtils.should_receive(:cp_r).with('a', 'doc/a')
+ FileUtils.should_receive(:cp_r).with('b', 'doc/b')
+ @yardoc.run *%w( --asset a --asset b )
+ @yardoc.assets.should == {'a' => 'a', 'b' => 'b'}
+ end
+
+ it "should not allow from or to to refer to a path above current path" do
+ log.should_receive(:warn).exactly(4).times.with(/invalid/i)
+ @yardoc.run *%w( --asset ../../../etc/passwd )
+ @yardoc.assets.should be_empty
+ @yardoc.run *%w( --asset a/b/c/d/../../../../../../etc/passwd )
+ @yardoc.assets.should be_empty
+ @yardoc.run *%w( --asset /etc/passwd )
+ @yardoc.assets.should be_empty
+ @yardoc.run *%w( --asset normal:/etc/passwd )
+ @yardoc.assets.should be_empty
+ end
+
+ it "should allow from:to syntax" do
+ FileUtils.should_receive(:cp_r).with('foo', 'doc/bar')
+ @yardoc.run *%w( --asset foo:bar )
+ @yardoc.assets.should == {'foo' => 'bar'}
+ end
+ end
end
describe '--no-private option' do
Please sign in to comment.
Something went wrong with that request. Please try again.