Class: Guard::Knife

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/knife.rb

Constant Summary collapse

VERSION =
'0.1.2'

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Knife

Initialize a Guard.

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the Guard file watchers

  • options (Hash) (defaults to: {})

    the custom Guard options



11
12
13
14
# File 'lib/guard/knife.rb', line 11

def initialize(watchers = [], options = {})
  super
  @options = {}.update(options)
end

Instance Method Details

#knife_optionsObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/guard/knife.rb', line 77

def knife_options
  options = ''
  @options.each do |key, value|
    case key
    when :config
      options += "-c #{value} "
    end
  end

  options
end

#normalize(paths) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/guard/knife.rb', line 54

def normalize(paths)
  paths.map! do |path|
    next if path.match(/\.swp$/)  # vim swap file

    # only upload cookbook once in case many files change
    path.gsub(/^(cookbooks\/[^\/]*)\/.*$/, '\1')
  end
  paths.compact.uniq
end

#reloadObject

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…

Raises:

  • (:task_has_failed)

    when reload has failed



29
30
# File 'lib/guard/knife.rb', line 29

def reload
end

#run_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Raises:

  • (:task_has_failed)

    when run_all has failed



35
36
# File 'lib/guard/knife.rb', line 35

def run_all
end

#run_on_change(paths) ⇒ Object

Called on file(s) modifications that the Guard watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



41
42
43
44
45
46
# File 'lib/guard/knife.rb', line 41

def run_on_change(paths)
  paths = normalize(paths)
  paths.each do |path|
    upload(path)
  end
end

#run_on_deletion(paths) ⇒ Object

Called on file(s) deletions that the Guard watches.

Parameters:

  • paths (Array<String>)

    the deleted files or paths

Raises:

  • (:task_has_failed)

    when run_on_change has failed



51
52
# File 'lib/guard/knife.rb', line 51

def run_on_deletion(paths)
end

#startObject

Call once when Guard starts. Please override initialize method to init stuff.

Raises:

  • (:task_has_failed)

    when start has failed



18
19
# File 'lib/guard/knife.rb', line 18

def start
end

#stopObject

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).

Raises:

  • (:task_has_failed)

    when stop has failed



23
24
# File 'lib/guard/knife.rb', line 23

def stop
end

#upload(path) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/guard/knife.rb', line 64

def upload(path)
  if path.match(/^cookbooks\/([^\/]*)/)
    upload_cookbook($1)
  elsif path.match(/^data_bags\/(.*)\/.*\.json$/)
    data_bag = $1
    upload_databag(data_bag, path)
  elsif path.match(/^(environments\/.*\.rb)$/)
    upload_environment($1)
  elsif path.match(/^(roles\/.*.rb)$/)
    upload_role($1)
  end
end

#upload_cookbook(cookbook) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/guard/knife.rb', line 89

def upload_cookbook(cookbook)
  if system("knife cookbook upload #{cookbook} #{knife_options}")
    ::Guard::Notifier.notify("Cookbook #{cookbook} uploaded", :title => 'Knife')
  else
    ::Guard::Notifier.notify("Cookbook #{cookbook} failed to upload", :title => 'Knife', :image => :failed)
  end
end

#upload_databag(data_bag, path) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/guard/knife.rb', line 97

def upload_databag(data_bag, path)
  item = File.basename path, '.json'
  if system("knife data bag from file #{data_bag} #{path} #{knife_options}")
    ::Guard::Notifier.notify("Data bag item #{data_bag}::#{item} uploaded", :title => 'Knife')
  else
    ::Guard::Notifier.notify("Data bag item #{data_bag}::#{item} failed to upload", :title => 'Knife', :image => :failed)
  end
end

#upload_environment(environment) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/guard/knife.rb', line 106

def upload_environment(environment)
  if system("knife environment from file #{environment} #{knife_options}")
    ::Guard::Notifier.notify("Environment #{environment} uploaded", :title => 'Knife')
  else
    ::Guard::Notifier.notify("Environment #{environment} failed to upload", :title => 'Knife', :image => :failed)
  end
end

#upload_role(role) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/guard/knife.rb', line 114

def upload_role(role)
  if system("knife role from file #{role} #{knife_options}")
    ::Guard::Notifier.notify("Role #{role} uploaded", :title => 'Knife')
  else
    ::Guard::Notifier.notify("Role #{role} upload failed", :title => 'Knife', :image => :failed)
  end
end