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
-
#copy_file_if_missing(src, dest, opt = {}) ⇒ nil
Copy a file from
src
todest
, but only ifdest
does not already exist. -
#files_are_same?(file1, file2) ⇒ Boolean
Whether or not the two files have the same content.
-
#get_template(path, opt = {}) ⇒ ERB, String
Get the template with the given name.
-
#touch_directory(path, opt = {}) ⇒ nil
Recursively create directories in the given path if they are missing.
-
#touch_file(path, opt = {}) ⇒ nil
Create the file at the given path, Creates directories along the path as required.
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.
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.
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
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. 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.
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.
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 |