Class: Generamba::ModuleGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/generamba/module_generator.rb

Overview

Responsible for creating the whole code module using information from the CLI

Instance Method Summary collapse

Instance Method Details

#generate_module(name, code_module, template) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/generamba/module_generator.rb', line 11

def generate_module(name, code_module, template)
	# Setting up Xcode objects
	project = XcodeprojHelper.obtain_project(code_module.xcodeproj_path)

	# Configuring file paths
	FileUtils.mkdir_p code_module.project_file_path if code_module.project_file_path
	FileUtils.mkdir_p code_module.test_file_path if code_module.test_file_path

	# Creating code files
	if code_module.project_targets && code_module.project_group_path && code_module.project_file_path
		puts('Creating code files...')
		process_files_if_needed(template.code_files,
														code_module,
														template,
														project,
														code_module.project_targets,
														code_module.project_group_path,
														code_module.project_file_path)
	end

	# Creating test files
	if code_module.test_targets && code_module.test_group_path && code_module.test_file_path
		puts('Creating test files...')
		process_files_if_needed(template.test_files,
														code_module,
														template,
														project,
														code_module.test_targets,
														code_module.test_group_path,
														code_module.test_file_path,
														[code_module.project_group_path])
	end

	# Saving the current changes in the Xcode project
	project.save

	puts 'Module successfully created!'
	puts "Name: #{name}".green
	puts "Project file path: #{code_module.project_file_path}".green if code_module.project_file_path
	puts "Project group path: #{code_module.project_group_path}".green if code_module.project_group_path
	puts "Test file path: #{code_module.test_file_path}".green if code_module.test_file_path
	puts "Test group path: #{code_module.test_group_path}".green if code_module.test_group_path
end

#process_files_if_needed(files, code_module, template, project, targets, group_path, dir_path, processed_groups = []) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/generamba/module_generator.rb', line 55

def process_files_if_needed(files, code_module, template, project, targets, group_path, dir_path, processed_groups = [])
	# It's possible that current project doesn't test targets configured, so it doesn't need to generate tests.
	# The same is for files property - a template can have only test or project files
	if targets.count == 0 || files == nil || files.count == 0 || dir_path == nil || group_path == nil
		return
	end

	XcodeprojHelper.clear_group(project, targets, group_path) unless processed_groups.include? group_path
	files.each do |file|
		unless file[TEMPLATE_FILE_PATH_KEY]
			directory_name = file[TEMPLATE_NAME_KEY].gsub(/^\/|\/$/, '')
			file_group = dir_path.join(directory_name)

			FileUtils.mkdir_p file_group
			XcodeprojHelper.add_group_to_project(project, group_path, dir_path, directory_name)

			next
		end

		file_group = File.dirname(file[TEMPLATE_NAME_KEY])
		file_group = nil if file_group == '.'

		module_info = ModuleInfoGenerator.new(code_module)

		# Generating the content of the code file and it's name
		file_name, file_content = ContentGenerator.create_file(file, module_info.scope, template)
		file_path = dir_path
		file_path = file_path.join(file_group) if file_group
		file_path = file_path.join(file_name) if file_name

		# Creating the file in the filesystem
		FileUtils.mkdir_p File.dirname(file_path)
		File.open(file_path, 'w+') do |f|
			f.write(file_content)
		end

		file_is_resource = file[TEMPLATE_FILE_IS_RESOURCE_KEY]

		# Creating the file in the Xcode project
		XcodeprojHelper.add_file_to_project_and_targets(project,
																										targets,
																										group_path,
																										dir_path,
																										file_group,
																										file_name,
																										file_is_resource)
	end
end