treevisitor 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/examples/directory_without_subdirectory.rb +35 -0
- data/lib/treevisitor/cli/cli_tree.rb +2 -2
- data/lib/treevisitor/dir_tree_walker.rb +38 -37
- data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +1 -1
- data/lib/treevisitor.rb +0 -1
- data/spec/spec_helper.rb +1 -3
- data/spec/treevisitor/cli/cli_tree_spec.rb +7 -7
- data/spec/treevisitor/dir_processor_spec.rb +1 -1
- data/spec/treevisitor/dir_tree_walker_spec.rb +19 -2
- metadata +4 -3
data/VERSION.yml
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "lib" ) )
|
2
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
3
|
+
require 'treevisitor'
|
4
|
+
|
5
|
+
require 'ostruct'
|
6
|
+
#
|
7
|
+
# Find directory without subdirectories
|
8
|
+
#
|
9
|
+
class DirWithoutSubDir < TreeVisitor::TreeNodeVisitor
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super
|
13
|
+
@stack = []
|
14
|
+
@nr = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def enter_tree_node( pathname )
|
18
|
+
@nr += 1
|
19
|
+
info = OpenStruct.new(:nr => @nr, :pathname => pathname)
|
20
|
+
@stack.push( info )
|
21
|
+
end
|
22
|
+
|
23
|
+
def exit_tree_node( pathname )
|
24
|
+
info = @stack.pop
|
25
|
+
if info.nr == @nr
|
26
|
+
puts "leaf: #{pathname}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
dtw = TreeVisitor::DirTreeWalker.new( ".." )
|
33
|
+
dtw.ignore /^\./
|
34
|
+
dtw.visit_file=false
|
35
|
+
dtw.run( DirWithoutSubDir.new )
|
@@ -78,7 +78,7 @@ module TreeVisitor
|
|
78
78
|
dtw.ignore(/^\.[^.]+/) # ignore all file starting with "."
|
79
79
|
end
|
80
80
|
|
81
|
-
dtw.
|
81
|
+
dtw.visit_file = !options[:only_directories]
|
82
82
|
|
83
83
|
case options[:algo]
|
84
84
|
when 'build'
|
@@ -91,7 +91,7 @@ module TreeVisitor
|
|
91
91
|
visitor = PrintDirTreeVisitor.new
|
92
92
|
dtw.run( visitor )
|
93
93
|
end
|
94
|
-
|
94
|
+
0
|
95
95
|
end
|
96
96
|
|
97
97
|
end
|
@@ -9,26 +9,26 @@ module TreeVisitor
|
|
9
9
|
#
|
10
10
|
# @param [String] dirname the root of the tree (top level directory)
|
11
11
|
#
|
12
|
-
def initialize(
|
12
|
+
def initialize(dirname)
|
13
13
|
@dirname = dirname
|
14
|
-
unless File.directory?(
|
14
|
+
unless File.directory?(dirname)
|
15
15
|
raise "#{dirname} is not a directory!"
|
16
16
|
end
|
17
17
|
|
18
|
-
@visitor
|
18
|
+
@visitor = nil
|
19
19
|
|
20
20
|
#
|
21
21
|
# pattern
|
22
22
|
#
|
23
|
-
@ignore_dir_patterns
|
23
|
+
@ignore_dir_patterns = []
|
24
24
|
@ignore_file_patterns = []
|
25
25
|
|
26
|
-
@match_file_patterns
|
26
|
+
@match_file_patterns = []
|
27
27
|
|
28
28
|
#
|
29
29
|
# options
|
30
30
|
#
|
31
|
-
@
|
31
|
+
@visit_file = true
|
32
32
|
end
|
33
33
|
|
34
34
|
##########################################################################
|
@@ -42,22 +42,25 @@ module TreeVisitor
|
|
42
42
|
def ignore(pattern)
|
43
43
|
@ignore_dir_patterns << pattern
|
44
44
|
@ignore_file_patterns << pattern
|
45
|
+
self
|
45
46
|
end
|
46
47
|
|
47
48
|
#
|
48
49
|
# Ignore a directory (Tree) matching pattern
|
49
50
|
# @param [RegEx] pattern
|
50
51
|
#
|
51
|
-
def ignore_dir(
|
52
|
+
def ignore_dir(pattern)
|
52
53
|
@ignore_dir_patterns << pattern
|
54
|
+
self
|
53
55
|
end
|
54
56
|
|
55
57
|
#
|
56
58
|
# Ignore a file (Leaf) matching pattern
|
57
59
|
# @param [RegEx] pattern
|
58
60
|
#
|
59
|
-
def ignore_file(
|
61
|
+
def ignore_file(pattern)
|
60
62
|
@ignore_file_patterns << pattern
|
63
|
+
self
|
61
64
|
end
|
62
65
|
|
63
66
|
#
|
@@ -66,29 +69,29 @@ module TreeVisitor
|
|
66
69
|
#
|
67
70
|
# @param [RegEx] pattern
|
68
71
|
#
|
69
|
-
def match(
|
72
|
+
def match(pattern)
|
70
73
|
@match_file_patterns << pattern
|
74
|
+
self
|
71
75
|
end
|
72
76
|
|
73
77
|
##########################################################################
|
74
78
|
# Options
|
75
79
|
|
76
80
|
#
|
77
|
-
#
|
81
|
+
# it is true to visit file
|
78
82
|
#
|
79
|
-
attr_accessor :
|
83
|
+
attr_accessor :visit_file
|
80
84
|
|
81
85
|
##########################################################################
|
82
86
|
|
83
|
-
|
84
87
|
#
|
85
88
|
# Test directory ignore pattern
|
86
89
|
#
|
87
90
|
# @param [String] directory name
|
88
91
|
# @return [boolean] if dirname match almost one pattern
|
89
92
|
#
|
90
|
-
def ignore_dir?(
|
91
|
-
_include?(
|
93
|
+
def ignore_dir?(dirname)
|
94
|
+
_include?(@ignore_dir_patterns, File.basename(dirname))
|
92
95
|
end
|
93
96
|
|
94
97
|
#
|
@@ -97,8 +100,8 @@ module TreeVisitor
|
|
97
100
|
# @param [String] file name
|
98
101
|
# @return [boolean] if filename match almost one pattern
|
99
102
|
#
|
100
|
-
def ignore_file?(
|
101
|
-
_include?(
|
103
|
+
def ignore_file?(filename)
|
104
|
+
_include?(@ignore_file_patterns, File.basename(filename))
|
102
105
|
end
|
103
106
|
|
104
107
|
#
|
@@ -107,19 +110,20 @@ module TreeVisitor
|
|
107
110
|
# @param [String] file name
|
108
111
|
# @return [boolean] if filename match almost one pattern
|
109
112
|
#
|
110
|
-
def match?(
|
113
|
+
def match?(filename)
|
111
114
|
return true if @match_file_patterns.empty?
|
112
|
-
_include?(
|
115
|
+
_include?(@match_file_patterns, File.basename(filename))
|
113
116
|
end
|
114
117
|
|
115
118
|
#
|
116
119
|
# Run the visitor through the directory tree
|
117
120
|
#
|
118
|
-
#
|
121
|
+
# @param [TreeNodeVisitor]
|
122
|
+
# @return [TreeNodeVisitor] the visitor
|
119
123
|
#
|
120
|
-
def run(
|
124
|
+
def run(tree_node_visitor)
|
121
125
|
@visitor = tree_node_visitor
|
122
|
-
process_directory(
|
126
|
+
process_directory(File.expand_path(@dirname))
|
123
127
|
tree_node_visitor
|
124
128
|
end
|
125
129
|
|
@@ -127,9 +131,9 @@ module TreeVisitor
|
|
127
131
|
|
128
132
|
def _include?(patterns, basename)
|
129
133
|
# return false if the patters.empty?
|
130
|
-
patterns.find{ |pattern|
|
131
|
-
if pattern.
|
132
|
-
pattern.match(
|
134
|
+
patterns.find { |pattern|
|
135
|
+
if pattern.kind_of? Regexp
|
136
|
+
pattern.match(basename)
|
133
137
|
else
|
134
138
|
basename == pattern
|
135
139
|
end
|
@@ -139,26 +143,23 @@ module TreeVisitor
|
|
139
143
|
#
|
140
144
|
# recurse on other directories
|
141
145
|
#
|
142
|
-
def process_directory(
|
143
|
-
@visitor.enter_tree_node(
|
146
|
+
def process_directory(dirname)
|
147
|
+
@visitor.enter_tree_node(dirname)
|
144
148
|
# return if ignore_dir?( dirname )
|
145
149
|
|
146
|
-
Dir.entries(
|
147
|
-
next if basename == "." or basename == ".."
|
148
|
-
pathname = File.join(
|
150
|
+
Dir.entries(dirname).sort.each { |basename|
|
151
|
+
next if basename == "." or basename == ".." # ignore always "." and ".."
|
152
|
+
pathname = File.join(dirname, basename)
|
149
153
|
|
150
|
-
if File.directory?(
|
151
|
-
|
152
|
-
process_directory( pathname ) unless ignore_dir?( basename )
|
154
|
+
if File.directory?(pathname)
|
155
|
+
process_directory(pathname) unless ignore_dir?(basename)
|
153
156
|
else
|
154
|
-
if
|
155
|
-
|
156
|
-
@visitor.visit_leaf_node( pathname )
|
157
|
-
end
|
157
|
+
if !!@visit_file && match?(basename) && !ignore_file?(basename)
|
158
|
+
@visitor.visit_leaf_node(pathname)
|
158
159
|
end
|
159
160
|
end
|
160
161
|
}
|
161
|
-
@visitor.exit_tree_node(
|
162
|
+
@visitor.exit_tree_node(dirname)
|
162
163
|
end
|
163
164
|
end
|
164
165
|
end
|
data/lib/treevisitor.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -6,13 +6,11 @@ require 'treevisitor_cli'
|
|
6
6
|
|
7
7
|
include TreeVisitor
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
FIXTURES = File.expand_path( File.join( File.dirname(__FILE__), "fixtures" ) )
|
11
10
|
|
12
11
|
# Spec::Runner.configure do |config|
|
13
12
|
# end
|
14
13
|
|
15
|
-
|
16
14
|
require 'test/unit'
|
17
15
|
require "stringio"
|
18
16
|
|
@@ -16,32 +16,32 @@ describe CliTree do
|
|
16
16
|
args = %w{--version}
|
17
17
|
CliTree.new.parse_args(args)
|
18
18
|
end
|
19
|
-
version =
|
19
|
+
version = TreeVisitor.version
|
20
20
|
out.should match version
|
21
21
|
end
|
22
22
|
|
23
23
|
it "should accepts switch -d (directories only)" do
|
24
24
|
out = with_stdout_captured do
|
25
25
|
args = %w{-d}
|
26
|
-
args <<
|
26
|
+
args << File.join(FIXTURES, "test_dir")
|
27
27
|
CliTree.new.parse_args(args)
|
28
28
|
end
|
29
|
-
#
|
29
|
+
# puts out
|
30
30
|
out.split("\n").length.should == 6
|
31
31
|
|
32
32
|
out = with_stdout_captured do
|
33
33
|
args = %w{-da}
|
34
|
-
args <<
|
34
|
+
args << File.join(FIXTURES, "test_dir")
|
35
35
|
CliTree.new.parse_args(args)
|
36
36
|
end
|
37
|
-
#puts out
|
37
|
+
# puts out
|
38
38
|
out.split("\n").length.should == 7
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should accepts switch -a (all files)" do
|
42
42
|
out = with_stdout_captured do
|
43
43
|
args = %w{-a}
|
44
|
-
args <<
|
44
|
+
args << File.join(FIXTURES, "test_dir")
|
45
45
|
CliTree.new.parse_args(args)
|
46
46
|
end
|
47
47
|
# pp out
|
@@ -49,7 +49,7 @@ describe CliTree do
|
|
49
49
|
|
50
50
|
out = with_stdout_captured do
|
51
51
|
args = []
|
52
|
-
args <<
|
52
|
+
args << File.join(FIXTURES, "test_dir")
|
53
53
|
CliTree.new.parse_args(args)
|
54
54
|
end
|
55
55
|
# puts out
|
@@ -4,8 +4,7 @@ require File.join(File.dirname(__FILE__), "..", "spec_helper")
|
|
4
4
|
describe DirTreeWalker do
|
5
5
|
|
6
6
|
it "should accumulate file names" do
|
7
|
-
dir_tree_walker = DirTreeWalker.new(
|
8
|
-
dir_tree_walker.ignore_dir( ".svn" )
|
7
|
+
dir_tree_walker = DirTreeWalker.new( File.join(FIXTURES, "test_dir") )
|
9
8
|
|
10
9
|
accumulator = []
|
11
10
|
visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename( pathname ) }
|
@@ -14,6 +13,17 @@ describe DirTreeWalker do
|
|
14
13
|
accumulator.sort.should == %w{ test_dir dir.1 dir.1.2 file.1.2.1 file.1.1 dir.2 file.2.1 .dir_with_dot dummy.txt}.sort
|
15
14
|
end
|
16
15
|
|
16
|
+
it "should accumulate file names 2" do
|
17
|
+
dir_tree_walker = DirTreeWalker.new( File.join( FIXTURES, "test_dir_2" ) )
|
18
|
+
dir_tree_walker.ignore( "sub" )
|
19
|
+
|
20
|
+
accumulator = []
|
21
|
+
visitor = BlockTreeNodeVisitor.new { |pathname| accumulator << File.basename( pathname ) }
|
22
|
+
dir_tree_walker.run( visitor )
|
23
|
+
# accumulator.length.should == 4
|
24
|
+
accumulator.sort.should == %w{ [Dsube] test_dir_2}.sort
|
25
|
+
end
|
26
|
+
|
17
27
|
it "should ignore files" do
|
18
28
|
dtp = DirTreeWalker.new( "." )
|
19
29
|
|
@@ -30,4 +40,11 @@ describe DirTreeWalker do
|
|
30
40
|
dtp.ignore_file?( "xvpics" ).should be_true
|
31
41
|
end
|
32
42
|
|
43
|
+
it "should ignore files 2" do
|
44
|
+
dtp = DirTreeWalker.new( "." )
|
45
|
+
|
46
|
+
dtp.ignore("sub")
|
47
|
+
dtp.ignore_file?( "[Dsube]" ).should be_false
|
48
|
+
dtp.ignore_dir?( "[Dsube]" ).should be_false
|
49
|
+
end
|
33
50
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Tokiro
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-05 00:00:00 +01:00
|
18
18
|
default_executable: tree.rb
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- README.rdoc
|
109
109
|
- Rakefile
|
110
110
|
- VERSION.yml
|
111
|
+
- examples/directory_without_subdirectory.rb
|
111
112
|
- examples/find_files.rb
|
112
113
|
- examples/print_files.rb
|
113
114
|
- lib/tree_visitor.rb
|