Module: Reciper::Helpers

Defined in:
lib/reciper/helpers.rb

Instance Method Summary collapse

Instance Method Details

#copy_file(filename, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/reciper/helpers.rb', line 8

def copy_file(filename, options={})
  destination_dir = @ruby_app_path + "/" + (options[:to] || "")

  unless File.directory?(destination_dir)
    FileUtils.mkdir_p(destination_dir)
  end

  FileUtils.cp(@recipe_path + "/" + filename, destination_dir)

  @operations << [:copy, (options[:to] || "") + filename]
end

#copy_line_range(from, to, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/reciper/helpers.rb', line 43

def copy_line_range(from, to, options={})
  if options[:from_lines]
    from_lines = Range.new(options[:from_lines].first - 1, options[:from_lines].last - 1)
  else
    from_lines = (0..-1)
  end

  from_file_lines = File.open(@recipe_path + "/" + from, "r").readlines
  output_lines = File.read(@ruby_app_path + "/" + to).split("\n")
  original_output = output_lines.dup
  to_file_output = File.open(@ruby_app_path + "/" + to, "w")

  to_output = output_lines.insert(options[:to_line] - 1, from_file_lines.map(&:chomp).slice(from_lines)).flatten!.join("\n")

  to_file_output.write(to_output)

  to_file_output.close

  @operations << [:copy_range, to, original_output.join("\n")]
end

#rollbackObject



65
66
67
68
69
70
71
72
73
# File 'lib/reciper/helpers.rb', line 65

def rollback
  @operations.reverse.each do |operation|
    if operation[0] == :copy
      FileUtils.rm(@ruby_app_path + "/" + operation[1])
    elsif operation[0] == :copy_range
      File.open(@ruby_app_path + "/" + operation[1], "w") { |file| file.write(operation[2]) }
    end
  end
end

#run_rake_task(task) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/reciper/helpers.rb', line 33

def run_rake_task(task)
  Dir.chdir(@ruby_app_path) do
    spawn("bundle exec rake #{task}", :out => "/dev/null", :err => "/dev/null")

    Process.wait
  end

  $?.exitstatus == 0
end

#run_tests(options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/reciper/helpers.rb', line 20

def run_tests(options={})
  Dir.chdir(@ruby_app_path) do
    response = `bundle exec rspec spec`

    if response =~ /([.FE]+)/
      $1.split("").reject { |char| char == "." }.size
    else
      puts "Can't get any test output"
      fail NoTestOutput
    end
  end
end