module Cog::Generator::FileMethods

Methods for working with files

Public Instance Methods

copy_file_if_missing(src, dest, opt={}) click to toggle source

Copy a file from src to dest, but only if dest does not already exist. @param src [String] where to copy from @param dest [String] where to copy to @option opt [Boolean] :quiet (false) suppress writing to STDOUT? @return [nil]

# File lib/cog/generator/file_methods.rb, line 36
def copy_file_if_missing(src, dest, opt={})
  unless File.exists? dest
    touch_directory File.dirname(dest), opt
    FileUtils.cp src, dest
    STDOUT.write "Created #{dest.relative_to_project_root}\n".color(:green) unless opt[:quiet]
  end
end
files_are_same?(file1, file2) click to toggle source

@param file1 [File] a file @param file2 [File] another file @return [Boolean] whether or not the two files have the same content

# File lib/cog/generator/file_methods.rb, line 71
def files_are_same?(file1, file2)
  File.exists?(file1) && File.exists?(file2) && File.read(file1) == File.read(file2)
end
get_template(path, opt={}) click to toggle source

Get the template with the given name @param path [String] path to file relative to the {Config#template_path} @option opt [Boolean] :absolute (false) is the path argument absolute? @option opt [Boolean] :as_path (false) return the template as an ERB instance (false) or an absolute path to the file (true) @return [ERB, String] an instance of {ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html ERB} or an absolute path to the template

# File lib/cog/generator/file_methods.rb, line 12
def get_template(path, opt={})
  path = path.to_s
  path += '.erb'
  fullpath = if opt[:absolute]
    path
  else
    Cog.template_path.reverse.inject('') do |found, prefix|
      x = File.join prefix, path
      found.empty? && File.exists?(x) ? x : found
    end
  end
  raise Errors::NoSuchTemplate.new path unless File.exists? fullpath
  if opt[:as_path]
    File.expand_path fullpath
  else
    ERB.new File.read(fullpath), 0, '>'
  end
end
touch_directory(path, opt={}) click to toggle source

Recursively create directories in the given path if they are missing. @param path [String] a file system path representing a directory @option opt [Boolean] :quiet (false) suppress writing to STDOUT? @return [nil]

# File lib/cog/generator/file_methods.rb, line 48
def touch_directory(path, opt={})
  unless File.exists? path
    FileUtils.mkdir_p path
    STDOUT.write "Created #{path.relative_to_project_root}\n".color(:green) unless opt[:quiet]
  end
  nil
end
touch_file(path, opt={}) click to toggle source

Create the file at the given path, Creates directories along the path as required. @param path [String] a file system path representing a file @option opt [Boolean] :quiet (false) suppress writing to STDOUT? @return [nil]

# File lib/cog/generator/file_methods.rb, line 61
def touch_file(path, opt={})
  touch_directory File.dirname(path), opt
  FileUtils.touch path
  STDOUT.write "Created #{path.relative_to_project_root}\n".color(:green) unless opt[:quiet]
  nil
end