gyro 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -797
  3. data/bin/gyro +91 -84
  4. data/documentation/enum.png +0 -0
  5. data/documentation/enum_json.png +0 -0
  6. data/documentation/ignored.png +0 -0
  7. data/documentation/json.png +0 -0
  8. data/documentation/primary_key.png +0 -0
  9. data/documentation/read_only.png +0 -0
  10. data/documentation/simple_entity.png +0 -0
  11. data/documentation/support_annotation.png +0 -0
  12. data/documentation/transformer.png +0 -0
  13. data/documentation/transformers.png +0 -0
  14. data/lib/gyro.rb +18 -29
  15. data/lib/gyro/generator/json.rb +30 -0
  16. data/lib/gyro/generator/liquid.rb +22 -0
  17. data/lib/gyro/generator/liquid/filters.rb +53 -0
  18. data/lib/gyro/generator/liquid/liquid.rb +124 -0
  19. data/lib/gyro/generator/liquid/whitespace_patch.rb +61 -0
  20. data/lib/gyro/log.rb +46 -0
  21. data/lib/gyro/parser/xcdatamodel.rb +20 -0
  22. data/lib/gyro/parser/xcdatamodel/attribute.rb +129 -0
  23. data/lib/gyro/parser/xcdatamodel/entity.rb +195 -0
  24. data/lib/gyro/parser/xcdatamodel/relationship.rb +84 -0
  25. data/lib/gyro/parser/xcdatamodel/xcdatamodel.rb +65 -0
  26. data/lib/gyro/template.rb +68 -0
  27. data/lib/gyro/version.rb +19 -0
  28. data/lib/templates/android/README.md +164 -0
  29. data/lib/templates/android/entity.liquid +49 -0
  30. data/lib/templates/android/entity_filename.liquid +1 -0
  31. data/lib/templates/android/enum.liquid +44 -0
  32. data/lib/templates/android/enum_filename.liquid +1 -0
  33. data/lib/templates/android/inc/_attributes_enum.liquid +17 -0
  34. data/lib/templates/android/inc/_attributes_getter_setter.liquid +58 -0
  35. data/lib/templates/android/inc/_attributes_properties.liquid +35 -0
  36. data/lib/templates/android/inc/_enum_getter_setter.liquid +19 -0
  37. data/lib/templates/android/inc/_primitives.liquid +30 -0
  38. data/lib/templates/android/inc/_relationships_enum.liquid +14 -0
  39. data/lib/templates/android/inc/_relationships_getter_setter.liquid +47 -0
  40. data/lib/templates/android/inc/_relationships_properties.liquid +27 -0
  41. data/lib/templates/android/inc/_type_converter.liquid +22 -0
  42. data/lib/templates/android/inc/_wrapper_type_converter.liquid +22 -0
  43. data/lib/templates/decodable/README.md +34 -0
  44. data/lib/templates/decodable/entity.liquid +57 -0
  45. data/lib/templates/decodable/entity_filename.liquid +1 -0
  46. data/lib/templates/decodable/enum.liquid +0 -0
  47. data/lib/templates/decodable/enum_filename.liquid +0 -0
  48. data/lib/templates/object-mapper/README.md +40 -0
  49. data/lib/templates/object-mapper/entity.liquid +66 -0
  50. data/lib/templates/object-mapper/entity_filename.liquid +1 -0
  51. data/lib/templates/object-mapper/enum.liquid +0 -0
  52. data/lib/templates/object-mapper/enum_filename.liquid +0 -0
  53. data/lib/templates/swift3-variant/README.md +60 -0
  54. data/lib/templates/swift3-variant/entity.liquid +12 -0
  55. data/lib/templates/swift3-variant/entity_filename.liquid +1 -0
  56. data/lib/templates/swift3-variant/enum.liquid +15 -0
  57. data/lib/templates/swift3-variant/enum_filename.liquid +1 -0
  58. data/lib/templates/swift3-variant/inc/_attributes_enum.liquid +13 -0
  59. data/lib/templates/swift3-variant/inc/_attributes_properties.liquid +32 -0
  60. data/lib/templates/swift3-variant/inc/_default_value_converter.liquid +14 -0
  61. data/lib/templates/swift3-variant/inc/_enum_attribute_property.liquid +26 -0
  62. data/lib/templates/swift3-variant/inc/_ignored_properties.liquid +24 -0
  63. data/lib/templates/swift3-variant/inc/_indexed_properties.liquid +19 -0
  64. data/lib/templates/swift3-variant/inc/_inverse_properties.liquid +11 -0
  65. data/lib/templates/swift3-variant/inc/_optional_attribute_property.liquid +5 -0
  66. data/lib/templates/swift3-variant/inc/_primary_key.liquid +5 -0
  67. data/lib/templates/swift3-variant/inc/_relationship_properties.liquid +9 -0
  68. data/lib/templates/swift3-variant/inc/_relationships_enum.liquid +17 -0
  69. data/lib/templates/swift3-variant/inc/_type_converter.liquid +20 -0
  70. data/lib/templates/swift3/README.md +60 -0
  71. data/lib/templates/swift3/entity.liquid +12 -0
  72. data/lib/templates/swift3/entity_filename.liquid +1 -0
  73. data/lib/templates/swift3/enum.liquid +15 -0
  74. data/lib/templates/swift3/enum_filename.liquid +1 -0
  75. data/lib/templates/swift3/inc/_attributes_enum.liquid +13 -0
  76. data/lib/templates/swift3/inc/_attributes_properties.liquid +32 -0
  77. data/lib/templates/swift3/inc/_default_value_converter.liquid +14 -0
  78. data/lib/templates/swift3/inc/_enum_attribute_property.liquid +26 -0
  79. data/lib/templates/swift3/inc/_ignored_properties.liquid +24 -0
  80. data/lib/templates/swift3/inc/_indexed_properties.liquid +19 -0
  81. data/lib/templates/swift3/inc/_inverse_properties.liquid +11 -0
  82. data/lib/templates/swift3/inc/_optional_attribute_property.liquid +5 -0
  83. data/lib/templates/swift3/inc/_primary_key.liquid +5 -0
  84. data/lib/templates/swift3/inc/_relationship_properties.liquid +9 -0
  85. data/lib/templates/swift3/inc/_relationships_enum.liquid +17 -0
  86. data/lib/templates/swift3/inc/_type_converter.liquid +20 -0
  87. metadata +134 -31
  88. data/lib/gyro/realm/java/converter.rb +0 -63
  89. data/lib/gyro/realm/java/enum_generator.rb +0 -128
  90. data/lib/gyro/realm/java/generator.rb +0 -183
  91. data/lib/gyro/realm/java/templates.rb +0 -67
  92. data/lib/gyro/realm/objc/converter.rb +0 -63
  93. data/lib/gyro/realm/objc/enum_generator.rb +0 -86
  94. data/lib/gyro/realm/objc/generator.rb +0 -373
  95. data/lib/gyro/realm/objc/json_category_generator.rb +0 -172
  96. data/lib/gyro/realm/objc/protocol_generator.rb +0 -59
  97. data/lib/gyro/realm/objc/templates.rb +0 -100
  98. data/lib/gyro/realm/swift/converter.rb +0 -74
  99. data/lib/gyro/realm/swift/enum_generator.rb +0 -73
  100. data/lib/gyro/realm/swift/generator.rb +0 -265
  101. data/lib/gyro/realm/swift/object_mapper_generator.rb +0 -124
  102. data/lib/gyro/realm/swift/templates.rb +0 -64
  103. data/lib/gyro/utils/file_utils.rb +0 -31
  104. data/lib/gyro/utils/log.rb +0 -68
  105. data/lib/gyro/utils/raise.rb +0 -23
  106. data/lib/gyro/utils/string_xcdatamodel.rb +0 -58
  107. data/lib/gyro/xcdatamodel/parser/attribute.rb +0 -98
  108. data/lib/gyro/xcdatamodel/parser/entity.rb +0 -234
  109. data/lib/gyro/xcdatamodel/parser/relationship.rb +0 -70
  110. data/lib/gyro/xcdatamodel/parser/xcdatamodel.rb +0 -59
@@ -0,0 +1,84 @@
1
+ # Copyright 2016 - Niji
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Gyro
16
+ module Parser
17
+ module XCDataModel
18
+ # One Relationship between attributes in the xcdatamodel
19
+ #
20
+ class Relationship
21
+ attr_accessor :entity_name, :name, :type, :optional, :deletion_rule
22
+ attr_accessor :inverse_name, :inverse_type, :json_key_path, :support_annotation
23
+ attr_accessor :realm_ignored
24
+ attr_accessor :destination
25
+
26
+ alias realm_ignored? realm_ignored
27
+
28
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
29
+ def initialize(relationship_xml, entity_name)
30
+ @entity_name = entity_name
31
+ @name = relationship_xml.xpath('@name').to_s
32
+ @optional = relationship_xml.xpath('@optional').to_s == 'YES' ? true : false
33
+ @deletion_rule = relationship_xml.xpath('@deletionRule').to_s
34
+ @inverse_name = relationship_xml.xpath('@inverseName').to_s
35
+ @inverse_type = relationship_xml.xpath('@destinationEntity').to_s
36
+ @json_key_path = Gyro::Parser::XCDataModel.user_info(relationship_xml, 'JSONKeyPath')
37
+ @realm_ignored = Gyro::Parser::XCDataModel.user_info(relationship_xml, 'realmIgnored').empty? ? false : true
38
+ @support_annotation = Gyro::Parser::XCDataModel.user_info(relationship_xml, 'supportAnnotation')
39
+ load_type(relationship_xml)
40
+ @destination = Gyro::Parser::XCDataModel.user_info(relationship_xml, 'destination')
41
+ search_for_error
42
+ end
43
+
44
+ def to_h
45
+ { 'entity_name' => entity_name, 'name' => name, 'type' => type.to_s,
46
+ 'optional' => optional, 'deletion_rule' => deletion_rule,
47
+ 'inverse_name' => inverse_name, 'inverse_type' => inverse_type,
48
+ 'json_key_path' => json_key_path, 'support_annotation' => support_annotation,
49
+ 'realm_ignored' => realm_ignored, 'destination' => destination, 'inverse' => inverse? }
50
+ end
51
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
52
+
53
+ def to_s
54
+ "\tRelationship => name=#{@name} | type=#{@type} | optional=#{@optional} | deletion_rule=#{@deletion_rule}\n"
55
+ end
56
+
57
+ def inverse?
58
+ @name.end_with?('_')
59
+ end
60
+
61
+ private ################################################################
62
+
63
+ def load_type(relationship_xml)
64
+ max_count = relationship_xml.xpath('@maxCount').to_s
65
+ @type = !max_count.nil? && (max_count == '1') ? :to_one : :to_many
66
+ end
67
+
68
+ def search_for_error
69
+ # rubocop:disable Style/GuardClause
70
+ if inverse_type.empty? && destination.empty?
71
+ message = %(The relationship "#{@name}" from "#{@entity_name}" is wrong - please fix it)
72
+ Gyro::Log.fail!(message, stacktrace: true)
73
+ end
74
+ if !destination.empty? && type != :to_many
75
+ message = %(The relationship "#{@name}" from "#{@entity_name}" is wrong - ) +
76
+ %(please set a 'No Value' relationship as 'To Many')
77
+ Gyro::Log.fail!(message, stacktrace: true)
78
+ end
79
+ # rubocop:enable Style/GuardClause
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,65 @@
1
+ # Copyright 2016 - Niji
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Gyro
16
+ module Parser
17
+ # Parser for CoreData's xcdatamodel files
18
+ #
19
+ module XCDataModel
20
+ def self.find_in_dir(dir)
21
+ Dir.chdir(dir) do
22
+ files = Dir.glob('*.xcdatamodel')
23
+ files.first.nil? ? nil : File.expand_path(files.first, dir)
24
+ end
25
+ end
26
+
27
+ def self.user_info(xml, key)
28
+ xml.xpath("userInfo/entry[@key='#{key}']/@value").to_s
29
+ end
30
+
31
+ # Represents the whole xcdatamodel file struture, once parsed
32
+ #
33
+ class XCDataModel
34
+ attr_accessor :entities
35
+
36
+ def initialize(xcdatamodel_dir)
37
+ contents_file = File.join(xcdatamodel_dir, 'contents')
38
+ Gyro::Log.fail!('Unable to find contents of xcdatamodel', stacktrace: true) unless File.exist?(contents_file)
39
+ @entities = {}
40
+ file = File.open(contents_file)
41
+ document_xml = Nokogiri::XML(file).remove_namespaces!
42
+ file.close
43
+ load_entities(document_xml)
44
+ end
45
+
46
+ def to_h
47
+ { 'entities' => entities.values.map(&:to_h) }
48
+ end
49
+
50
+ def to_s
51
+ @entities.values.map(&:to_s).join
52
+ end
53
+
54
+ private
55
+
56
+ def load_entities(document_xml)
57
+ document_xml.xpath('//entity').each do |node|
58
+ entity = Entity.new(node)
59
+ @entities[entity.name] = entity
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,68 @@
1
+ # Copyright 2016 - Niji
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Gyro
16
+ # Gyro Template Helper
17
+ #
18
+ module Template
19
+ def self.print_list
20
+ Gyro::Template.directory.children.select(&:directory?).each do |entry|
21
+ puts " - #{entry.basename}"
22
+ end
23
+ end
24
+
25
+ def self.print_infos(template)
26
+ readme = if template.include?('/')
27
+ Pathname.new(template) + 'README.md'
28
+ else
29
+ Gyro::Template.directory + template + 'README.md'
30
+ end
31
+
32
+ Gyro::Log.fail!("No README.md found for template #{template}.") unless readme.exist?
33
+ puts readme.read
34
+ end
35
+
36
+ def self.directory
37
+ Pathname.new(File.dirname(__FILE__)) + '../templates'
38
+ end
39
+
40
+ def self.find(template_param)
41
+ if template_param.include? '/'
42
+ find_by_path(template_param)
43
+ else
44
+ find_by_name(template_param)
45
+ end
46
+ end
47
+
48
+ def self.find_by_path(path)
49
+ template_dir = Pathname.new(path)
50
+ unless template_dir.exist?
51
+ Gyro::Log.fail!('You need to specify existing template directory using --template option' \
52
+ ' (see --help for more info)')
53
+ end
54
+
55
+ return template_dir if template_dir.directory?
56
+ return template_dir.dirname if template_dir.file?
57
+ Gyro::Log.fail!('You need to specify right template directory using --template option' \
58
+ ' (see --help for more info)')
59
+ end
60
+
61
+ def self.find_by_name(name)
62
+ template_dir = Gyro::Template.directory + name
63
+ return template_dir if template_dir.exist?
64
+ Gyro::Log.fail!('You need to specify existing default template name using --template option' \
65
+ ' (see --help for more info)')
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,19 @@
1
+ # Copyright 2016 - Niji
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Gyro Version
16
+ #
17
+ module Gyro
18
+ VERSION = '1.0.0'.freeze
19
+ end
@@ -0,0 +1,164 @@
1
+ # Android Template Information
2
+
3
+ | Name | Description |
4
+ | --------- | ----------------- |
5
+ | Folder name | templates/android |
6
+ | Invocation example | `gyro -m <model> -t android …` |
7
+ | Language | Java |
8
+
9
+ If you want to use this template you need to work with `Realm`.
10
+
11
+ # Caracteristics
12
+
13
+ In this template you have additional parameters to inject constants :
14
+
15
+ - package (ex : **com.gyro.model.realm**)
16
+ - use_wrappers (define if you want to use wrapper types (ex Boolean) instead of primitive types (ex boolean) when you attribute is **optional** inside xcdatamodel)
17
+ - support_annotations (you can define annotation for properties ex : **@android.support.annotation.IntRange(from=0,to=255)** or **@android.support.annotation.Nullable**)
18
+
19
+ # Usage
20
+
21
+
22
+ Package exemple :
23
+
24
+ ```bash
25
+ gyro -m <model> -t android -o <output> --param package:com.gyro.model.realm
26
+ ```
27
+
28
+ Use wrappers exemple :
29
+
30
+ ```bash
31
+ gyro -m <model> -t android -o <output> --param use_wrappers:true
32
+ ```
33
+
34
+ Support annotations exemple :
35
+
36
+ ```bash
37
+ gyro -m <model> -t android -o <output> --param support_annotations:true
38
+ ```
39
+
40
+ And you can combine options :
41
+
42
+ ```bash
43
+ gyro -m <model> -t android -o <output> --param package:com.gyro.model.realm --param support_annotations:true --param use_wrappers:true
44
+ ```
45
+
46
+
47
+ # Generated Code
48
+
49
+ Combine options :
50
+
51
+ `FidelityCard.java`:
52
+
53
+ ```java
54
+ package com.gyro.model.realm;
55
+
56
+ /* DO NOT EDIT | Generated by gyro */
57
+
58
+ import io.realm.RealmObject;
59
+
60
+ public class FidelityCard extends RealmObject {
61
+
62
+ public static final class Attributes {
63
+ public static final String IDENTIFIER = "identifier";
64
+ public static final String POINTS = "points";
65
+
66
+ private Attributes() {
67
+ // Hide constructor
68
+ }
69
+ }
70
+
71
+ public static final class Relationships {
72
+ public static final String USER = "user";
73
+
74
+ private Relationships() {
75
+ // Hide constructor
76
+ }
77
+ }
78
+
79
+ private short identifier;
80
+ @android.support.annotation.IntRange(from=0,to=255)
81
+ private Integer points;
82
+ private User user;
83
+
84
+ public short getIdentifier() {
85
+ return identifier;
86
+ }
87
+
88
+ public void setIdentifier(final short identifier) {
89
+ this.identifier = identifier;
90
+ }
91
+
92
+ @android.support.annotation.Nullable
93
+ @android.support.annotation.IntRange(from=0,to=255)
94
+ public Integer getPoints() {
95
+ return points;
96
+ }
97
+
98
+ public void setPoints(@android.support.annotation.Nullable @android.support.annotation.IntRange(from=0,to=255) final Integer points) {
99
+ this.points = points;
100
+ }
101
+
102
+ @android.support.annotation.NonNull
103
+ public User getUser() {
104
+ return user;
105
+ }
106
+
107
+ public void setUser(@android.support.annotation.NonNull final User user) {
108
+ this.user = user;
109
+ }
110
+ }
111
+ ```
112
+
113
+ Compare to generated code without options :
114
+
115
+ `FidelityCard.java`:
116
+
117
+ ```java
118
+ /* DO NOT EDIT | Generated by gyro */
119
+
120
+ import io.realm.RealmObject;
121
+ import io.realm.annotations.PrimaryKey;
122
+
123
+ public class Product extends RealmObject {
124
+
125
+ public static final class Attributes {
126
+ public static final String BRAND = "brand";
127
+ public static final String NAME = "name";
128
+ public static final String PRICE = "price";
129
+
130
+ private Attributes() {
131
+ // Hide constructor
132
+ }
133
+ }
134
+
135
+ private String brand;
136
+ @PrimaryKey
137
+ private String name;
138
+ private int price;
139
+
140
+ public String getBrand() {
141
+ return brand;
142
+ }
143
+
144
+ public void setBrand(final String brand) {
145
+ this.brand = brand;
146
+ }
147
+
148
+ public String getName() {
149
+ return name;
150
+ }
151
+
152
+ public void setName(final String name) {
153
+ this.name = name;
154
+ }
155
+
156
+ public int getPrice() {
157
+ return price;
158
+ }
159
+
160
+ public void setPrice(final int price) {
161
+ this.price = price;
162
+ }
163
+ }
164
+ ```
@@ -0,0 +1,49 @@
1
+ {%- if params.package.size > 0 -%}
2
+ package {{ params.package }};
3
+ {%- endif %}
4
+
5
+ /* DO NOT EDIT | Generated by gyro */
6
+ {{ empty_line }}
7
+ {%- if entity.has_json_key_path == true %}
8
+ import com.google.gson.annotations.SerializedName;
9
+ {% comment %} *** Empty line *** {% endcomment %}
10
+ {%- endif %}
11
+ {%- if entity.has_date_attribute == true %}
12
+ import java.util.Date;
13
+ {%- endif %}
14
+ {%- if entity.has_list_relationship == true %}
15
+ import java.util.List;
16
+ {%- endif %}
17
+ {%- if entity.has_date_attribute == true or entity.has_list_relationship == true %}
18
+ {{ empty_line }}
19
+ {%- endif %}
20
+ {%- if entity.has_list_attributes == true %}
21
+ import io.realm.RealmList;
22
+ {%- endif %}
23
+ import io.realm.RealmObject;
24
+ {%- if entity.has_ignored == true %}
25
+ import io.realm.annotations.Ignore;
26
+ {%- endif %}
27
+ {%- if entity.has_indexed_attributes == true %}
28
+ import io.realm.annotations.Index;
29
+ {%- endif %}
30
+ {%- if entity.has_primary_key == true %}
31
+ import io.realm.annotations.PrimaryKey;
32
+ {%- endif %}
33
+
34
+ {% if entity.comment.size > 0 -%}
35
+ /**
36
+ * {{ entity.comment }}
37
+ */
38
+ {% endif %}
39
+ {%- assign primary_key = entity.identity_attribute -%}
40
+ public class {{ entity.name }} extends RealmObject {
41
+
42
+ {%- include 'inc/attributes_enum' %}
43
+ {%- include 'inc/relationships_enum' %}
44
+ {% include 'inc/attributes_properties' %}
45
+ {%- include 'inc/relationships_properties' %}
46
+ {%- include 'inc/attributes_getter_setter' %}
47
+ {%- include 'inc/relationships_getter_setter' %}
48
+ {%- include 'inc/enum_getter_setter' %}
49
+ }