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"

Class Method Summary collapse

Class Method Details

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

Main method that generate the VIPER files structure



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vipergen/generator.rb', line 9

def self.generate_viper(template, language, name, path)
	puts "Generating VIPER-Module"
	puts "Template: #{template}"
	puts "Language: #{language}"
	puts "Name: #{name}"
	puts "Path: #{path}"
	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)
end

.rename_file(file, name) ⇒ Object

Rename a given file

  • It renames the name of the file

  • It renames the content of the file



35
36
37
38
39
# File 'lib/vipergen/generator.rb', line 35

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

.rename_file_content(filename, name) ⇒ Object

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vipergen/generator.rb', line 43

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

	# Replacing content
	content = content.gsub((Vipergen::Generator::REPLACEMENT_KEY), name)

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

.rename_files(files, name) ⇒ Object

Rename all the files in the files array

  • It renames the name of the file

  • It renames the content of the file



25
26
27
28
29
30
# File 'lib/vipergen/generator.rb', line 25

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