Class: FileCreator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FileCreator

Returns a new instance of FileCreator.



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

def initialize(options = {})
  @options = options
end

Class Method Details

.loggerObject



6
7
8
# File 'lib/ccios/file_creator.rb', line 6

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

Instance Method Details

#create_empty_directory(group) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/ccios/file_creator.rb', line 49

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(prefix, suffix, group, target) ⇒ Object



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

def create_file(prefix, suffix, group, target)
  file_path = File.join(group.real_path, prefix + suffix + '.swift')

  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')

  templater_options = templater_options(target)
  code_templater = CodeTemplater.new(templater_options)
  file_content = code_templater.content_for_suffix(prefix, suffix)
  file.puts(file_content)

  file.close
  file_ref = group.new_reference(file_path)
  target.add_file_references([file_ref])
end

#git_usernameObject



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

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

#loggerObject



10
11
12
# File 'lib/ccios/file_creator.rb', line 10

def logger
  FileCreator.logger
end


57
58
59
60
61
62
63
64
65
# File 'lib/ccios/file_creator.rb', line 57

def print_file_content(prefix, suffix)
  file_name = suffix + '.swift'

  code_templater = CodeTemplater.new(@options)
  template = code_templater.content_for_suffix(prefix, suffix)

  logger.info "Add this snippet to #{file_name}"
  logger.info template
end

#templater_options(target) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/ccios/file_creator.rb', line 18

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