starscope 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ Changelog
2
+ =========
3
+
4
+ v0.0.4 (trunk)
5
+ --------------
6
+
7
+ New Features:
8
+ * Line-mode (use the `-l` flag)
9
+
10
+ Other Improvements:
11
+ * Better error handling
12
+ * General polish
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- starscope (0.0.3)
5
- parser
4
+ starscope (0.0.4)
5
+ parser (~> 1.4)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -6,6 +6,23 @@ across the fantastic Cscope tool [1]. Sadly, it only works for C (and sort of
6
6
  works for C++).
7
7
 
8
8
  StarScope is a similar tool for Ruby, with a design intended to make it easy to
9
- add support for other languages at some point within the same framework.
9
+ add support for other languages at some point within the same framework (thus
10
+ the name StarScope, ie \*scope).
11
+
12
+ Install it as a gem:
13
+ ```
14
+ $ gem install starscope
15
+ ```
16
+
17
+ Build your database, by just running it in the project directory:
18
+ ```
19
+ $ cd ~/my-project
20
+ $ starscope.rb
21
+ ```
22
+
23
+ Ask it things with `-q`
24
+ ```
25
+ $ starscope.rb -q calls,new # Lists all callers of new
26
+ ```
10
27
 
11
28
  [1] http://cscope.sourceforge.net/
@@ -24,6 +24,9 @@ END
24
24
  opts.on("-d", "--dump [TABLE]", "Dumps the DB or specified table to stdout") do |tbl|
25
25
  options[:dump] = tbl || true
26
26
  end
27
+ opts.on("-l", "--line-mode", "Starts line-oriented interface") do
28
+ options[:linemode] = true
29
+ end
27
30
  opts.on("-q", "--query TABLE,QUERY", "Looks up QUERY in TABLE") do |query|
28
31
  options[:query] = query
29
32
  end
@@ -50,39 +53,49 @@ END
50
53
 
51
54
  end.parse!
52
55
 
53
- # Load the database
56
+ def print_summary(db)
57
+ db.summary.each do |name, count|
58
+ printf("%-8s %5d keys\n", name, count)
59
+ end
60
+ end
61
+
62
+ def run_query(db, input, separator)
63
+ table, value = input.split(separator, 2)
64
+ if not value
65
+ $stderr.puts "Invalid query - did you separate your table and query with '#{separator}'?"
66
+ return
67
+ end
68
+ puts db.query(table.to_sym, value)
69
+ rescue StarScope::DB::NoTableError
70
+ $stderr.puts "Table '#{table}' doesn't exist."
71
+ end
72
+
73
+ if options[:auto] and not options[:write]
74
+ options[:write] = DEFAULT_DB
75
+ end
76
+
77
+ if File.exists?(DEFAULT_DB) and not options[:read]
78
+ options[:read] = DEFAULT_DB
79
+ end
80
+
54
81
  db = StarScope::DB.new
55
- new = true
82
+
56
83
  if options[:read]
57
84
  db.load(options[:read])
58
- new = false
59
- elsif File.exists?(DEFAULT_DB)
60
- db.load(DEFAULT_DB)
61
- new = false
62
- end
63
-
64
- if ARGV.empty?
65
- db.add_dirs(['.']) if new
85
+ db.add_dirs(ARGV)
86
+ elsif ARGV.empty?
87
+ db.add_dirs(['.'])
66
88
  else
67
89
  db.add_dirs(ARGV)
68
90
  end
69
91
 
70
- # Update it
71
- db.update if options[:auto] and not new
92
+ db.update if options[:read] and options[:auto]
72
93
 
73
- # Write it
74
- if options[:auto] || options[:write]
75
- db.save(options[:write] || DEFAULT_DB)
76
- end
94
+ db.save(options[:write]) if options[:write]
77
95
 
78
- if options[:query]
79
- table, value = options[:query].split(',', 2)
80
- db.query(table.to_sym, value)
81
- end
96
+ run_query(db, options[:query], ',') if options[:query]
82
97
 
83
- if options[:summary]
84
- db.print_summary
85
- end
98
+ print_summary(db) if options[:summary]
86
99
 
87
100
  if options[:dump]
88
101
  if options[:dump].is_a? String
@@ -91,3 +104,31 @@ if options[:dump]
91
104
  db.dump_all
92
105
  end
93
106
  end
107
+
108
+ if options[:linemode]
109
+ puts <<END
110
+ Normal input is of the form
111
+ table query
112
+ and returns the result of that query. The following special commands
113
+ are also recognized:
114
+ !summary
115
+ !update
116
+ !quit
117
+
118
+ END
119
+ while true
120
+ print "> "
121
+ input = gets.chomp
122
+ case input
123
+ when "!summary"
124
+ print_summary(db)
125
+ when "!update"
126
+ db.update
127
+ db.save(options[:write]) if options[:write]
128
+ when "!quit"
129
+ exit
130
+ else
131
+ run_query(db, input, ' ')
132
+ end
133
+ end
134
+ end
@@ -8,6 +8,8 @@ class StarScope::DB
8
8
 
9
9
  DB_FORMAT = 1
10
10
 
11
+ class NoTableError < StandardError; end
12
+
11
13
  def initialize
12
14
  @dirs = []
13
15
  @files = {}
@@ -37,7 +39,7 @@ class StarScope::DB
37
39
  end
38
40
 
39
41
  def add_dirs(dirs)
40
- @dirs << dirs
42
+ @dirs += dirs
41
43
  dirs.each do |dir|
42
44
  Dir["#{dir}/**/*"].each do |file|
43
45
  add_file(file)
@@ -52,6 +54,7 @@ class StarScope::DB
52
54
  end
53
55
 
54
56
  def dump_table(table)
57
+ raise NoTableError if not @tables[table]
55
58
  puts "== Table: #{table} =="
56
59
  @tables[table].each do |val, data|
57
60
  puts "#{val}"
@@ -66,22 +69,24 @@ class StarScope::DB
66
69
  @tables.keys.each {|tbl| dump_table(tbl)}
67
70
  end
68
71
 
69
- def print_summary
70
- @tables.each do |name, tbl|
71
- puts "#{name} - #{tbl.keys.count} entries"
72
+ def summary
73
+ ret = {}
74
+
75
+ @tables.each_key do |key|
76
+ ret[key] = @tables[key].keys.count
72
77
  end
78
+
79
+ ret
73
80
  end
74
81
 
75
82
  def query(table, value)
76
83
  fqn = value.split("::")
84
+ raise NoTableError if not @tables[table]
77
85
  results = @tables[table][fqn[-1].to_sym]
86
+ return [] if results.nil? || results.empty?
78
87
  results.sort! {|a,b| b.score_match(fqn) <=> a.score_match(fqn)}
79
88
  best_score = results[0].score_match(fqn)
80
- results.each do |result|
81
- # 4 is a guess at the best heuristic, not magic
82
- return if best_score - result.score_match(fqn) > 4
83
- puts result
84
- end
89
+ results.select {|result| best_score - result.score_match(fqn) < 4}
85
90
  end
86
91
 
87
92
  private
@@ -1,3 +1,3 @@
1
1
  module StarScope
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_dependency 'parser'
19
+ s.add_dependency 'parser', '~> 1.4'
20
20
  s.add_development_dependency 'bundler', '~> 1.3'
21
21
  s.add_development_dependency 'rake'
22
22
  s.add_development_dependency 'pry'
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: starscope
3
3
  version: !ruby/object:Gem::Version
4
+ version: 0.0.4
4
5
  prerelease:
5
- version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Evan Huus
@@ -12,85 +12,85 @@ cert_chain: []
12
12
  date: 2013-06-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- version_requirements: !ruby/object:Gem::Requirement
15
+ name: parser
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - ! '>='
19
+ - - ~>
18
20
  - !ruby/object:Gem::Version
19
- version: '0'
20
- none: false
21
- name: parser
21
+ version: '1.4'
22
22
  type: :runtime
23
23
  prerelease: false
24
- requirement: !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
25
26
  requirements:
26
- - - ! '>='
27
+ - - ~>
27
28
  - !ruby/object:Gem::Version
28
- version: '0'
29
- none: false
29
+ version: '1.4'
30
30
  - !ruby/object:Gem::Dependency
31
- version_requirements: !ruby/object:Gem::Requirement
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
32
34
  requirements:
33
35
  - - ~>
34
36
  - !ruby/object:Gem::Version
35
37
  version: '1.3'
36
- none: false
37
- name: bundler
38
38
  type: :development
39
39
  prerelease: false
40
- requirement: !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
41
42
  requirements:
42
43
  - - ~>
43
44
  - !ruby/object:Gem::Version
44
45
  version: '1.3'
45
- none: false
46
46
  - !ruby/object:Gem::Dependency
47
- version_requirements: !ruby/object:Gem::Requirement
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
48
50
  requirements:
49
51
  - - ! '>='
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
- none: false
53
- name: rake
54
54
  type: :development
55
55
  prerelease: false
56
- requirement: !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
57
58
  requirements:
58
59
  - - ! '>='
59
60
  - !ruby/object:Gem::Version
60
61
  version: '0'
61
- none: false
62
62
  - !ruby/object:Gem::Dependency
63
- version_requirements: !ruby/object:Gem::Requirement
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
64
66
  requirements:
65
67
  - - ! '>='
66
68
  - !ruby/object:Gem::Version
67
69
  version: '0'
68
- none: false
69
- name: pry
70
70
  type: :development
71
71
  prerelease: false
72
- requirement: !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
73
74
  requirements:
74
75
  - - ! '>='
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
- none: false
78
78
  - !ruby/object:Gem::Dependency
79
- version_requirements: !ruby/object:Gem::Requirement
79
+ name: pry-debugger
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
80
82
  requirements:
81
83
  - - ! '>='
82
84
  - !ruby/object:Gem::Version
83
85
  version: '0'
84
- none: false
85
- name: pry-debugger
86
86
  type: :development
87
87
  prerelease: false
88
- requirement: !ruby/object:Gem::Requirement
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
89
90
  requirements:
90
91
  - - ! '>='
91
92
  - !ruby/object:Gem::Version
92
93
  version: '0'
93
- none: false
94
94
  description: A tool like the venerable cscope, but for ruby and other languages
95
95
  email: evan.huus@jadedpixel.com
96
96
  executables:
@@ -99,6 +99,7 @@ extensions: []
99
99
  extra_rdoc_files: []
100
100
  files:
101
101
  - .gitignore
102
+ - CHANGELOG.md
102
103
  - Gemfile
103
104
  - Gemfile.lock
104
105
  - LICENSE.txt
@@ -119,17 +120,17 @@ rdoc_options: []
119
120
  require_paths:
120
121
  - lib
121
122
  required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
122
124
  requirements:
123
125
  - - ! '>='
124
126
  - !ruby/object:Gem::Version
125
127
  version: '0'
126
- none: false
127
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
128
130
  requirements:
129
131
  - - ! '>='
130
132
  - !ruby/object:Gem::Version
131
133
  version: '0'
132
- none: false
133
134
  requirements: []
134
135
  rubyforge_project:
135
136
  rubygems_version: 1.8.23