Module: Cog::Generator::FileMethods

Included in:
Cog::Generator
Defined in:
lib/cog/generator/file_methods.rb

Overview

Methods for working with files

Instance Method Summary collapse

Instance Method Details

#copy_file_if_missing(src, dest, opt = {}) ⇒ nil

Copy a file from src to dest, but only if dest does not already exist.

Parameters:

  • src (String)

    where to copy from

  • dest (String)

    where to copy to

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

    a customizable set of options

Options Hash (opt):

  • :quiet (Boolean) — default: false

    suppress writing to STDOUT?

Returns:

  • (nil)


36
37
38
39
40
41
42
# 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) ⇒ Boolean

Returns whether or not the two files have the same content.

Parameters:

  • file1 (File)

    a file

  • file2 (File)

    another file

Returns:

  • (Boolean)

    whether or not the two files have the same content



71
72
73
# 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 = {}) ⇒ ERB, String

Get the template with the given name

Parameters:

  • path (String)

    path to file relative to the Config#template_path

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

    a customizable set of options

Options Hash (opt):

  • :absolute (Boolean) — default: false

    is the path argument absolute?

  • :as_path (Boolean) — default: false

    return the template as an ERB instance (false) or an absolute path to the file (true)

Returns:

  • (ERB, String)

    an instance of ERB or an absolute path to the template



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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 = {}) ⇒ nil

Recursively create directories in the given path if they are missing.

Parameters:

  • path (String)

    a file system path representing a directory

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

    a customizable set of options

Options Hash (opt):

  • :quiet (Boolean) — default: false

    suppress writing to STDOUT?

Returns:

  • (nil)


48
49
50
51
52
53
54
# 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 = {}) ⇒ nil

Create the file at the given path, Creates directories along the path as required.

Parameters:

  • path (String)

    a file system path representing a file

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

    a customizable set of options

Options Hash (opt):

  • :quiet (Boolean) — default: false

    suppress writing to STDOUT?

Returns:

  • (nil)


61
62
63
64
65
66
# 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