Module: Flak::FileActions

Included in:
Target
Defined in:
lib/flak/rake/file_actions.rb

Instance Method Summary collapse

Instance Method Details

#make_directory_for(file) ⇒ Object

Make a directory to contain a file. Create directories recursively if necessary.

Parameters:

  • file (String)

    the file that needs a directory.



43
44
45
# File 'lib/flak/rake/file_actions.rb', line 43

def make_directory_for(file)
  FileUtils.mkdir_p file.pathmap('%d')
end

Create a symlink. Remove it if it already exists.

Parameters:

  • target_file (String)

    the file to point the new file at.

  • new_file (String)

    the new file.



16
17
18
19
20
21
# File 'lib/flak/rake/file_actions.rb', line 16

def rebuild_symlink(target_file,new_file)
  if File.symlink?(new_file)
    File.unlink(new_file)
  end
  File.symlink(target_file, new_file)
end

#remove_and_copy(source, destination) ⇒ Object

Remove a file and copy a new file over. If we just copy source to destination and destination exists and is a directory, then the src is put in the destination, as opposed to overwriting it. For this reason, we delete destination first.

Parameters:

  • source (String)

    the source file.

  • destination (String)

    the destination file.



32
33
34
35
# File 'lib/flak/rake/file_actions.rb', line 32

def remove_and_copy(source,destination)
  remove_file(destination, true) 
  cp_r source, destination
end

#write_erb_template(erb_file, released_file, opts = {}) ⇒ Object

Create a destination file by filtering a source file through ERB.

Parameters:

  • erb_file (String)

    the file to be filtered.

  • released_file (String)

    the result filename.

  • opts (Hash) (defaults to: {})

    the options.

Options Hash (opts):

  • :chmod (String)

    The file mode.



54
55
56
57
58
59
60
61
62
63
# File 'lib/flak/rake/file_actions.rb', line 54

def write_erb_template(erb_file,released_file, opts={})
  if (!(opts[:no_force] && File.exists?(released_file) ))
    template = ERB.new( File.read(erb_file) , 0, "%<>")
    File.open(released_file, 'w') do |f|
      puts "template #{erb_file} #{released_file}"
      f.puts(template.result(binding))
      f.chmod(opts[:chmod].to_i)   if opts[:chmod]
    end
  end
end