myrurema 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/VERSION +1 -1
  2. data/bin/rurema +1 -201
  3. data/main.rb +19 -0
  4. data/spec/commands.rb +68 -0
  5. data/src/myrurema.rb +205 -0
  6. metadata +9 -6
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/bin/rurema CHANGED
@@ -1,204 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- require 'optparse'
3
- require 'pathname'
4
- require 'shellwords'
5
-
6
- class Pathname; alias / +; end
7
-
8
- class Options
9
- def initialize(argv)
10
- @command = nil
11
- @open_browser = false
12
- @port = nil
13
- @dry_run = false
14
- @ruremadir = Pathname("~/.rurema").expand_path
15
- @rubyver = RUBY_VERSION
16
- @query = ""
17
-
18
- @optionparser = OptionParser.new{|o|
19
- o.on("--init",
20
- "initialize rurema"){
21
- @command = :init
22
- }
23
- o.on("--update",
24
- "update documents and database"){
25
- @command = :update
26
- }
27
- o.on("--server",
28
- "start web server"){
29
- @command = :server
30
- }
31
-
32
- o.on("--port=N",
33
- "port number of the web browser (only meaningful with --server)"){|n|
34
- @port = n.to_i
35
- }
36
- o.on("--browser",
37
- "open web browser (only meaningful with --server)"){
38
- @open_browser = true
39
- }
40
- o.on("--dry-run",
41
- "show commands only"){
42
- @dry_run = true
43
- }
44
- o.on("--ruremadir=PATH",
45
- "specify rurema directory (default: #{@ruremadir})"){|path|
46
- @ruremadir = Pathname(path)
47
- }
48
- o.on("--rubyver=STR",
49
- "specify Ruby version (default: #{@rubyver})"){|str|
50
- @rubyver = str
51
- }
52
- o.on("--version",
53
- "show version of myrurema"){
54
- puts MyRurema::VERSION
55
- exit
56
- }
57
- o.on("--help",
58
- "show this message"){
59
- puts o
60
- exit
61
- }
62
- }
63
- @query, @num = @optionparser.parse(argv)
64
- @query = "" if @query.nil?
65
- @num = @num.to_i if @num
66
- end
67
- attr_reader :dry_run, :ruremadir, :rubyver, :open_browser
68
- attr_reader :command, :query, :num, :port
69
-
70
- def usage
71
- puts @optionparser
72
- end
73
- end
74
-
75
- class MyRurema
76
- VERSION = File.read((Pathname(__FILE__).dirname/"../VERSION").expand_path)
77
- SVN_URL = "http://jp.rubyist.net/svn/rurema"
78
-
79
- def initialize(opt=Options.new(ARGV))
80
- @opt = opt
81
- end
82
-
83
- def run
84
- case
85
- when @opt.command
86
- send(@opt.command)
87
- when @opt.query.empty?
88
- @opt.usage
89
- when @opt.num
90
- search_num(@opt.query, @opt.num, @opt.rubyver)
91
- else
92
- search(@opt.query, @opt.rubyver)
93
- end
94
- end
95
-
96
- def init
97
- sh "svn co -rHEAD #{SVN_URL}/doctree/trunk #{doctree_path}"
98
- sh "svn co -rHEAD #{SVN_URL}/bitclust/trunk #{bitclust_path}"
99
- init_db(@opt.rubyver)
100
- end
101
-
102
- def update
103
- sh "svn up #{doctree_path}"
104
- refresh_db(@opt.rubyver)
105
- end
106
-
107
- def search(query, ver)
108
- should_have_db(ver)
109
-
110
- sh "#{bitclust_path/'bin/refe.rb'}" +
111
- " #{Shellwords.escape query} -d #{db_path(ver)}", :silent => true
112
- end
113
-
114
- def search_num(query, num, ver)
115
- should_have_db(ver)
116
-
117
- result = `#{bitclust_path/'bin/refe.rb'} #{query} -d #{db_path(ver)}`
118
- word = result.split[num-1]
119
- if word
120
- word.gsub!(/\.#/, ".") # avoid multi-hit for a module function
121
- puts "searching #{word}"
122
- search(word, ver)
123
- else
124
- error "less than #{num} entries found"
125
- end
126
- end
127
-
128
- def server
129
- port = @opt.port || default_port(@opt.rubyver)
130
- th = Thread.new{
131
- sh "#{bitclust_path/'standalone.rb'}" +
132
- " --baseurl=http://localhost:#{port}" +
133
- " --port=#{port}" +
134
- " --database=#{db_path(@opt.rubyver)}" +
135
- " --debug"
136
- }
137
- if @opt.open_browser
138
- sleep 1 # wait for the server to start
139
- cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open"
140
- sh "#{cmd} http://localhost:#{port}/view/"
141
- end
142
- th.join
143
- end
144
-
145
- private
146
-
147
- def default_port(ver)
148
- "10" + ver.scan(/\d/).join
149
- end
150
-
151
- def init_db(ver)
152
- sh "#{bitclust_path/'bin/bitclust.rb'}" +
153
- " -d #{db_path(ver)} init version=#{ver} encoding=euc-jp"
154
-
155
- refresh_db(ver)
156
- end
157
-
158
- def refresh_db(ver)
159
- puts "Updating Rurema database:"
160
- puts "This will take a few minutes. Please be patient."
161
- sh "#{bitclust_path/'bin/bitclust.rb'}" +
162
- " -d #{db_path(ver)}" +
163
- " update --stdlibtree=#{doctree_path/'refm/api/src'}"
164
- end
165
-
166
- def bitclust_path
167
- @opt.ruremadir / "bitclust"
168
- end
169
-
170
- def doctree_path
171
- @opt.ruremadir / "doctree"
172
- end
173
-
174
- def should_have_db(ver)
175
- unless has_db?(ver)
176
- puts "You don't have a database for ruby #{ver}."
177
- puts "Make it now? [y/n]"
178
- if $stdin.gets.chomp.downcase == "y"
179
- init_db(ver)
180
- else
181
- exit
182
- end
183
- end
184
- end
185
-
186
- def has_db?(ver)
187
- db_path(ver).directory?
188
- end
189
-
190
- def db_path(ver)
191
- @opt.ruremadir / "db" / ver
192
- end
193
-
194
- def sh(cmd, opt={})
195
- puts cmd unless opt[:silent]
196
- system cmd unless @opt.dry_run
197
- end
198
-
199
- def error(msg)
200
- $stderr.puts msg
201
- end
202
- end
2
+ require File.expand_path("../src/myrurema", File.dirname(__FILE__))
203
3
 
204
4
  MyRurema.new.run
data/main.rb ADDED
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ require 'rubygems'
4
+ require 'ruby-station'; RubyStation.parse_argv
5
+ require File.expand_path("src/myrurema", File.dirname(__FILE__))
6
+
7
+ opt = Options.new(ARGV)
8
+ ruremadir = Pathname(RubyStation.data_dir)
9
+
10
+ if ruremadir.entries.size == 2
11
+ opt.command = :init
12
+ opt.ruremadir = RubyStation.data_dir
13
+ MyRurema.new(opt).run
14
+ end
15
+
16
+ opt.command = :server
17
+ opt.ruremadir = RubyStation.data_dir
18
+ opt.port = RubyStation.port
19
+ MyRurema.new(opt).run
data/spec/commands.rb ADDED
@@ -0,0 +1,68 @@
1
+ require File.expand_path("../src/myrurema", File.dirname(__FILE__))
2
+
3
+ class MyRurema
4
+ def sh(cmd, opt={})
5
+ @cmds ||= []
6
+ @cmds << cmd
7
+ end
8
+ attr_reader :cmds
9
+ end
10
+
11
+ CASES = {
12
+ "rurema --init" => [
13
+ %r{svn co .*doctree},
14
+ %r{svn co .*bitclust},
15
+ %r{bitclust.*init},
16
+ %r{bitclust.*update},
17
+ ],
18
+
19
+ "rurema --init --rubyver=1.8.7" => [
20
+ %r{bitclust.*init version=1.8.7}
21
+ ],
22
+
23
+ "rurema --init --ruremadir=/tmp" => [
24
+ %r{svn co .*/tmp}
25
+ ],
26
+
27
+ "rurema --update" => [
28
+ %r{svn up},
29
+ %r{bitclust.*update}
30
+ ],
31
+
32
+ "rurema Array" => [
33
+ %r{refe.*Array},
34
+ ],
35
+
36
+ "rurema --server" => [
37
+ %r{standalone},
38
+ ],
39
+
40
+ "rurema --server --port=9898" => [
41
+ %r{standalone.*--port=9898},
42
+ ],
43
+
44
+ "rurema --server --browser" => [
45
+ %r{standalone},
46
+ %r{(start|open) http://localhost:}
47
+ ],
48
+ }
49
+
50
+ describe MyRurema do
51
+ it "should execute expected commands" do
52
+ CASES.each do |command, expects|
53
+ opt = Options.new(command.split[1..-1])
54
+ my = MyRurema.new(opt)
55
+ my.run
56
+
57
+ cmds = my.cmds
58
+ i = 0
59
+ expects.each do |pattern|
60
+ pattern.should satisfy{
61
+ i = cmds[i..-1].index{|cmd| pattern =~ cmd}
62
+ not i.nil?
63
+ }
64
+ i += 1
65
+ end
66
+ end
67
+ end
68
+ end
data/src/myrurema.rb ADDED
@@ -0,0 +1,205 @@
1
+ require 'optparse'
2
+ require 'pathname'
3
+ require 'shellwords'
4
+
5
+ class Pathname; alias / +; end
6
+
7
+ class Options
8
+ def initialize(argv)
9
+ @command = nil
10
+ @open_browser = false
11
+ @port = nil
12
+ @dry_run = false
13
+ @ruremadir = Pathname("~/.rurema").expand_path
14
+ @rubyver = RUBY_VERSION
15
+ @query = ""
16
+
17
+ @optionparser = OptionParser.new{|o|
18
+ o.on("--init",
19
+ "initialize rurema"){
20
+ @command = :init
21
+ }
22
+ o.on("--update",
23
+ "update documents and database"){
24
+ @command = :update
25
+ }
26
+ o.on("--server",
27
+ "start web server"){
28
+ @command = :server
29
+ }
30
+
31
+ o.on("--port=N",
32
+ "port number of the web browser (only meaningful with --server)"){|n|
33
+ @port = n.to_i
34
+ }
35
+ o.on("--browser",
36
+ "open web browser (only meaningful with --server)"){
37
+ @open_browser = true
38
+ }
39
+ o.on("--dry-run",
40
+ "show commands only"){
41
+ @dry_run = true
42
+ }
43
+ o.on("--ruremadir=PATH",
44
+ "specify rurema directory (default: #{@ruremadir})"){|path|
45
+ @ruremadir = Pathname(path)
46
+ }
47
+ o.on("--rubyver=STR",
48
+ "specify Ruby version (default: #{@rubyver})"){|str|
49
+ @rubyver = str
50
+ }
51
+ o.on("--version",
52
+ "show version of myrurema"){
53
+ puts MyRurema::VERSION
54
+ exit
55
+ }
56
+ o.on("--help",
57
+ "show this message"){
58
+ puts o
59
+ exit
60
+ }
61
+ }
62
+ @query, @num = @optionparser.parse(argv)
63
+ @query = "" if @query.nil?
64
+ @num = @num.to_i if @num
65
+ end
66
+ attr_accessor :dry_run, :ruremadir, :rubyver, :open_browser
67
+ attr_accessor :command, :query, :num, :port
68
+
69
+ def ruremadir=(dir)
70
+ @ruremadir = Pathname(dir)
71
+ end
72
+
73
+ def usage
74
+ puts @optionparser
75
+ end
76
+ end
77
+
78
+ class MyRurema
79
+ VERSION = File.read((Pathname(__FILE__).dirname/"../VERSION").expand_path)
80
+ SVN_URL = "http://jp.rubyist.net/svn/rurema"
81
+
82
+ def initialize(opt=Options.new(ARGV))
83
+ @opt = opt
84
+ end
85
+
86
+ def run
87
+ case
88
+ when @opt.command
89
+ send(@opt.command)
90
+ when @opt.query.empty?
91
+ @opt.usage
92
+ when @opt.num
93
+ search_num(@opt.query, @opt.num, @opt.rubyver)
94
+ else
95
+ search(@opt.query, @opt.rubyver)
96
+ end
97
+ end
98
+
99
+ def init
100
+ sh "svn co -rHEAD #{SVN_URL}/doctree/trunk #{doctree_path}"
101
+ sh "svn co -rHEAD #{SVN_URL}/bitclust/trunk #{bitclust_path}"
102
+ init_db(@opt.rubyver)
103
+ end
104
+
105
+ def update
106
+ sh "svn up #{doctree_path}"
107
+ refresh_db(@opt.rubyver)
108
+ end
109
+
110
+ def search(query, ver)
111
+ should_have_db(ver)
112
+
113
+ sh "#{bitclust_path/'bin/refe.rb'}" +
114
+ " #{Shellwords.escape query} -d #{db_path(ver)}", :silent => true
115
+ end
116
+
117
+ def search_num(query, num, ver)
118
+ should_have_db(ver)
119
+
120
+ result = `#{bitclust_path/'bin/refe.rb'} #{query} -d #{db_path(ver)}`
121
+ word = result.split[num-1]
122
+ if word
123
+ word.gsub!(/\.#/, ".") # avoid multi-hit for a module function
124
+ puts "searching #{word}"
125
+ search(word, ver)
126
+ else
127
+ error "less than #{num} entries found"
128
+ end
129
+ end
130
+
131
+ def server
132
+ port = @opt.port || default_port(@opt.rubyver)
133
+ th = Thread.new{
134
+ sh "#{bitclust_path/'standalone.rb'}" +
135
+ " --baseurl=http://localhost:#{port}" +
136
+ " --port=#{port}" +
137
+ " --database=#{db_path(@opt.rubyver)}" +
138
+ " --debug"
139
+ }
140
+ if @opt.open_browser
141
+ sleep 1 # wait for the server to start
142
+ cmd = (/mswin/ =~ RUBY_PLATFORM) ? "start" : "open"
143
+ sh "#{cmd} http://localhost:#{port}/view/"
144
+ end
145
+ th.join
146
+ end
147
+
148
+ private
149
+
150
+ def default_port(ver)
151
+ "10" + ver.scan(/\d/).join
152
+ end
153
+
154
+ def init_db(ver)
155
+ sh "#{bitclust_path/'bin/bitclust.rb'}" +
156
+ " -d #{db_path(ver)} init version=#{ver} encoding=euc-jp"
157
+
158
+ refresh_db(ver)
159
+ end
160
+
161
+ def refresh_db(ver)
162
+ puts "Updating Rurema database:"
163
+ puts "This will take a few minutes. Please be patient."
164
+ sh "#{bitclust_path/'bin/bitclust.rb'}" +
165
+ " -d #{db_path(ver)}" +
166
+ " update --stdlibtree=#{doctree_path/'refm/api/src'}"
167
+ end
168
+
169
+ def bitclust_path
170
+ @opt.ruremadir / "bitclust"
171
+ end
172
+
173
+ def doctree_path
174
+ @opt.ruremadir / "doctree"
175
+ end
176
+
177
+ def should_have_db(ver)
178
+ unless has_db?(ver)
179
+ puts "You don't have a database for ruby #{ver}."
180
+ puts "Make it now? [y/n]"
181
+ if $stdin.gets.chomp.downcase == "y"
182
+ init_db(ver)
183
+ else
184
+ exit
185
+ end
186
+ end
187
+ end
188
+
189
+ def has_db?(ver)
190
+ db_path(ver).directory?
191
+ end
192
+
193
+ def db_path(ver)
194
+ @opt.ruremadir / "db" / ver
195
+ end
196
+
197
+ def sh(cmd, opt={})
198
+ puts cmd unless opt[:silent]
199
+ system cmd unless @opt.dry_run
200
+ end
201
+
202
+ def error(msg)
203
+ $stderr.puts msg
204
+ end
205
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myrurema
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Yutaka HARA
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-01 00:00:00 +09:00
18
+ date: 2010-09-01 00:00:00 +09:00
19
19
  default_executable: rurema
20
20
  dependencies: []
21
21
 
@@ -33,6 +33,9 @@ files:
33
33
  - VERSION
34
34
  - bin/rurema
35
35
  - features/basic_commands.feature
36
+ - main.rb
37
+ - spec/commands.rb
38
+ - src/myrurema.rb
36
39
  has_rdoc: true
37
40
  homepage: http://github.com/yhara/myrurema
38
41
  licenses: []
@@ -67,5 +70,5 @@ rubygems_version: 1.3.7
67
70
  signing_key:
68
71
  specification_version: 3
69
72
  summary: A tool for Rurema (the new Japanese Ruby reference manual)
70
- test_files: []
71
-
73
+ test_files:
74
+ - spec/commands.rb