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
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
86
87
88
89
90
91
# 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
          if code_module.project_file_root && code_module.test_file_root
              FileUtils.mkdir_p code_module.project_file_root if code_module.project_file_root
              FileUtils.mkdir_p code_module.test_file_root if code_module.test_file_root
          else
              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
          end

	FileUtils.mkdir_p code_module.test_unit_path if code_module.test_unit_path
	FileUtils.mkdir_p code_module.test_snapshot_path if code_module.test_snapshot_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,
														code_module.project_file_root)
	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.test_file_root,
														[code_module.project_group_path])
	end

	# Creating unit test files
	if code_module.test_unit_target && code_module.test_unit_path && code_module.test_unit_testable_import
		puts('Creating unit test files...')
		process_files_if_needed(template.test_unit_files,
														code_module,
														template,
														project,
														[code_module.test_unit_target],
														code_module.test_unit_path,
														code_module.test_unit_path,
														code_module.test_unit_path,
														[code_module.test_unit_path])
	end

	# Creating snapshot test files
	if code_module.test_snapshot_target && code_module.test_snapshot_path && code_module.test_snapshot_testable_import
		puts('Creating snapshot test files...')
		process_files_if_needed(template.test_snapshot_files,
														code_module,
														template,
														project,
														[code_module.test_snapshot_target],
														code_module.test_snapshot_path,
														code_module.test_snapshot_path,
														code_module.test_snapshot_path,
														[code_module.test_snapshot_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, root_path, processed_groups = []) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/generamba/module_generator.rb', line 93

def process_files_if_needed(files, code_module, template, project, targets, group_path, dir_path, root_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, code_module.create_logical_groups) 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 root_path
			XcodeprojHelper.add_group_to_project(project, group_path, dir_path, directory_name, code_module.create_logical_groups)

			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)
              if code_module.create_logical_groups
                  file_path = root_path
              else
                  file_path = dir_path
                  file_path = file_path.join(file_group) if file_group
              end
              
		file_path = file_path.join(file_name) if file_name

		# Creating the file in the filesystem
              FileUtils.mkdir_p File.dirname(file_path) if !code_module.create_logical_groups
		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,
																										root_path,
																										file_is_resource)
	end
end