PTGenerator 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README +13 -0
- data/bin/ptgen +4 -0
- data/lib/FileGenerator.rb +43 -0
- data/lib/HTTPRequestDataParser/HTTPCommandParser.rb +66 -0
- data/lib/HTTPRequestDataParser/HTTPRequestDataParser.rb +97 -0
- data/lib/ModelGenerator/AnnotationGenerator.rb +10 -0
- data/lib/ModelGenerator/Announcement.rb +25 -0
- data/lib/ModelGenerator/CommandTask.rb +189 -0
- data/lib/ModelGenerator/CommonParam.rb +103 -0
- data/lib/ModelGenerator/DBGenerator.rb +91 -0
- data/lib/ModelGenerator/Detector.rb +61 -0
- data/lib/ModelGenerator/EnumGenerator.rb +25 -0
- data/lib/ModelGenerator/FormatTransformer.rb +65 -0
- data/lib/ModelGenerator/Helper.rb +71 -0
- data/lib/ModelGenerator/JsonGenerator.rb +110 -0
- data/lib/ModelGenerator/MethodGenerator.rb +43 -0
- data/lib/ModelGenerator/ModelGenerator.rb +137 -0
- data/lib/ModelGenerator/NimbusGenerator.rb +44 -0
- data/lib/ModelGenerator/Project.rb +27 -0
- data/lib/ModelGenerator/resource/Model/ModelTemple.h +11 -0
- data/lib/ModelGenerator/resource/Model/ModelTemple.m +19 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 204cb9907f42206b424ea5769c7d6283a3ff68f3
|
4
|
+
data.tar.gz: 24f048e27e359af6fc686ae001cfeecdcc00962c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c696c4db26e1768f632fa6ff2e3b311fd49070038caa7c414d3b1844305d37f7297617ed3a873466db4d55fab4d1714d31ee93ff91d6a2abbd8083d25162b3cb
|
7
|
+
data.tar.gz: 4d758d5d360255da2b5936d1dcd21abb2ff0608cfa569c78cfdb02fb9697cbab6f39078d6a660a31a907a43ca9ab33832f862f74cfa957e7639919e5c8d0d71f
|
data/README
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
== Modelgenerator GemPlugin
|
2
|
+
|
3
|
+
You should document your project here.
|
4
|
+
|
5
|
+
# ptgen is a tools for model. It works on mac. If you use mantle for your entity. you can use it.
|
6
|
+
# I create it for my project. The model can contain [database] [request] [nimbus] option method.
|
7
|
+
# There is not prefect now .
|
8
|
+
# when you create your model, you maybe contain manay property to create. and some for request
|
9
|
+
# others for database . You must make your database and request column correspond to property index.
|
10
|
+
# I will fix it latter.
|
11
|
+
# I will make it more prefect
|
12
|
+
|
13
|
+
|
data/bin/ptgen
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module FileGenerator
|
2
|
+
|
3
|
+
autoload :Announcement, 'ModelGenerator/Announcement'
|
4
|
+
autoload :CommandTask, 'ModelGenerator/CommandTask'
|
5
|
+
autoload :CommonParam, 'ModelGenerator/CommonParam'
|
6
|
+
autoload :FormatTransformer, 'ModelGenerator/FormatTransformer'
|
7
|
+
autoload :ModelGenerator, 'ModelGenerator/ModelGenerator'
|
8
|
+
autoload :HooksManager, 'ModelGenerator/Project'
|
9
|
+
autoload :Helper, 'ModelGenerator/Helper'
|
10
|
+
autoload :HTTPCommandParser, 'HTTPRequestDataParser/HTTPCommandParser'
|
11
|
+
|
12
|
+
|
13
|
+
def self.generate_model
|
14
|
+
if ARGV.length==0 || ARGV == nil
|
15
|
+
puts "error: Parameter does not match,there is not any parameter"
|
16
|
+
return nil
|
17
|
+
end
|
18
|
+
|
19
|
+
begin
|
20
|
+
@model_generator=ModelGenerator.new
|
21
|
+
commandTask = @model_generator.analyze_command(ARGV)
|
22
|
+
if commandTask.flags.join.include?("h")
|
23
|
+
puts Helper.help
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
commandParse = HTTPCommandParser.new(ARGV)
|
28
|
+
is_ok =commandParse.parseCommand
|
29
|
+
|
30
|
+
if is_ok == false
|
31
|
+
@model_generator.generate_header
|
32
|
+
@model_generator.generate_source
|
33
|
+
end
|
34
|
+
|
35
|
+
rescue Exception => e
|
36
|
+
puts e
|
37
|
+
else
|
38
|
+
commandTask.cacheCommand
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
class HTTPCommandParser
|
2
|
+
|
3
|
+
autoload :HTTPRequestDataParser, 'HTTPRequestDataParser/HTTPRequestDataParser'
|
4
|
+
|
5
|
+
attr_accessor :path_class_mapping , :url_str , :params, :primary_key
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
@command = args
|
9
|
+
@path_class_mapping = Hash.new
|
10
|
+
@url_str = ""
|
11
|
+
@params = ""
|
12
|
+
@primary_key = ""
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
def parseCommand
|
17
|
+
url_begin = false
|
18
|
+
path_class_mapping_begin = false;
|
19
|
+
primary_key_begin = false
|
20
|
+
|
21
|
+
@command.each do |param|
|
22
|
+
if param[0] == "-"
|
23
|
+
flag=param[1]
|
24
|
+
case flag
|
25
|
+
when "u"
|
26
|
+
url_begin = true
|
27
|
+
path_class_mapping_begin = false
|
28
|
+
@params = param.gsub '-u' , '-a'
|
29
|
+
when "m"
|
30
|
+
path_class_mapping_begin = true
|
31
|
+
url_begin = false
|
32
|
+
when "k"
|
33
|
+
primary_key_begin = true
|
34
|
+
path_class_mapping_begin = false
|
35
|
+
url_begin = false
|
36
|
+
end
|
37
|
+
else
|
38
|
+
if url_begin
|
39
|
+
@url_str = param.delete "\""
|
40
|
+
elsif path_class_mapping_begin
|
41
|
+
components = param.split(":")
|
42
|
+
if components.length >=2
|
43
|
+
path = components[0]
|
44
|
+
class_name = components[1]
|
45
|
+
@path_class_mapping[path] = class_name
|
46
|
+
else
|
47
|
+
puts "command params error : please use -m [path]:[class_name] style"
|
48
|
+
end
|
49
|
+
elsif primary_key_begin
|
50
|
+
@primary_key = param
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
if @url_str !=nil && @url_str !=""
|
56
|
+
parser = HTTPRequestDataParser.new
|
57
|
+
parser.flags = @params
|
58
|
+
parser.primary_key = @primary_key
|
59
|
+
parser.fetch(@url_str,@path_class_mapping)
|
60
|
+
return true
|
61
|
+
else
|
62
|
+
return false
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class HTTPRequestDataParser
|
5
|
+
|
6
|
+
autoload :ModelGenerator, 'ModelGenerator/ModelGenerator'
|
7
|
+
|
8
|
+
attr_accessor :flags
|
9
|
+
attr_accessor :primary_key
|
10
|
+
|
11
|
+
def type_mapping(parse_object)
|
12
|
+
if parse_object.kind_of? Fixnum
|
13
|
+
return "int"
|
14
|
+
elsif parse_object.kind_of? Float
|
15
|
+
return "float"
|
16
|
+
elsif parse_object.kind_of? String
|
17
|
+
return "string"
|
18
|
+
elsif parse_object.kind_of? Array
|
19
|
+
return "array"
|
20
|
+
elsif parse_object.kind_of? FalseClass
|
21
|
+
return "bool"
|
22
|
+
elsif parse_object.kind_of? TrueClass
|
23
|
+
return "bool"
|
24
|
+
elsif parse_object.kind_of? Hash
|
25
|
+
return "custom"
|
26
|
+
end
|
27
|
+
|
28
|
+
return nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch(urlString,class_mapping)
|
32
|
+
puts "start request #{urlString}..."
|
33
|
+
uri = URI(urlString)
|
34
|
+
res = Net::HTTP.get_response(uri)
|
35
|
+
if res.is_a?(Net::HTTPSuccess)
|
36
|
+
json_objects = JSON.parse(res.body)
|
37
|
+
parse(json_objects,class_mapping)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_hash_from_array(json_object)
|
42
|
+
if json_object.instance_of? Array
|
43
|
+
if json_object.length > 0
|
44
|
+
json_object = json_object[0]
|
45
|
+
json_object = get_hash_from_array(json_object)
|
46
|
+
else
|
47
|
+
return json_object
|
48
|
+
end
|
49
|
+
else
|
50
|
+
return json_object
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def parse(json_object,class_mapping)
|
55
|
+
|
56
|
+
json_object = get_hash_from_array(json_object)
|
57
|
+
|
58
|
+
model_generator=ModelGenerator.new
|
59
|
+
class_mapping.each_pair do |key , value|
|
60
|
+
|
61
|
+
command_line = value
|
62
|
+
command_line = command_line + " " + @flags if @flags
|
63
|
+
path_components = key.split "/"
|
64
|
+
|
65
|
+
final_object = json_object
|
66
|
+
path_components.each do |compoment|
|
67
|
+
compoment_value = final_object[compoment]
|
68
|
+
if compoment_value == nil || compoment_value.length ==0
|
69
|
+
puts "Make sure your http response is JSON and your JSON content path is correct"
|
70
|
+
else
|
71
|
+
final_object = get_hash_from_array(compoment_value)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
#parse finnal object to command_line
|
75
|
+
index = 0
|
76
|
+
final_object.each_pair do |key ,value|
|
77
|
+
param = @primary_key == key || (@primary_key.empty? && index==0) ? "*" : ""
|
78
|
+
param += "#{key}:#{type_mapping value}"
|
79
|
+
command_line = command_line + " " + param
|
80
|
+
index+=1
|
81
|
+
end
|
82
|
+
|
83
|
+
foo_argv = command_line.split " "
|
84
|
+
commandTask = model_generator.analyze_command(foo_argv)
|
85
|
+
model_generator.generate_header
|
86
|
+
model_generator.generate_source
|
87
|
+
|
88
|
+
puts "generate #{value} entity"
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# .eg
|
2
|
+
#
|
3
|
+
# CKAppleItemModel.h
|
4
|
+
# ProjectTemple
|
5
|
+
#
|
6
|
+
# Created by Mac on 14-5-26.
|
7
|
+
# Copyright (c) 2014年 Mac. All rights reserved.
|
8
|
+
#
|
9
|
+
|
10
|
+
class Announcement
|
11
|
+
attr_accessor :name
|
12
|
+
attr_accessor :projectName
|
13
|
+
attr_accessor :author
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@projectName="your_project_name"
|
17
|
+
@author="CK"
|
18
|
+
@name="entity_name"
|
19
|
+
end
|
20
|
+
|
21
|
+
def createDeclare
|
22
|
+
return "//\n// #{@name}.h\n// #{@projectName}\n//\n// Created by #{@author} on #{t=Time.now ; t.strftime("%y-%m-%d")}.\n\
|
23
|
+
// Copyright (c) #{t=Time.now ; t.strftime("%Y")}年 #{@author}. All rights reserved."
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,189 @@
|
|
1
|
+
class CommandTask
|
2
|
+
|
3
|
+
|
4
|
+
attr_reader :property_name_type_hash
|
5
|
+
attr_reader :property_em_name_type_hash
|
6
|
+
attr_reader :property_name_format_hash
|
7
|
+
attr_reader :property_name_json_hash
|
8
|
+
attr_reader :json_name_type_hash
|
9
|
+
attr_reader :json_name_format_hash
|
10
|
+
attr_reader :property_name_db_hash
|
11
|
+
attr_reader :flags
|
12
|
+
attr_reader :command
|
13
|
+
attr_reader :isreverse
|
14
|
+
attr_reader :is_force_reset
|
15
|
+
attr_reader :entity_name
|
16
|
+
attr_reader :primary_key
|
17
|
+
attr_reader :parent_class
|
18
|
+
|
19
|
+
def initialize(args)
|
20
|
+
@command = args
|
21
|
+
@isreverse = false
|
22
|
+
@is_force_reset =false
|
23
|
+
@property_name_type_hash = Hash.new
|
24
|
+
@property_em_name_type_hash = Hash.new
|
25
|
+
@property_name_format_hash = Hash.new
|
26
|
+
@property_name_json_hash = Hash.new
|
27
|
+
@json_name_type_hash = Hash.new
|
28
|
+
@json_name_format_hash = Hash.new
|
29
|
+
@property_name_db_hash = Hash.new
|
30
|
+
@flags = Array.new
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
def parseCommand
|
35
|
+
property_begin =false
|
36
|
+
json_begin =false
|
37
|
+
db_begin =false
|
38
|
+
|
39
|
+
if @command[0].include?(":")
|
40
|
+
class_info=@command[0].delete(" ")
|
41
|
+
entity_info_array=class_info.split(":")
|
42
|
+
@entity_name=entity_info_array[0].capitalize+"Entity"
|
43
|
+
@parent_class=entity_info_array[1].capitalize+"Entity"
|
44
|
+
else
|
45
|
+
@entity_name=@command[0].capitalize+"Entity"
|
46
|
+
end
|
47
|
+
|
48
|
+
property_name_array= Array.new
|
49
|
+
json_name_array= Array.new
|
50
|
+
db_name_array= Array.new
|
51
|
+
@command.each do |param|
|
52
|
+
if param[0] == "-"
|
53
|
+
flag=param[1]
|
54
|
+
case flag
|
55
|
+
when "r"
|
56
|
+
@flags << flag
|
57
|
+
@isreverse = true
|
58
|
+
property_begin= false
|
59
|
+
json_begin = false
|
60
|
+
db_begin = false
|
61
|
+
when "l"
|
62
|
+
@flags << flag
|
63
|
+
property_begin = true
|
64
|
+
json_begin = false
|
65
|
+
db_begin =false
|
66
|
+
when "s"
|
67
|
+
@flags << flag
|
68
|
+
json_begin = true
|
69
|
+
property_begin = false
|
70
|
+
db_begin = false
|
71
|
+
when "d"
|
72
|
+
@flags << flag
|
73
|
+
db_begin = true
|
74
|
+
json_begin = false
|
75
|
+
property_begin = false
|
76
|
+
when "n"
|
77
|
+
@flags << flag
|
78
|
+
property_begin= false
|
79
|
+
json_begin = false
|
80
|
+
db_begin = false
|
81
|
+
when "f"
|
82
|
+
@flags << flag
|
83
|
+
@is_force_reset=true
|
84
|
+
db_begin = false
|
85
|
+
json_begin = false
|
86
|
+
property_begin = false
|
87
|
+
when "h"
|
88
|
+
@flags << flag
|
89
|
+
@is_force_reset=false
|
90
|
+
db_begin = false
|
91
|
+
json_begin = false
|
92
|
+
property_begin = false
|
93
|
+
when "a"
|
94
|
+
param.each_char do |chr|
|
95
|
+
if chr == 'l'
|
96
|
+
@flags << chr
|
97
|
+
property_begin = true
|
98
|
+
elsif chr == 's'
|
99
|
+
@flags << chr
|
100
|
+
json_begin = true
|
101
|
+
elsif chr == 'd'
|
102
|
+
@flags << chr
|
103
|
+
db_begin = true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
else
|
108
|
+
|
109
|
+
if property_begin == true
|
110
|
+
property_info= param.split(":")
|
111
|
+
name = ""
|
112
|
+
type = ""
|
113
|
+
type_name = ""
|
114
|
+
format = ""
|
115
|
+
if property_info.length >=1 then name=property_info[0].delete("*") end
|
116
|
+
name = "entityId" if name == 'id'
|
117
|
+
if property_info.length >=2
|
118
|
+
type=property_info[1]
|
119
|
+
if type.include?("enum")
|
120
|
+
type_info_array=type.split(".")
|
121
|
+
type=type_info_array[0]
|
122
|
+
type_name=type_info_array[1]
|
123
|
+
end
|
124
|
+
end
|
125
|
+
if property_info.length >=3 then format=property_info[2] end
|
126
|
+
@property_name_type_hash[name] = type
|
127
|
+
if type_name.length >0
|
128
|
+
@property_em_name_type_hash[name] = type_name
|
129
|
+
end
|
130
|
+
@property_name_format_hash[name] = format
|
131
|
+
property_name_array << name
|
132
|
+
end
|
133
|
+
|
134
|
+
if json_begin == true
|
135
|
+
json_info= param.split(":")
|
136
|
+
name = ""
|
137
|
+
type = ""
|
138
|
+
format = ""
|
139
|
+
if json_info.length >=1 then name=json_info[0].delete("*") end
|
140
|
+
if json_info.length >=2 then type=json_info[1] end
|
141
|
+
if json_info.length >=3 then format=json_info[2] end
|
142
|
+
@json_name_type_hash[name] = type
|
143
|
+
@json_name_format_hash[name] = format
|
144
|
+
json_name_array << name
|
145
|
+
end
|
146
|
+
|
147
|
+
if db_begin == true
|
148
|
+
name = param.split(":")
|
149
|
+
db_name_array << name[0]
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
index =0
|
155
|
+
property_name_array.each do |name|
|
156
|
+
if index >= json_name_array.length
|
157
|
+
@property_name_json_hash[name] = nil
|
158
|
+
else
|
159
|
+
@property_name_json_hash[name] = json_name_array[index]
|
160
|
+
end
|
161
|
+
|
162
|
+
if index >= db_name_array.length
|
163
|
+
@property_name_db_hash[name] = nil
|
164
|
+
else
|
165
|
+
db_name= db_name_array[index]
|
166
|
+
if db_name && db_name.length >=1
|
167
|
+
if db_name[0]=="*"
|
168
|
+
@primary_key=name
|
169
|
+
db_name = db_name.delete("*")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
@property_name_db_hash[name] = db_name
|
173
|
+
end
|
174
|
+
index+=1
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
|
179
|
+
|
180
|
+
def cacheCommand
|
181
|
+
command_content=@command.join " "
|
182
|
+
pwdPath=Dir.pwd
|
183
|
+
file=File.new("#{pwdPath}/command","a")
|
184
|
+
file << "ptgen " +command_content +"\n"
|
185
|
+
file.chmod(0777)
|
186
|
+
file.close
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
class CommonParam
|
2
|
+
def CommonParam.entity_name
|
3
|
+
return /\[NAME\]/
|
4
|
+
end
|
5
|
+
|
6
|
+
def CommonParam.file_declare
|
7
|
+
return /\[DECLARE\]/
|
8
|
+
end
|
9
|
+
|
10
|
+
def CommonParam.property_declare
|
11
|
+
return /\[PROPERTY_DECLARE\]/
|
12
|
+
end
|
13
|
+
|
14
|
+
def CommonParam.json_column_mapping
|
15
|
+
return /\[JSON_COLUMN_MAPPING\]/
|
16
|
+
end
|
17
|
+
|
18
|
+
def CommonParam.property_mapping
|
19
|
+
return /\[PROPERTY_MAPPING\]/
|
20
|
+
end
|
21
|
+
|
22
|
+
def CommonParam.type_transformer
|
23
|
+
return /\[TYPE_TRANSFORMER\]/
|
24
|
+
end
|
25
|
+
|
26
|
+
def CommonParam.table_column_declare
|
27
|
+
return /\[TABLE_COLUMN_DECLARE\]/
|
28
|
+
end
|
29
|
+
|
30
|
+
def CommonParam.table_name
|
31
|
+
return /\[TABLE_NAME\]/
|
32
|
+
end
|
33
|
+
|
34
|
+
def CommonParam.table_primary_key
|
35
|
+
return /\[TABLE_PRIMARY_KEY\]/
|
36
|
+
end
|
37
|
+
|
38
|
+
def CommonParam.table_mapping
|
39
|
+
return /\[TABLE_MAPPING\]/
|
40
|
+
end
|
41
|
+
|
42
|
+
def CommonParam.enum_declare
|
43
|
+
return /\[ENUM_DECLARE\]/
|
44
|
+
end
|
45
|
+
|
46
|
+
def CommonParam.header_import
|
47
|
+
return /\[HEADER_IMPORT\]/
|
48
|
+
end
|
49
|
+
|
50
|
+
def CommonParam.nimbus_method
|
51
|
+
return /\[NIMBUS_METHOD\]/
|
52
|
+
end
|
53
|
+
|
54
|
+
def CommonParam.type_mapping
|
55
|
+
return {"string" => "NSString *",
|
56
|
+
"int" => "NSInteger",
|
57
|
+
"uint" => "NSUInteger",
|
58
|
+
"float" => "CGFloat",
|
59
|
+
"double" => "CGFloat",
|
60
|
+
"number" => "NSNumber *",
|
61
|
+
"date" => "NSDate *",
|
62
|
+
"data" => "NSData *",
|
63
|
+
"time" => "NSTimeInterval",
|
64
|
+
"url" => "NSURL *",
|
65
|
+
"array" => "NSArray *",
|
66
|
+
"marray" => "NSMutableArray *",
|
67
|
+
"dic" => "NSDictionary *",
|
68
|
+
"mdic" => "NSMutableDictionary *",
|
69
|
+
"indexset" => "NSIndexSet *",
|
70
|
+
"mindexset" => "NSIndexPath *",
|
71
|
+
"indexpath" => "NSIndexPath *",
|
72
|
+
"mindexpath" => "NSMutableIndexSet *",
|
73
|
+
"bool" => "BOOL",
|
74
|
+
"enum" => "enum",
|
75
|
+
"mapping" => "<#mapping#>",
|
76
|
+
"custom" => "<#custom#>"}
|
77
|
+
end
|
78
|
+
|
79
|
+
def CommonParam.reference_type_mapping
|
80
|
+
return {"string" => "strong",
|
81
|
+
"int" => "assign",
|
82
|
+
"float" => "assign",
|
83
|
+
"double" => "assign",
|
84
|
+
"number" => "strong",
|
85
|
+
"uint" => "assign",
|
86
|
+
"date" => "strong",
|
87
|
+
"data" => "strong",
|
88
|
+
"time" => "assign",
|
89
|
+
"url" => "strong",
|
90
|
+
"array" => "strong",
|
91
|
+
"marray" => "strong",
|
92
|
+
"dic" => "strong",
|
93
|
+
"mdic" => "strong",
|
94
|
+
"indexset" => "strong",
|
95
|
+
"mindexset" => "strong",
|
96
|
+
"indexpath" => "strong",
|
97
|
+
"mindexpath" => "strong",
|
98
|
+
"bool" => "assign",
|
99
|
+
"enum" => "assign",
|
100
|
+
"mapping" => "strong",
|
101
|
+
"custom" => "<#refrence_type#>"}
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
class DBGenerator
|
2
|
+
autoload :CommonParam, 'ModelGenerator/CommonParam'
|
3
|
+
autoload :CommandTask, 'ModelGenerator/CommandTask'
|
4
|
+
autoload :AnnotationGenerator, 'ModelGenerator/AnnotationGenerator'
|
5
|
+
autoload :MethodGenerator, 'ModelGenerator/MethodGenerator'
|
6
|
+
|
7
|
+
attr_reader :commandTask
|
8
|
+
|
9
|
+
def initialize(command)
|
10
|
+
@commandTask=command
|
11
|
+
@method_generator=MethodGenerator.new(command)
|
12
|
+
if !@commandTask.primary_key && @commandTask.flags.join.include?("d")
|
13
|
+
raise "error : you must set table primary key"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def generate_table_column_declare
|
18
|
+
if @commandTask.flags.join.include?("d")
|
19
|
+
db_column_mappings_content=""
|
20
|
+
@commandTask.property_name_db_hash.keys.each do |name|
|
21
|
+
column=@commandTask.property_name_db_hash[name]
|
22
|
+
name = name.slice(0,1).capitalize + name.slice(1..-1)
|
23
|
+
if(column)
|
24
|
+
db_column_mappings_content << "static NSString * #{name}TableKey = @\"#{column}\";\n"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
if db_column_mappings_content.length > 0
|
28
|
+
annotation = AnnotationGenerator.generate_single_annotation("table column declare")
|
29
|
+
db_column_mappings_content = annotation + db_column_mappings_content
|
30
|
+
end
|
31
|
+
return db_column_mappings_content
|
32
|
+
end
|
33
|
+
|
34
|
+
return ""
|
35
|
+
end
|
36
|
+
|
37
|
+
def generate_table_name
|
38
|
+
if @commandTask.flags.join.include?("d")
|
39
|
+
annotation = AnnotationGenerator.generate_mark_annotation("DB method")
|
40
|
+
has_super=@commandTask.parent_class
|
41
|
+
content =@method_generator.generate_method("+","NSString *","getTableName","return @\"tb_#{@commandTask.command[0].downcase}\";",has_super)
|
42
|
+
return (annotation + content)
|
43
|
+
end
|
44
|
+
|
45
|
+
return ""
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate_primary_key
|
49
|
+
if @commandTask.flags.join.include?("d")
|
50
|
+
has_super=@commandTask.parent_class
|
51
|
+
primary_key = @commandTask.primary_key.slice(0,1).capitalize + @commandTask.primary_key.slice(1..-1)
|
52
|
+
content =@method_generator.generate_method("+","NSString *","getPrimaryKey","return #{primary_key}TableKey;",has_super)
|
53
|
+
return content
|
54
|
+
end
|
55
|
+
return ""
|
56
|
+
end
|
57
|
+
|
58
|
+
def generate_table_mapping
|
59
|
+
if @commandTask.flags.join.include?("d")
|
60
|
+
|
61
|
+
hasDBColumn=false
|
62
|
+
long_space=" "
|
63
|
+
property_mapping_content="return @{\n"
|
64
|
+
@commandTask.property_name_db_hash.each do |key , value|
|
65
|
+
if value
|
66
|
+
hasDBColumn=true
|
67
|
+
cap_key = key.slice(0,1).capitalize + key.slice(1..-1)
|
68
|
+
property_mapping_content << "#{long_space} @\"#{key}\":#{cap_key}TableKey,\n"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
property_mapping_content=property_mapping_content.chomp(",\n")
|
73
|
+
property_mapping_content << "\n"
|
74
|
+
property_mapping_content << "#{long_space}};"
|
75
|
+
|
76
|
+
if hasDBColumn
|
77
|
+
has_super=@commandTask.parent_class
|
78
|
+
content =@method_generator.generate_method("+","NSDictionary *","getTableMapping",property_mapping_content,has_super)
|
79
|
+
return content
|
80
|
+
else
|
81
|
+
return ""
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
return ""
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
#this class not surport override method, in future may fix it
|
2
|
+
class Detector
|
3
|
+
attr_reader :methods_implement_hash
|
4
|
+
attr_reader :methods_name_implment_hash
|
5
|
+
attr_reader :content
|
6
|
+
|
7
|
+
def initialize(file_path)
|
8
|
+
if File.exist?(file_path)
|
9
|
+
file=File.new(file_path,"r")
|
10
|
+
if file
|
11
|
+
@content=file.read
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
@methods_implement_hash = Hash.new
|
16
|
+
@methods_name_implment_hash = Hash.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def detect_method
|
20
|
+
if !@content then return end
|
21
|
+
|
22
|
+
method_regular=/^[+-].*\(.*\)[\s\S]*?(\w*)[\s\S]*?\n}/
|
23
|
+
@content.gsub(method_regular).each do |method_implment|
|
24
|
+
@methods_implement_hash[method_implment]= true
|
25
|
+
method_name= $1
|
26
|
+
@methods_name_implment_hash[method_name] = method_implment
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def get_method_name(method_implement)
|
34
|
+
name = ""
|
35
|
+
name_regular= /[+-].*\(.*\)[\s\S]*?(\w*)[\s\S]*?}/
|
36
|
+
method_implement.gsub(name_regular).each do |method_name|
|
37
|
+
name = $1
|
38
|
+
end
|
39
|
+
|
40
|
+
return name
|
41
|
+
end
|
42
|
+
|
43
|
+
#if same return false , else return file method implement
|
44
|
+
#the meanning of this method is not very suitable
|
45
|
+
def method_implment_same? (method_implement)
|
46
|
+
if !methods_implement_hash then return end
|
47
|
+
|
48
|
+
method_name=self.get_method_name(method_implement)
|
49
|
+
file_method_implement= @methods_name_implment_hash[method_name]
|
50
|
+
if file_method_implement
|
51
|
+
if file_method_implement == method_implement
|
52
|
+
return false
|
53
|
+
else
|
54
|
+
return file_method_implement +"\n"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
return false
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class EnumGenerator
|
2
|
+
autoload :AnnotationGenerator, 'ModelGenerator/AnnotationGenerator'
|
3
|
+
|
4
|
+
attr_reader :name_type_hash
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
@name_type_hash=args
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate_em_declare
|
11
|
+
content = ""
|
12
|
+
@name_type_hash.each do |property_name,type_name|
|
13
|
+
content << %Q/
|
14
|
+
typedef enum {
|
15
|
+
<#enum_content#>
|
16
|
+
}#{type_name};/
|
17
|
+
end
|
18
|
+
if content.length > 0
|
19
|
+
annotation = AnnotationGenerator.generate_single_annotation("emum declare")
|
20
|
+
content = annotation + content
|
21
|
+
end
|
22
|
+
|
23
|
+
return content
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class FormatTransformer
|
2
|
+
$json_var="x"
|
3
|
+
#reverse please exchange property and json position
|
4
|
+
def FormatTransformer.transform(property_type,property_format,json_type,json_formate)
|
5
|
+
property_format = '<#format#>' if property_format.nil? || property_format.empty?
|
6
|
+
json_formate = '<#format#>' if json_formate.nil? || json_formate.empty?
|
7
|
+
if property_type == "string"
|
8
|
+
if json_type == property_type && property_format != json_formate
|
9
|
+
return "[NSString stringWithFormat:@\"#{property_format}\",#{$json_var}];"
|
10
|
+
elsif json_type=="int"
|
11
|
+
if property_format == '<#format#>'
|
12
|
+
return "[NSString stringWithFormat:@\"%ld\",(long)#{$json_var}];"
|
13
|
+
else
|
14
|
+
return "[NSString stringWithFormat:@\"#{property_format}\",#{$json_var}];"
|
15
|
+
end
|
16
|
+
elsif json_type=="double" || json_type=="float"
|
17
|
+
if property_format == '<#format#>'
|
18
|
+
return "[NSString stringWithFormat:@\"%f\",#{$json_var}];"
|
19
|
+
else
|
20
|
+
return "[NSString stringWithFormat:@\"#{property_format}\",#{$json_var}];"
|
21
|
+
end
|
22
|
+
elsif json_type=="date"
|
23
|
+
return %Q/NSDateFormatter * formatter=[[NSDateFormatter alloc] init],\\
|
24
|
+
\t\t\t formatter.dateFormat=@\"#{property_format}\",\\
|
25
|
+
\t\t\t NSDate * date=[formatter stringFromDate:@\"#{$json_var}\"],\\
|
26
|
+
\t\t\t date;/
|
27
|
+
else
|
28
|
+
return nil
|
29
|
+
end
|
30
|
+
elsif property_type =="int"
|
31
|
+
if json_type == "string" || json_type == "number"
|
32
|
+
return "[#{$json_var} integerValue];"
|
33
|
+
else
|
34
|
+
return "<#"+"custom"+"#>"
|
35
|
+
end
|
36
|
+
elsif property_type =="float"
|
37
|
+
if json_type == "string" || json_type == "number"
|
38
|
+
return "[#{$json_var} floatValue];"
|
39
|
+
else
|
40
|
+
return "<#"+"custom"+"#>"
|
41
|
+
end
|
42
|
+
elsif property_type =="double" || property_type =="time"
|
43
|
+
if json_type == "string" || json_type == "number"
|
44
|
+
return "[#{$json_var} doubleValue]"
|
45
|
+
elsif json_type=="date"
|
46
|
+
return "[#{$json_var} timeIntervalSinceReferenceDate];"
|
47
|
+
else
|
48
|
+
return "<#"+"custom"+"#>"
|
49
|
+
end
|
50
|
+
elsif property_type =="date"
|
51
|
+
if json_type == "string"
|
52
|
+
return %Q/NSDateFormatter * formatter=[[NSDateFormatter alloc] init],\\
|
53
|
+
\t\t\t formatter.dateFormat=@\"#{json_formate}\",\\
|
54
|
+
\t\t\t NSDate * date=[formatter dateFromString:@\"#{$json_var}\"],\\
|
55
|
+
\t\t\t date;/
|
56
|
+
elsif json_type=="time" || json_type=="double"
|
57
|
+
return " [NSDate dateWithTimeIntervalSinceReferenceDate:#{$json_var}];"
|
58
|
+
else
|
59
|
+
return "<#"+"custom"+"#>"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
class Helper
|
2
|
+
|
3
|
+
def self.help
|
4
|
+
content=%q{
|
5
|
+
# ptgen is a tools for model. It works on mac. If you use mantle for your entity. you can use it.
|
6
|
+
# I create it for my project. The model can contain [database] [request] [nimbus] option method.
|
7
|
+
# There is not prefect now .
|
8
|
+
# when you create your model, you maybe contain manay property to create. and some for request
|
9
|
+
# others for database . You must make your database and request column correspond to property index.
|
10
|
+
# I will fix it latter.
|
11
|
+
# I will make it more prefect
|
12
|
+
#
|
13
|
+
# flags:
|
14
|
+
# -l local. it's property
|
15
|
+
# -s server. it's json column name
|
16
|
+
# -d database. it's table column name
|
17
|
+
# -f force. force to overide modified method
|
18
|
+
# -r not used now.
|
19
|
+
# -a[lsd] generate same name
|
20
|
+
# -u[lsd] generate model according http response
|
21
|
+
# -m class mapping ,eg: 'data/student:student' , 'data/student' is path. last student is class name.
|
22
|
+
# -k primary key for database ,if -u contain d, you can use it ,otherwise it default the first property
|
23
|
+
# -h help
|
24
|
+
# if your entity is subclass to other class, you can append :[superClassName] behind
|
25
|
+
# [entityName]
|
26
|
+
# example:
|
27
|
+
# ptgen student -l name:string -s sname:string -d *tname #* is primary key.
|
28
|
+
#
|
29
|
+
# ptgen student:person -l name:string -s sname:string -d *tname
|
30
|
+
#
|
31
|
+
# it created Student.h and Student.m file in current path.
|
32
|
+
# property is name. and database method and json transformer method
|
33
|
+
# created
|
34
|
+
#
|
35
|
+
# version 2.0.0 new feture :
|
36
|
+
# 1. add -alsd param , [lsd] is option. it create model file conveniently with name property , server and db column same
|
37
|
+
# 2. add Http get request to generate model file
|
38
|
+
#
|
39
|
+
# example:
|
40
|
+
# 'ptgen -ulsd "http://www.i4.cn/ajax.php?a=getoldnewsforpage&itype=2&n=20" -m /:Student'
|
41
|
+
# it will generate model property according response of request , property name is same with server name . only support get request for json now
|
42
|
+
# note : please replace & with @ in url string
|
43
|
+
# 'ptgen -alsd *name:string'
|
44
|
+
#
|
45
|
+
# type :
|
46
|
+
# "string" => "NSString *"
|
47
|
+
# "int" => "NSInteger"
|
48
|
+
# "uint" => "NSUInteger"
|
49
|
+
# "float" => "float"
|
50
|
+
# "double" => "double"
|
51
|
+
# "number" => "NSNumber *"
|
52
|
+
# "date" => "NSDate *"
|
53
|
+
# "data" => "NSData *"
|
54
|
+
# "time" => "NSTimeInterval"
|
55
|
+
# "url" => "NSURL *"
|
56
|
+
# "array" => "NSArray *"
|
57
|
+
# "marray" => "NSMutableArray *"
|
58
|
+
# "dic" => "NSDictionary *"
|
59
|
+
# "mdic" => "NSMutableDictionary *"
|
60
|
+
# "indexset" => "NSIndexSet *"
|
61
|
+
# "mindexset" => "NSIndexPath *"
|
62
|
+
# "indexpath" => "NSIndexPath *"
|
63
|
+
# "mindexpath" => "NSMutableIndexSet *"
|
64
|
+
# "bool" => "BOOL",
|
65
|
+
# "enum" => "enum",
|
66
|
+
# "mapping"
|
67
|
+
# "custom"
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
class JsonGenerator
|
2
|
+
autoload :CommonParam, 'ModelGenerator/CommonParam'
|
3
|
+
autoload :CommandTask, 'ModelGenerator/CommandTask'
|
4
|
+
autoload :AnnotationGenerator, 'ModelGenerator/AnnotationGenerator'
|
5
|
+
autoload :FormatTransformer, 'ModelGenerator/FormatTransformer'
|
6
|
+
autoload :MethodGenerator, 'ModelGenerator/MethodGenerator'
|
7
|
+
|
8
|
+
attr_reader :commandTask
|
9
|
+
attr_reader :method_generator
|
10
|
+
|
11
|
+
def initialize(command)
|
12
|
+
@commandTask=command
|
13
|
+
@method_generator=MethodGenerator.new(command)
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_json_column_mapping
|
17
|
+
json_column_mappings_content=""
|
18
|
+
@commandTask.property_name_json_hash.keys.each do |name|
|
19
|
+
column=@commandTask.property_name_json_hash[name]
|
20
|
+
if(column)
|
21
|
+
cap_name = name.slice(0,1).capitalize + name.slice(1..-1)
|
22
|
+
json_column_mappings_content << "static NSString * #{cap_name}JsonKey = @\"#{column}\";\n"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
if json_column_mappings_content.length > 0
|
26
|
+
annotation = AnnotationGenerator.generate_single_annotation("json column declare")
|
27
|
+
json_column_mappings_content = annotation + json_column_mappings_content
|
28
|
+
end
|
29
|
+
return json_column_mappings_content
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_property_mapping
|
33
|
+
hasJsonColumn=false
|
34
|
+
long_space=" "
|
35
|
+
property_mapping_content="return @{\n"
|
36
|
+
@commandTask.property_name_json_hash.each do |key , value|
|
37
|
+
if value
|
38
|
+
hasJsonColumn=true
|
39
|
+
cap_key = key.slice(0,1).capitalize + key.slice(1..-1)
|
40
|
+
property_mapping_content << "#{long_space} @\"#{key}\":#{cap_key}JsonKey,\n"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
property_mapping_content=property_mapping_content.chomp(",\n")
|
45
|
+
property_mapping_content << "\n"
|
46
|
+
property_mapping_content << "#{long_space}};"
|
47
|
+
|
48
|
+
if hasJsonColumn
|
49
|
+
has_super=@commandTask.parent_class
|
50
|
+
content =@method_generator.generate_method("+","NSDictionary *","JSONKeyPathsByPropertyKey","#{property_mapping_content}",has_super)
|
51
|
+
annotation = AnnotationGenerator.generate_mark_annotation("json method")
|
52
|
+
content = annotation + content
|
53
|
+
return content
|
54
|
+
else
|
55
|
+
return ""
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def generate_formater
|
60
|
+
formater_content=""
|
61
|
+
@commandTask.property_name_type_hash.each do |key , value|
|
62
|
+
json_column=@commandTask.property_name_json_hash[key]
|
63
|
+
json_type=@commandTask.json_name_type_hash[json_column]
|
64
|
+
json_format=@commandTask.json_name_format_hash[json_column]
|
65
|
+
property_format=@commandTask.property_name_format_hash[key]
|
66
|
+
|
67
|
+
formater_method_content= generate_formater_method_content(key,value,property_format,json_type,json_format)
|
68
|
+
if formater_method_content !=nil && json_column
|
69
|
+
has_super=@commandTask.parent_class
|
70
|
+
formater_content << @method_generator.generate_method("+","NSValueTransformer *" , "#{key}AtJSONTransformer","#{formater_method_content}",has_super)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
return formater_content
|
75
|
+
end
|
76
|
+
|
77
|
+
def generate_formater_method_content(property_name,property_type,property_format,json_type,json_formate)
|
78
|
+
|
79
|
+
content=""
|
80
|
+
|
81
|
+
if property_type=="url" && json_type=="string"
|
82
|
+
content << "return [NSValueTransformer valueTransformerForName:MTLURLValueTransformerName];\n"
|
83
|
+
elsif (property_type=="bool" && json_type=="int")
|
84
|
+
content << "return [NSValueTransformer valueTransformerForName:MTLBooleanValueTransformerName];\n"
|
85
|
+
elsif property_type != json_type || (property_type == json_type && property_format != json_formate)
|
86
|
+
content = %Q/return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(#{CommonParam.type_mapping[json_type]} x) {
|
87
|
+
return #{FormatTransformer.transform(property_type,property_format,json_type,json_formate)}
|
88
|
+
} reverseBlock:^(#{CommonParam.type_mapping[property_type]} x) {
|
89
|
+
return #{FormatTransformer.transform(json_type,json_formate,property_type,property_format)}
|
90
|
+
}];/
|
91
|
+
elsif property_type=="mapping" && json_type=="mapping"
|
92
|
+
content = %Q/return [NSValueTransformer mtl_valueMappingTransformerWithDictionary:@{
|
93
|
+
<#dictionary_content#>
|
94
|
+
}];/
|
95
|
+
|
96
|
+
elsif !CommonParam.type_mapping[property_type]
|
97
|
+
content << "return [NSValueTransformer mtl_JSONDictionaryTransformerWithModelClass:#{property_name}.class];"
|
98
|
+
elsif property_type=="custom"
|
99
|
+
content = %Q/return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSString *str) {
|
100
|
+
return <#ConvertFormat#>;
|
101
|
+
} reverseBlock:^(NSDate *date) {
|
102
|
+
return <#ReverseConvertFormat#>;
|
103
|
+
}];/
|
104
|
+
else
|
105
|
+
content=nil
|
106
|
+
end
|
107
|
+
return content
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class MethodGenerator
|
2
|
+
autoload :Detector, 'ModelGenerator/Detector'
|
3
|
+
|
4
|
+
attr_reader :detector
|
5
|
+
attr_reader :commandTask
|
6
|
+
|
7
|
+
def initialize(command)
|
8
|
+
@commandTask = command
|
9
|
+
file_path="#{Dir.pwd}/#{command.entity_name}.m"
|
10
|
+
@detector=Detector.new(file_path)
|
11
|
+
@detector.detect_method
|
12
|
+
end
|
13
|
+
|
14
|
+
def generate_method(method_type,return_type,methond_name,method_content,has_custom_super_class)
|
15
|
+
if return_type.downcase.include?("dictionary") && has_custom_super_class
|
16
|
+
current_content= method_content.gsub(/return/,"NSDictionary *subDictionary=")
|
17
|
+
method_content =%Q/NSMutableDictionary * content=[[super #{methond_name}] mutableCopy];
|
18
|
+
#{current_content}
|
19
|
+
[content addEntriesFromDictionary:subDictionary];
|
20
|
+
return content;
|
21
|
+
/
|
22
|
+
|
23
|
+
elsif return_type.downcase.include?("array") && has_custom_super_class
|
24
|
+
current_content= method_content.gsub(/return/,"NSArray *subArray=")
|
25
|
+
method_content =%Q/NSMutableArray * content=[[super #{methond_name}] mutableCopy];
|
26
|
+
#{current_content}
|
27
|
+
[content addObjectsFromArray:subArray];
|
28
|
+
return content;
|
29
|
+
/
|
30
|
+
|
31
|
+
end
|
32
|
+
method_implement = "#{method_type}(#{return_type})#{methond_name}\n{\n\t#{method_content}\n}\n"
|
33
|
+
|
34
|
+
|
35
|
+
if !@commandTask.is_force_reset
|
36
|
+
result= @detector.method_implment_same?(method_implement)
|
37
|
+
return result ? result : method_implement
|
38
|
+
end
|
39
|
+
|
40
|
+
return method_implement
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
class ModelGenerator
|
4
|
+
autoload :Announcement, 'ModelGenerator/Announcement'
|
5
|
+
autoload :CommonParam, 'ModelGenerator/CommonParam'
|
6
|
+
autoload :CommandTask, 'ModelGenerator/CommandTask'
|
7
|
+
autoload :DBGenerator, 'ModelGenerator/DBGenerator'
|
8
|
+
autoload :JsonGenerator, 'ModelGenerator/JsonGenerator'
|
9
|
+
autoload :EnumGenerator, 'ModelGenerator/EnumGenerator'
|
10
|
+
autoload :NimbusGenerator, 'ModelGenerator/NimbusGenerator'
|
11
|
+
|
12
|
+
attr_accessor :name
|
13
|
+
attr_accessor :author
|
14
|
+
attr_reader :project_name
|
15
|
+
attr_reader :organization
|
16
|
+
attr_reader :commandTask
|
17
|
+
attr_reader :db_generator
|
18
|
+
attr_reader :json_generator
|
19
|
+
attr_reader :enum_generator
|
20
|
+
attr_reader :nimbus_generator
|
21
|
+
|
22
|
+
def initialize
|
23
|
+
@organization="<" +"#" + "organization" + "#" + ">"
|
24
|
+
@project_name="<" +"#" + "project_name" + "#" + ">"
|
25
|
+
@author="Unknow Author"
|
26
|
+
end
|
27
|
+
|
28
|
+
def seek_project_info
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def analyze_command(args)
|
33
|
+
@commandTask=CommandTask.new(args)
|
34
|
+
@commandTask.parseCommand
|
35
|
+
|
36
|
+
@db_generator=DBGenerator.new(@commandTask)
|
37
|
+
@json_generator=JsonGenerator.new(@commandTask)
|
38
|
+
@enum_generator=EnumGenerator.new(@commandTask.property_em_name_type_hash)
|
39
|
+
@nimbus_generator=NimbusGenerator.new(@commandTask)
|
40
|
+
|
41
|
+
return @commandTask
|
42
|
+
end
|
43
|
+
|
44
|
+
def generate_header
|
45
|
+
parent_path=File.expand_path('..', __FILE__)
|
46
|
+
headerPath="#{parent_path}/resource/Model/ModelTemple.h"
|
47
|
+
|
48
|
+
annouce=Announcement.new
|
49
|
+
annouce.name=@commandTask.entity_name
|
50
|
+
file_declare=annouce.createDeclare
|
51
|
+
|
52
|
+
|
53
|
+
entity_name=@commandTask.parent_class ? (@commandTask.entity_name + " : " + @commandTask.parent_class) : (@commandTask.entity_name + ": MTLModel<MTLJSONSerializing>")
|
54
|
+
|
55
|
+
to_header_content=File.read(headerPath)
|
56
|
+
|
57
|
+
header_import=""
|
58
|
+
if @commandTask.parent_class
|
59
|
+
header_import="#import \"#{@commandTask.entity_name}.h\"\n"
|
60
|
+
end
|
61
|
+
to_header_content.gsub!(CommonParam.header_import,header_import)
|
62
|
+
|
63
|
+
to_header_content.gsub!(CommonParam.file_declare,file_declare)
|
64
|
+
to_header_content.gsub!(CommonParam.enum_declare,@enum_generator.generate_em_declare)
|
65
|
+
to_header_content.gsub!(CommonParam.entity_name,entity_name)
|
66
|
+
to_header_content.gsub!(CommonParam.property_declare,self.generate_property_list)
|
67
|
+
|
68
|
+
|
69
|
+
f= File.new("#{Dir.pwd}/#{@commandTask.entity_name}.h","w")
|
70
|
+
f.syswrite(to_header_content)
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def generate_source
|
75
|
+
parent_path=File.expand_path('..', __FILE__)
|
76
|
+
source_path="#{parent_path}/resource/Model/ModelTemple.m"
|
77
|
+
to_source_content=File.read(source_path)
|
78
|
+
|
79
|
+
annouce=Announcement.new
|
80
|
+
annouce.name=@commandTask.entity_name
|
81
|
+
file_declare=annouce.createDeclare
|
82
|
+
entity_name=@commandTask.entity_name
|
83
|
+
|
84
|
+
|
85
|
+
to_source_content.gsub!(CommonParam.file_declare,file_declare)
|
86
|
+
to_source_content.gsub!(CommonParam.entity_name,entity_name)
|
87
|
+
if @json_generator.generate_property_mapping
|
88
|
+
to_source_content.gsub!(CommonParam.property_mapping,@json_generator.generate_property_mapping)
|
89
|
+
end
|
90
|
+
if @json_generator.generate_json_column_mapping
|
91
|
+
to_source_content.gsub!(CommonParam.json_column_mapping,@json_generator.generate_json_column_mapping)
|
92
|
+
end
|
93
|
+
if @json_generator.generate_formater
|
94
|
+
to_source_content.gsub!(CommonParam.type_transformer,@json_generator.generate_formater)
|
95
|
+
end
|
96
|
+
if @db_generator.generate_table_column_declare
|
97
|
+
to_source_content.gsub!(CommonParam.table_column_declare,@db_generator.generate_table_column_declare)
|
98
|
+
end
|
99
|
+
if @db_generator.generate_table_name
|
100
|
+
to_source_content.gsub!(CommonParam.table_name,@db_generator.generate_table_name)
|
101
|
+
end
|
102
|
+
if @db_generator.generate_primary_key
|
103
|
+
to_source_content.gsub!(CommonParam.table_primary_key,@db_generator.generate_primary_key)
|
104
|
+
end
|
105
|
+
if @db_generator.generate_table_mapping
|
106
|
+
to_source_content.gsub!(CommonParam.table_mapping,@db_generator.generate_table_mapping)
|
107
|
+
end
|
108
|
+
if @nimbus_generator.generate_need_methods
|
109
|
+
to_source_content.gsub!(CommonParam.nimbus_method,@nimbus_generator.generate_need_methods)
|
110
|
+
end
|
111
|
+
|
112
|
+
|
113
|
+
f= File.new("#{Dir.pwd}/#{entity_name}.m","w")
|
114
|
+
f.syswrite(to_source_content)
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
def generate_property_list
|
120
|
+
property_list_content=""
|
121
|
+
@commandTask.property_name_type_hash.each do |name,type|
|
122
|
+
var_type=type
|
123
|
+
reference_type=CommonParam.reference_type_mapping[var_type];
|
124
|
+
type_name= @commandTask.property_em_name_type_hash[name]
|
125
|
+
if type_name
|
126
|
+
property_type=@commandTask.property_em_name_type_hash[name]
|
127
|
+
else
|
128
|
+
property_type=CommonParam.type_mapping[var_type]
|
129
|
+
end
|
130
|
+
property_name=name
|
131
|
+
property_list_content << "@property(nonatomic,#{reference_type}) #{property_type} #{property_name};\n"
|
132
|
+
end
|
133
|
+
return property_list_content
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
class NimbusGenerator
|
2
|
+
autoload :CommandTask, 'ModelGenerator/CommandTask'
|
3
|
+
autoload :MethodGenerator, 'ModelGenerator/MethodGenerator'
|
4
|
+
autoload :AnnotationGenerator, 'ModelGenerator/AnnotationGenerator'
|
5
|
+
|
6
|
+
attr_reader :commandTask
|
7
|
+
|
8
|
+
def initialize(command)
|
9
|
+
@commandTask=command
|
10
|
+
@method_generator=MethodGenerator.new(command)
|
11
|
+
if !@commandTask.primary_key && @commandTask.flags.join.include?("d")
|
12
|
+
raise "error : you must set table primary key"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def generate_need_methods
|
17
|
+
content = ""
|
18
|
+
if !self.generate_cell_class_method.empty? || !self.generate_cell_style_method.empty?
|
19
|
+
content << AnnotationGenerator.generate_mark_annotation("Nimbus method")
|
20
|
+
content << self.generate_cell_class_method
|
21
|
+
content << "\n"
|
22
|
+
content << self.generate_cell_style_method
|
23
|
+
end
|
24
|
+
|
25
|
+
return content
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate_cell_class_method
|
29
|
+
if @commandTask.flags.join.include?("n")
|
30
|
+
return @method_generator.generate_method("-","Class","cellClass","return <#CellClass#>;",false);
|
31
|
+
end
|
32
|
+
|
33
|
+
return ""
|
34
|
+
end
|
35
|
+
|
36
|
+
def generate_cell_style_method
|
37
|
+
if @commandTask.flags.join.include?("n")
|
38
|
+
return @method_generator.generate_method("-","UITableViewCellStyle","cellStyle","return <#CellStyle#>;",false);
|
39
|
+
end
|
40
|
+
|
41
|
+
return ""
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'xcodeproj'
|
2
|
+
|
3
|
+
class ProjectInfo
|
4
|
+
attr_reader :project_path
|
5
|
+
attr_reader :project
|
6
|
+
|
7
|
+
def seek_xcodeproj(cmd_path)
|
8
|
+
Dir.foreach(cmd_path) do |filename|
|
9
|
+
if File.extname(filename)==".xcodeproj"
|
10
|
+
@project_path=filename
|
11
|
+
@project=Xcodeproj::Project.open(@project_path)
|
12
|
+
puts @project_path
|
13
|
+
puts @Project.build_configuration_list
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def organization
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
test = ProjectInfo.new
|
27
|
+
test.seek_xcodeproj("/Users/mac/Desktop/备份/")
|
@@ -0,0 +1,19 @@
|
|
1
|
+
[DECLARE]
|
2
|
+
|
3
|
+
#import "[NAME].h"
|
4
|
+
#import <MTLValueTransformer.h>
|
5
|
+
|
6
|
+
[JSON_COLUMN_MAPPING]
|
7
|
+
[TABLE_COLUMN_DECLARE]
|
8
|
+
|
9
|
+
@implementation [NAME]
|
10
|
+
|
11
|
+
[PROPERTY_MAPPING]
|
12
|
+
[TYPE_TRANSFORMER]
|
13
|
+
|
14
|
+
[TABLE_NAME]
|
15
|
+
[TABLE_PRIMARY_KEY]
|
16
|
+
[TABLE_MAPPING]
|
17
|
+
|
18
|
+
[NIMBUS_METHOD]
|
19
|
+
@end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: PTGenerator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cheng kai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: -m create model file
|
14
|
+
email: 710317434@qq.com
|
15
|
+
executables:
|
16
|
+
- ptgen
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README
|
20
|
+
files:
|
21
|
+
- README
|
22
|
+
- bin/ptgen
|
23
|
+
- lib/FileGenerator.rb
|
24
|
+
- lib/HTTPRequestDataParser/HTTPCommandParser.rb
|
25
|
+
- lib/HTTPRequestDataParser/HTTPRequestDataParser.rb
|
26
|
+
- lib/ModelGenerator/AnnotationGenerator.rb
|
27
|
+
- lib/ModelGenerator/Announcement.rb
|
28
|
+
- lib/ModelGenerator/CommandTask.rb
|
29
|
+
- lib/ModelGenerator/CommonParam.rb
|
30
|
+
- lib/ModelGenerator/DBGenerator.rb
|
31
|
+
- lib/ModelGenerator/Detector.rb
|
32
|
+
- lib/ModelGenerator/EnumGenerator.rb
|
33
|
+
- lib/ModelGenerator/FormatTransformer.rb
|
34
|
+
- lib/ModelGenerator/Helper.rb
|
35
|
+
- lib/ModelGenerator/JsonGenerator.rb
|
36
|
+
- lib/ModelGenerator/MethodGenerator.rb
|
37
|
+
- lib/ModelGenerator/ModelGenerator.rb
|
38
|
+
- lib/ModelGenerator/NimbusGenerator.rb
|
39
|
+
- lib/ModelGenerator/Project.rb
|
40
|
+
- lib/ModelGenerator/resource/Model/ModelTemple.h
|
41
|
+
- lib/ModelGenerator/resource/Model/ModelTemple.m
|
42
|
+
homepage: http://rubygems.org/gems/PTGenerator
|
43
|
+
licenses: []
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 2.4.8
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: use command line to create project temple file. This version function only
|
65
|
+
for entity model
|
66
|
+
test_files: []
|