module Peony::Actions

Public Instance Methods

apply(path, config={}) click to toggle source

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"
# File lib/peony/actions.rb, line 175
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_root() click to toggle source
# File lib/peony/actions.rb, line 8
def destination_root
  destination_stack.last
end
destination_stack() click to toggle source
# File lib/peony/actions.rb, line 4
def destination_stack
  @destination_stack ||= [File.expand_path(Dir.pwd || '')]
end
erb(file, b=binding) click to toggle source

### erb Evaluates an ERB block in the current scope and returns a string.

a = 1
b = 2

# Assuming foo.erb is <%= a %> and <%= b %>
puts erb('foo.erb')

#=> "1 and 2"

Returns the output string of the ERB template.

# File lib/peony/actions.rb, line 56
def erb(file, b=binding)
  require 'erb'
  ERB.new(File.read(file), nil, '-').result(b)
end
in_root() { || ... } click to toggle source

Goes to the root and execute the given block.

# File lib/peony/actions.rb, line 159
def in_root
  inside(destination_stack.first) { yield }
end
inside(dir='', config={}) { |destination_root| ... } click to toggle source

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.

# File lib/peony/actions.rb, line 132
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) && !dry_run
    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 = {}) click to toggle source

### 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.
# File lib/peony/actions.rb, line 117
def invoke(task, config = {})
  Rake.application.invoke_task task
  Rake::Task[task].reenable if config[:reenable]
end
measure() { || ... } click to toggle source

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

# File lib/peony/actions.rb, line 99
def measure(&blk)
  t = Time.now
  output = yield
  [Time.now - t, output]
end
mkdir_p(*dirs) click to toggle source
# File lib/peony/actions.rb, line 62
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
original_destination_root(path, remove_dot = true) click to toggle source

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

# File lib/peony/actions.rb, line 15
def 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
parse_args() click to toggle source
# File lib/peony/actions.rb, line 25
def parse_args
  ARGV.each do |arg|
    set $1.strip.to_sym, $2 if arg =~ /([^=]+)=(.+)/
  end
end
report_time(&blk) click to toggle source

### 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
# File lib/peony/actions.rb, line 90
def report_time(&blk)
  time, output = measure &blk
  say 'Elapsed time: %.2f seconds' % [time], :yellow
  output
end
run(command, config={}) click to toggle source

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
# File lib/peony/actions.rb, line 206
def run(command, config={})
  destination = 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) click to toggle source
# File lib/peony/actions.rb, line 75
def sudo(cmd)
  run "sudo #{cmd}"
end
template(from, to, override=false, sudo=false) click to toggle source
# File lib/peony/actions.rb, line 31
def template(from, to, override=false, sudo=false)
  template = find_templates(from).first
  raise "Can't find tempalte #{from} in directory #{template_paths}." unless template
  raise "File #{to} have already exists." if !override && File.exists?(to)
  say "copy #{template} to #{to}", :green

  target = sudo ? "/tmp/peony-#{rand(10000)}" : to
  open(target, 'w+') do |out|
    out.write(erb(template))
  end
  sudo "mv #{tmp} #{to}" if sudo
end
touch(*files) click to toggle source
# File lib/peony/actions.rb, line 70
def touch(*files)
  say "touch #{files}", :yellow, true
  FileUtils.touch(files)
end