Class: FileCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/ccios/file_creator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerObject



20
21
22
# File 'lib/ccios/file_creator.rb', line 20

def self.logger
  @@logger ||= create_logger
end

Instance Method Details

#create_empty_directory(group) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/ccios/file_creator.rb', line 75

def create_empty_directory(group)
  dirname = group.real_path
  FileUtils.mkdir_p dirname unless File.directory?(dirname)

  git_keep_path = File.join(dirname, ".gitkeep")
  FileUtils.touch(git_keep_path) if Dir.empty?(dirname)
end

#create_file_using_template_path(template_path, generated_filename, group, targets, context) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ccios/file_creator.rb', line 47

def create_file_using_template_path(template_path, generated_filename, group, targets, context)
  file_path = File.join(group.real_path, generated_filename)

  raise "File #{file_path} already exists" if File.exist?(file_path)
  dirname = File.dirname(file_path)
  FileUtils.mkdir_p dirname unless File.directory?(dirname)
  file = File.new(file_path, 'w')

  context = context.merge(templater_options(targets[0]))
  file_content = CodeTemplater.new.render_file_content_from_template(template_path, generated_filename, context)

  file.puts(file_content)

  file.close
  file_ref = group.new_reference(file_path)
  targets.each do |target|
    target.add_file_references([file_ref])
  end
end

#get_unknown_template_tags_for(template_path) ⇒ Object



41
42
43
44
45
# File 'lib/ccios/file_creator.rb', line 41

def get_unknown_template_tags_for(template_path)
  tags = CodeTemplater.new.get_unknown_context_keys_for_template(template_path)
  tags.subtract(Set["project_name", "full_username", "date"])
  tags
end

#git_usernameObject



37
38
39
# File 'lib/ccios/file_creator.rb', line 37

def git_username
  `git config user.name`.strip
end

#loggerObject



24
25
26
# File 'lib/ccios/file_creator.rb', line 24

def logger
  FileCreator.logger
end


67
68
69
70
71
72
73
# File 'lib/ccios/file_creator.rb', line 67

def print_file_content_using_template(filename, template_path, context)
  file_content = CodeTemplater.new.render_file_content_from_template(template_path, filename, context)
  Mustache.render(File.read(template_path), context)

  logger.info "Add this snippet to #{filename}"
  logger.info file_content
end

#templater_options(target) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/ccios/file_creator.rb', line 28

def templater_options(target)
  defaults = {
    project_name: target.display_name,
    full_username: git_username,
    date: DateTime.now.strftime("%d/%m/%Y"),
  }
  defaults
end