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.



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

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

Class Method Details

.loggerObject



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

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

Instance Method Details

#create_empty_directory(group) ⇒ Object



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

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



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

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



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

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

#loggerObject



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

def logger
  FileCreator.logger
end


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

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



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

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