Module: Peony::Actions

Defined in:
lib/peony/actions.rb

Instance Method Summary collapse

Instance Method Details

#apply(path, config = {}) ⇒ Object

Loads an external file and execute it in the instance binding.

Parameters

path<String>

The path to the file to execute. Can be a web address or a relative path from the source root.

Examples

apply "http://gist.github.com/103208"

apply "recipes/jquery.rb"


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/peony/actions.rb', line 129

def apply(path, config={})
  verbose = config.fetch(:verbose, true)
  is_uri  = path =~ /^https?\:\/\//
  path    = find_recipes(path).first unless is_uri

  say_status :apply, path, verbose
  self.padding_up if verbose

  if is_uri
    contents = open(path, 'Accept' => 'application/x-peony-template') {|io| io.read }
  else
    contents = open(path) {|io| io.read }
  end

  instance_eval(contents, path)
  self.padding_down if verbose
end

#destination_rootObject



4
5
6
7
# File 'lib/peony/actions.rb', line 4

def destination_root
  @destination_stack ||= [File.expand_path(Dir.pwd || '')]
  @destination_stack.last
end

#in_rootObject

Goes to the root and execute the given block.



113
114
115
# File 'lib/peony/actions.rb', line 113

def in_root
  inside(@destination_stack.first) { yield }
end

#inside(dir = '', config = {}, &block) ⇒ Object

Do something in the root or on a provided subfolder. If a relative path is given it’s referenced from the current root. The full path is yielded to the block you provide. The path is set back to the previous path when the method exits.

Parameters

dir<String>

the directory to move to.

config<Hash>

give :verbose => true to log and use padding.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/peony/actions.rb', line 86

def inside(dir='', config={}, &block)
  verbose = config.fetch(:verbose, false)
  dry_run = ENV['dry-run']

  say_status :inside, dir, verbose
  self.padding_up if verbose
  @destination_stack.push File.expand_path(dir, destination_root)

  # If the directory doesnt exist and we're not pretending
  if !File.exist?(destination_root) && !pretend
    FileUtils.mkdir_p(destination_root)
  end

  if dry_run
    # In dry_run mode, just yield down to the block
    block.arity == 1 ? yield(destination_root) : yield
  else
    FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
  end

  @destination_stack.pop
  self.padding_down if verbose
end

#invoke(task, config = {}) ⇒ Object

### invoke Invokes another Rake task.

Invokes the task given in ‘task`. Returns nothing.

invoke :'git:clone'
invoke :restart

Options:

reenable (bool) - Execute the task even next time.


71
72
73
74
# File 'lib/peony/actions.rb', line 71

def invoke(task, config = {})
  Rake.application.invoke_task task
  Rake::Task[task].reenable if config[:reenable]
end

#measure(&blk) ⇒ Object

### measure Measures the time (in seconds) a block takes. Returns a [time, output] tuple.



53
54
55
56
57
# File 'lib/peony/actions.rb', line 53

def measure(&blk)
  t = Time.now
  output = yield
  [Time.now - t, output]
end

#mkdir_p(*dirs) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/peony/actions.rb', line 21

def mkdir_p(*dirs)
  dirs.each do|dir|
    say "mkdir #{dir}", :yellow, true
    FileUtils.mkdir_p(dir) if !FileTest.exists?(dir)
    fail "#{dir} must be a directory!" unless FileTest.directory?(dir)
  end
end

#relative_to_original_destination_root(path, remove_dot = true) ⇒ Object

Returns the given path relative to the absolute root (ie, root where the script started).



12
13
14
15
16
17
18
19
# File 'lib/peony/actions.rb', line 12

def relative_to_original_destination_root(path, remove_dot = true)
  path = path.dup
  if path.gsub!(@destination_stack[0], '.')
    remove_dot ? (path[2..-1] || '') : path
  else
    path
  end
end

#report_time(&blk) ⇒ Object

### report_time Report time elapsed in the block. Returns the output of the block.

report_time do
  sleep 2
  # do other things
end

# Output:
# Elapsed time: 2.00 seconds


44
45
46
47
48
# File 'lib/peony/actions.rb', line 44

def report_time(&blk)
  time, output = measure &blk
  say 'Elapsed time: %.2f seconds' % [time], :yellow
  output
end

#run(command, config = {}) ⇒ Object

Executes a command returning the contents of the command.

Parameters

command<String>

the command to be executed.

config<Hash>

give :verbose => false to not log the status, :capture => true to hide to output. Specify :with to append an executable to command executation.

Example

inside('vendor') do
  run('ln -s ~/edge rails')
end


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/peony/actions.rb', line 160

def run(command, config={})
  destination = relative_to_original_destination_root(destination_root, false)
  desc = "#{command} from #{destination.inspect}"

  if config[:with]
    desc = "#{File.basename(config[:with].to_s)} #{desc}"
    command = "#{config[:with]} #{command}"
  end

  say_status :run, desc, config.fetch(:verbose, true)

  unless ENV['dry-run']
    config[:capture] ? `#{command}` : system("#{command}")
  end
end

#sudo(cmd) ⇒ Object



29
30
31
# File 'lib/peony/actions.rb', line 29

def sudo(cmd)
  run "sudo #{cmd}"
end