dir_friend 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c521ee35292eca17bf8f359334ff82317ff9c1c6
4
- data.tar.gz: 47802dbb3d9f0bd4196e3548bbc9de2003f4d883
3
+ metadata.gz: 686edf0244b614d5c5ed36dd54cc5e156a4bfdfc
4
+ data.tar.gz: cd4f54a0c1d265a811d657ea8ec9ebd92e2cf562
5
5
  SHA512:
6
- metadata.gz: 271eab6489bfbfc6dc8bd3f01ee10fbcf86d8480c32bd9e0493a8c7d4763ad9ba47968ecd0db64f7a318d6c13ec3c8580bdf5ff9524bca163f47f9deffb6665e
7
- data.tar.gz: 82d14d0bef8ca232e4130f50d39831750d96d61e559342b692289c53c87c648bc8a6a927940c13c3fcbc3043d94b38104e7c871c3c1fea6c7ac868370962ba7a
6
+ metadata.gz: 55d033751543bd9db638f3c680a7845b07af5481f7a3b18881742150129ec61db7bbe8e34cdbfac3551f7c012180bee47c6132ed7b92b66270082c4f038825c7
7
+ data.tar.gz: b447ba2ff5418c2d04933841b73697c4c9960eb8f518b8237e7d18db18806619bd48bd13d6e201a9743224a13dcf915f93acda11a27b1748dcadcb5ed7a8cc66
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # DirFriend
2
2
 
3
- `DirFriend` is a tool for visualizing file directory.
3
+ `DirFriend` is a tool for generating a DOT file which represent file directories.
4
4
 
5
5
  ## Installation
6
6
 
@@ -26,10 +26,15 @@ In your terminal, try followings;
26
26
  # Create a dot file for path/to/project
27
27
  % dir_friend dot path/to/project
28
28
 
29
- # Create with some options
29
+ # Create it with some options
30
30
  % dir_friend dot path/to/project -l fdp -c blues --dir_shape box
31
31
  % dir_friend dot path/to/project -g "bgcolor:azure,rkdir:LR,splines:ortho"
32
32
 
33
+ `dot` subcommand first creates `config.yaml` in 'YOUR_HOME_DIR/.dirfriend', which contains some pre-defined theme settings. You can call any of the themes with '--theme (or -t)' option for `dot` subcommand.
34
+
35
+ % dir_friend dot path/to/project --theme blueegg
36
+
37
+
33
38
  In your ruby script;
34
39
 
35
40
  ```ruby
data/lib/dir_friend.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  require 'gviz'
2
2
  require 'tempfile'
3
+ require 'fileutils'
3
4
 
4
- require "dir_friend/version"
5
- require "dir_friend/f"
6
- require "dir_friend/d"
7
- require "dir_friend/any"
8
- require "dir_friend/graph"
5
+ require 'dir_friend/version'
6
+ require 'dir_friend/core_ext'
7
+ require 'dir_friend/f'
8
+ require 'dir_friend/d'
9
+ require 'dir_friend/any'
10
+ require 'dir_friend/graph'
11
+ require 'dir_friend/config'
9
12
  require 'dir_friend/command'
10
13
 
11
14
  module DirFriend
@@ -1,4 +1,5 @@
1
1
  require 'thor'
2
+ require 'yaml'
2
3
 
3
4
  module DirFriend
4
5
  class Command < Thor
@@ -10,12 +11,12 @@ module DirFriend
10
11
  end
11
12
 
12
13
  desc "dot PATH", "Create a graphviz dot file for PATH"
13
- long_desc <<-EOS
14
- ex.
14
+ long_desc ~<<-EOS
15
+ ex.
15
16
 
16
- `dir_friend dot path/ -l fdp -c blues, -e "arrowhead:none"`
17
+ `dir_friend dot path/ -l fdp -c blues, -e "arrowhead:none"`
17
18
 
18
- `dir_friend dot path/ -c greens -g "bgcolor:azure,rankdir:LR,splines:ortho"`
19
+ `dir_friend dot path/ -c greens -g "bgcolor:azure,rankdir:LR,splines:ortho"`
19
20
  EOS
20
21
  option :layout, aliases:"-l"
21
22
  option :colorscheme, aliases:"-c"
@@ -27,13 +28,15 @@ ex.
27
28
  option :save, aliases:"-s", default:'a'
28
29
  option :depth, aliases:"-d", default:9
29
30
  option :with_open, aliases:"-o", default: true, type: :boolean
31
+ option :theme, aliases:"-t"
30
32
  def dot(path)
31
- opt = options.dup.inject({}) { |h, (k,v)| h[k.intern] = v; h }
33
+ opt = options.to_keysym_hash
32
34
  save_path = opt.delete(:save)
33
35
  opt = opt_parser(opt)
36
+
34
37
  dir = D.new(path, depth:options[:depth].to_i)
35
38
  dir.to_dot(opt).save(save_path)
36
- puts "Dot file created: `#{save_path}.dot`"
39
+ puts "'#{save_path}.dot' created in the current directory."
37
40
 
38
41
  if options[:with_open] && OS.mac?
39
42
  run(%Q{open "#{save_path}.dot"}, verbose: false)
@@ -67,7 +70,12 @@ DirFriend is a tool for visualizing file directory.
67
70
  opt.update({attr => Hash[ kv_arr ]})
68
71
  end
69
72
  end
70
- opt
73
+ if Config.enable
74
+ theme = Config.read(opt.delete(:theme))
75
+ theme.merge(opt)
76
+ else
77
+ opt
78
+ end
71
79
  end
72
80
  end
73
81
  end
@@ -0,0 +1,63 @@
1
+
2
+ module DirFriend
3
+ class Config
4
+ CONFIG_PATH = File.join(ENV['HOME'], '.dirfriend/config.yaml')
5
+ CONFIG_FILE = File.basename(CONFIG_PATH)
6
+ class << self
7
+ def read(theme)
8
+ new.read(theme)
9
+ end
10
+ attr_accessor :enable
11
+ end
12
+ self.enable = true
13
+
14
+ def read(theme)
15
+ if theme
16
+ use_passed_theme(theme)
17
+ else
18
+ use_default_theme
19
+ end
20
+ end
21
+
22
+ def themes
23
+ @themes ||= YAML.load_file(CONFIG_PATH).to_keysym_hash
24
+ rescue Errno::ENOENT
25
+ create
26
+ {}
27
+ rescue Psych::SyntaxError => e
28
+ abort "Syntax errors found in your '#{CONFIG_FILE}': #{e}."
29
+ end
30
+
31
+ private
32
+ def create
33
+ dir = File.dirname(CONFIG_PATH)
34
+ Dir.mkdir(dir) unless Dir.exist?(dir)
35
+ FileUtils.copy(template, CONFIG_PATH)
36
+ puts "'#{CONFIG_FILE}' created in #{dir}."
37
+ puts "You can set a theme for dot from pre-defined or created by you!"
38
+ rescue => e
39
+ abort "Something go wrong: #{e}."
40
+ end
41
+
42
+ def template
43
+ File.join(__dir__, 'template.yaml')
44
+ end
45
+
46
+ def use_passed_theme(theme)
47
+ themes[theme.intern].tap do |tm|
48
+ abort "Theme: '#{theme}' not found in your #{CONFIG_FILE}." unless tm
49
+ end
50
+ end
51
+
52
+ def use_default_theme
53
+ case defo = themes.delete(:default)
54
+ when Symbol, String
55
+ themes[defo.intern] || {}
56
+ when Hash
57
+ defo
58
+ else
59
+ {}
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,23 @@
1
+ module HashExtension
2
+ def to_keysym_hash
3
+ self.inject({}) do |h, (k, v)|
4
+ h[k.intern] = begin
5
+ case v
6
+ when Hash then v.to_keysym_hash
7
+ else v
8
+ end
9
+ end
10
+ h
11
+ end
12
+ end
13
+ end
14
+
15
+ module StringExtension
16
+ def ~
17
+ margin = scan(/^ +/).map(&:size).min
18
+ gsub(/^ {#{margin}}/, '')
19
+ end
20
+ end
21
+
22
+ Hash.send(:include, HashExtension)
23
+ String.send(:include, StringExtension)
@@ -0,0 +1,68 @@
1
+ # This is a template for `dir_friend dot` subcommand.
2
+ #
3
+ # You can set a theme to default by giving
4
+ # one of the theme names or a hash settings directly
5
+ #
6
+ # ex.
7
+ # default: :pop
8
+ #
9
+ # default:
10
+ # colorscheme: reds
11
+ # style: fdp
12
+ #
13
+ # Themes can be called with '--theme' or '-t' option of `dot` subcommand.
14
+ #
15
+ # ex. `dir_friend dot path/ -t pop
16
+
17
+ default: tree
18
+
19
+ tree:
20
+ global:
21
+ rankdir: LR
22
+ splines: ortho
23
+ dir_shape: diamond
24
+
25
+ blueegg:
26
+ colorscheme: blues
27
+ edges:
28
+ color: lightblue
29
+ nodes:
30
+ shape: egg
31
+
32
+ inline:
33
+ layout: osage
34
+ colorscheme: greens
35
+ nodes:
36
+ shape: box
37
+ edges:
38
+ color: gold
39
+
40
+ closeup:
41
+ layout: neato
42
+ colorscheme: purd
43
+ nodes:
44
+ shape: polygon
45
+
46
+ flower:
47
+ layout: circo
48
+ colorscheme: rdylgn
49
+ nodes:
50
+ shape: doublecircle
51
+
52
+ rigid:
53
+ layout: sfdp
54
+ colorscheme: greys
55
+ nodes:
56
+ shape: doubleoctagon
57
+
58
+ star:
59
+ layout: dot
60
+ colorscheme: set310
61
+ global:
62
+ bgcolor: midnightblue
63
+ splines: curved
64
+ edges:
65
+ color: snow
66
+ arrowhead: none
67
+ nodes:
68
+ shape: star
@@ -1,3 +1,3 @@
1
1
  module DirFriend
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/command_spec.rb CHANGED
@@ -20,6 +20,7 @@ describe DirFriend::Command do
20
20
  describe '#dot' do
21
21
  before(:each) do
22
22
  %w(A A/D A/D/G).each { |d| Dir.mkdir d }
23
+ DirFriend::Config.enable = false
23
24
  end
24
25
 
25
26
  around do |example|
@@ -37,7 +38,7 @@ describe DirFriend::Command do
37
38
  with('open "a.dot"', verbose: false)
38
39
 
39
40
  stdout = capture(:stdout) { described_class.start(['dot', 'A']) }
40
- expect(stdout).to include 'Dot file created:'
41
+ expect(stdout).to include 'created in the current directory'
41
42
  end
42
43
 
43
44
  it 'should parse option' do
@@ -48,7 +49,7 @@ describe DirFriend::Command do
48
49
  stdout = capture(:stdout) {
49
50
  described_class.start(['dot', '--with-open=false', 'A'])
50
51
  }
51
- expect(stdout).to include 'Dot file created:'
52
+ expect(stdout).to include 'created in the current directory'
52
53
  end
53
54
  end if DirFriend::OS.mac?
54
55
 
@@ -65,7 +66,7 @@ describe DirFriend::Command do
65
66
  stdout = capture(:stdout) {
66
67
  described_class.start(['dot', '--with-open', 'A'])
67
68
  }
68
- expect(stdout).to include 'Dot file created:'
69
+ expect(stdout).to include 'created in the current directory'
69
70
  end
70
71
  end
71
72
  end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+ require 'fakefs/spec_helpers'
3
+
4
+
5
+ describe DirFriend::Config do
6
+ include FakeFS::SpecHelpers
7
+
8
+ before(:each) do
9
+ stub_const("DirFriend::Config::CONFIG_PATH", "config.yaml")
10
+ File.write(described_class::CONFIG_PATH, ~<<-EOS)
11
+ default: :theme1
12
+ theme1:
13
+ colorscheme: reds
14
+ layout: fdp
15
+ theme2:
16
+ colorscheme: blues
17
+ layout: dot
18
+ EOS
19
+ File.write('template.yaml', '')
20
+ end
21
+
22
+ describe '#themes' do
23
+ context 'config file exist' do
24
+ it 'read user settings' do
25
+ themes = described_class.new.themes
26
+ expected = { default: :theme1,
27
+ theme1:{colorscheme:'reds', layout:'fdp'},
28
+ theme2:{colorscheme:'blues', layout:'dot'} }
29
+ expect(themes).to eq expected
30
+ end
31
+ end
32
+
33
+ context 'config file not exist' do
34
+ it 'creates a config file and returns a empty hash' do
35
+ config = described_class.new
36
+ config.instance_variable_set("@themes", nil)
37
+ stub_const("DirFriend::Config::CONFIG_PATH", "wrong_name_config.yaml")
38
+ config.stub(:template).and_return('template.yaml')
39
+ themes = config.themes
40
+ expect(themes).to be_empty
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#read' do
46
+ context 'with nil argument' do
47
+ it 'returns default theme' do
48
+ theme = described_class.read(nil)
49
+ expected = {colorscheme:'reds', layout:'fdp'}
50
+ expect(theme).to eq expected
51
+ end
52
+ end
53
+
54
+ context 'with a theme argument' do
55
+ it 'returns the theme' do
56
+ theme = described_class.read(:theme2)
57
+ expected = {colorscheme:'blues', layout:'dot'}
58
+ expect(theme).to eq expected
59
+ end
60
+ end
61
+
62
+ context 'when the default value is a hash data' do
63
+ before(:each) do
64
+ File.write(described_class::CONFIG_PATH, ~<<-EOS)
65
+ default:
66
+ layout: fdp
67
+ graph:
68
+ splines: ortho
69
+ theme1:
70
+ colorscheme: reds
71
+ layout: fdp
72
+ theme2:
73
+ colorscheme: blues
74
+ layout: dot
75
+ EOS
76
+ end
77
+
78
+ it 'returns the default value' do
79
+ theme = described_class.read(nil)
80
+ expected = {layout:'fdp', graph:{splines:'ortho'}}
81
+ expect(theme).to eq expected
82
+ end
83
+ end
84
+ end
85
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dir_friend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kyoendo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-29 00:00:00.000000000 Z
11
+ date: 2013-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gviz
@@ -114,11 +114,15 @@ files:
114
114
  - lib/dir_friend.rb
115
115
  - lib/dir_friend/any.rb
116
116
  - lib/dir_friend/command.rb
117
+ - lib/dir_friend/config.rb
118
+ - lib/dir_friend/core_ext.rb
117
119
  - lib/dir_friend/d.rb
118
120
  - lib/dir_friend/f.rb
119
121
  - lib/dir_friend/graph.rb
122
+ - lib/dir_friend/template.yaml
120
123
  - lib/dir_friend/version.rb
121
124
  - spec/command_spec.rb
125
+ - spec/config_spec.rb
122
126
  - spec/dir_friend_spec.rb
123
127
  - spec/graph_spec.rb
124
128
  - spec/spec_helper.rb
@@ -148,6 +152,7 @@ specification_version: 4
148
152
  summary: '`DirFriend` is a tool for visualizing file directory.'
149
153
  test_files:
150
154
  - spec/command_spec.rb
155
+ - spec/config_spec.rb
151
156
  - spec/dir_friend_spec.rb
152
157
  - spec/graph_spec.rb
153
158
  - spec/spec_helper.rb