Class: Shepherd::Command::Init

Inherits:
Object
  • Object
show all
Defined in:
lib/shepherd/commands/init.rb

Instance Method Summary collapse

Instance Method Details

#descObject



67
68
69
# File 'lib/shepherd/commands/init.rb', line 67

def desc
  "initialize a new project"
end

#initObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shepherd/commands/init.rb', line 3

def init
  @opts = Trollop::options do
    banner <<-EOB
usage: shep init [options] [-h|--help]

examples:
  cd /my/awesome/program
  shep init                              # name of that sheep would be 'program'
  
  shep init --path /my/awesome/program   # here as well
  
  shep init --path /my/awesome/program\\
        --name yay                   # it's would be 'yay'

options are:
EOB
    opt :path, "a root path to the project", :default => Dir.pwd
    opt :name, "set a projects name (default: a top directory name from --path will be taken)", :type => :string
    opt :quiet, "don't show what's going on"
    opt :help, "show me and exit", :short => '-h'
  end
  
  @state = {}
  # delete the last '/' in path if present
  @state[:path] = @opts[:path].chomp "/"
  # use this top directory name unless --name is set
  @state[:name] = @opts[:name] ? @opts[:name] : @opts[:path].split("/").last
  
  # let's check if the --path exists
  if !Dir.exists? @state[:path]
    puts "[shep] exit 5: no such directory: #{@state[:path]}"
    exit 5
  end
  
  # let's check if there already is a sheep with the same path and/or name
  res = Shepherd::Db.new.get_first_row "select * from sheeps where name = ? or path = ?", @state[:name], @state[:path]
  if res
    puts "[shep] exit 4: there already is a sheep with that name and/or path"
    exit 4
  end
  
  Shepherd::Counter.new(@state[:path]) do |count|
    @state[:files] = count.files
    @state[:lines] = count.lines
    @state[:chars] = count.chars
    @state[:bytes] = count.bytes
  end
  
  puts "Our brave-hearted Shepherd gained a new sheep!

   path: \e[1;34m#{@state[:path]}\e[0;0m
   name: \e[1;32m#{@state[:name]}\e[0;0m
  
  state: #{@state[:files].to_nice} files
     #{@state[:lines].to_nice} lines
     #{@state[:chars].to_nice} chars
     
     #{Shepherd::Utils.nice_bytes(@state[:bytes])} (#{@state[:bytes].to_nice} bytes)
  
" unless @opts[:quiet]
  
  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]
end