Class: MVCgen::Generator

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

Overview

Cosntants

Constant Summary collapse

LANGUAGES =

Constants

["swift", "podfile"]
REPLACEMENT_KEY =
"MVCGEN"

Class Method Summary collapse

Class Method Details

.add_files_togroup(project, group, target) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mvcgen/generator.rb', line 36

def self.add_files_togroup(project, group, target)

	if File.exist?(File.realpath(group.path))

		Dir.foreach(group.path) do |entry|
			filePath = File.join(group.path, entry)

			# puts filePath

			if filePath.to_s.end_with?(".DS_Store", ".xcconfig") then
				# ignore

			elsif filePath.to_s.end_with?(".lproj") then
				if @variant_group.nil?
					@variant_group = group.new_variant_group("Localizable.strings");
				end
				string_file = File.join(filePath, "Localizable.strings")
				fileReference = @variant_group.new_reference(string_file)
				target.add_resources([fileReference])

			elsif self.is_resource_group(entry) then
				fileReference = group.new_reference(filePath)
				target.add_resources([fileReference])
			elsif !File.directory?(filePath) then

				fileReference = group.new_reference(filePath)
				if filePath.to_s.end_with?(".swift", ".mm", ".m", ".cpp") then
					target.add_file_references([fileReference])		
				elsif filePath.to_s.end_with?(".pch") then

				elsif filePath.to_s.end_with?("Info.plist") && entry == "Info.plist" then

				elsif filePath.to_s.end_with?(".h") then
					# target.headers_build_phase.add_file_reference(fileReference)
				elsif filePath.to_s.end_with?(".framework") || filePath.to_s.end_with?(".a") then
					target.frameworks_build_phases.add_file_reference(fileReference)
				elsif 
					target.add_resources([fileReference])
				end
			elsif File.directory?(filePath) && entry != '.' && entry != '..' then

				subGroup = group.find_subpath(entry, true)
				subGroup.set_source_tree(group.source_tree)
				subGroup.set_path(File.join(group.path, entry))
				add_files_togroup(project, target, subGroup)

			end
		end
	end
end

.add_to_xcode(path, name, filespath) ⇒ Object



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

def self.add_to_xcode(path, name, filespath)
	puts "Adding files to xcode project..."
	# proj = Xcodeproj::Project.new("#{path}/#{name}.xcodeproj")
	# app_target_dev = proj.new_target(:application, "#{name} - Development", :ios, '10.0')
	# header_ref = proj.main_group.new_file('./Class.swift')

	project_file = "#{path}/#{name}.xcodeproj"
	proj = Xcodeproj::Project.open(project_file)

	# TODO: view this to improve path files: allowable_project_paths
	self.add_files_togroup(proj, proj.main_group, proj.targets.first)
	# self.addxcodefiles(filespath,proj.main_group,proj.targets.first)
	
	proj.save
	puts "Finish adding files"
end

.addxcodefiles(direc, current_group, main_target) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mvcgen/generator.rb', line 87

def self.addxcodefiles(direc, current_group, main_target)
	Dir.glob(direc) do |item|
		next if item == '.' or item == '.DS_Store'
			if File.directory?(item)
			new_folder = File.basename(item)
			created_group = current_group.new_group(new_folder)
			self.addxcodefiles("#{item}/*", created_group, main_target)
		elsif item == "Assets.xcassets"
			main_target.add_frameworks_bundles([item])
			break
		else 
		  i = current_group.new_file(item)
		  main_target.add_file_references([i])
		end
	end
end

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

Main method that generate the MVC files structure



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mvcgen/generator.rb', line 105

def self.generate_mvc(template, language, name, path)

	puts "Generating MVC-Module"
	puts "Template: #{template}"
	puts "Language: #{language}"
	puts "Name: #{name}"
	puts "Path: #{path}"

	# Copy language files to project folder
	path_from = MVCgen::FileManager.path_from(template, language)
	path_to = MVCgen::FileManager.destination_mvc_path(path, name)
	MVCgen::FileManager.copy(path_from, path_to)

	# Copy podfile to project folder
	podfile_path = MVCgen::FileManager.path_from(template, "podfile")
	expand_path = File.expand_path(path)
	MVCgen::FileManager.copy(podfile_path, expand_path)

	# TODO: later if needed
	files = MVCgen::FileManager.files_in_path(expand_path)
	rename_files(files,name)
	self.add_to_xcode(path, name, path_to)
end

.is_resource_group(file) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/mvcgen/generator.rb', line 28

def self.is_resource_group(file)
	extname= file[/\.[^\.]+$/]
	if extname == '.bundle' || extname == '.xcassets' then
		return true
	end
	return false
end

.rename_file(file, name) ⇒ Object

Rename a given file

  • It renames the name of the file

  • It renames the content of the file



144
145
146
147
148
# File 'lib/mvcgen/generator.rb', line 144

def self.rename_file(file, name)
	new_path = file.gsub((MVCgen::Generator::REPLACEMENT_KEY), name)
	MVCgen::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 MVC replaced by ‘name’



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mvcgen/generator.rb', line 152

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

	# Replacing content
	content = content.gsub((MVCgen::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



132
133
134
135
136
137
138
139
# File 'lib/mvcgen/generator.rb', line 132

def self.rename_files(files, name)
	files.each do |file|
		rename_file_content(file, name)
		# if file.include? (MVCgen::Generator::REPLACEMENT_KEY)
		# 	rename_file(file, name)
		# end
	end
end