Module: OrderlyGarden::DSL

Defined in:
lib/orderly_garden/dsl.rb

Overview

Rake DSL constructs.

Instance Method Summary collapse

Instance Method Details

#parent_task(name) ⇒ Object

Define a task named ‘name` that runs all tasks under an identically named `namespace`.



26
27
28
29
30
31
32
33
34
# File 'lib/orderly_garden/dsl.rb', line 26

def parent_task(name)
  task name do
    Rake::Task
      .tasks
      .select { |t| t.name =~ /^#{name}:/ }
      .sort { |a, b| a.name <=> b.name }
      .each(&:execute)
  end
end

#with_tempfile(fname = nil, &_block) ⇒ Object

Create and manage a temp file, replacing ‘fname` with the temp file, if `fname` is provided.



5
6
7
8
9
10
# File 'lib/orderly_garden/dsl.rb', line 5

def with_tempfile(fname = nil, &_block)
  Tempfile.open("tmp") do |f|
    yield f.path, f.path.shellescape
    FileUtils.cp(f.path, fname) unless fname.nil?
  end
end

#write_file(file_name, file_contents) ⇒ Object

Write an array of strings to a file, adding newline separators, and ensuring a trailing newline at the end of a file.



14
15
16
17
18
19
20
21
22
23
# File 'lib/orderly_garden/dsl.rb', line 14

def write_file(file_name, file_contents)
  contents =  file_contents
              .flatten
              .select { |line| line }
              .join("\n")
  File.open(file_name, "w") do |fh|
    fh.write(contents)
    fh.write("\n")
  end
end