Class: IOS::ProjectManipulator

Inherits:
Object
  • Object
show all
Defined in:
lib/ios/module/setup/ProjectManipulator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ProjectManipulator

Returns a new instance of ProjectManipulator.



11
12
13
14
15
16
17
18
19
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 11

def initialize(options)
	@configurator = options.fetch(:configurator)
	@keep_demo = options.fetch(:keep_demo)
	@module_type = options.fetch(:module_type)
	@has_swiftgen = options.fetch(:has_swiftgen)
	@has_interface = options.fetch(:has_interface)
	@has_test = options.fetch(:has_test)
	@module_root_path = ""
end

Instance Attribute Details

#configuratorObject (readonly)

Returns the value of attribute configurator.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def configurator
  @configurator
end

#has_interfaceObject (readonly)

Returns the value of attribute has_interface.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def has_interface
  @has_interface
end

#has_swiftgenObject (readonly)

Returns the value of attribute has_swiftgen.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def has_swiftgen
  @has_swiftgen
end

#has_testObject (readonly)

Returns the value of attribute has_test.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def has_test
  @has_test
end

#keep_demoObject (readonly)

Returns the value of attribute keep_demo.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def keep_demo
  @keep_demo
end

#module_pathObject (readonly)

Returns the value of attribute module_path.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def module_path
  @module_path
end

#module_root_pathObject (readonly)

Returns the value of attribute module_root_path.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def module_root_path
  @module_root_path
end

#module_typeObject (readonly)

Returns the value of attribute module_type.



5
6
7
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 5

def module_type
  @module_type
end

Class Method Details

.perform(options) ⇒ Object



7
8
9
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 7

def self.perform(options)
	new(options).perform
end

Instance Method Details

#createExampleProjectObject



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/ios/module/setup/ProjectManipulator.rb', line 75

def createExampleProject
	@configurator.printMessage("Criando projeto Exemplo")
	projectFileName = File.join(@module_root_path, "Example")
	FileUtils.cp_r(File.join(@configurator.template_path, "/Example/Example"), @module_root_path)

	podDevInjection = "pod '${POD_NAME}', path: '../'"

	if @has_test == :yes 
		podDevInjection = "pod '${POD_NAME}', path: '../', :testspecs => ['Tests']"
	end

	if @has_interface == :yes 
		podDevInjection += "\n	pod '${POD_NAME}Interface', path: '../'"
	end

	if @module_type == :feature 
		podDevInjection += "\n\n	pod 'SBFoundation', path: '../../../CoreModules/SBFoundation'"
		podDevInjection += "\n	pod 'SBUIKit', path: '../../../CoreModules/SBUIKit'"
		podDevInjection += "\n	pod 'SBRouterServiceInterface', path: '../../../CoreModules/SBRouterService'"
		podDevInjection += "\n	pod 'SBDependencyInterface', path: '../../../CoreModules/SBDependency'"
	end

	text = File.read projectFileName + "/Podfile"
	text.gsub!("${DEV_PODS}", podDevInjection)
	text.gsub!("${POD_NAME}", @configurator.pod_name)
	File.open(projectFileName + "/Podfile", "w") { |file| file.puts text}
	@configurator.printDone
end

#createModuleDummiesObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 126

def createModuleDummies
	@configurator.printMessage("Gerando arquivos de exemplo")
	
	if @module_type == :feature
		FileUtils.touch File.join(@module_path, "Sources", "Features", "Dummy.swift")
	else
		FileUtils.touch File.join(@module_path, "Sources", "DummySource.swift")
	end
	
	# Header.h
	headerFilename = File.join(@module_path, "Resources", @configurator.pod_name + ".h")
	FileUtils.cp(
		File.join(@configurator.template_path, "Feature", "Header_template"),
		headerFilename
	)
	text = File.read(headerFilename)
	text.gsub!("${POD_NAME}", @configurator.pod_name)
	File.open(headerFilename, "w") { |file| file.puts text}

	# Info.plist
	FileUtils.cp(
		File.join(@configurator.template_path, "Feature", "Info_template"),
		File.join(@module_path, "Resources", "Info.plist")
	)

	if @has_swiftgen == :yes
		# Bundle
		bundleFilename = File.join(@module_path, "Sources", "ModuleConfiguration", "Bundle.swift")
		FileUtils.cp(
			File.join(@configurator.template_path, "Feature", "bundle_template"),
			bundleFilename
		)

		text = File.read(bundleFilename)
		text.gsub!("${POD_NAME}", @configurator.pod_name)
		File.open(bundleFilename, "w") { |file| file.puts text}

		# Strings
		FileUtils.touch File.join(@module_path, "Resources", "Localizable.strings")
		File.open(File.join(@module_path, "Resources", "Localizable.strings"), "w") { |file|
			file.puts "\"example.title.text\" = \"EXAMPLE\";"
		}

		# Assets
		FileUtils.mkdir_p(File.join(@module_path, "Resources", "Assets.xcassets"))
	end

	@configurator.printDone
end

#createModuleImplFolderObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 104

def createModuleImplFolder
	@configurator.printMessage("Gerando pastas base")
	FileUtils.mkdir_p File.join(@module_path, "Sources")
	if @module_type == :feature 
		FileUtils.mkdir_p File.join(@module_path, "Sources", "Features")
	end

	if @has_swiftgen == :yes
		FileUtils.mkdir_p File.join(@module_path, "Sources", "ModuleConfiguration")
	end

	FileUtils.mkdir_p File.join(@module_path, "Resources")
	@configurator.printDone
end

#createModuleInterfaceDummiesObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 176

def createModuleInterfaceDummies 
	@configurator.printMessage("Gerando arquivos de exemplo na Interface")
	FileUtils.touch File.join(@module_path + "Interface", "Sources", "DummyInterface.swift")

	# Info.plist
	FileUtils.cp(
		File.join(@configurator.template_path, "Feature", "Info_template"),
		File.join(@module_path + "Interface", "Resources", "Info.plist")
	)

	# Header.h
	headerFilename = File.join(@module_path + "Interface", "Resources", @configurator.pod_name + ".h")
	FileUtils.cp(
		File.join(@configurator.template_path, "Feature", "Header_template"),
		headerFilename
	)
	text = File.read(headerFilename)
	text.gsub!("${POD_NAME}", @configurator.pod_name)
	File.open(headerFilename, "w") { |file| file.puts text}

	@configurator.printDone
end

#createModuleInterfaceFolderObject



119
120
121
122
123
124
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 119

def createModuleInterfaceFolder
	@configurator.printMessage("Gerando pastas de interface")
	FileUtils.mkdir_p File.join(@module_path + "Interface" , "Sources")
	FileUtils.mkdir_p File.join(@module_path + "Interface", "Resources")
	@configurator.printDone
end

#createModulePathObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 53

def createModulePath
	@configurator.printMessage("Criando estrutura do módulo")
	@module_root_path = File.join(project_folder, [module_folder, @configurator.pod_name])
	@module_path = File.join(@module_root_path, @configurator.pod_name)

	# Create a new folder with pod name 
	FileUtils.mkdir_p @module_path

	# Create a new folder with pod name interface
	if @has_interface == :yes
		FileUtils.mkdir_p @module_path + "Interface"
	end

	# Create a new folder with pod name test
	if @has_test == :yes
		FileUtils.mkdir_p @module_path + "Tests"
	end

	Dir.chdir @module_root_path
	@configurator.printDone
end

#createPodspecObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 214

def createPodspec
	@configurator.printMessage("Configurando arquivo Podspec")
	podspecFilename = "#{module_root_path}/#{@configurator.pod_name}.podspec"
	FileUtils.cp(File.join(@configurator.template_path, "NAME.podspec"), podspecFilename)
	text = File.read podspecFilename
	
	podTests = ""
	if @has_test == :yes 
		podTests = "# TESTS"
		podTests += "\n		s.test_spec 'Tests' do |test_spec|"
		podTests += "\n			test_spec.source_files  = '${POD_NAME}Tests/**/*'"
		podTests += "\n			test_spec.exclude_files = '${POD_NAME}Tests/Resources/*.plist'"
		podTests += "\n		end"
	end

	podInternal = "s.dependency 'SBFoundation'"

	if @module_type == :feature 
		podInternal += "\n		s.dependency 'SBUIKit'"
		podInternal += "\n		s.dependency 'SBDependencyInterface'"
	end

	if @has_interface == :yes
		podInternal += "\n		s.dependency '${POD_NAME}Interface'"
	end

	text.gsub!("${FEATURE_INTERNAL}", podInternal)
	text.gsub!("${FEATURE_TEST}", podTests)
	text.gsub!("${POD_NAME}", @configurator.pod_name)
	text.gsub!("${USER_NAME}", @configurator.user_name)
	text.gsub!("${USER_EMAIL}", @configurator.user_email)
	File.open(podspecFilename, "w") { |file| file.puts text}
	@configurator.printDone
end

#createPodspecInterfaceObject



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 249

def createPodspecInterface
	@configurator.printMessage("Configurando arquivo Podspec Interface")
	podspecFilename = "#{module_root_path}/#{@configurator.pod_name}Interface.podspec"
	FileUtils.cp(File.join(@configurator.template_path, "NAMEInterface.podspec"), podspecFilename)
	text = File.read podspecFilename

	podInternal = ""
	if @module_type == :feature
		podInternal += "\n		s.dependency 'SBRouterServiceInterface'"
	end

	text.gsub!("${FEATURE_INTERNAL}", podInternal)
	text.gsub!("${POD_NAME}", @configurator.pod_name)
	text.gsub!("${USER_NAME}", @configurator.user_name)
	text.gsub!("${USER_EMAIL}", @configurator.user_email)
	File.open(podspecFilename, "w") { |file| file.puts text}
	@configurator.printDone
end

#createSwiftgenObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 199

def createSwiftgen
	@configurator.printMessage("Criando arquivo swiftgen")
	fileName = File.join(@module_root_path, "swiftgen.yml")
	FileUtils.cp(
		File.join(@configurator.template_path, "swiftgen.yml"),
		fileName
	)

	text = File.read(fileName)
	text.gsub!("${POD_NAME}", @configurator.pod_name)

	File.open(fileName, "w") { |file| file.puts text}
	@configurator.printDone
end

#createTestsObject



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 268

def createTests
	@configurator.printMessage("Gerando pastas de testes")
	# Folders
	FileUtils.mkdir_p File.join(@module_path + "Tests" , "Resources")
	FileUtils.mkdir_p File.join(@module_path + "Tests" , "Sources")
	FileUtils.mkdir_p File.join(@module_path + "Tests" , "Tests")

	# Dummies
	FileUtils.touch File.join(@module_path + "Tests", "Sources", "DummyModel.swift")
	FileUtils.touch File.join(@module_path + "Tests", "Tests", "DummyTest.swift")

	# Info.plist
	FileUtils.cp(
		File.join(@configurator.template_path, "Feature", "Info_template"),
		File.join(@module_path + "Tests", "Resources", "Info.plist")
	)
	
	@configurator.printDone
end

#module_folderObject



48
49
50
51
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 48

def module_folder
	return "FeatureModules" if @module_type == :feature
	"CoreModules"
end

#project_folderObject



42
43
44
45
46
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 42

def project_folder
	$basePath = `git rev-parse --show-toplevel`.chomp
	raise "Para rodar a rotina, você deve estar na raiz do projeto" if $basePath == "."
	return $basePath
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ios/module/setup/ProjectManipulator.rb', line 21

def run
	createModulePath

	createModuleImplFolder
	createModuleDummies
	createPodspec

	if @has_interface == :yes
		createModuleInterfaceFolder
		createModuleInterfaceDummies
		createPodspecInterface
	end

	if @has_test == :yes 
		createTests
	end

	createSwiftgen if @has_swiftgen == :yes 
	createExampleProject if @keep_demo == :yes
end