iosgen 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +37 -0
- data/.hound.yml +6 -0
- data/.rubocop.yml +7 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +110 -0
- data/Guardfile +16 -0
- data/LICENSE +22 -0
- data/README.md +158 -0
- data/Rakefile +28 -0
- data/bin/iosgen +6 -0
- data/iosgen.gemspec +29 -0
- data/lib/ios_gen/base/action.rb +15 -0
- data/lib/ios_gen/base/base_factory.rb +73 -0
- data/lib/ios_gen/base/interactor.rb +15 -0
- data/lib/ios_gen/base/property.rb +13 -0
- data/lib/ios_gen/base/view_controller.rb +15 -0
- data/lib/ios_gen/base/view_model.rb +16 -0
- data/lib/ios_gen/generator/base_generator.rb +61 -0
- data/lib/ios_gen/generator/objc/action_formatter.rb +35 -0
- data/lib/ios_gen/generator/objc/interactor_formatter.rb +68 -0
- data/lib/ios_gen/generator/objc/objc_formatter.rb +30 -0
- data/lib/ios_gen/generator/objc/view_controller_formatter.rb +37 -0
- data/lib/ios_gen/generator/objc/view_model_formatter.rb +88 -0
- data/lib/ios_gen/ios_gen_thor.rb +17 -0
- data/lib/iosgen.rb +5 -0
- data/spec/ios_gen/base/action_spec.rb +50 -0
- data/spec/ios_gen/base/base_factory_spec.rb +238 -0
- data/spec/ios_gen/base/interactor_spec.rb +60 -0
- data/spec/ios_gen/base/property_spec.rb +26 -0
- data/spec/ios_gen/base/view_controller_spec.rb +18 -0
- data/spec/ios_gen/base/view_model_spec.rb +70 -0
- data/spec/ios_gen/generator/base_generator_spec.rb +84 -0
- data/spec/ios_gen/generator/objc/action_formatter_spec.rb +88 -0
- data/spec/ios_gen/generator/objc/interactor_formatter_spec.rb +79 -0
- data/spec/ios_gen/generator/objc/objc_formatter_spec.rb +52 -0
- data/spec/ios_gen/generator/objc/view_controller_formatter_spec.rb +52 -0
- data/spec/ios_gen/generator/objc/view_model_formatter_spec.rb +125 -0
- data/spec/ios_gen/generator/spec.json +58 -0
- data/spec/spec_helper.rb +8 -0
- data/templates/objc/Interactor.h.erb +11 -0
- data/templates/objc/Interactor.m.erb +13 -0
- data/templates/objc/InteractorProtocol.h.erb +9 -0
- data/templates/objc/UIViewController.h.erb +8 -0
- data/templates/objc/UIViewController.m.erb +31 -0
- data/templates/objc/ViewModel.h.erb +9 -0
- data/templates/objc/ViewModel.m.erb +13 -0
- data/templates/objc/ViewModelProtocol.h.erb +16 -0
- metadata +178 -0
@@ -0,0 +1,15 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Base
|
3
|
+
# Action
|
4
|
+
class Action
|
5
|
+
attr_accessor :description, :name, :return_type, :arguments
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@description = params[:description]
|
9
|
+
@name = params[:name]
|
10
|
+
@return_type = params[:return_type]
|
11
|
+
@arguments = params[:arguments]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Base
|
3
|
+
# Create base object from json
|
4
|
+
class BaseFactory
|
5
|
+
NAME_KEY = 'name'
|
6
|
+
TYPE_KEY = 'type'
|
7
|
+
DESCRIPTION_KEY = 'description'
|
8
|
+
RETURN_KEY = 'return_type'
|
9
|
+
ARGUMENTS_KEY = 'arguments'
|
10
|
+
PROPERTIES_KEY = 'properties'
|
11
|
+
ACTIONS_KEY = 'actions'
|
12
|
+
INTERACTORS_KEY = 'interactors'
|
13
|
+
|
14
|
+
def parse_property(hash)
|
15
|
+
Property.new(type: hash[TYPE_KEY], name: hash[NAME_KEY])
|
16
|
+
end
|
17
|
+
|
18
|
+
def parse_action(hash)
|
19
|
+
Action.new(description: hash[DESCRIPTION_KEY],
|
20
|
+
name: hash[NAME_KEY],
|
21
|
+
return_type: hash[RETURN_KEY],
|
22
|
+
arguments: parse_properties(hash[ARGUMENTS_KEY]))
|
23
|
+
end
|
24
|
+
|
25
|
+
def parse_interactor(hash)
|
26
|
+
Interactor.new(description: hash[DESCRIPTION_KEY],
|
27
|
+
name: hash[NAME_KEY],
|
28
|
+
properties: parse_properties(hash[PROPERTIES_KEY]),
|
29
|
+
actions: parse_actions(hash[ACTIONS_KEY]))
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_view_model(hash)
|
33
|
+
ViewModel.new(description: hash[DESCRIPTION_KEY],
|
34
|
+
name: hash[NAME_KEY],
|
35
|
+
properties: parse_properties(hash[PROPERTIES_KEY]),
|
36
|
+
actions: parse_actions(hash[ACTIONS_KEY]),
|
37
|
+
interactors: parse_interactors(hash[INTERACTORS_KEY]))
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_properties(arguments)
|
41
|
+
return if arguments.nil?
|
42
|
+
arguments_properties = []
|
43
|
+
arguments.each do |property|
|
44
|
+
arguments_properties.push(parse_property(property))
|
45
|
+
end
|
46
|
+
arguments_properties
|
47
|
+
end
|
48
|
+
|
49
|
+
def parse_actions(actions)
|
50
|
+
return if actions.nil?
|
51
|
+
actions_array = []
|
52
|
+
actions.each do |action|
|
53
|
+
actions_array.push(parse_action(action))
|
54
|
+
end
|
55
|
+
actions_array
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_interactors(interactors)
|
59
|
+
return if interactors.nil?
|
60
|
+
interactors_array = []
|
61
|
+
interactors.each do |interactor|
|
62
|
+
interactors_array.push(parse_interactor(interactor))
|
63
|
+
end
|
64
|
+
interactors_array
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_view_controller(hash)
|
68
|
+
ViewController.new(name: hash[NAME_KEY],
|
69
|
+
description: hash[DESCRIPTION_KEY])
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Base
|
3
|
+
# Interactor class
|
4
|
+
class Interactor
|
5
|
+
attr_accessor :description, :name, :properties, :actions
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@description = params[:description]
|
9
|
+
@name = params[:name]
|
10
|
+
@properties = params[:properties]
|
11
|
+
@actions = params[:actions]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Base
|
3
|
+
# ViewController Model
|
4
|
+
class ViewController
|
5
|
+
attr_reader :name, :description
|
6
|
+
attr_accessor :view_model
|
7
|
+
|
8
|
+
def initialize(params = {})
|
9
|
+
@name = params[:name]
|
10
|
+
@description = params[:description]
|
11
|
+
@view_model = params[:view_model]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Base
|
3
|
+
# ViewModel class
|
4
|
+
class ViewModel
|
5
|
+
attr_accessor :description, :name, :properties, :actions, :interactors
|
6
|
+
|
7
|
+
def initialize(params = {})
|
8
|
+
@description = params[:description]
|
9
|
+
@name = params[:name]
|
10
|
+
@properties = params[:properties]
|
11
|
+
@actions = params[:actions]
|
12
|
+
@interactors = params[:interactors]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
module IOSGen
|
5
|
+
module Generator
|
6
|
+
# Base Generator
|
7
|
+
class BaseGenerator
|
8
|
+
VIEWCONTROLLER_KEY = 'view_controller'
|
9
|
+
VIEWMODEL_KEY = 'view_model'
|
10
|
+
INTERACTORS_KEY = 'interactors'
|
11
|
+
|
12
|
+
attr_reader :type, :file_spec, :destination
|
13
|
+
attr_reader :view_model, :view_controller
|
14
|
+
attr_reader :factory, :formatter
|
15
|
+
|
16
|
+
def initialize(hash = {})
|
17
|
+
@type = hash[:type]
|
18
|
+
@file_spec = hash[:file_spec]
|
19
|
+
@destination = hash[:destination]
|
20
|
+
@factory = IOSGen::Base::BaseFactory.new
|
21
|
+
interactor_formatter = Objc::InteractorFormatter.new
|
22
|
+
view_model_formatter = Objc::ViewModelFormatter.new(interactor_formatter)
|
23
|
+
view_controller_formatter = Objc::ViewControllerFormatter.new(view_model_formatter)
|
24
|
+
@formatter = Objc::Formatter.new(view_controller_formatter, view_model_formatter, interactor_formatter)
|
25
|
+
parse
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate
|
29
|
+
generate_view_model
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parse
|
35
|
+
file = File.read(@file_spec)
|
36
|
+
hash = JSON.parse(file)
|
37
|
+
@view_model = @factory.parse_view_model(hash[VIEWMODEL_KEY])
|
38
|
+
@view_controller = @factory.parse_view_controller(hash[VIEWCONTROLLER_KEY])
|
39
|
+
@view_controller.view_model = @view_model unless @view_controller.nil?
|
40
|
+
@formatter.view_model = @view_model unless @view_model.nil?
|
41
|
+
@formatter.view_controller = @view_controller unless @view_controller.nil?
|
42
|
+
end
|
43
|
+
|
44
|
+
def generate_view_model
|
45
|
+
@formatter.generate do |file_name, template|
|
46
|
+
generate_template(file_name, template, @destination)
|
47
|
+
puts "[+] Created #{file_name}".green
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def generate_template(name, template, destination = '.')
|
52
|
+
path_template = File.expand_path("../../../#{template}", File.dirname(__FILE__))
|
53
|
+
renderer = ERB.new(File.read(path_template))
|
54
|
+
destination_path = "#{destination}#{name}"
|
55
|
+
File.open(destination_path, 'w') do |f|
|
56
|
+
f.write renderer.result(binding)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Generator
|
3
|
+
module Objc
|
4
|
+
# Objective-C Action Formatter
|
5
|
+
class ActionFormatter
|
6
|
+
def generate_interface(action)
|
7
|
+
result = ''
|
8
|
+
result += "// #{action.description}\n" unless action.description.nil?
|
9
|
+
result += "- (#{action.return_type})"
|
10
|
+
result += parse_action_arguments(action) + ';'
|
11
|
+
result
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_implementation(action)
|
15
|
+
result = "- (#{action.return_type})"
|
16
|
+
result += parse_action_arguments(action) + "\n{\n}"
|
17
|
+
result
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def parse_action_arguments(action)
|
23
|
+
index = 0
|
24
|
+
final_name = "#{action.name}".gsub(':') do
|
25
|
+
argument = action.arguments[index]
|
26
|
+
index += 1
|
27
|
+
":(#{argument.type})#{argument.name} "
|
28
|
+
end
|
29
|
+
final_name = final_name.chop if index > 0
|
30
|
+
final_name
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Generator
|
3
|
+
module Objc
|
4
|
+
# Objective-C Interactor Formatter
|
5
|
+
class InteractorFormatter
|
6
|
+
# File Names Interactors
|
7
|
+
attr_reader :header_file_name, :_impl_file_name
|
8
|
+
attr_reader :protocol_file_name
|
9
|
+
# Interactor Protocol
|
10
|
+
attr_reader :protocol_name
|
11
|
+
# Interactor Porperties
|
12
|
+
attr_reader :properties_header
|
13
|
+
# Interactor Actions
|
14
|
+
attr_reader :actions_header, :actions_impl
|
15
|
+
# Property
|
16
|
+
attr_accessor :interactor
|
17
|
+
|
18
|
+
def header_file_name
|
19
|
+
"#{@interactor.name}.h"
|
20
|
+
end
|
21
|
+
|
22
|
+
def impl_file_name
|
23
|
+
"#{@interactor.name}.m"
|
24
|
+
end
|
25
|
+
|
26
|
+
def protocol_file_name
|
27
|
+
"#{@interactor.name}Protocol.h"
|
28
|
+
end
|
29
|
+
|
30
|
+
def protocol_name
|
31
|
+
"#{@interactor.name}Protocol"
|
32
|
+
end
|
33
|
+
|
34
|
+
def properties_header
|
35
|
+
properties = ''
|
36
|
+
@interactor.properties.each do |property|
|
37
|
+
properties += "@property (nonatomic, strong) #{property.type}#{property.name};\n"
|
38
|
+
end unless @interactor.properties.nil?
|
39
|
+
properties.chop
|
40
|
+
end
|
41
|
+
|
42
|
+
def actions_header
|
43
|
+
actions = ''
|
44
|
+
action_formatter = ActionFormatter.new
|
45
|
+
@interactor.actions.each do |action|
|
46
|
+
actions += "#{action_formatter.generate_interface(action)}\n"
|
47
|
+
end
|
48
|
+
actions.chop
|
49
|
+
end
|
50
|
+
|
51
|
+
def actions_impl
|
52
|
+
actions = ''
|
53
|
+
action_formatter = ActionFormatter.new
|
54
|
+
@interactor.actions.each do |action|
|
55
|
+
actions += "#{action_formatter.generate_implementation(action)}\n"
|
56
|
+
end
|
57
|
+
actions.chop
|
58
|
+
end
|
59
|
+
|
60
|
+
def generate(&block)
|
61
|
+
block.call(protocol_file_name, 'templates/objc/InteractorProtocol.h.erb')
|
62
|
+
block.call(header_file_name, 'templates/objc/Interactor.h.erb')
|
63
|
+
block.call(impl_file_name, 'templates/objc/Interactor.m.erb')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Generator
|
3
|
+
module Objc
|
4
|
+
# Objc Formatter
|
5
|
+
class Formatter
|
6
|
+
# ViewModel and Interactors
|
7
|
+
attr_accessor :view_controller, :view_model, :interactors
|
8
|
+
# Formatters
|
9
|
+
attr_reader :view_controller_formatter, :view_model_formatter
|
10
|
+
attr_reader :interactor_formatter
|
11
|
+
|
12
|
+
def initialize(view_controller_formatter = ViewControllerFormatter.new,
|
13
|
+
view_model_formatter = ViewModelFormatter.new,
|
14
|
+
interactor_formatter = InteractorFormatter.new)
|
15
|
+
@view_controller_formatter = view_controller_formatter
|
16
|
+
@view_model_formatter = view_model_formatter
|
17
|
+
@interfactor_formatter = interactor_formatter
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate(&block)
|
21
|
+
@view_controller_formatter.view_controller = @view_controller
|
22
|
+
@view_controller_formatter.view_model = @view_model
|
23
|
+
@view_model_formatter.view_model = @view_model
|
24
|
+
@view_controller_formatter.generate(&block)
|
25
|
+
@view_model_formatter.generate(&block)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Generator
|
3
|
+
module Objc
|
4
|
+
# Objective-C Formatter for ViewController
|
5
|
+
class ViewControllerFormatter
|
6
|
+
# ViewController and ViewModel
|
7
|
+
attr_accessor :view_controller, :view_model
|
8
|
+
# File Names
|
9
|
+
attr_reader :header_file_name, :impl_file_name
|
10
|
+
# ViewModel Formatter
|
11
|
+
attr_reader :view_model_formatter
|
12
|
+
|
13
|
+
def initialize(view_model_formatter = ViewModelFormatter.new)
|
14
|
+
@view_model_formatter = view_model_formatter
|
15
|
+
end
|
16
|
+
|
17
|
+
def view_model=(view_model)
|
18
|
+
@view_model = view_model
|
19
|
+
@view_model_formatter.view_model = @view_model
|
20
|
+
end
|
21
|
+
|
22
|
+
def header_file_name
|
23
|
+
"#{view_controller.name}.h"
|
24
|
+
end
|
25
|
+
|
26
|
+
def impl_file_name
|
27
|
+
"#{view_controller.name}.m"
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate(&block)
|
31
|
+
block.call(header_file_name, 'templates/objc/UIViewController.h.erb')
|
32
|
+
block.call(impl_file_name, 'templates/objc/UIViewController.m.erb')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module IOSGen
|
2
|
+
module Generator
|
3
|
+
module Objc
|
4
|
+
# Objetive-C ViewModel Formatter
|
5
|
+
class ViewModelFormatter
|
6
|
+
# ViewModel and Interactors
|
7
|
+
attr_accessor :view_model
|
8
|
+
# File Names ViewModel
|
9
|
+
attr_reader :header_file_name, :impl_file_name
|
10
|
+
attr_reader :protocol_file_name
|
11
|
+
# ViewModel Protocol
|
12
|
+
attr_reader :protocol_name, :protocol_delegate
|
13
|
+
# ViewModel Porperties
|
14
|
+
attr_reader :properties_header
|
15
|
+
# ViewModel Actions
|
16
|
+
attr_reader :actions_header, :actions_impl
|
17
|
+
# Interactor Formatter
|
18
|
+
attr_reader :interactor_formatter
|
19
|
+
|
20
|
+
def initialize(interactor_formatter = InteractorFormatter.new)
|
21
|
+
@interactor_formatter = interactor_formatter
|
22
|
+
end
|
23
|
+
|
24
|
+
def header_file_name
|
25
|
+
"#{@view_model.name}.h"
|
26
|
+
end
|
27
|
+
|
28
|
+
def impl_file_name
|
29
|
+
"#{@view_model.name}.m"
|
30
|
+
end
|
31
|
+
|
32
|
+
def protocol_file_name
|
33
|
+
"#{@view_model.name}Protocol.h"
|
34
|
+
end
|
35
|
+
|
36
|
+
def protocol_name
|
37
|
+
"#{@view_model.name}Protocol"
|
38
|
+
end
|
39
|
+
|
40
|
+
def protocol_delegate
|
41
|
+
"#{@view_model.name}ProtocolDelegate"
|
42
|
+
end
|
43
|
+
|
44
|
+
def properties_header
|
45
|
+
properties = ''
|
46
|
+
@view_model.properties.each do |property|
|
47
|
+
properties += "@property (nonatomic, strong) #{property.type}#{property.name};\n"
|
48
|
+
end
|
49
|
+
properties.chop
|
50
|
+
end
|
51
|
+
|
52
|
+
def actions_header
|
53
|
+
actions = ''
|
54
|
+
action_formatter = ActionFormatter.new
|
55
|
+
@view_model.actions.each do |action|
|
56
|
+
actions += "#{action_formatter.generate_interface(action)}\n"
|
57
|
+
end
|
58
|
+
actions.chop
|
59
|
+
end
|
60
|
+
|
61
|
+
def actions_impl
|
62
|
+
actions = ''
|
63
|
+
action_formatter = ActionFormatter.new
|
64
|
+
@view_model.actions.each do |action|
|
65
|
+
actions += "#{action_formatter.generate_implementation(action)}\n"
|
66
|
+
end
|
67
|
+
actions.chop
|
68
|
+
end
|
69
|
+
|
70
|
+
def generate(&block)
|
71
|
+
block.call(protocol_file_name, 'templates/objc/ViewModelProtocol.h.erb')
|
72
|
+
block.call(header_file_name, 'templates/objc/ViewModel.h.erb')
|
73
|
+
block.call(impl_file_name, 'templates/objc/ViewModel.m.erb')
|
74
|
+
generate_interactor(&block)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def generate_interactor(&block)
|
80
|
+
@view_model.interactors.each do |interactor|
|
81
|
+
@interactor_formatter.interactor = interactor
|
82
|
+
@interactor_formatter.generate(&block)
|
83
|
+
end unless @view_model.interactors.nil?
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|