gwtools 0.0.13 → 0.0.15

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: 7d9ac7103f49aa7472021c8a7b66d2937718a43c69753eb679427ec1a211bff7
4
- data.tar.gz: 739815ba75420526ea48db985d73fc30f769aa30e8d153ed1b811a373ffa51c2
3
+ metadata.gz: d9008f2c63bf9819bb028d56605f9f971c9f75e27865a54faae2115d8b1c384f
4
+ data.tar.gz: cd71025cf2a99576b58686ae67469de00086b571516ca574f3639f7485e5675a
5
5
  SHA512:
6
- metadata.gz: def094dbfbf093ed45bbbff27000e1686b5f54f8a5a95e2ba217e61f5224bc60ba8559a246146388ad151114cc8df524f33c3d9d5941d214096e85daa3e9a1be
7
- data.tar.gz: a7209c21c28ebc0b5be46bba41134758a5c26edc33cd2ce400c1e1f6cb53e3f221204e44e8089379d3f56d3b0536bc089a4fe792c1313e463427817016b7252e
6
+ metadata.gz: a55cdc1621a3c012d7d40922078af8080ceff67b43f0f9007d08b6f21c4cd473247a071d3d776c0a45d9eafa4c9d9bf68e464734aac6135d3de0167ab00a4068
7
+ data.tar.gz: 68fd58663f5eecc0a587dcfb80cb62a287b31e51d81262fc8c210584a00aa23e05f237bc695ba1fcc4d5966877f15cfe0118ac0eea76a0c78cb21b3367a63ae1
@@ -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,23 +114,21 @@ 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"
154
124
  FileUtils.mkdir_p(target_path + "/#{pre_name}") unless File.directory?(target_path + "/#{pre_name}")
155
125
 
156
- File.open(target_swift_file, "w") do |file|
157
- file.write("{}")
158
- end
159
126
  # 从 .swift 文件读取 enum
160
- Parser.extract_from(target_swift_file)
161
- json1Hash = JSON.parse(Parser.get_clear_enums.to_json)
162
-
127
+ json1Hash = {}
128
+ if File.exist?(target_swift_file)
129
+ Parser.extract_from(target_swift_file)
130
+ json1Hash = JSON.parse(Parser.get_clear_enums.to_json)
131
+ end
163
132
  # 从 url 解析 enum
164
133
  url_enum = Parser.enum_from_url(url)
165
134
  json2Hash = {url_enum.enum_name.to_sym => url_enum}
@@ -175,92 +144,87 @@ public extension <%="#{@api_name}"%> {
175
144
  `swiftformat --swiftversion 5.0 #{target_swift_file}`
176
145
  end
177
146
 
178
- def self.createPathFile(dir_path, dir, dirs, index,
179
- url_path,
180
- url,
147
+ def self.createPathFile(target_path,
148
+ dirs,
149
+ full_url,
150
+ path,
181
151
  header,
182
152
  httpMethod,
183
153
  parameters,
184
154
  body,
185
155
  responseJson)
186
- app_name = "AppAPI.#{dirs[0..index-1].join('.')}"
187
- if index == 0
188
- app_name = "AppAPI"
189
- end
190
- if dir == 'restful'
191
- swift_model = generate_model("#{httpMethod.capitalize.chomp}Model", responseJson, 'data')
192
- prefixPath = getPrefixPath(url_path)
193
-
194
- return unless !prefixPath.empty?
195
-
196
- modulePath = getModulePath(url_path, prefixPath)
197
- detailPath = getDetailPath(url_path, prefixPath, modulePath)
198
-
199
- readme = """
200
- 请求参数
201
- URL = #{url.chomp}
202
- HTTPMETHOD = #{httpMethod.chomp}
203
-
204
- ViewModel
205
- class <##SomeViewModel##> {
206
- let disposeBag = DisposeBag()
207
-
208
- public struct Input {
209
- let <##AnyObservable##>: Observable<<##AnyObservableType##>>
210
- }
211
-
212
- public struct Output {
213
- let data: BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>
214
- }
215
-
216
- public func transform(input: Input) -> Output {
217
- let outDatas = BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>(value: [])
218
- input.<##AnyObservable##>.flatMapLatest({ [weak self] () -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> in
219
- guard let self = self else { return Observable.just([]) }
220
- return self.reqData()
221
- }).subscribe(onNext: { (items) in
222
- outDatas.accept(outDatas.value + items)
223
- }, onError: { error in
224
- outDatas.accept([])
225
- }).disposed(by: disposeBag)
226
-
227
- return Output(data: outDatas)
228
- }
229
- }
230
-
231
- extension <##SomeViewModel##> {
232
- func reqData() -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> {
233
- #{app_name}.#{httpMethod.capitalize.chomp}()
234
- .compactMap { $0 } // 解析出需要的类型
235
- .observeOn(MainScheduler.instance)
236
- .catchErrorJustReturn([])
237
- }
238
- }
239
- """
240
-
241
- ModelReqGenerateConfig.new(
242
- "#{dir_path}.swift",
243
- app_name,
244
- httpMethod.capitalize.chomp,
245
- prefixPath,
246
- modulePath,
247
- detailPath,
248
- "#{httpMethod.capitalize.chomp}Model",
249
- swift_model,
250
- @json_data_is_arr,
251
- readme
252
- )
253
-
254
- `swiftformat --swiftversion 5.0 #{dir_path}.swift`
255
- else
256
- PathGenerateConfig.new(
257
- "#{dir_path}.swift",
258
- app_name,
259
- dir
260
- )
261
- end
156
+ app_name = dirs.join('.')
157
+
158
+ swift_model = generate_model("#{httpMethod.capitalize.chomp}Model", responseJson, 'data')
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
+ false,
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"
262
225
 
263
226
  end
227
+
264
228
  def self.capitalize_first_letter(str)
265
229
  str[0].upcase + str[1..-1]
266
230
  end
@@ -328,4 +292,4 @@ public extension <%="#{@api_name}"%> {
328
292
 
329
293
  end
330
294
  end
331
- end
295
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.13"
4
+ VERSION = "0.0.15"
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.13
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - chenglq