gwtools 0.0.14 → 0.0.16

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: 578a4b3c9661d4f9abcb537ff04fa85f9fe4539511f6146a86e51395bf0d4594
4
- data.tar.gz: 84186267dcd12996e6383d0042fbc73d442a9d9e38e444b01b564ddd2453b331
3
+ metadata.gz: 815b48333cc7b62c19b09a7b974d11f4da0a06af2223242df93d41e1f94a45dd
4
+ data.tar.gz: d05b35e6e038c5ed00a44b167936550fede33a58bdc14d4fe9d75b080e62ad33
5
5
  SHA512:
6
- metadata.gz: ffec173635df34cfdb8f2546c49382ddabb461d07ae322bac0aad46855f414462d6bbc3bdbd8fb1574dcb5cd1639745cab3446f522c8f9d6cb918f29a23deab9
7
- data.tar.gz: 6ad68012b9573bed42efc66b5a8dea87db70b16af339331dc56cc3d5900f6c7a3f375513995791eb1bdd114fcf045fedaaf86bd2061ad7c791ae39c4567a913d
6
+ metadata.gz: d4c9a8f1ba05cf7a3003f4c6c00d599a978f761953350423baab24b364b0e5c82ab9902fe37297b1a65a0f7983267d6195702b3413917e176758d22c47d4a823
7
+ data.tar.gz: b519de2f870e800d5a33444e0579bc2a40bfe29536fcdc4bee4b68b41c5c9a86d61e568e6743dcb169ff675d576ba5f581057adbc621bf5f3d0c061ea97e38cf
@@ -37,13 +37,13 @@ module Gwtools
37
37
  def makeDirAndFile(url)
38
38
  uri = URI(url)
39
39
  path = uri.path
40
- file_name = 'restful'
40
+ # file_name = 'restful'
41
41
 
42
42
  # remove leading '/'
43
43
  path = path[1..-1] if path.start_with? '/'
44
44
 
45
45
  # split path by '/' and create directories
46
- dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") } + [file_name]
46
+ dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") }# + [file_name]
47
47
 
48
48
  # puts @target_path
49
49
  # puts Parser.enum_from_url(url).to_json
@@ -53,24 +53,16 @@ module Gwtools
53
53
  #
54
54
  FileUtils.mkdir_p(@target_path) unless File.directory?(@target_path)
55
55
  Generate::Swift.createFile(@target_path, path)
56
- # dirs.each_with_index do |dir, index|
57
- # # construct directory path
58
- # dir_path = @target_path + '/' + dirs[0..index].join('/')
59
- #
60
- # # create directory if it doesn't exist, with out file_name
61
- # if dir != file_name
62
- # FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
63
- # end
64
- # # create file
65
- # Generate::Swift.createPathFile(dir_path, dir, dirs, index,
66
- # path,
67
- # @url,
68
- # @header,
69
- # @httpMethod,
70
- # @parameters,
71
- # @body,
72
- # @responseJson
73
- # )
56
+ Generate::Swift.createPathFile(@target_path,
57
+ dirs,
58
+ @url,
59
+ path,
60
+ @header,
61
+ @httpMethod,
62
+ @parameters,
63
+ @body,
64
+ @responseJson
65
+ )
74
66
  # end
75
67
  end
76
68
 
@@ -7,35 +7,6 @@ require 'pp'
7
7
  module Gwtools
8
8
  class Generate
9
9
  class Swift
10
-
11
- class PathGenerateConfig
12
- include(ERB::Util)
13
- attr_accessor :api_name, :this_path
14
-
15
- def config_file
16
- # ERB.new(File.read('../lib/erb/path_url.swift.erb'), nil, '>').result(binding)
17
- template = <<-ERB
18
- // 此文件由代码生成,不要任何修
19
-
20
- extension <%="#{@api_name}"%> {
21
- // <%="#{@this_path}\n"%>
22
- public struct <%="#{@this_path}"%> { }
23
- }
24
- ERB
25
- ERB.new(template, nil, '>').result(binding)
26
- end
27
-
28
- def initialize(file_path, api_name, this_path)
29
- @api_name = api_name
30
- @this_path = this_path
31
-
32
- File.open(file_path, 'w+') do |f|
33
- f.puts(config_file)
34
- end
35
- end
36
-
37
- end
38
-
39
10
  class ModelReqGenerateConfig
40
11
  include(ERB::Util)
41
12
  attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath,
@@ -108,7 +79,7 @@ public extension <%="#{@api_name}"%> {
108
79
  @model_data = model_data
109
80
  @json_data_is_arr = json_data_is_arr
110
81
  @readme = readme
111
-
82
+ puts "write to ==> #{file_path}"
112
83
  File.open(file_path, 'w+') do |f|
113
84
  f.puts(config_file)
114
85
  end
@@ -143,11 +114,10 @@ public extension <%="#{@api_name}"%> {
143
114
 
144
115
  def self.getDetailPath(dir_path, prefixPath, modulePath)
145
116
  return dir_path.gsub(/^\/|\/$/, "").gsub(/\A#{prefixPath}/, "")
146
- .gsub(/^\/|\/$/, "").gsub(/\A#{modulePath}/, "")
147
- .gsub(/^\/|\/$/, "")
117
+ .gsub(/^\/|\/$/, "").gsub(/\A#{modulePath}/, "")
118
+ .gsub(/^\/|\/$/, "")
148
119
  end
149
120
 
150
-
151
121
  def self.createFile(target_path, url)
152
122
  pre_name = 'pre_path'
153
123
  target_swift_file = "#{target_path + "/#{pre_name}"}/#{url.split('/').first}.swift"
@@ -174,92 +144,87 @@ public extension <%="#{@api_name}"%> {
174
144
  `swiftformat --swiftversion 5.0 #{target_swift_file}`
175
145
  end
176
146
 
177
- def self.createPathFile(dir_path, dir, dirs, index,
178
- url_path,
179
- url,
147
+ def self.createPathFile(target_path,
148
+ dirs,
149
+ full_url,
150
+ path,
180
151
  header,
181
152
  httpMethod,
182
153
  parameters,
183
154
  body,
184
155
  responseJson)
185
- app_name = "AppAPI.#{dirs[0..index-1].join('.')}"
186
- if index == 0
187
- app_name = "AppAPI"
188
- end
189
- if dir == 'restful'
190
- swift_model = generate_model("#{httpMethod.capitalize.chomp}Model", responseJson, 'data')
191
- prefixPath = getPrefixPath(url_path)
192
-
193
- return unless !prefixPath.empty?
194
-
195
- modulePath = getModulePath(url_path, prefixPath)
196
- detailPath = getDetailPath(url_path, prefixPath, modulePath)
197
-
198
- readme = """
199
- 请求参数
200
- URL = #{url.chomp}
201
- HTTPMETHOD = #{httpMethod.chomp}
202
-
203
- ViewModel
204
- class <##SomeViewModel##> {
205
- let disposeBag = DisposeBag()
206
-
207
- public struct Input {
208
- let <##AnyObservable##>: Observable<<##AnyObservableType##>>
209
- }
210
-
211
- public struct Output {
212
- let data: BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>
213
- }
214
-
215
- public func transform(input: Input) -> Output {
216
- let outDatas = BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>(value: [])
217
- input.<##AnyObservable##>.flatMapLatest({ [weak self] () -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> in
218
- guard let self = self else { return Observable.just([]) }
219
- return self.reqData()
220
- }).subscribe(onNext: { (items) in
221
- outDatas.accept(outDatas.value + items)
222
- }, onError: { error in
223
- outDatas.accept([])
224
- }).disposed(by: disposeBag)
225
-
226
- return Output(data: outDatas)
227
- }
228
- }
229
-
230
- extension <##SomeViewModel##> {
231
- func reqData() -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> {
232
- #{app_name}.#{httpMethod.capitalize.chomp}()
233
- .compactMap { $0 } // 解析出需要的类型
234
- .observeOn(MainScheduler.instance)
235
- .catchErrorJustReturn([])
236
- }
237
- }
238
- """
239
-
240
- ModelReqGenerateConfig.new(
241
- "#{dir_path}.swift",
242
- app_name,
243
- httpMethod.capitalize.chomp,
244
- prefixPath,
245
- modulePath,
246
- detailPath,
247
- "#{httpMethod.capitalize.chomp}Model",
248
- swift_model,
249
- @json_data_is_arr,
250
- readme
251
- )
252
-
253
- `swiftformat --swiftversion 5.0 #{dir_path}.swift`
254
- else
255
- PathGenerateConfig.new(
256
- "#{dir_path}.swift",
257
- app_name,
258
- dir
259
- )
260
- end
156
+ app_name = dirs.join('.')
157
+ root_path = 'data' # 这里只关心 data 下的数据
158
+ swift_model = generate_model("#{httpMethod.capitalize.chomp}Model", responseJson, root_path)
159
+ prefixPath = getPrefixPath(path)
160
+ prefixPath = getPrefixPath(path)
161
+
162
+ return unless !prefixPath.empty?
163
+
164
+ modulePath = getModulePath(path, prefixPath)
165
+ detailPath = getDetailPath(path, prefixPath, modulePath)
166
+
167
+ readme = """
168
+ 请求参数
169
+ URL = #{path.chomp}
170
+ HTTPMETHOD = #{httpMethod.chomp}
171
+
172
+ ViewModel
173
+ class <##SomeViewModel##> {
174
+ let disposeBag = DisposeBag()
175
+
176
+ public struct Input {
177
+ let <##AnyObservable##>: Observable<<##AnyObservableType##>>
178
+ }
179
+
180
+ public struct Output {
181
+ let data: BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>
182
+ }
183
+
184
+ public func transform(input: Input) -> Output {
185
+ let outDatas = BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>(value: [])
186
+ input.<##AnyObservable##>.flatMapLatest({ [weak self] () -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> in
187
+ guard let self = self else { return Observable.just([]) }
188
+ return self.reqData()
189
+ }).subscribe(onNext: { (items) in
190
+ outDatas.accept(outDatas.value + items)
191
+ }, onError: { error in
192
+ outDatas.accept([])
193
+ }).disposed(by: disposeBag)
194
+
195
+ return Output(data: outDatas)
196
+ }
197
+ }
198
+
199
+ extension <##SomeViewModel##> {
200
+ func reqData() -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> {
201
+ #{app_name}.#{httpMethod.capitalize.chomp}()
202
+ .compactMap { $0 } // 解析出需要的类型
203
+ .observeOn(MainScheduler.instance)
204
+ .catchErrorJustReturn([])
205
+ }
206
+ }
207
+ """
208
+
209
+ ModelReqGenerateConfig.new(
210
+ "#{target_path + '/' + app_name}.swift",
211
+ app_name,
212
+ httpMethod.capitalize.chomp,
213
+ prefixPath,
214
+ modulePath,
215
+ detailPath,
216
+ "#{httpMethod.capitalize.chomp}Model",
217
+ swift_model,
218
+ responseJson[root_path].is_a?(Array),
219
+ readme
220
+ )
221
+
222
+ `swiftformat --swiftversion 5.0 "#{target_path + '/' + app_name}.swift",`
223
+
224
+ # puts target_swift_file = "#{target_path}/#{app_name}.swift"
261
225
 
262
226
  end
227
+
263
228
  def self.capitalize_first_letter(str)
264
229
  str[0].upcase + str[1..-1]
265
230
  end
@@ -327,4 +292,4 @@ public extension <%="#{@api_name}"%> {
327
292
 
328
293
  end
329
294
  end
330
- end
295
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.14"
4
+ VERSION = "0.0.16"
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.14
4
+ version: 0.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - chenglq