shepherd 0.1.2 → 0.1.3

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/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # shepherd 0.1.3 (18-09-2011)
2
+
3
+ + Now we can `show` some sheeps!
4
+ + `init` command now saves the inited projects!
5
+ + Provide setup utility.
6
+
1
7
  # shepherd 0.1.2 (18-07-2011)
2
8
 
3
9
  + Make the `Counter` class
data/README.md CHANGED
@@ -24,11 +24,11 @@ Shepherd
24
24
  </tr>
25
25
  <tr>
26
26
  <td><b>Latest version</b></td>
27
- <td>0.1.1</td>
27
+ <td>0.1.3</td>
28
28
  </tr>
29
29
  <tr>
30
30
  <td><b>Release date</b></td>
31
- <td>July 4th, 2011</td>
31
+ <td>September 18th, 2011</td>
32
32
  </tr>
33
33
  <tr>
34
34
  <td><b>Stage</b></td>
data/lib/shepherd.rb CHANGED
@@ -1,9 +1,58 @@
1
1
  module Shepherd
2
2
  ROOT = File.expand_path(File.dirname(__FILE__))
3
3
 
4
+ autoload :Utils, "#{ROOT}/shepherd/utils"
5
+ autoload :Setup, "#{ROOT}/shepherd/setup"
4
6
  autoload :Command, "#{ROOT}/shepherd/command"
5
7
  autoload :Counter, "#{ROOT}/shepherd/counter"
6
8
  autoload :Db, "#{ROOT}/shepherd/db"
7
9
  autoload :Cli, "#{ROOT}/shepherd/cli"
8
10
  autoload :Version, "#{ROOT}/shepherd/version"
11
+
12
+ # Stay DRY
13
+ extend Utils
14
+
15
+ # Check if the setup was once done
16
+ if !Dir.exists? "#{Dir.home}/.shepherd"
17
+ # Setup was not done so we'll do it, but...
18
+ Setup.new
19
+ sleep 0.5
20
+ puts "Hello there!"
21
+ sleep 2
22
+ puts "It's your first time using Shepherd, isn't it?"
23
+ sleep 4
24
+ puts "Knew it!"
25
+ sleep 1.5
26
+ puts "But before the first run, we would have to make some setup first..."
27
+ sleep 4.5
28
+ puts "Do you know who's the cousin of zebra?"
29
+ sleep 3
30
+ puts "You don't?? Phew.."
31
+ sleep 2
32
+ puts "Ghehe, I know, but I won't tell you.."
33
+ sleep 2.8
34
+ puts "Oh, no, the setup is *not* running now..."
35
+ sleep 2
36
+ puts "Okey, okey! Calm down and don't yell at me.."
37
+ sleep 3
38
+ puts "All right, as you wish, I am doing the setup. You have 10.."
39
+ sleep 3.7
40
+ puts "No, 5 seconds to abort."
41
+ sleep 2
42
+ print "5"; sleep 0.4; print "."; sleep 0.4; print "."; sleep 0.2; print " "
43
+ print "4"; sleep 0.4; print "."; sleep 0.4; print "."; sleep 0.2; print " "
44
+ print "3"; sleep 0.1; print "."; sleep 0.1; print "."; sleep 0.1; print " "
45
+ puts "Too late!"
46
+ sleep 10
47
+ puts "Oh my gosh.. I forgot to click this big, red button which says 'Stop annoying people and do the setup'. Sorry.."
48
+ sleep 6.5
49
+ puts "Well, I suppose you actually are a human.."
50
+ sleep 4
51
+ puts "Okey, okey!\n\n"
52
+ sleep 2.2
53
+ puts "*click*\n\n"
54
+ sleep 0.5
55
+ # Ghehe :)
56
+ # Setup.new
57
+ end
9
58
  end
data/lib/shepherd/cli.rb CHANGED
@@ -3,6 +3,27 @@ require "trollop"
3
3
  module Shepherd
4
4
 
5
5
  # Command Line Interface class
6
+ #
7
+ # === Usage
8
+ #
9
+ # module Shepherd
10
+ # Cli.new.run!
11
+ # end
12
+ #
13
+ # or
14
+ #
15
+ # Shepherd::Cli.new.run!
16
+ #
17
+ # === Exit statuses
18
+ #
19
+ # - *0* Everything went just fine :)
20
+ # - *1* User said ^C :]
21
+ # - *2* User wanted a UnknownCommand
22
+ # - *3* The database file was not found
23
+ # - *4* User wanted to init another sheep/project with the same name and/or path
24
+ # - *5* User wanted to init a project in a path that doesn't exist
25
+ # - *6* User wanted to see a sheep that was not inited
26
+ #
6
27
  class Cli
7
28
 
8
29
  # Kinda self explanatory
@@ -12,6 +33,7 @@ module Shepherd
12
33
  attr_accessor :command
13
34
 
14
35
  # Require *all* command files
36
+ # TODO: Is it possible to make it use autoload? It'd be cool! :)
15
37
  Dir[File.join(File.dirname(__FILE__), "commands", "*.rb")].each do |all_command_files|
16
38
  require all_command_files
17
39
  end
@@ -43,7 +65,7 @@ module Shepherd
43
65
  eval "Command::#{cmd}.new.desc"
44
66
  else
45
67
  # If there is not
46
- "~ no description provided ~"
68
+ "---"
47
69
  end
48
70
  out << " " << cmd.downcase.to_s << " " * spaces << desc
49
71
  # If this command is the last one, don't make a new line
@@ -53,7 +75,7 @@ module Shepherd
53
75
  end
54
76
  end
55
77
  out
56
- end
78
+ end # commands_list:Method
57
79
 
58
80
  # Check if command really exists
59
81
  #
@@ -69,7 +91,7 @@ module Shepherd
69
91
  #
70
92
  # @return [Hash] array full of options
71
93
  $opts = Trollop::options do
72
- version "Shepherd be t'e version #{Version::STRING}"
94
+ version "shepherd version #{Version::STRING}"
73
95
  banner <<-EOB
74
96
  usage: shep [options] <command>
75
97
 
@@ -77,7 +99,7 @@ commands are:
77
99
 
78
100
  #{Cli.new.commands_list}
79
101
 
80
- shep <command> --help for more info about specified command
102
+ shep <command> --help/-h for more info about specified command
81
103
 
82
104
  options are:
83
105
  EOB
@@ -96,12 +118,12 @@ EOB
96
118
  exit 0
97
119
  rescue UnknownCommand => e
98
120
  puts e.message
99
- exit 1
121
+ exit 2
100
122
  rescue Db::DatabaseNotFound => e
101
123
  puts e.message
102
- exit 1
124
+ exit 3
103
125
  rescue Interrupt
104
- puts "\n\n~> interrupted"
126
+ puts "\n\n!# interrupted"
105
127
  exit 1
106
128
  end
107
129
  end # run!:Method
@@ -5,6 +5,15 @@ module Shepherd::Command
5
5
  banner <<-EOB
6
6
  usage: shep [options] init [-h|--help]
7
7
 
8
+ examples:
9
+ cd /my/awesome/program
10
+ shep init # name of that sheep would be 'program'
11
+
12
+ shep init --path /my/awesome/program # here as well
13
+
14
+ shep init --path /my/awesome/program\\
15
+ --name yay # it's would be 'yay'
16
+
8
17
  options are:
9
18
  EOB
10
19
  opt :path, "a root path to the project", :default => Dir.pwd
@@ -19,29 +28,40 @@ EOB
19
28
  # use this top directory name unless --name is set
20
29
  @state[:name] = @opts[:name] ? @opts[:name] : @opts[:path].split("/").last
21
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
+
22
44
  Shepherd::Counter.new(@state[:path]) do |count|
23
45
  @state[:files] = count.files
24
46
  @state[:lines] = count.lines
25
47
  @state[:chars] = count.chars
26
48
  @state[:bytes] = count.bytes
27
- @state[:rawbytes] = count.rawbytes
28
49
  end
29
50
 
30
- puts "Our brave-hearted Shepherd initializes a new project!
51
+ puts "Our brave-hearted Shepherd gained a new sheep!
31
52
 
32
53
  path: \e[1;34m#{@state[:path]}\e[0;0m
33
54
  name: \e[1;32m#{@state[:name]}\e[0;0m
34
55
 
35
- state: #{@state[:files].size} files
56
+ state: #{@state[:files]} files
36
57
  #{@state[:lines]} lines
37
58
  #{@state[:chars]} chars
38
59
 
39
- #{@state[:bytes]} (#{@state[:rawbytes]} bytes)
60
+ #{Shepherd::Utils.nice_bytes(@state[:bytes])} (#{@state[:bytes]} bytes)
40
61
 
41
62
  " unless @opts[:quiet]
42
-
43
- # TODO: save to database
44
- # Shepherd::Db.new.execute "inserting query"
63
+
64
+ Shepherd::Db.new.execute "insert into sheeps(id, name, path, files, lines, chars, bytes, inited_at) values(NULL, ?, ?, ?, ?, ?, ?, datetime())", @state[:name], @state[:path], @state[:files], @state[:lines], @state[:chars], @state[:bytes]
45
65
  end
46
66
 
47
67
  def desc
@@ -0,0 +1,52 @@
1
+ module Shepherd::Command
2
+ class Show
3
+ def init
4
+ @opts = Trollop::options do
5
+ banner <<-EOB
6
+ usage: shep show [sheep] [options]
7
+
8
+ options are:
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)
24
+
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
+
40
+ EOP
41
+ else
42
+ puts "[shep] exit 6: there is no such sheep: #{@what}"
43
+ exit 6
44
+ end
45
+ end
46
+ end
47
+
48
+ def desc
49
+ "list sheeps you have initialized"
50
+ end
51
+ end
52
+ end
@@ -50,34 +50,6 @@ module Shepherd
50
50
  yield self if block_given?
51
51
  end
52
52
 
53
- # Convert bytes into more human readable format.
54
- # Found nice resolution here[http://www.ruby-forum.com/topic/119703] which was originally written by {Jeff Emminger}[http://www.ruby-forum.com/user/show/jemminger] and I just add this part to automaticly set the unit. Thank you!
55
- #
56
- # @param [Integer] number a number to be formatted
57
- # @return [String] a formatted number
58
- def nice_bytes number
59
- units = {:b => 1,
60
- :kb => 2**10,
61
- :mb => 2**20,
62
- :gb => 2**30,
63
- :tb => 2**40}
64
-
65
- unit = :b
66
- case number
67
- when 1...2**10
68
- unit = :b
69
- when 2**10...2**20
70
- unit = :kb
71
- when 2**20...2**30
72
- unit = :mb
73
- when 2**30...2**40
74
- unit = :gb
75
- when 2**40...2**50
76
- unit = :tb
77
- end
78
- "#{sprintf("%.#{0}f", number / units[unit.to_s.downcase.to_sym])} #{unit.to_s.upcase}"
79
- end
80
-
81
53
  # Count the files (excluding dotfiles).
82
54
  #
83
55
  # @return [Array] list of all files (dotfiles are not included)
@@ -94,7 +66,7 @@ module Shepherd
94
66
  @files << path
95
67
  end
96
68
  end
97
- @files
69
+ @files.size
98
70
  end
99
71
 
100
72
  # Count the lines.
@@ -122,19 +94,12 @@ module Shepherd
122
94
  # Count the bytes. This *wont* be converted all the time.
123
95
  #
124
96
  # @return [Integer] bytes amount
125
- def rawbytes
126
- @rawbytes = 0
97
+ def bytes
98
+ @bytes = 0
127
99
  @files.each do |file|
128
- @rawbytes += File.new("#{file}").size
100
+ @bytes += File.new("#{file}").size
129
101
  end
130
- @rawbytes
131
- end
132
-
133
- # Count the bytes. This *will* be formatted into something like: 2898 KB
134
- #
135
- # @return [String] formatted bytes
136
- def bytes
137
- @bytes = nice_bytes rawbytes
102
+ @bytes
138
103
  end
139
104
  end
140
105
  end
@@ -0,0 +1,37 @@
1
+ require "fileutils"
2
+
3
+ module Shepherd
4
+ class Setup
5
+ def initialize
6
+ @schema = <<-EOS
7
+ create table if not exists sheeps (
8
+ id integer primary key autoincrement,
9
+ name varchar(128) not null,
10
+ path varchar(256) not null,
11
+ files integer(6) not null,
12
+ lines integer(7) not null,
13
+ chars integer(10) not null,
14
+ bytes integer(10) not null,
15
+ inited_at datetime not null
16
+ );
17
+ EOS
18
+
19
+ crdir "#{Dir.home}/.shepherd"
20
+ crfile "#{Dir.home}/.shepherd/herd.db"
21
+ puts "[shep] setup: making a real database: #{Dir.home}/.shepherd/herd.db"
22
+ Db.new.execute "#{@schema}"
23
+ exit 0
24
+ end
25
+
26
+ private
27
+ def crdir dir
28
+ puts "[shep] setup: creating dir: #{dir}"
29
+ ::FileUtils.mkdir "#{Dir.home}/.shepherd"
30
+ end
31
+
32
+ def crfile file
33
+ puts "[shep] setup: creating file: #{file}"
34
+ ::FileUtils.touch "#{Dir.home}/.shepherd/herd.db"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,60 @@
1
+ module Shepherd
2
+ module Utils
3
+ def yes? msg = "Would you like to proceed?"
4
+ print "#{msg} [Yn]: "
5
+ case $stdin.gets
6
+ when /(y|yes)/i
7
+ true
8
+ when "\n"
9
+ true
10
+ when /(n|no)/i
11
+ false
12
+ else
13
+ false
14
+ end
15
+ end # yes?:Method
16
+
17
+ # Convert bytes into more human readable format.
18
+ # I have found nice resolution here[http://www.ruby-forum.com/topic/119703]
19
+ # which was originally written by Jeff Emminger and I just add this part
20
+ # to automaticly set the unit. Thank you, Jeff!
21
+ #
22
+ # @param [Integer] number of bytes to be formatted
23
+ # @return [String] a formatted number with the unit appended to it
24
+ def self.nice_bytes number
25
+ units = {:b => 1,
26
+ :kb => 2**10,
27
+ :mb => 2**20,
28
+ :gb => 2**30,
29
+ :tb => 2**40}
30
+
31
+ unit = :b
32
+ case number
33
+ when 1...2**10
34
+ unit = :b
35
+ when 2**10...2**20
36
+ unit = :kb
37
+ when 2**20...2**30
38
+ unit = :mb
39
+ when 2**30...2**40
40
+ unit = :gb
41
+ when 2**40...2**50
42
+ unit = :tb
43
+ end
44
+ "#{sprintf("%.#{0}f", number / units[unit.to_s.downcase.to_sym])} #{unit.to_s.upcase}"
45
+ end
46
+ end
47
+ end
48
+
49
+ class Integer
50
+ def to_nice
51
+ s = self.to_s
52
+
53
+ if s.include? ?.
54
+ pre, post = s.split '.'
55
+ "#{pre.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse}.#{post}"
56
+ else
57
+ s.reverse.gsub( /\d{3}(?=\d)/, '\&.' ).reverse
58
+ end
59
+ end
60
+ end
@@ -2,7 +2,7 @@ module Shepherd
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 2
5
+ PATCH = 3
6
6
  BUILD = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join "."
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: shepherd
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.1.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Szymon Urba\xC5\x9B"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-18 00:00:00 Z
13
+ date: 2011-09-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sqlite3
@@ -66,8 +66,11 @@ files:
66
66
  - lib/shepherd/cli.rb
67
67
  - lib/shepherd/command.rb
68
68
  - lib/shepherd/commands/init.rb
69
+ - lib/shepherd/commands/show.rb
69
70
  - lib/shepherd/counter.rb
70
71
  - lib/shepherd/db.rb
72
+ - lib/shepherd/setup.rb
73
+ - lib/shepherd/utils.rb
71
74
  - lib/shepherd/version.rb
72
75
  - lib/trollop.rb
73
76
  - shepherd.gemspec