Class: Vipergen::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/vipergen/generator.rb

Overview

Cosntants

Constant Summary collapse

LANGUAGES =

Constants

["swift", "objc"]
REPLACEMENT_KEY =
"VIPER"
AUTHOR_REPLACEMENT_KEY =
"AUTHOR"
YEAR_REPLACEMENT_KEY =
"YEAR"
COMPANY_REPLACEMENT_KEY =
"COMPANY"

Class Method Summary collapse

Class Method Details

.generate_viper(template, language, name, path, author, company) ⇒ Object

Main method that generate the VIPER files structure



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vipergen/generator.rb', line 12

def self.generate_viper(template, language, name, path, author, company)
	puts "Generating VIPER-Module"
	puts "Template: #{template}"
	puts "Language: #{language}"
	puts "Name: #{name}"
	puts "Path: #{path}"
	puts "Author: #{author}"
      		puts "Company: #{company}"
	path_from = Vipergen::FileManager.path_from(template, language)
	path_to = Vipergen::FileManager.destination_viper_path(path, name)
	Vipergen::FileManager.copy(path_from, path_to)
	files = Vipergen::FileManager.files_in_path(path_to)
	rename_files(files, name, author, company)
end

.rename_file(file, name, author, company) ⇒ Object

Rename a given file

  • It renames the name of the file

  • It renames the content of the file



40
41
42
43
44
# File 'lib/vipergen/generator.rb', line 40

def self.rename_file(file, name, author, company)
	new_path = file.gsub((Vipergen::Generator::REPLACEMENT_KEY), name)
	Vipergen::FileManager.move(file, new_path)
	rename_file_content(new_path, name, author, company)
end

.rename_file_content(filename, name, author, company) ⇒ Object

Rename the file content @return: An String with the every VIPER replaced by ‘name’



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

def self.rename_file_content(filename, name, author, company)
	# Reading content
	file = File.open(filename, "rb")
	content = file.read
	file.close

	# Replacing content
	content = content.gsub((Vipergen::Generator::REPLACEMENT_KEY), name)
	content = content.gsub((Vipergen::Generator::AUTHOR_REPLACEMENT_KEY), author)
	content = content.gsub((Vipergen::Generator::YEAR_REPLACEMENT_KEY), "#{Time.new.year}")
	content = content.gsub((Vipergen::Generator::COMPANY_REPLACEMENT_KEY), company)

	# Saving content with replaced string
	File.open(filename, "w+") do |file|
 				file.write(content)
	end
end

.rename_files(files, name, author, company) ⇒ Object

Rename all the files in the files array

  • It renames the name of the file

  • It renames the content of the file



30
31
32
33
34
35
# File 'lib/vipergen/generator.rb', line 30

def self.rename_files(files, name, author, company)
	files.each do |file|
		raise SyntaxError unless file.include? (Vipergen::Generator::REPLACEMENT_KEY)
		rename_file(file, name, author, company)
	end
end