tree.rb 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gemtest +0 -0
- data/LICENSE.txt +20 -0
- data/README.md +160 -0
- data/Rakefile +12 -0
- data/bin/tree.rb +9 -0
- data/examples/directory_walker/directory_without_subdirectory.rb +37 -0
- data/examples/directory_walker/find_files.rb +18 -0
- data/examples/directory_walker/print_files.rb +12 -0
- data/examples/protovis/directory_to_json_visitor.rb +15 -0
- data/examples/protovis/index.html +87 -0
- data/examples/protovis/protovis-r3.2.js +277 -0
- data/examples/protovis/treevisitor.js +33 -0
- data/examples/protovis/treevisitor.png +0 -0
- data/lib/tree_visitor.rb +2 -0
- data/lib/treevisitor/abs_node.rb +144 -0
- data/lib/treevisitor/basic_tree_node_visitor.rb +44 -0
- data/lib/treevisitor/cli/cli_tree.rb +121 -0
- data/lib/treevisitor/directory_walker.rb +300 -0
- data/lib/treevisitor/leaf_node.rb +33 -0
- data/lib/treevisitor/tree_node.rb +296 -0
- data/lib/treevisitor/tree_node_visitor.rb +120 -0
- data/lib/treevisitor/util/dir_processor.rb +46 -0
- data/lib/treevisitor/version.rb +4 -0
- data/lib/treevisitor/visitors/block_tree_node_visitor.rb +21 -0
- data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +57 -0
- data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +48 -0
- data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +39 -0
- data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +27 -0
- data/lib/treevisitor/visitors/directory_to_hash_visitor.rb +33 -0
- data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +20 -0
- data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +21 -0
- data/lib/treevisitor/visitors/print_tree_node_visitor.rb +40 -0
- data/lib/treevisitor.rb +40 -0
- data/lib/treevisitor_cli.rb +7 -0
- data/spec/fixtures/test_dir_1/.dir_with_dot/dummy.txt +0 -0
- data/spec/fixtures/test_dir_1/dir.1/dir.1.2/file.1.2.1 +0 -0
- data/spec/fixtures/test_dir_1/dir.1/file.1.1 +0 -0
- data/spec/fixtures/test_dir_1/dir.2/file.2.1 +0 -0
- data/spec/fixtures/test_dir_2/[Dsube]/sub/.gitkeep +0 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/treevisitor/cli/cli_tree_spec.rb +69 -0
- data/spec/treevisitor/directory_walker_spec.rb +99 -0
- data/spec/treevisitor/tree_dsl_spec.rb +57 -0
- data/spec/treevisitor/tree_dsl_with_derived_class1_spec.rb +53 -0
- data/spec/treevisitor/tree_dsl_with_derived_class_spec.rb +51 -0
- data/spec/treevisitor/tree_node_paths_spec.rb +70 -0
- data/spec/treevisitor/tree_node_spec.rb +135 -0
- data/spec/treevisitor/tree_node_visitor_delegate_spec.rb +35 -0
- data/spec/treevisitor/tree_node_visitor_dsl_spec.rb +66 -0
- data/spec/treevisitor/util/dir_processor_spec.rb +13 -0
- data/spec/treevisitor/visitors/block_tree_node_visitor_spec.rb +25 -0
- data/spec/treevisitor/visitors/callback_tree_node_visitor2_spec.rb +38 -0
- data/spec/treevisitor/visitors/depth_tree_node_visitor_spec.rb +28 -0
- data/spec/treevisitor/visitors/tree_node_visitors_spec.rb +27 -0
- data/tasks/rspec.rake +34 -0
- data/tasks/yard.rake +36 -0
- data/tree.rb.gemspec +70 -0
- metadata +231 -0
data/.gemtest
ADDED
File without changes
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2010 tokiro.oyama@gmail.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
tree.rb / tree visitor library
|
2
|
+
================================================
|
3
|
+
|
4
|
+
**tree.rb** is a 'clone' of tree unix command.
|
5
|
+
The gem contains also a library to build tree with a dsl (domain specific language), and
|
6
|
+
an implementation of visitor design pattern.
|
7
|
+
|
8
|
+
## Usage as command line
|
9
|
+
|
10
|
+
To use the command line tool tree.rb execute into command line tree.rb --help
|
11
|
+
Try tree.rb -h to get help on command line.
|
12
|
+
|
13
|
+
<pre>
|
14
|
+
$ tree.rb --help
|
15
|
+
Usage: tree.rb [options] [directory]
|
16
|
+
list contents of directories in a tree-like format
|
17
|
+
this is a almost :-) a clone of tree unix command written in ruby
|
18
|
+
Code https://github.com/tokiro/treevisitor. Feedback to tokiro.oyama@gmail.com
|
19
|
+
|
20
|
+
options:
|
21
|
+
-h, --help Show this message
|
22
|
+
--version Show the version
|
23
|
+
-a All file are listed
|
24
|
+
-d List directories only
|
25
|
+
-v, --[no-]verbose Run verbosely
|
26
|
+
-q, --quiet quiet mode as --no-verbose
|
27
|
+
-f, --format ALGO select an algo
|
28
|
+
(b,v,j,y,build-dir,print-dir,json,yaml)
|
29
|
+
</pre>
|
30
|
+
|
31
|
+
To print the tree of a directory execute tree.rb with the name of the directory.
|
32
|
+
For example:
|
33
|
+
|
34
|
+
<pre>
|
35
|
+
$ tree.rb lib
|
36
|
+
|
37
|
+
lib
|
38
|
+
|-- tree_visitor.rb
|
39
|
+
|-- treevisitor.rb
|
40
|
+
|-- treevisitor_cli.rb
|
41
|
+
`-- treevisitor
|
42
|
+
|-- abs_node.rb
|
43
|
+
|-- dir_processor.rb
|
44
|
+
|-- dir_tree_walker.rb
|
45
|
+
|-- leaf_node.rb
|
46
|
+
|-- tree_node.rb
|
47
|
+
|-- tree_node_visitor.rb
|
48
|
+
|-- cli
|
49
|
+
| `-- cli_tree.rb
|
50
|
+
`-- visitors
|
51
|
+
|-- block_tree_node_visitor.rb
|
52
|
+
|-- build_dir_tree_visitor.rb
|
53
|
+
|-- callback_tree_node_visitor2.rb
|
54
|
+
|-- clone_tree_node_visitor.rb
|
55
|
+
|-- depth_tree_node_visitor.rb
|
56
|
+
|-- flat_print_tree_node_visitors.rb
|
57
|
+
|-- print_dir_tree_visitor.rb
|
58
|
+
`-- print_tree_node_visitor.rb
|
59
|
+
</pre>
|
60
|
+
|
61
|
+
The **tree.rb** can generate json structure suitable to be visualized with [protovis][1] javascript library.
|
62
|
+
See the examples directory.
|
63
|
+
|
64
|
+
<pre>
|
65
|
+
$ ruby bin/tree.rb --format json lib
|
66
|
+
|
67
|
+
{
|
68
|
+
"lib": {
|
69
|
+
"tree_visitor.rb": 45,
|
70
|
+
"treevisitor": {
|
71
|
+
"abs_node.rb": 2858,
|
72
|
+
"basic_tree_node_visitor.rb": 578,
|
73
|
+
"cli": {
|
74
|
+
"cli_tree.rb": 2929
|
75
|
+
},
|
76
|
+
"directory_walker.rb": 5049,
|
77
|
+
"leaf_node.rb": 526,
|
78
|
+
"tree_node.rb": 5665,
|
79
|
+
"tree_node_visitor.rb": 1263,
|
80
|
+
"util": {
|
81
|
+
"dir_processor.rb": 928
|
82
|
+
},
|
83
|
+
"visitors": {
|
84
|
+
"block_tree_node_visitor.rb": 386,
|
85
|
+
"build_dir_tree_visitor.rb": 1167,
|
86
|
+
"callback_tree_node_visitor2.rb": 1652,
|
87
|
+
"clone_tree_node_visitor.rb": 837,
|
88
|
+
"depth_tree_node_visitor.rb": 427,
|
89
|
+
"directory_to_hash_visitor.rb": 639,
|
90
|
+
"flat_print_tree_node_visitors.rb": 341,
|
91
|
+
"print_dir_tree_visitor.rb": 387,
|
92
|
+
"print_tree_node_visitor.rb": 707
|
93
|
+
}
|
94
|
+
},
|
95
|
+
"treevisitor.rb": 976,
|
96
|
+
"treevisitor_cli.rb": 87
|
97
|
+
}
|
98
|
+
</pre>
|
99
|
+
|
100
|
+
The json structure can be transformed into image:
|
101
|
+
<img src='https://github.com/tokiro/tree.rb/raw/master/examples/protovis/treevisitor.png'/>
|
102
|
+
|
103
|
+
## Usage as library
|
104
|
+
See examples directory to use treevisitor in your code.
|
105
|
+
|
106
|
+
DSL to build tree:
|
107
|
+
<pre>
|
108
|
+
tree = TreeNode.create do
|
109
|
+
node "root" do
|
110
|
+
leaf "l1"
|
111
|
+
node "sub" do
|
112
|
+
leaf "l3"
|
113
|
+
end
|
114
|
+
node "wo leaves"
|
115
|
+
end
|
116
|
+
</pre>
|
117
|
+
|
118
|
+
## Documentation
|
119
|
+
|
120
|
+
http://rubydoc.info/gems/tree.rb
|
121
|
+
|
122
|
+
|
123
|
+
## Installation
|
124
|
+
|
125
|
+
To use the command line tool tree.rb, it is enough to install the gem:
|
126
|
+
|
127
|
+
[sudo] gem install tree.rb
|
128
|
+
|
129
|
+
To use into your project, add this line to your application's Gemfile:
|
130
|
+
|
131
|
+
gem 'tree.rb'
|
132
|
+
|
133
|
+
And then execute:
|
134
|
+
|
135
|
+
$ bundle
|
136
|
+
|
137
|
+
## Contributing
|
138
|
+
|
139
|
+
Everybody is welcome to contribute to this project by commenting the source code, suggesting modifications or new ideas,
|
140
|
+
reporting bugs, writing some documentation and, of course, you're also welcome to contribute with patches as well!
|
141
|
+
|
142
|
+
To contribute:
|
143
|
+
|
144
|
+
1. Fork it
|
145
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
146
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
147
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
148
|
+
5. Create new Pull Request
|
149
|
+
|
150
|
+
### Rubies
|
151
|
+
|
152
|
+
This gem have been tested on [MRI][8] 1.9.3
|
153
|
+
|
154
|
+
### Copyright
|
155
|
+
|
156
|
+
Copyright (c) 2009-2012 tokiro.oyama@gmail.com. See LICENSE for details.
|
157
|
+
|
158
|
+
[1]: http://vis.stanford.edu/protovis/
|
159
|
+
[8]: http://www.ruby-lang.org/en/
|
160
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# -*- encoding: utf-8; mode: ruby -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
|
5
|
+
require 'bundler'
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
# Load tasks
|
9
|
+
Dir.glob('tasks/**/*.rake').each { |r| Rake.application.add_import r }
|
10
|
+
|
11
|
+
task :test => :spec
|
12
|
+
task :default => :spec
|
data/bin/tree.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
5
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
6
|
+
require 'treevisitor'
|
7
|
+
|
8
|
+
#
|
9
|
+
# Find directories without subdirectories
|
10
|
+
#
|
11
|
+
class DirWithoutSubDir < TreeVisitor::BasicTreeNodeVisitor
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
@stack = []
|
16
|
+
@nr = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
def enter_node( pathname )
|
20
|
+
@nr += 1
|
21
|
+
info = OpenStruct.new(:nr => @nr, :pathname => pathname)
|
22
|
+
@stack.push( info )
|
23
|
+
end
|
24
|
+
|
25
|
+
def exit_node( pathname )
|
26
|
+
info = @stack.pop
|
27
|
+
if info.nr == @nr
|
28
|
+
puts "leaf: #{pathname}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
dtw = TreeVisitor::DirTreeWalker.new( ".." )
|
35
|
+
dtw.ignore /^\./
|
36
|
+
dtw.visit_file=false
|
37
|
+
dtw.run( DirWithoutSubDir.new )
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
4
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
5
|
+
require 'treevisitor'
|
6
|
+
include TreeVisitor
|
7
|
+
|
8
|
+
class MyVisitor < BasicTreeNodeVisitor
|
9
|
+
def visit_leaf( pathname )
|
10
|
+
puts pathname
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
dtw = DirTreeWalker.new( File.join(File.dirname(__FILE__), "..", ".." ) )
|
15
|
+
dtw.match "leaf_node.rb"
|
16
|
+
dtw.match "abs_node.rb"
|
17
|
+
dtw.run( MyVisitor.new )
|
18
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
cwd = File.expand_path( File.join( File.dirname(__FILE__), "..", "..", "lib" ) )
|
3
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
4
|
+
require 'treevisitor'
|
5
|
+
include TreeVisitor
|
6
|
+
|
7
|
+
dtw = DirTreeWalker.new( :ignore => ".git" )
|
8
|
+
dtw.run ".." do
|
9
|
+
on_leaf do |pathname|
|
10
|
+
puts pathname
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
|
5
|
+
cwd = File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "lib"))
|
6
|
+
$:.unshift(cwd) unless $:.include?(cwd)
|
7
|
+
require 'treevisitor'
|
8
|
+
include TreeVisitor
|
9
|
+
|
10
|
+
dir = File.expand_path( File.join("..", "..", "lib") )
|
11
|
+
|
12
|
+
dtw = TreeVisitor::DirTreeWalker.new( :ignore => [/^\./, "doc", "pkg"] )
|
13
|
+
root = dtw.run(dir, DirectoryToHashVisitor.new(dir)).root
|
14
|
+
str = "var treevisitor = #{JSON.pretty_generate(root)};"
|
15
|
+
File.open( File.dirname(__FILE__) + "/treevisitor.js", "w"){ |f| f.write str }
|
@@ -0,0 +1,87 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<title>Indented Tree</title>
|
4
|
+
<script type="text/javascript" src="protovis-r3.2.js"></script>
|
5
|
+
<script type="text/javascript" src="treevisitor.js"></script>
|
6
|
+
|
7
|
+
<style type="text/css">
|
8
|
+
body {
|
9
|
+
margin: 0;
|
10
|
+
display: table;
|
11
|
+
height: 100%;
|
12
|
+
width: 100%;
|
13
|
+
font: 14px/134% Helvetica Neue, sans-serif;
|
14
|
+
}
|
15
|
+
|
16
|
+
#center {
|
17
|
+
display: table-cell;
|
18
|
+
vertical-align: middle;
|
19
|
+
}
|
20
|
+
|
21
|
+
#fig {
|
22
|
+
position: relative;
|
23
|
+
margin: auto;
|
24
|
+
}
|
25
|
+
|
26
|
+
</style>
|
27
|
+
</head>
|
28
|
+
<body>
|
29
|
+
|
30
|
+
|
31
|
+
<script type="text/javascript+protovis">
|
32
|
+
|
33
|
+
for (first in treevisitor) break;
|
34
|
+
|
35
|
+
var root = pv.dom(treevisitor)
|
36
|
+
.root(first)
|
37
|
+
.sort(function(a, b) pv.naturalOrder(a.nodeName, b.nodeName));
|
38
|
+
|
39
|
+
/* Recursively compute the package sizes. */
|
40
|
+
root.visitAfter(function(n) {
|
41
|
+
if (n.firstChild) {
|
42
|
+
n.nodeValue = pv.sum(n.childNodes, function(n) n.nodeValue);
|
43
|
+
}
|
44
|
+
});
|
45
|
+
|
46
|
+
var vis = new pv.Panel()
|
47
|
+
.width(400)
|
48
|
+
.height(function() (root.nodes().length + 1) * 12)
|
49
|
+
.margin(5);
|
50
|
+
|
51
|
+
var layout = vis.add(pv.Layout.Indent)
|
52
|
+
.nodes(function() root.nodes())
|
53
|
+
.depth(25)
|
54
|
+
.breadth(12);
|
55
|
+
|
56
|
+
layout.link.add(pv.Line);
|
57
|
+
|
58
|
+
var node = layout.node.add(pv.Panel)
|
59
|
+
.top(function(n) n.y - 6)
|
60
|
+
.height(12)
|
61
|
+
.right(6)
|
62
|
+
.strokeStyle(null)
|
63
|
+
.events("all")
|
64
|
+
.event("mousedown", toggle);
|
65
|
+
|
66
|
+
node.anchor("left").add(pv.Dot)
|
67
|
+
.strokeStyle("#1f77b4")
|
68
|
+
.fillStyle(function(n) n.toggled ? "#1f77b4" : n.firstChild ? "#aec7e8" : "#ff7f0e")
|
69
|
+
.title(function t(d) d.parentNode ? (t(d.parentNode) + "." + d.nodeName) : d.nodeName)
|
70
|
+
.anchor("right").add(pv.Label)
|
71
|
+
.text(function(n) n.nodeName);
|
72
|
+
|
73
|
+
node.anchor("right").add(pv.Label)
|
74
|
+
.textStyle(function(n) n.firstChild || n.toggled ? "#aaa" : "#000")
|
75
|
+
.text(function(n) (n.nodeValue >> 10) + "KB");
|
76
|
+
|
77
|
+
vis.render();
|
78
|
+
|
79
|
+
/* Toggles the selected node, then updates the layout. */
|
80
|
+
function toggle(n) {
|
81
|
+
n.toggle(pv.event.altKey);
|
82
|
+
return layout.reset().root;
|
83
|
+
}
|
84
|
+
|
85
|
+
</script>
|
86
|
+
</body>
|
87
|
+
</html>
|