shepherd 0.3.0 → 0.3.1

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.
@@ -1,65 +1,65 @@
1
1
  module Shepherd
2
2
 
3
- # A (actually, very) simple class, which is used to determine whether is this class a command, or not.
4
- #
5
- # == Creating a command
6
- # A Shepherd's command is just a class, which is kept inside +Shepherd::Command+.
7
- # Just create a ruby file inside +lib/shepherd/commands/+ called however you like, eg. foo.rb.
8
- # This file should look kinda like this stuff:
9
- #
10
- # module Shepherd::Command
11
- # class Foobar # this is the commands name; that would be: shep foobar
12
- # def init # when you call: shep <command>, init method here is executed.
13
- # puts "foobarinize!"
14
- # end
15
- # end
16
- # end
17
- #
18
- # You can also specify some other method than +init+ (eg. ahoy) and then use an +alias+ method:
19
- #
20
- # module Shepherd::Command
21
- # class Pirate
22
- # def ahoy
23
- # puts "Ahoy, aad'enturre!"
24
- # end
25
- # alias :init :ahoy
26
- # end
27
- # end
28
- #
29
- # == Options parsing
30
- # For --options parsing, Shepherd uses Trollop[http://trollop.rubyforge.org/] (which by the way is AWESOME!):
31
- #
32
- # module Shepherd::Command
33
- # class Foobar
34
- # def init
35
- # opts = Trollop::options do
36
- # opt :monkey, "Use monkey mode" # flag --monkey, default false
37
- # opt :goat, "Use goat mode", :default => true # flag --goat, default true
38
- # opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
39
- # opt :num_thumbs, "Number of thumbs", :type => :int # integer --num-thumbs <i>, default nil
40
- # end
41
- # end
42
- # end
43
- # end
44
- #
45
- # You can of course use for example +OptionParser+, if you want. But.. Trollop[http://trollop.rubyforge.org/] is AWESOME! THAT's why wise Shepherd is using it ;)
46
- #
47
- # == Multiple commands
48
- # Also, yau can create more than one command inside one file:
49
- #
50
- # module Shepherd::Command
51
- # class Foobar
52
- # def init
53
- # puts "foobarinize!"
54
- # end
55
- # end
56
- #
57
- # class Barbaz
58
- # def init
59
- # puts "barbazinize!"
60
- # end
61
- # end
62
- # end
63
- #
64
- module Command; end
3
+ # A (actually, very) simple class, which is used to determine whether is this class a command, or not.
4
+ #
5
+ # == Creating a command
6
+ # A Shepherd's command is just a class, which is kept inside +Shepherd::Command+.
7
+ # Just create a ruby file inside +lib/shepherd/commands/+ called however you like, eg. foo.rb.
8
+ # This file should look kinda like this stuff:
9
+ #
10
+ # module Shepherd::Command
11
+ # class Foobar # this is the commands name; that would be: shep foobar
12
+ # def init # when you call: shep <command>, init method here is executed.
13
+ # puts "foobarinize!"
14
+ # end
15
+ # end
16
+ # end
17
+ #
18
+ # You can also specify some other method than +init+ (eg. ahoy) and then use an +alias+ method:
19
+ #
20
+ # module Shepherd::Command
21
+ # class Pirate
22
+ # def ahoy
23
+ # puts "Ahoy, aad'enturre!"
24
+ # end
25
+ # alias :init :ahoy
26
+ # end
27
+ # end
28
+ #
29
+ # == Options parsing
30
+ # For --options parsing, Shepherd uses Trollop[http://trollop.rubyforge.org/] (which by the way is AWESOME!):
31
+ #
32
+ # module Shepherd::Command
33
+ # class Foobar
34
+ # def init
35
+ # opts = Trollop::options do
36
+ # opt :monkey, "Use monkey mode" # flag --monkey, default false
37
+ # opt :goat, "Use goat mode", :default => true # flag --goat, default true
38
+ # opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
39
+ # opt :num_thumbs, "Number of thumbs", :type => :int # integer --num-thumbs <i>, default nil
40
+ # end
41
+ # end
42
+ # end
43
+ # end
44
+ #
45
+ # You can of course use for example +OptionParser+, if you want. But.. Trollop[http://trollop.rubyforge.org/] is AWESOME! THAT's why wise Shepherd is using it ;)
46
+ #
47
+ # == Multiple commands
48
+ # Also, yau can create more than one command inside one file:
49
+ #
50
+ # module Shepherd::Command
51
+ # class Foobar
52
+ # def init
53
+ # puts "foobarinize!"
54
+ # end
55
+ # end
56
+ #
57
+ # class Barbaz
58
+ # def init
59
+ # puts "barbazinize!"
60
+ # end
61
+ # end
62
+ # end
63
+ #
64
+ module Command; end
65
65
  end
@@ -1,8 +1,8 @@
1
1
  module Shepherd::Command
2
- class Init
3
- def init
4
- @opts = Trollop::options do
5
- banner <<-EOB
2
+ class Init
3
+ def init
4
+ @opts = Trollop::options do
5
+ banner <<-EOB
6
6
  usage: shep init [options] [-h|--help]
7
7
 
8
8
  examples:
@@ -16,39 +16,39 @@ examples:
16
16
 
17
17
  options are:
18
18
  EOB
19
- opt :path, "a root path to the project", :default => Dir.pwd
20
- opt :name, "set a projects name (default: a top directory name from --path will be taken)", :type => :string
21
- opt :quiet, "don't show what's going on"
22
- opt :help, "show me and exit", :short => '-h'
23
- end
24
-
25
- @state = {}
26
- # delete the last '/' in path if present
27
- @state[:path] = @opts[:path].chomp "/"
28
- # use this top directory name unless --name is set
29
- @state[:name] = @opts[:name] ? @opts[:name] : @opts[:path].split("/").last
30
-
31
- # let's check if the --path exists
32
- if !Dir.exists? @state[:path]
33
- puts "[shep] exit 5: no such directory: #{@state[:path]}"
34
- exit 5
35
- end
36
-
37
- # let's check if there already is a sheep with the same path and/or name
38
- res = Shepherd::Db.new.get_first_row "select * from sheeps where name = ? or path = ?", @state[:name], @state[:path]
39
- if res
40
- puts "[shep] exit 4: there already is a sheep with that name and/or path"
41
- exit 4
42
- end
43
-
44
- Shepherd::Counter.new(@state[:path]) do |count|
45
- @state[:files] = count.files
46
- @state[:lines] = count.lines
47
- @state[:chars] = count.chars
48
- @state[:bytes] = count.bytes
49
- end
50
-
51
- puts "Our brave-hearted Shepherd gained a new sheep!
19
+ opt :path, "a root path to the project", :default => Dir.pwd
20
+ opt :name, "set a projects name (default: a top directory name from --path will be taken)", :type => :string
21
+ opt :quiet, "don't show what's going on"
22
+ opt :help, "show me and exit", :short => '-h'
23
+ end
24
+
25
+ @state = {}
26
+ # delete the last '/' in path if present
27
+ @state[:path] = @opts[:path].chomp "/"
28
+ # use this top directory name unless --name is set
29
+ @state[:name] = @opts[:name] ? @opts[:name] : @opts[:path].split("/").last
30
+
31
+ # let's check if the --path exists
32
+ if !Dir.exists? @state[:path]
33
+ puts "[shep] exit 5: no such directory: #{@state[:path]}"
34
+ exit 5
35
+ end
36
+
37
+ # let's check if there already is a sheep with the same path and/or name
38
+ res = Shepherd::Db.new.get_first_row "select * from sheeps where name = ? or path = ?", @state[:name], @state[:path]
39
+ if res
40
+ puts "[shep] exit 4: there already is a sheep with that name and/or path"
41
+ exit 4
42
+ end
43
+
44
+ Shepherd::Counter.new(@state[:path]) do |count|
45
+ @state[:files] = count.files
46
+ @state[:lines] = count.lines
47
+ @state[:chars] = count.chars
48
+ @state[:bytes] = count.bytes
49
+ end
50
+
51
+ puts "Our brave-hearted Shepherd gained a new sheep!
52
52
 
53
53
  path: \e[1;34m#{@state[:path]}\e[0;0m
54
54
  name: \e[1;32m#{@state[:name]}\e[0;0m
@@ -60,12 +60,12 @@ EOB
60
60
  #{Shepherd::Utils.nice_bytes(@state[:bytes])} (#{@state[:bytes].to_nice} bytes)
61
61
 
62
62
  " unless @opts[:quiet]
63
-
64
- Shepherd::Db.new.execute "insert into sheeps(id, name, path, files, lines, chars, bytes, inited_at, updated_at) values(NULL, ?, ?, ?, ?, ?, ?, datetime(), datetime())", @state[:name], @state[:path], @state[:files], @state[:lines], @state[:chars], @state[:bytes]
65
- end
66
-
67
- def desc
68
- "initialize a new project"
69
- end
70
- end
63
+
64
+ Shepherd::Db.new.execute "insert into sheeps(id, name, path, files, lines, chars, bytes, inited_at, updated_at) values(NULL, ?, ?, ?, ?, ?, ?, datetime(), datetime())", @state[:name], @state[:path], @state[:files], @state[:lines], @state[:chars], @state[:bytes]
65
+ end
66
+
67
+ def desc
68
+ "initialize a new project"
69
+ end
70
+ end
71
71
  end
@@ -0,0 +1,29 @@
1
+ module Shepherd::Command
2
+ class List
3
+ def init
4
+ @opts = Trollop::options do
5
+ banner <<-EOB
6
+ usage: shep list [options]
7
+ EOB
8
+
9
+ opt :one_line, "print each project in one line"
10
+ opt :help, "print me and exit", :short => '-h'
11
+ end
12
+
13
+ Shepherd::Db.new.execute "select * from sheeps" do |sheep|
14
+ if !@opts[:one_line]
15
+ puts "#{sheep[0]}. \e[1;32m#{sheep[1]}\e[0;0m in \e[1;34m#{sheep[2]}\e[0;0m
16
+ #{sheep[3].to_nice} files #{sheep[4].to_nice} lines #{sheep[5].to_nice} chars
17
+ #{Shepherd::Utils.nice_bytes(sheep[6])} (#{sheep[6].to_nice} bytes)
18
+ "
19
+ else
20
+ puts "#{sheep[0]}. \e[1;32m#{sheep[1]}\e[0;0m in \e[1;34m#{sheep[2]}\e[0;0m"
21
+ end
22
+ end
23
+ end
24
+
25
+ def desc
26
+ "show the projects you've inited"
27
+ end
28
+ end
29
+ end
@@ -1,34 +1,34 @@
1
1
  module Shepherd::Command
2
- class Rm
3
- def init
4
- @opts = Trollop::options do
5
- banner <<-EOB
2
+ class Rm
3
+ def init
4
+ @opts = Trollop::options do
5
+ banner <<-EOB
6
6
  usage: shep [options] rm <id>
7
7
  EOB
8
- opt :quiet, "don't produce output"
9
- opt :help, "show me and exit", :short => '-h'
10
- end
8
+ opt :quiet, "don't produce output"
9
+ opt :help, "show me and exit", :short => '-h'
10
+ end
11
11
 
12
- unless id = ARGV.first
13
- puts "[shep] error: no sheep specified."
14
- exit 1
15
- end
12
+ unless id = ARGV.first
13
+ puts "[shep] error: no sheep specified."
14
+ exit 1
15
+ end
16
16
 
17
- id = id.to_i
17
+ id = id.to_i
18
18
 
19
- if Shepherd::Db.new.get_first_row "select * from sheeps where id = ? limit 1", id
20
- if Shepherd::Db.new.execute "delete from sheeps where id = ?", id
21
- puts "[shep] good: successfuly deleted sheep." unless @opts[:quiet]
22
- else
23
- puts "[shep] error: something went wrong." unless @opts[:quiet]
24
- end
25
- else
26
- puts "[shep] error: no such sheep." unless @opts[:quiet]
27
- end
28
- end
19
+ if Shepherd::Db.new.get_first_row "select * from sheeps where id = ? limit 1", id
20
+ if Shepherd::Db.new.execute "delete from sheeps where id = ?", id
21
+ puts "[shep] good: successfuly deleted sheep." unless @opts[:quiet]
22
+ else
23
+ puts "[shep] error: something went wrong." unless @opts[:quiet]
24
+ end
25
+ else
26
+ puts "[shep] error: no such sheep." unless @opts[:quiet]
27
+ end
28
+ end
29
29
 
30
- def desc
31
- "remove a specific project"
32
- end
33
- end
30
+ def desc
31
+ "remove a specific project"
32
+ end
33
+ end
34
34
  end
@@ -1,53 +1,43 @@
1
1
  module Shepherd::Command
2
- class Show
3
- def init
4
- @opts = Trollop::options do
5
- banner <<-EOB
2
+ class Show
3
+ def init
4
+ @opts = Trollop::options do
5
+ banner <<-EOB
6
6
  usage: shep show [sheep] [options]
7
7
 
8
8
  options are:
9
9
  EOB
10
- opt :oneline, "print one sheep by one line"
11
- opt :help, "show me and exit"
12
- end
13
-
14
- # This is what we should show. +:everyone+ means every sheep we got
15
- # and ARGV[0] would be the name of the one specified sheep.
16
- @what = ARGV[0] || :everyone
17
- case @what
18
- when :everyone
19
- Shepherd::Db.new.execute "select * from sheeps" do |sheep|
20
- if !@opts[:oneline]
21
- puts "#{sheep[0]}. \e[1;32m#{sheep[1]}\e[0;0m in \e[1;34m#{sheep[2]}\e[0;0m
22
- #{sheep[3].to_nice} files #{sheep[4].to_nice} lines #{sheep[5].to_nice} chars
23
- #{Shepherd::Utils.nice_bytes(sheep[6])} (#{sheep[6].to_nice} bytes)
10
+ opt :help, "show me and exit"
11
+ end
12
+
13
+ if name = ARGV.shift
14
+ sheep = Shepherd::Db.new.get_first_row "select * from sheeps where name = ?", name
15
+ if sheep
16
+ puts "
17
+ id: \e[1;35m#{sheep[0]}\e[0;0m
18
+ path: \e[1;34m#{sheep[1]}\e[0;0m
19
+ name: \e[1;32m#{sheep[2]}\e[0;0m
24
20
 
25
- "
26
- else
27
- puts "#{sheep[0]}. \e[1;32m#{sheep[1]}\e[0;0m in \e[1;34m#{sheep[2]}\e[0;0m"
28
- end
29
- end
30
- else
31
- sheep = Shepherd::Db.new.get_first_row "select * from sheeps where name = ?", @what
32
- if sheep
33
- puts <<-EOP
34
- #{sheep[0]}. \e[1;32m#{sheep[1]}\e[0;0m in \e[1;34m#{sheep[2]}\e[0;0m
35
- #{sheep[3].to_nice} files #{sheep[4].to_nice} lines #{sheep[5].to_nice} chars
36
- #{Shepherd::Utils.nice_bytes(sheep[6])} (#{sheep[6].to_nice} bytes)
37
-
38
- initialized at #{sheep[7]}
39
- updated at #{sheep[8]}
40
-
41
- EOP
42
- else
43
- puts "[shep] exit 6: there is no such sheep: #{@what}"
44
- exit 6
45
- end
46
- end
47
- end
48
-
49
- def desc
50
- "list sheeps you have initialized"
51
- end
52
- end
21
+ state: #{sheep[3].to_nice} files
22
+ #{sheep[4].to_nice} lines
23
+ #{sheep[5].to_nice} chars
24
+
25
+ #{Shepherd::Utils.nice_bytes(sheep[6])} (#{sheep[6].to_nice} bytes)
26
+
27
+ initialized at #{sheep[7]}
28
+ updated at #{sheep[8]}
29
+ "
30
+ else
31
+ puts "[shep] exit 6: there is no such sheep: #{name}"
32
+ exit 6
33
+ end
34
+ else
35
+ puts "no sheep specified"
36
+ end
37
+ end
38
+
39
+ def desc
40
+ "show a specific project with some details"
41
+ end
42
+ end
53
43
  end
@@ -1,105 +1,105 @@
1
1
  require "find"
2
2
 
3
3
  module Shepherd
4
-
5
- # A simple class that counts all the needed data (like files, lines, bytes and so-on) in given destination path.
6
- #
7
- # == Usage
8
- #
9
- # Without using a block:
10
- # module Shepherd
11
- # count = Counter.new(/path/to/destination/dir)
12
- # count.files #=> 26
13
- # count.lines #=> 2319
14
- # count.chars #=> 79240
15
- # end
16
- #
17
- # With a block:
18
- # module Shepherd
19
- # Counter.new(/path/to/destination/dir) do |count|
20
- # count.files #=> 26
21
- # count.lines #=> 2319
22
- # count.chars #=> 79240
23
- # end
24
- # end
25
- #
26
- # Inside a +Shepherd::Command::Klass+ you should use it like so:
27
- # module Shepherd::Command
28
- # class Klass
29
- # Shepherd::Counter.new(/path/to/destination/dir) do |count|
30
- # count.files #=> 26
31
- # count.lines #=> 2319
32
- # count.chars #=> 79240
33
- # end
34
- # end
35
- # end
36
- #
37
- # @return [Shepherd::Counter] a new instance of +Shepherd::Counter+
38
- # @yield [Shepherd::Counter] a new instance of +Shepherd::Counter+
39
- #
40
- class Counter
41
- # A new instance of +Shepherd::Counter+
42
- #
43
- # @param [String] path a path to the project
44
- # @return [Shepherd::Counter] a new instance of +Shepherd::Counter+
45
- # @yield [Shepherd::Counter] a new instance of +Shepherd::Counter+
46
- def initialize path
47
- # delete the last '/' in path if present, just to make sure :)
48
- @path = path.chomp "/"
49
-
50
- yield self if block_given?
51
- end
52
-
53
- # Count the files (excluding dotfiles).
54
- #
55
- # @return [Array] list of all files (dotfiles are not included)
56
- def files
57
- @files = []
58
- Find.find(@path) do |path|
59
- if File.directory? path
60
- if File.basename(path)[0] == ?.
61
- Find.prune # don't look any further into this directory.
62
- else
63
- next
64
- end
65
- else
66
- @files << path
67
- end
68
- end
69
- @files.size
70
- end
71
-
72
- # Count the lines.
73
- #
74
- # @return [Integer] lines amount
75
- def lines
76
- @lines = 0
77
- @files.each do |file|
78
- @lines += `cat '#{file}' | wc -l`.to_i
79
- end
80
- @lines
81
- end
82
-
83
- # Count the characters.
84
- #
85
- # @return [Integer] characters amount
86
- def chars
87
- @chars = 0
88
- @files.each do |file|
89
- @chars += `cat '#{file}' | wc -m`.to_i
90
- end
91
- @chars
92
- end
93
-
94
- # Count the bytes. This *wont* be converted all the time.
95
- #
96
- # @return [Integer] bytes amount
97
- def bytes
98
- @bytes = 0
99
- @files.each do |file|
100
- @bytes += File.new("#{file}").size
101
- end
102
- @bytes
103
- end
104
- end
4
+
5
+ # A simple class that counts all the needed data (like files, lines, bytes and so-on) in given destination path.
6
+ #
7
+ # == Usage
8
+ #
9
+ # Without using a block:
10
+ # module Shepherd
11
+ # count = Counter.new(/path/to/destination/dir)
12
+ # count.files #=> 26
13
+ # count.lines #=> 2319
14
+ # count.chars #=> 79240
15
+ # end
16
+ #
17
+ # With a block:
18
+ # module Shepherd
19
+ # Counter.new(/path/to/destination/dir) do |count|
20
+ # count.files #=> 26
21
+ # count.lines #=> 2319
22
+ # count.chars #=> 79240
23
+ # end
24
+ # end
25
+ #
26
+ # Inside a +Shepherd::Command::Klass+ you should use it like so:
27
+ # module Shepherd::Command
28
+ # class Klass
29
+ # Shepherd::Counter.new(/path/to/destination/dir) do |count|
30
+ # count.files #=> 26
31
+ # count.lines #=> 2319
32
+ # count.chars #=> 79240
33
+ # end
34
+ # end
35
+ # end
36
+ #
37
+ # @return [Shepherd::Counter] a new instance of +Shepherd::Counter+
38
+ # @yield [Shepherd::Counter] a new instance of +Shepherd::Counter+
39
+ #
40
+ class Counter
41
+ # A new instance of +Shepherd::Counter+
42
+ #
43
+ # @param [String] path a path to the project
44
+ # @return [Shepherd::Counter] a new instance of +Shepherd::Counter+
45
+ # @yield [Shepherd::Counter] a new instance of +Shepherd::Counter+
46
+ def initialize path
47
+ # delete the last '/' in path if present, just to make sure :)
48
+ @path = path.chomp "/"
49
+
50
+ yield self if block_given?
51
+ end
52
+
53
+ # Count the files (excluding dotfiles).
54
+ #
55
+ # @return [Array] list of all files (dotfiles are not included)
56
+ def files
57
+ @files = []
58
+ Find.find(@path) do |path|
59
+ if File.directory? path
60
+ if File.basename(path)[0] == ?.
61
+ Find.prune # don't look any further into this directory.
62
+ else
63
+ next
64
+ end
65
+ else
66
+ @files << path
67
+ end
68
+ end
69
+ @files.size
70
+ end
71
+
72
+ # Count the lines.
73
+ #
74
+ # @return [Integer] lines amount
75
+ def lines
76
+ @lines = 0
77
+ @files.each do |file|
78
+ @lines += `cat '#{file}' | wc -l`.to_i
79
+ end
80
+ @lines
81
+ end
82
+
83
+ # Count the characters.
84
+ #
85
+ # @return [Integer] characters amount
86
+ def chars
87
+ @chars = 0
88
+ @files.each do |file|
89
+ @chars += `cat '#{file}' | wc -m`.to_i
90
+ end
91
+ @chars
92
+ end
93
+
94
+ # Count the bytes. This *wont* be converted all the time.
95
+ #
96
+ # @return [Integer] bytes amount
97
+ def bytes
98
+ @bytes = 0
99
+ @files.each do |file|
100
+ @bytes += File.new("#{file}").size
101
+ end
102
+ @bytes
103
+ end
104
+ end
105
105
  end