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



27
28
29
# File 'lib/ccios/file_creator.rb', line 27

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

Instance Method Details

#create_empty_directory(group) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/ccios/file_creator.rb', line 86

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, project, context) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ccios/file_creator.rb', line 58

def create_file_using_template_path(template_path, generated_filename, group, targets, project, 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, project))
  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



52
53
54
55
56
# File 'lib/ccios/file_creator.rb', line 52

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



48
49
50
# File 'lib/ccios/file_creator.rb', line 48

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

#loggerObject



31
32
33
# File 'lib/ccios/file_creator.rb', line 31

def logger
  FileCreator.logger
end


78
79
80
81
82
83
84
# File 'lib/ccios/file_creator.rb', line 78

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(targets, project) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ccios/file_creator.rb', line 35

def templater_options(targets, project)
  defaults = {
    full_username: git_username,
    date: DateTime.now.strftime("%d/%m/%Y"),
  }
  if targets.count == 1
    defaults["project_name"] = targets[0].display_name
  else
    defaults["project_name"] = project.project_name_from_path
  end
  defaults
end