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



15
16
17
# File 'lib/ccios/file_creator.rb', line 15

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

Instance Method Details

#create_empty_directory(dirname) ⇒ Object



84
85
86
87
88
89
# File 'lib/ccios/file_creator.rb', line 84

def create_empty_directory(dirname)
  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_empty_directory_for_group(group) ⇒ Object



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

def create_empty_directory_for_group(group)
  dirname = group.real_path
  create_empty_directory(dirname)
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ccios/file_creator.rb', line 54

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
  group.register_file_to_targets(file_path, targets)
end

#get_unknown_template_tags_for(template_path) ⇒ Object



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

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



44
45
46
# File 'lib/ccios/file_creator.rb', line 44

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

#loggerObject



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

def logger
  FileCreator.logger
end


71
72
73
74
75
76
77
# File 'lib/ccios/file_creator.rb', line 71

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ccios/file_creator.rb', line 23

def templater_options(targets, project)
  defaults = {
    full_username: git_username,
    date: DateTime.now.strftime("%d/%m/%Y"),
  }
  if project.nil?
    if targets.count == 1
      defaults["project_name"] = targets[0]
    else
      raise "A file outside an xcode project cannot require multiple targets"
    end
  else
    if targets.count == 1
      defaults["project_name"] = targets[0].display_name
    else
      defaults["project_name"] = project.project_name_from_path
    end
  end
  defaults
end