Module: Zing::FileHelper

Defined in:
lib/zing/file_helper.rb

Class Method Summary collapse

Class Method Details

.copy_file_templates(options, feature) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zing/file_helper.rb', line 28

def self.copy_file_templates(options, feature)
  project_absolute_dir = options.project_absolute_dir

  # get the template directories based on the params used
  directories = if feature[/options_base/i]
    Dir.glob(File.dirname(__FILE__) + "/file_templates/base/**")
  elsif feature[/options_push/i]
    Dir.glob(File.dirname(__FILE__) + "/file_templates/push/**")
  else
    []
  end
  
  # traverse each directory and copy the template files
  directories.each do |dir_name|

    dir_name_last = dir_name.split("/").last

    Dir.glob("#{dir_name}/*").each do |file|

      filename = file.split("/").last.gsub(".erb", "")

      # read file as ERB templates
      content = File.open(file, "r").read
      template = ERB.new content

      # file copy location
      file_location = "#{project_absolute_dir}/#{dir_name_last}/#{filename}"

      # write the contents of each file to the project directory
      create_file(file_location, template.result(binding))

    end

  end

end

.create_directory(dir_path) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/zing/file_helper.rb', line 8

def self.create_directory(dir_path)
  if File.exist?(dir_path)
    puts "exists".yellow + " #{dir_path}"
  else
    FileUtils.mkdir_p(dir_path)
    puts "create".green + " #{dir_path}"
  end
end

.create_file(file_path, file_content) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/zing/file_helper.rb', line 17

def self.create_file(file_path, file_content)
  if File.exist?(file_path)
    puts "exists".yellow + " #{file_path}"
  else
    File.open("#{file_path}", "w") do |file|
      file.puts file_content
    end
    puts "create".green + " #{file_path}"
  end
end