gwtools 0.0.5 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 43e5aaf57e5a1887d0fd602c416c4727a8163296e5cc0cb23c2a3cd6226bcee7
4
- data.tar.gz: 72c5d96b01cf73ace17487f392213b6c7e9c186b5c9ecfb82d7884cd11c7d4bf
3
+ metadata.gz: 001eef35553a92118abccc74a73ef133fce57509e775e9cb7361b014a2bc6436
4
+ data.tar.gz: 35ed21f791aee545295149c39b747b789d02057616b7c0e747861e9fb6f9ff14
5
5
  SHA512:
6
- metadata.gz: 5ed11c0b2efadafa7c0564431fc8191a70451b1601ef4603a13078e10b570326c0802a44044f15e80596c12c75765c29c94712c58974fd492756275843307890
7
- data.tar.gz: f5eda98e671f387149f20d879efa1aaf5698b7c0544b1224c07f52d50200388daff4fb1171755b7947ce700826929a973d5057163e9efdb1d238919770f5c156
6
+ metadata.gz: dd259aa0fde7b6c9c958b5b49b8bd83dc70d4cfde0dc3eafea2265f1b6976a657ebd4beee4c559b866e25e133e02d58b704312babed58cfac442bf010c78d229
7
+ data.tar.gz: 2e8e27d43e284ccf7301d0ce5eff6b3e9b49cd18908ce32e4c87dadded65104da415d5cb8bb753b1b87f5992e58020278f4a0dd3d08e8f806970f797b77d5a38
@@ -32,8 +32,8 @@ module Gwtools
32
32
  # """
33
33
 
34
34
  makeDirAndFile(@url)
35
- # 生成对应Model文件
36
- Generate::Swift.createModelAndRequest(@target_path, @responseJson)
35
+ # # 生成对应Model文件
36
+ # Generate::Swift.createModelAndRequest(@target_path, @responseJson)
37
37
 
38
38
  end
39
39
 
@@ -57,7 +57,7 @@ module Gwtools
57
57
  end
58
58
 
59
59
  # create file
60
- Generate::Swift.createPathFile(dir_path, dir, dirs, index)
60
+ Generate::Swift.createPathFile(dir_path, dir, dirs, index, @httpMethod, @responseJson, path)
61
61
  end
62
62
  end
63
63
 
@@ -17,7 +17,7 @@ module Gwtools
17
17
  end
18
18
 
19
19
  def validate!
20
- # super TODO: 这里还没检查参数
20
+ help! 'file_path is required.' unless @file_path
21
21
  end
22
22
 
23
23
  def run
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'erb'
4
4
  require 'json'
5
+ require 'pp'
5
6
 
6
7
  module Gwtools
7
8
  class Generate
@@ -37,13 +38,61 @@ extension <%="#{@api_name}"%> {
37
38
 
38
39
  class ModelReqGenerateConfig
39
40
  include(ERB::Util)
40
- attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data
41
+ attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data, :json_data_is_arr
41
42
 
43
+ # 将 erb 文件的一个实例化,绑定到当前的一个对象的上下文
42
44
  def config_file
43
- ERB.new(File.read('../lib/erb/model_req.swift.erb'), nil, '>').result(binding)
45
+ # ERB.new(File.read('restful_url.swift.erb'), nil, '>').result(binding)
46
+ template = <<-ERB
47
+ /// 此文件由代码生成,不要修改任何路径相关参数,仅可对 headerParms 和 parms 做处理
48
+
49
+ import RxSwift
50
+ import GWNetWork
51
+ import GWUserCenterBase
52
+ import SwiftyUserDefaults
53
+ import HandyJSON
54
+
55
+ public extension <%="#{@api_name}"%> {
56
+ <%="#{@model_data}\n"%>
57
+ }
58
+
59
+ public extension <%="#{@api_name}"%> {
60
+ static func <%="#{@httpmethod}"%>(/*custom_params,*/
61
+ isCache: Bool = false,
62
+ isLoading: Bool = false,
63
+ module: String? = nil,
64
+ <% if #{@json_data_is_arr} %>
65
+ des: String? = nil) -> Observable<[Self.<%="#{@model_name}"%>]> {
66
+ <% else %>
67
+ des: String? = nil) -> Observable<Self.<%="#{@model_name}"%>> {
68
+ <% end %>
69
+ // ================= 逻辑处理 =================
70
+ let headerParms: [String : String] = [:]
71
+ let parms: [String : Any] = [:]
72
+ // ================= 逻辑处理 =================
73
+
74
+ return GWNetworkManager.request(requestType: .<%="#{@httpmethod}"%>,
75
+ prefixPath: "<%="#{@prefixPath}"%>",
76
+ modulePath: "<%="#{@modulePath}"%>",
77
+ detailPath: "<%="#{@detailPath}"%>",
78
+ parms: parms,
79
+ headerParms: headerParms,
80
+ isCache: isCache,
81
+ isLoading: isLoading,
82
+ module: module,
83
+ des: des)
84
+ <% if #{@json_data_is_arr} %>
85
+ .mapHandyJSON2Array(Self.<%="#{@model_name}"%>.self)
86
+ <% else %>
87
+ .mapHandyJSON(Self.<%="#{@model_name}"%>.self)
88
+ <% end %>
89
+ }
90
+ }
91
+ ERB
92
+ ERB.new(template, nil, '>').result(binding)
44
93
  end
45
94
 
46
- def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data)
95
+ def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data, json_data_is_arr)
47
96
  @api_name = api_name
48
97
  @httpmethod = httpmethod
49
98
  @prefixPath = prefixPath
@@ -51,6 +100,7 @@ extension <%="#{@api_name}"%> {
51
100
  @detailPath = detailPath
52
101
  @model_name = model_name
53
102
  @model_data = model_data
103
+ @json_data_is_arr = json_data_is_arr
54
104
 
55
105
  File.open(file_path, 'w+') do |f|
56
106
  f.puts(config_file)
@@ -58,22 +108,89 @@ extension <%="#{@api_name}"%> {
58
108
  end
59
109
  end
60
110
 
61
- def self.createPathFile(dir_path, dir, dirs, index)
62
- if index <= 0
63
- return
111
+ def self.getPrefixPath(dir_path)
112
+ prefixPaths = %w[app-api/api/v1.0 app-api/api/v2.0]
113
+
114
+ prefixPaths.each do |prefix_path|
115
+ return prefix_path.gsub(/^\/|\/$/, "") if dir_path.start_with?(prefix_path)
64
116
  end
65
- PathGenerateConfig.new(
66
- "#{dir_path}.swift",
67
- "AppAPI.#{dirs[0..index-1].join('.')}",
68
- dir
69
- )
117
+
118
+ return ''
70
119
  end
71
120
 
72
- def self.generate_model(name, json_data)
73
- swift_model = "struct #{name}: HandyJson {\n"
121
+ def self.getModulePath(dir_path, prefixPath)
122
+ modulePaths = %w[content community]
123
+ dir_path = dir_path.gsub(/\A#{prefixPath}/, "").gsub(/^\/|\/$/, "")
124
+ modulePaths.each do |modulePath|
125
+ return modulePath if dir_path.start_with?(modulePath)
126
+ end
127
+
128
+ return ''
129
+ end
130
+
131
+ def self.getDetailPath(dir_path, prefixPath, modulePath)
132
+ return dir_path.gsub(/^\/|\/$/, "").gsub(/\A#{prefixPath}/, "")
133
+ .gsub(/^\/|\/$/, "").gsub(/\A#{modulePath}/, "")
134
+ .gsub(/^\/|\/$/, "")
135
+ end
136
+
137
+ def self.createPathFile(dir_path, dir, dirs, index, http_method, response_json, url_path)
138
+ app_name = "AppAPI.#{dirs[0..index-1].join('.')}"
139
+ if index == 0
140
+ app_name = "AppAPI"
141
+ end
142
+ if dir == 'restful'
143
+ swift_model = generate_model("#{http_method.capitalize.chomp}Model", response_json)
144
+ prefixPath = getPrefixPath(url_path)
145
+ modulePath = getModulePath(url_path, prefixPath)
146
+ detailPath = getDetailPath(url_path, prefixPath, modulePath)
147
+ ModelReqGenerateConfig.new(
148
+ "#{dir_path}.swift",
149
+ app_name,
150
+ http_method.capitalize.chomp,
151
+ prefixPath,
152
+ modulePath,
153
+ detailPath,
154
+ "#{http_method.capitalize.chomp}Model",
155
+ swift_model,
156
+ @json_data_is_arr
157
+ )
158
+
159
+ `swiftformat --swiftversion 5.0 #{dir_path}.swift`
160
+ else
161
+
162
+ PathGenerateConfig.new(
163
+ "#{dir_path}.swift",
164
+ app_name,
165
+ dir
166
+ )
167
+ end
168
+
169
+ end
170
+
171
+ def self.generate_model(name, json_data, level = 0)
172
+
74
173
  if json_data == nil
75
174
  return ''
76
175
  end
176
+
177
+ if level == 0
178
+ # 找到 真正要处理的 data 数据
179
+ json_data.each do |key, value|
180
+ if key == 'data'
181
+ case value
182
+ when Array
183
+ json_data = value.first
184
+ @json_data_is_arr = true
185
+ when Hash
186
+ json_data = value
187
+ @json_data_is_arr = false
188
+ end
189
+ end
190
+ end
191
+ end
192
+
193
+ swift_model = "struct #{name}: HandyJSON {\n"
77
194
  json_data.each do |key, value|
78
195
  case value
79
196
  when NilClass # nil 对象默认给个Stirng?类型
@@ -89,25 +206,22 @@ extension <%="#{@api_name}"%> {
89
206
  when String
90
207
  swift_model += " var #{key}: String?\n"
91
208
  when Array
92
- swift_model += " var #{key}: [#{key.capitalize}] = []\n"
93
- swift_model += generate_model("#{key.capitalize}", value.first)
209
+ if !value.empty?
210
+ swift_model += " var #{key}: [#{key.capitalize}] = []\n"
211
+ swift_model += generate_model("#{key.capitalize}", value.first, level+1)
212
+ else
213
+ swift_model += " var #{key}: [Any] = []\n"
214
+ end
94
215
  when Hash
95
216
  swift_model += " var #{key}: #{key.capitalize}?\n"
96
- swift_model += generate_model("#{key.capitalize}", value)
217
+ swift_model += generate_model("#{key.capitalize}", value, level+1)
97
218
  else
98
219
  swift_model += " var #{key}: #{value.class == String ? 'String' : 'Int'}\n"
99
220
  end
100
221
  end
101
- swift_model + "}\n"
222
+ swift_model + "public init() {}\n}\n"
102
223
  end
103
224
 
104
- def self.createModelAndRequest(target_path, responseJson)
105
- swift_model = generate_model("Model", responseJson)
106
- File.write(target_path + 'Model.swift', swift_model)
107
- end
108
-
109
- # 生成 Swift 模型
110
-
111
225
  end
112
226
  end
113
227
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gwtools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - chenglq