bond-yard 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless Object.const_defined?(:Gem)
3
+ require File.dirname(__FILE__) + "/lib/bond/yard"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "bond-yard"
7
+ s.version = Bond::Yard::VERSION
8
+ s.authors = ["Gabriel Horner"]
9
+ s.email = "gabriel.horner@gmail.com"
10
+ s.homepage = "http://github.com/cldwalker/bond-yard"
11
+ s.summary = "Generate completions from yard docs"
12
+ s.description = "This bond plugin generates completions for gems that have been documented with yard."
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.rubyforge_project = 'tagaholic'
15
+ s.has_rdoc = 'yard'
16
+ s.rdoc_options = ['--title', "bond-yard #{Bond::Yard::VERSION} Documentation"]
17
+ s.add_dependency 'yard', '>= 0.5.2'
18
+ s.add_development_dependency 'bacon', '>= 1.1.0'
19
+ s.add_development_dependency 'mocha', '>= 0.9.8'
20
+ s.add_development_dependency 'mocha-on-bacon'
21
+ s.add_development_dependency 'bacon-bits'
22
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
23
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
24
+ s.license = 'MIT'
25
+ end
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == 0.1.0
2
+ * Extracted from bond
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2010 Gabriel Horner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ == Description
2
+ This bond plugin generates completions for gems that have been documented with yard.
3
+
4
+ == Install
5
+ Install the gem with:
6
+
7
+ sudo gem install bond-yard
8
+
9
+ == Usage
10
+
11
+ Add to your ~/.irbrc
12
+
13
+ require 'bond/yard'
14
+
15
+ In your .irbrc or in the console, load completions for gems that have been documented with yard:
16
+
17
+ >> Bond.load_yard_gems 'bond'
18
+ Bond: Building/loading bond's .yardoc database ...
19
+ => ['bond']
20
+ >> Bond.start :[TAB]
21
+ :bare :debug :default_mission :default_search :eval_binding :eval_debug :gems :readline_plugin
22
+
23
+ For more examples, {see
24
+ here}[http://tagaholic.me/2010/05/19/documentation-generated-irb-autocompletions-with-bond-and-yard.html#yard_based_irb_autocompletions].
25
+
26
+ == Todo
27
+ * Generate method autocompletions for any arguments based on yard docs
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+
4
+ def gemspec
5
+ @gemspec ||= eval(File.read('.gemspec'), binding, '.gemspec')
6
+ end
7
+
8
+ desc "Build the gem"
9
+ task :gem=>:gemspec do
10
+ sh "gem build .gemspec"
11
+ FileUtils.mkdir_p 'pkg'
12
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
13
+ end
14
+
15
+ desc "Install the gem locally"
16
+ task :install => :gem do
17
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
18
+ end
19
+
20
+ desc "Generate the gemspec"
21
+ task :generate do
22
+ puts gemspec.to_ruby
23
+ end
24
+
25
+ desc "Validate the gemspec"
26
+ task :gemspec do
27
+ gemspec.validate
28
+ end
29
+
30
+ desc 'Run tests'
31
+ task :test do |t|
32
+ sh 'bacon -q -Ilib -I. test/*_test.rb'
33
+ end
34
+
35
+ task :default => :test
data/deps.rip ADDED
@@ -0,0 +1 @@
1
+ yard >=0.5.2
data/lib/bond/yard.rb ADDED
@@ -0,0 +1,85 @@
1
+ require 'bond'
2
+ require 'yard'
3
+
4
+ module Bond
5
+ # Adds class methods to Bond
6
+ module Yard
7
+ VERSION = '0.1.0'
8
+
9
+ # Generates and loads completions for yardoc documented gems.
10
+ # @param *gems Gem(s) with optional options hash at the end
11
+ # @option *gems :verbose[Boolean] (false) Displays additional information when building yardoc.
12
+ # @option *gems :reload[Boolean] (false) Rebuilds yard databases. Use when gems have changed versions.
13
+ def load_yard_gems(*gems)
14
+ Loader.load_yard_gems(*gems)
15
+ end
16
+
17
+ # Generates and loads completions for methods that take a hash of options and have been
18
+ # documented with @option.
19
+ module Loader
20
+ extend self
21
+
22
+ def load_yard_gems(*gems)
23
+ @options = gems[-1].is_a?(Hash) ? gems.pop : {}
24
+ gems.select {|e| load_yard_gem(e) }
25
+ end
26
+
27
+ def load_yard_gem(rubygem)
28
+ raise("Unable to find gem.") unless (yardoc = find_yardoc(rubygem))
29
+ completion_file = File.join(dir('yard_completions'), rubygem+'.rb')
30
+ create_completion_file(yardoc, completion_file) if !File.exists?(completion_file) || @options[:reload]
31
+ M.load_file completion_file
32
+ rescue
33
+ $stderr.puts "Bond Error: Didn't load yard completions for gem '#{rubygem}'. #{$!.message}"
34
+ end
35
+
36
+ def create_completion_file(yardoc, completion_file)
37
+ YARD::Registry.load!(yardoc)
38
+ methods_hash = find_methods_with_options
39
+ body = generate_method_completions(methods_hash)
40
+ File.open(completion_file, 'w') {|e| e.write body }
41
+ end
42
+
43
+ def find_yardoc(rubygem)
44
+ (file = YARD::Registry.yardoc_file_for_gem(rubygem) rescue nil) and return(file)
45
+ if (file = M.find_gem_file(rubygem, rubygem+'.rb'))
46
+ output_dir = File.join(dir('.yardocs'), rubygem)
47
+ cmd = ['yardoc', '-n', '-b', output_dir]
48
+ cmd << '-q' unless @options[:verbose]
49
+ cmd += ['-c', output_dir] unless @options[:reload]
50
+ cmd += [file, File.expand_path(file+'/..')+"/#{rubygem}/**/*.rb"]
51
+ puts "Bond: "+cmd.join(' ') if @options[:verbose]
52
+ puts "Bond: Building/loading #{rubygem}'s .yardoc database ..."
53
+ system *cmd
54
+ output_dir
55
+ end
56
+ end
57
+
58
+ def dir(subdir)
59
+ (@dirs ||= {})[subdir] ||= begin
60
+ require 'fileutils'
61
+ FileUtils.mkdir_p File.join(M.home, '.bond', subdir)
62
+ File.join(M.home, '.bond', subdir)
63
+ end
64
+ end
65
+
66
+ def find_methods_with_options
67
+ YARD::Registry.all(:method).inject({}) {|a,m|
68
+ opts = m.tags.select {|e| e.is_a?(YARD::Tags::OptionTag) }.map {|e| e.pair.name }
69
+ a[m.path] = opts if !opts.empty? && m.path
70
+ a
71
+ }
72
+ end
73
+
74
+ def generate_method_completions(methods_hash)
75
+ methods_hash.map do |meth, options|
76
+ options.map! {|e| e.sub(/^:/, '') }
77
+ meth = meth.sub(/#initialize$/, '.new')
78
+ %Q[complete(:method=>'#{meth}') {\n #{options.inspect}\n}]
79
+ end.join("\n")
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ Bond.extend Bond::Yard
data/test/deps.rip ADDED
@@ -0,0 +1,4 @@
1
+ bacon >=1.1.0
2
+ mocha >=0.9.8
3
+ mocha-on-bacon >=0
4
+ bacon-bits >=0
data/test/yard_test.rb ADDED
@@ -0,0 +1,128 @@
1
+ require 'bacon'
2
+ require 'bacon/bits'
3
+ require 'mocha'
4
+ require 'mocha-on-bacon'
5
+ require 'bond/yard'
6
+ require 'rbconfig'
7
+
8
+ class Bacon::Context
9
+ def capture_stderr(&block)
10
+ original_stderr = $stderr
11
+ $stderr = fake = StringIO.new
12
+ begin
13
+ yield
14
+ ensure
15
+ $stderr = original_stderr
16
+ end
17
+ fake.string
18
+ end
19
+
20
+ def capture_stdout(&block)
21
+ original_stdout = $stdout
22
+ $stdout = fake = StringIO.new
23
+ begin
24
+ yield
25
+ ensure
26
+ $stdout = original_stdout
27
+ end
28
+ fake.string
29
+ end
30
+ end
31
+ include Bond
32
+
33
+ describe 'Bond.load_yard_gems' do
34
+ def load_yard_gems(*args)
35
+ args = ['blah'] if args.empty?
36
+ Bond.load_yard_gems(*args)
37
+ end
38
+
39
+ it 'prints error if no yardoc found' do
40
+ Yard::Loader.expects(:find_yardoc).returns(nil)
41
+ capture_stderr { load_yard_gems('bond') }.should =~ /Didn't.*'bond'.* Unable to find/
42
+ end
43
+
44
+ describe 'loads yardoc found by' do
45
+ before {
46
+ Yard::Loader.expects(:create_completion_file)
47
+ M.expects(:load_file)
48
+ }
49
+
50
+ it 'registry' do
51
+ YARD::Registry.expects(:yardoc_file_for_gem).returns('/dir/.yardoc')
52
+ load_yard_gems
53
+ end
54
+
55
+ describe 'find_gem_file and' do
56
+ before {
57
+ YARD::Registry.expects(:yardoc_file_for_gem).returns(nil)
58
+ M.expects(:find_gem_file).returns('/dir/blah.rb')
59
+ }
60
+
61
+ it 'prints building message' do
62
+ Yard::Loader.expects(:system)
63
+ capture_stdout { load_yard_gems('blah') }.should =~ /Building.*blah's/
64
+ end
65
+
66
+ it "caches yardoc by default" do
67
+ Yard::Loader.expects(:system).with {|*args| args.include?('-c') }
68
+ capture_stdout { load_yard_gems }
69
+ end
70
+
71
+ it "doesn't cache yardoc with :reload option" do
72
+ Yard::Loader.expects(:system).with {|*args| !args.include?('-c') }
73
+ capture_stdout { load_yard_gems('blah', :reload=>true) }
74
+ end
75
+
76
+ it "prints multiple messages with :verbose option" do
77
+ Yard::Loader.expects(:system).with {|*args| !args.include?('-q') }
78
+ capture_stdout { load_yard_gems('blah', :verbose=>true) }.should =~ /yardoc -n/
79
+ end
80
+ end
81
+ end
82
+
83
+ describe 'creates completion file' do
84
+ before {
85
+ Yard::Loader.expects(:find_yardoc).returns('/dir/.yardoc')
86
+ M.expects(:load_file)
87
+ }
88
+
89
+ # rubinius implements Kernel#require with File.exists? which is different than MRI
90
+ unless Config::CONFIG["RUBY_SO_NAME"].to_s[/rubinius/i]
91
+ it "with :reload option" do
92
+ File.expects(:exists?).returns(true)
93
+ Yard::Loader.expects(:create_completion_file)
94
+ load_yard_gems 'blah', :reload=>true
95
+ end
96
+
97
+ it "with new completion file" do
98
+ File.expects(:exists?).returns(false)
99
+ Yard::Loader.expects(:create_completion_file)
100
+ load_yard_gems
101
+ end
102
+ end
103
+
104
+ describe 'which has' do
105
+ before { YARD::Registry.expects(:load!) }
106
+
107
+ it 'methods' do
108
+ Yard::Loader.expects(:find_methods_with_options).returns({"Bond::M.start"=>[':one', 'two']})
109
+ expected_body = %[complete(:method=>'Bond::M.start') {\n ["one", "two"]\n}]
110
+ File.expects(:open).yields mock('block') { expects(:write).with(expected_body) }
111
+ load_yard_gems
112
+ end
113
+
114
+ it 'no methods' do
115
+ Yard::Loader.expects(:find_methods_with_options).returns({})
116
+ File.expects(:open).yields mock('block') { expects(:write).with('') }
117
+ load_yard_gems
118
+ end
119
+
120
+ it 'methods that map from #initialize to .new' do
121
+ Yard::Loader.expects(:find_methods_with_options).returns({"Bond::Agent#initialize"=>[':one', 'two']})
122
+ expected_body = %[complete(:method=>'Bond::Agent.new') {\n ["one", "two"]\n}]
123
+ File.expects(:open).yields mock('block') { expects(:write).with(expected_body) }
124
+ load_yard_gems
125
+ end
126
+ end
127
+ end
128
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bond-yard
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Gabriel Horner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-07 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 0
32
+ - 5
33
+ - 2
34
+ version: 0.5.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bacon
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 1
48
+ - 1
49
+ - 0
50
+ version: 1.1.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: mocha
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 43
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 8
66
+ version: 0.9.8
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: mocha-on-bacon
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: bacon-bits
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ type: :development
96
+ version_requirements: *id005
97
+ description: This bond plugin generates completions for gems that have been documented with yard.
98
+ email: gabriel.horner@gmail.com
99
+ executables: []
100
+
101
+ extensions: []
102
+
103
+ extra_rdoc_files:
104
+ - README.rdoc
105
+ - LICENSE.txt
106
+ files:
107
+ - lib/bond/yard.rb
108
+ - test/yard_test.rb
109
+ - LICENSE.txt
110
+ - CHANGELOG.rdoc
111
+ - README.rdoc
112
+ - deps.rip
113
+ - test/deps.rip
114
+ - Rakefile
115
+ - .gemspec
116
+ has_rdoc: yard
117
+ homepage: http://github.com/cldwalker/bond-yard
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options:
122
+ - --title
123
+ - bond-yard 0.1.0 Documentation
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ hash: 3
132
+ segments:
133
+ - 0
134
+ version: "0"
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ none: false
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 23
141
+ segments:
142
+ - 1
143
+ - 3
144
+ - 6
145
+ version: 1.3.6
146
+ requirements: []
147
+
148
+ rubyforge_project: tagaholic
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Generate completions from yard docs
153
+ test_files: []
154
+