Class: Shake

Inherits:
Object
  • Object
show all
Includes:
Defaults
Defined in:
lib/shake.rb

Defined Under Namespace

Modules: Defaults, Params

Constant Summary collapse

VERSION =
"0.1.3"
Abort =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Defaults

included

Class Attribute Details

.commandObject (readonly)

Returns the value of attribute command.



10
11
12
# File 'lib/shake.rb', line 10

def command
  @command
end

.paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'lib/shake.rb', line 9

def params
  @params
end

Class Method Details

.default(what = nil, &blk) ⇒ Object

Sets or retrieves the default task.

Examples:

default :default_task
default { ... }


50
51
52
# File 'lib/shake.rb', line 50

def default(what=nil, &blk)
  @default = what || blk || @default
end

.err(str = "") ⇒ Object



124
125
126
# File 'lib/shake.rb', line 124

def err(str="")
  $stderr.write "#{str}\n"
end

.executableObject



120
121
122
# File 'lib/shake.rb', line 120

def executable
  File.basename($0)
end

.find_in_project(file) ⇒ Object

Traverse back it’s parents and find the file called ‘file`.



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/shake.rb', line 129

def find_in_project(file)
  dir = Dir.pwd
  while true
    path = File.join(dir, file)
    return path  if File.exists?(path)

    parent = File.expand_path(File.join(dir, '..'))
    return nil  if parent == dir

    dir = parent
  end
end

.invalid(what = nil, &blk) ⇒ Object

Sets or retrieves the ‘invalid command’ task. See ‘default` for examples.



56
57
58
# File 'lib/shake.rb', line 56

def invalid(what=nil, &blk)
  @invalid = what || blk || @invalid
end

.invoke(what, *args) ⇒ Object

Invokes a task with the given arguments. You may even nest multiple task invocations.

Examples:

invoke(:start)
invoke(:start, 'nginx', 'memcache')


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/shake.rb', line 68

def invoke(what, *args)
  old, @params = @params, args.extend(Params)
  return  if what.nil?

  begin
    return instance_eval(&what)  if what.is_a?(Proc)

    task = task(what)  or return nil
    instance_eval &task.proc
    true
  rescue Abort
    true
  ensure
    @params = old
  end
end

.pass(msg = nil) ⇒ Object

Stops the execution of a task.

Raises:



86
87
88
89
# File 'lib/shake.rb', line 86

def pass(msg=nil)
  err msg  unless msg.nil?
  raise Abort
end

.run(*argv) ⇒ Object

Runs with the given arguments and dispatches the appropriate task. Use ‘run!` if you want to go with command line arguments.

Example:

# This is equivalent to `invoke(:start, 'nginx')`
run 'start', 'nginx'


109
110
111
112
113
114
# File 'lib/shake.rb', line 109

def run(*argv)
  return invoke(default)  if argv.empty?

  @command = argv.shift
  invoke(@command, *argv) or invoke(invalid, *argv)
end

.run!Object



116
117
118
# File 'lib/shake.rb', line 116

def run!
  run *ARGV
end

.task(what = nil, options = {}, &blk) ⇒ Object

Sets or retrieves a task. If no arguments are given, it returns the last task defined.

Examples:

task(:start) { ... }
task(:start, description: 'Starts it') { ... }
task(:start)
task.description = "Starts it"


35
36
37
38
39
40
41
# File 'lib/shake.rb', line 35

def task(what=nil, options={}, &blk)
  return @last  if what.nil?

  key = what.to_s.downcase.to_sym
  @last = tasks[key] = new_task(options, &blk)  if block_given?
  tasks[key]
end

.tasksObject

Returns a list of tasks. It gives out a hash with symbols as keys, and openstructs as values.



21
22
23
# File 'lib/shake.rb', line 21

def tasks
  @tasks ||= Hash.new
end

.wrong_usageObject

Halts a task because of wrong usage.

Example:

wrong_usage  if params.any?


97
98
99
# File 'lib/shake.rb', line 97

def wrong_usage
  invoke(invalid) and pass
end