code_lister 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +90 -15
- data/lib/code_lister/cli.rb +22 -13
- data/lib/code_lister/code_lister.rb +40 -11
- data/lib/code_lister/main.rb +3 -1
- data/lib/code_lister/version.rb +1 -1
- data/spec/code_lister/code_lister_spec.rb +11 -1
- data/spec/fixtures/demo1.yyy.rb +1 -0
- data/spec/fixtures/demo2.yyy.rb +1 -0
- data/spec/fixtures/java/demo3.xxx.java +1 -1
- data/spec/fixtures/java/demo3.yyy.java +1 -0
- data/spec/fixtures/java/demo4.xxx.java +1 -1
- data/spec/fixtures/java/demo4.yyy.java +1 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab05b837c5a1c82a86f45b2a732e1c94b5aa9f4c
|
4
|
+
data.tar.gz: 7c511a1e1297223d9f96671045be7da41449e8d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c58b793ebff0bd9e64f588983f4950c2f83b8ebfae3d2b3b5df0303c199be31d2409682b5198389db3c70416a6904d3f860ff892261039e5cca13545967f95a4
|
7
|
+
data.tar.gz: c55a500f6437006295cb9dc1322a799d0420395ef6d761a970cc94a2cbba6533005c9ed763838da93c9478bcef2f2c88d9580d17acdb2c220b909d13614e527e
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
## code_lister
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/code_lister.svg)](http://badge.fury.io/rb/code_lister)
|
4
|
+
|
3
5
|
This is the simple gem to search/filter the files based on simple criteria.
|
4
|
-
It provides the functionality similar to `find` command in Linux/Unix system.
|
6
|
+
It provides the functionality similar to subset of `find` command in Linux/Unix system.
|
5
7
|
|
6
8
|
Initially it was part of my internal project. I extracted this out as a gem so
|
7
9
|
that I can re-use it in other project. I hope you will find it useful.
|
@@ -30,33 +32,91 @@ List all pdf and epub files in the `/opts/downloads/` directory recursively
|
|
30
32
|
$code_lister find --base-dir /opt/downloads/ --exts pdf epub --recursive
|
31
33
|
```
|
32
34
|
|
33
|
-
Usage:
|
35
|
+
Usage/Synopsis:
|
34
36
|
|
35
37
|
```
|
36
38
|
Usage:
|
37
39
|
code_lister find
|
38
40
|
|
39
41
|
Options:
|
40
|
-
-b, [--base-dir=BASE_DIR]
|
41
|
-
|
42
|
-
-e, [--exts=one two three]
|
43
|
-
|
44
|
-
-
|
45
|
-
-
|
46
|
-
|
47
|
-
-
|
48
|
-
|
49
|
-
|
42
|
+
-b, [--base-dir=BASE_DIR] # Base directory
|
43
|
+
# Default: . (current directory)
|
44
|
+
-e, [--exts=one two three] # List of extensions to search for
|
45
|
+
-n, [--inc-words=one two three] # List of words to be included in the result if any
|
46
|
+
-x, [--exc-words=one two three] # List of words to be excluded from the result if any
|
47
|
+
-i, [--ignore-case], [--no-ignore-case] # Match case insensitively
|
48
|
+
# Default: true
|
49
|
+
-r, [--recursive], [--no-recursive] # Search for files recursively
|
50
|
+
# Default: true
|
51
|
+
-v, [--version], [--no-version] # Display version information
|
52
|
+
|
53
|
+
List files by extensions, patterns, and simple criteria
|
54
|
+
```
|
55
|
+
|
56
|
+
Example Usage:
|
57
|
+
|
58
|
+
- Find all java and ruby files in a given directory
|
59
|
+
|
60
|
+
e.g.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
|
64
|
+
# find all files that ends with '*.java' or '*.rb' in a given directory
|
65
|
+
#
|
66
|
+
# ./bin/code_lister find -b spec/fixtures/ -e rb java
|
67
|
+
|
68
|
+
spec/fixtures/demo1.xxx.rb
|
69
|
+
spec/fixtures/demo1.yyy.rb
|
70
|
+
spec/fixtures/demo2.xxx.rb
|
71
|
+
spec/fixtures/demo2.yyy.rb
|
72
|
+
spec/fixtures/java/demo3.xxx.java
|
73
|
+
spec/fixtures/java/demo3.yyy.java
|
74
|
+
spec/fixtures/java/demo4.xxx.java
|
75
|
+
spec/fixtures/java/demo4.yyy.java
|
76
|
+
```
|
77
|
+
|
78
|
+
- Find all java java and ruby files but include only the files that contain the word `xxx`
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# ./bin/code_lister find -b spec/fixtures/ -e rb java -n xxx
|
82
|
+
spec/fixtures/demo1.xxx.rb
|
83
|
+
spec/fixtures/demo2.xxx.rb
|
84
|
+
spec/fixtures/java/demo3.xxx.java
|
85
|
+
spec/fixtures/java/demo4.xxx.java
|
86
|
+
|
87
|
+
```
|
88
|
+
|
89
|
+
- Same as previous step, but filter out result that contain the word `demo3` or `demo4`
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# ./bin/code_lister find -b spec/fixtures/ -e rb java -n xxx -x demo3 demo4
|
93
|
+
spec/fixtures/demo1.xxx.rb
|
94
|
+
spec/fixtures/demo2.xxx.rb
|
50
95
|
```
|
51
96
|
|
52
97
|
#### Using as ruby library
|
53
98
|
|
99
|
+
This is probably the proper way to utilize the library as the CLI only serve to
|
100
|
+
demonstrate purpose only.
|
101
|
+
|
102
|
+
Example of how you might use the library in your own project.
|
103
|
+
|
54
104
|
```ruby
|
55
105
|
require 'code_lister'
|
56
106
|
include CodeLister
|
107
|
+
|
57
108
|
# To search for everything that ends in '*.java' and '*.rb" recursively
|
58
|
-
|
59
|
-
|
109
|
+
file_list = CodeLister.files base_dir: "spec/fixtures",
|
110
|
+
exts: %w(rb java),
|
111
|
+
recursive: true
|
112
|
+
puts file_list
|
113
|
+
|
114
|
+
# To filter out the result you may do so with the `CodeLister.filter` method
|
115
|
+
|
116
|
+
new_list = CodeLister.filter(file_list, inc_words: %w(some list of word),
|
117
|
+
exc_words: %w(other text to excluded),
|
118
|
+
ignore_case: false)
|
119
|
+
|
60
120
|
```
|
61
121
|
|
62
122
|
### Development/Testing
|
@@ -76,7 +136,8 @@ rake irb
|
|
76
136
|
From inside `Pry` or `IRB`
|
77
137
|
|
78
138
|
```ruby
|
79
|
-
|
139
|
+
include CodeLister
|
140
|
+
|
80
141
|
# To search for everything that ends in '*.java' and '*.rb" recursively
|
81
142
|
list1 = CodeLister.files(base_dir: "spec/fixtures", exts: %w(rb java), recursive: true)
|
82
143
|
puts list1
|
@@ -84,8 +145,22 @@ puts list1
|
|
84
145
|
# To filter out the result list
|
85
146
|
list2 = CodeLister.filter(list1, inc_words: %w(final complete), exc_words: %w(demo test))
|
86
147
|
```
|
148
|
+
|
149
|
+
### Disclaimers
|
150
|
+
|
151
|
+
This is still work in progress. I may make several adjustment to the API and thus I might break
|
152
|
+
the compatibility as the result. Please let me know if you find any problem. Pull request is
|
153
|
+
always welcome.
|
154
|
+
|
87
155
|
### Changelogs
|
88
156
|
|
157
|
+
#### 0.0.4
|
158
|
+
|
159
|
+
- Add `ignore-case` option
|
160
|
+
- Use `-n' for `--include-words` and use `-i` for `--ignore-case`.
|
161
|
+
- Make the `--recursive` the default option
|
162
|
+
- Make the `--ignore-case` the default option
|
163
|
+
|
89
164
|
#### 0.0.3
|
90
165
|
|
91
166
|
- Update README.md to include better sample usage, and misc cleanup.
|
data/lib/code_lister/cli.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'thor'
|
2
2
|
require_relative 'core_ext/hash'
|
3
|
+
|
3
4
|
module CodeLister
|
4
5
|
class CLI < Thor
|
5
6
|
|
6
|
-
desc "find", "List files by
|
7
|
+
desc "find", "List files by extensions, patterns, and simple criteria"
|
7
8
|
|
8
9
|
method_option :base_dir,
|
9
10
|
aliases: "-b",
|
@@ -14,10 +15,10 @@ module CodeLister
|
|
14
15
|
aliases: "-e",
|
15
16
|
desc: "List of extensions to search for",
|
16
17
|
type: :array,
|
17
|
-
default:
|
18
|
+
default: []
|
18
19
|
|
19
20
|
method_option :inc_words,
|
20
|
-
aliases: "-
|
21
|
+
aliases: "-n",
|
21
22
|
desc: "List of words to be included in the result if any",
|
22
23
|
type: :array,
|
23
24
|
default: []
|
@@ -28,11 +29,17 @@ module CodeLister
|
|
28
29
|
type: :array,
|
29
30
|
default: []
|
30
31
|
|
32
|
+
method_option :ignore_case,
|
33
|
+
aliases: "-i",
|
34
|
+
desc: "Match case insensitively",
|
35
|
+
type: :boolean,
|
36
|
+
default: true
|
37
|
+
|
31
38
|
method_option :recursive,
|
32
39
|
aliases: "-r",
|
33
40
|
desc: "Search for files recursively",
|
34
41
|
type: :boolean,
|
35
|
-
default:
|
42
|
+
default: true
|
36
43
|
|
37
44
|
method_option :version,
|
38
45
|
aliases: "-v",
|
@@ -58,16 +65,18 @@ Usage:
|
|
58
65
|
code_lister find
|
59
66
|
|
60
67
|
Options:
|
61
|
-
-b, [--base-dir=BASE_DIR]
|
62
|
-
|
63
|
-
-e, [--exts=one two three]
|
64
|
-
|
65
|
-
-
|
66
|
-
-
|
67
|
-
|
68
|
-
-
|
68
|
+
-b, [--base-dir=BASE_DIR] # Base directory
|
69
|
+
# Default: . (current directory)
|
70
|
+
-e, [--exts=one two three] # List of extensions to search for
|
71
|
+
-n, [--inc-words=one two three] # List of words to be included in the result if any
|
72
|
+
-x, [--exc-words=one two three] # List of words to be excluded from the result if any
|
73
|
+
-i, [--ignore-case], [--no-ignore-case] # Match case insensitively
|
74
|
+
# Default: true
|
75
|
+
-r, [--recursive], [--no-recursive] # Search for files recursively
|
76
|
+
# Default: true
|
77
|
+
-v, [--version], [--no-version] # Display version information
|
69
78
|
|
70
|
-
List files
|
79
|
+
List files by extensions, patterns, and simple criteria
|
71
80
|
EOS
|
72
81
|
end
|
73
82
|
|
@@ -25,28 +25,57 @@ module CodeLister
|
|
25
25
|
def filter(file_list, args = {})
|
26
26
|
opts = {
|
27
27
|
inc_words: [],
|
28
|
-
exc_words: []
|
28
|
+
exc_words: [],
|
29
|
+
ignore_case: true
|
29
30
|
}.merge(args)
|
30
31
|
|
31
|
-
inc_words
|
32
|
-
exc_words
|
32
|
+
inc_words = opts[:inc_words]
|
33
|
+
exc_words = opts[:exc_words]
|
34
|
+
ignore_case = opts[:ignore_case]
|
33
35
|
|
34
|
-
take_any!(file_list,
|
35
|
-
drop_any!(file_list,
|
36
|
+
take_any!(file_list, opts)
|
37
|
+
drop_any!(file_list, opts)
|
36
38
|
|
37
39
|
file_list
|
38
40
|
end
|
39
41
|
|
40
|
-
|
42
|
+
protected
|
43
|
+
|
44
|
+
def take_any!(file_list, args = {})
|
45
|
+
words = args[:inc_words]
|
46
|
+
ignore_case = args[:ignore_case]
|
47
|
+
|
48
|
+
unless words.empty?
|
49
|
+
file_list.select! do |f|
|
50
|
+
words.any? do |w|
|
51
|
+
if ignore_case
|
52
|
+
/#{w}/i =~ File.basename(f)
|
53
|
+
else
|
54
|
+
/#{w}/ =~ File.basename(f)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
41
59
|
|
42
|
-
def take_any!(file_list, words)
|
43
|
-
# need to take only the filename
|
44
|
-
file_list.select! { |f| words.any? { |w| /#{w}/ =~ File.basename(f) } } unless words.empty?
|
45
60
|
file_list
|
46
61
|
end
|
47
62
|
|
48
|
-
def drop_any!(file_list,
|
49
|
-
|
63
|
+
def drop_any!(file_list, args = {})
|
64
|
+
words = args[:exc_words]
|
65
|
+
ignore_case = args[:ignore_case]
|
66
|
+
|
67
|
+
unless words.empty?
|
68
|
+
file_list.delete_if do |f|
|
69
|
+
words.any? do |w|
|
70
|
+
if ignore_case
|
71
|
+
/#{w}/i =~ File.basename(f)
|
72
|
+
else
|
73
|
+
/#{w}/ =~ File.basename(f)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
50
79
|
file_list
|
51
80
|
end
|
52
81
|
|
data/lib/code_lister/main.rb
CHANGED
@@ -13,7 +13,8 @@ module CodeLister
|
|
13
13
|
|
14
14
|
files = CodeLister.filter(files, inc_words: inc_words,
|
15
15
|
exc_words: exc_words)
|
16
|
-
|
16
|
+
|
17
|
+
# Note: for now just print out the list of files
|
17
18
|
puts files
|
18
19
|
|
19
20
|
files
|
@@ -25,6 +26,7 @@ module CodeLister
|
|
25
26
|
options = {
|
26
27
|
base_dir: Dir.pwd,
|
27
28
|
recursive: false,
|
29
|
+
ignore_case: true,
|
28
30
|
inc_words: [],
|
29
31
|
exc_words: [],
|
30
32
|
exts: []
|
data/lib/code_lister/version.rb
CHANGED
@@ -39,9 +39,19 @@ describe CodeLister do
|
|
39
39
|
|
40
40
|
it "filters out the words that we don't want" do
|
41
41
|
expect(CodeLister.filter(file_list, exc_words: %w(demo1))).to eq ["spec/fixtures/demo2.xxx.rb"]
|
42
|
-
|
43
42
|
expect(CodeLister.filter(file_list, exc_words: %w(xxx))).to eq []
|
44
43
|
end
|
44
|
+
|
45
|
+
context 'with ignore_case' do
|
46
|
+
it 'ignores case by default' do
|
47
|
+
expect(CodeLister.filter(file_list, exc_words: %w(DeMo1))).to eq CodeLister.filter(file_list, exc_words: %w(demo1))
|
48
|
+
expect(CodeLister.filter(file_list, exc_words: %w(DeMo1))).to eq ["spec/fixtures/demo2.xxx.rb"]
|
49
|
+
end
|
50
|
+
it 'does not ignore case if specified' do
|
51
|
+
expect(CodeLister.filter(file_list, exc_words: %w(DeMo1), ignore_case: false)).to eq ["spec/fixtures/demo1.xxx.rb",
|
52
|
+
"spec/fixtures/demo2.xxx.rb" ]
|
53
|
+
end
|
54
|
+
end
|
45
55
|
end
|
46
56
|
|
47
57
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# file: spec/fixtures/demo1.yyy.rb
|
@@ -0,0 +1 @@
|
|
1
|
+
# file: spec/fixtures/demo2.yyy.rb
|
@@ -1 +1 @@
|
|
1
|
-
// file: test/fixtures/java/demo3.java
|
1
|
+
// file: test/fixtures/java/demo3.xxx.java
|
@@ -0,0 +1 @@
|
|
1
|
+
// file: test/fixtures/java/demo3.yyy.java
|
@@ -1 +1 @@
|
|
1
|
-
// file: test/fixtures/java/demo4.java
|
1
|
+
// file: test/fixtures/java/demo4.xxx.java
|
@@ -0,0 +1 @@
|
|
1
|
+
// file: test/fixtures/java/demo4.yyy.java
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: code_lister
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Burin Choomnuan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -149,9 +149,13 @@ files:
|
|
149
149
|
- lib/code_lister/version.rb
|
150
150
|
- spec/code_lister/code_lister_spec.rb
|
151
151
|
- spec/fixtures/demo1.xxx.rb
|
152
|
+
- spec/fixtures/demo1.yyy.rb
|
152
153
|
- spec/fixtures/demo2.xxx.rb
|
154
|
+
- spec/fixtures/demo2.yyy.rb
|
153
155
|
- spec/fixtures/java/demo3.xxx.java
|
156
|
+
- spec/fixtures/java/demo3.yyy.java
|
154
157
|
- spec/fixtures/java/demo4.xxx.java
|
158
|
+
- spec/fixtures/java/demo4.yyy.java
|
155
159
|
- spec/spec_helper.rb
|
156
160
|
homepage: https://github.com/agilecreativity/code_lister
|
157
161
|
licenses:
|
@@ -180,8 +184,12 @@ summary: Search, filter files easily using the power of ruby
|
|
180
184
|
test_files:
|
181
185
|
- spec/code_lister/code_lister_spec.rb
|
182
186
|
- spec/fixtures/demo1.xxx.rb
|
187
|
+
- spec/fixtures/demo1.yyy.rb
|
183
188
|
- spec/fixtures/demo2.xxx.rb
|
189
|
+
- spec/fixtures/demo2.yyy.rb
|
184
190
|
- spec/fixtures/java/demo3.xxx.java
|
191
|
+
- spec/fixtures/java/demo3.yyy.java
|
185
192
|
- spec/fixtures/java/demo4.xxx.java
|
193
|
+
- spec/fixtures/java/demo4.yyy.java
|
186
194
|
- spec/spec_helper.rb
|
187
195
|
has_rdoc:
|