gwtools 0.0.7 → 0.0.9
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 +4 -4
- data/lib/gwtools/analysis/file_handler.rb +17 -8
- data/lib/gwtools/generate/swift.rb +83 -13
- data/lib/gwtools/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a0d8d1c08e191ea5994386b5ffa6ff37ead560cf421bacf85bc5378cabbff27d
|
4
|
+
data.tar.gz: 72b8d9b5c16ca85bc8dd2ae93451f03b09aea284fd617554647eb74c9ba52df9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a8bfa15a1f9f640e62796a01c1cb0475705a62b4ab5860ed9e6d98fb4c2f9f039f6b84951961b63c3cb155a6cb7333cb295764c4c4270647eeef83a7a91a267
|
7
|
+
data.tar.gz: b79801bece0b7663e9ff5cc3048147964d89df2a6531473fcf1670a7b4ad300ad9e4aae1ae3a368f852570085056394afaedf5a09d3ca1ce522d92cd9b8a1f58
|
@@ -32,32 +32,41 @@ module Gwtools
|
|
32
32
|
# """
|
33
33
|
|
34
34
|
makeDirAndFile(@url)
|
35
|
-
# # 生成对应Model文件
|
36
|
-
# Generate::Swift.createModelAndRequest(@target_path, @responseJson)
|
37
|
-
|
38
35
|
end
|
39
36
|
|
40
37
|
def makeDirAndFile(url)
|
41
38
|
uri = URI(url)
|
42
39
|
path = uri.path
|
40
|
+
file_name = 'restful'
|
43
41
|
|
44
42
|
# remove leading '/'
|
45
43
|
path = path[1..-1] if path.start_with? '/'
|
46
44
|
|
47
45
|
# split path by '/' and create directories
|
48
|
-
dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") } + [
|
46
|
+
dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") } + [file_name]
|
47
|
+
|
48
|
+
# 先判断路径下是否存在restful.swift
|
49
|
+
puts "文件已存在: #{@target_path + '/' + dirs.join('/') + '.swift'}" unless !File.exist?(@target_path + '/' + dirs.join('/') + '.swift')
|
50
|
+
|
49
51
|
FileUtils.mkdir_p(@target_path) unless File.directory?(@target_path)
|
50
52
|
dirs.each_with_index do |dir, index|
|
51
53
|
# construct directory path
|
52
54
|
dir_path = @target_path + '/' + dirs[0..index].join('/')
|
53
55
|
|
54
|
-
# create directory if it doesn't exist, with out
|
55
|
-
if dir !=
|
56
|
+
# create directory if it doesn't exist, with out file_name
|
57
|
+
if dir != file_name
|
56
58
|
FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
|
57
59
|
end
|
58
|
-
|
59
60
|
# create file
|
60
|
-
Generate::Swift.createPathFile(dir_path, dir, dirs, index,
|
61
|
+
Generate::Swift.createPathFile(dir_path, dir, dirs, index,
|
62
|
+
path,
|
63
|
+
@url,
|
64
|
+
@header,
|
65
|
+
@httpMethod,
|
66
|
+
@parameters,
|
67
|
+
@body,
|
68
|
+
@responseJson
|
69
|
+
)
|
61
70
|
end
|
62
71
|
end
|
63
72
|
|
@@ -38,7 +38,8 @@ extension <%="#{@api_name}"%> {
|
|
38
38
|
|
39
39
|
class ModelReqGenerateConfig
|
40
40
|
include(ERB::Util)
|
41
|
-
attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath,
|
41
|
+
attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath,
|
42
|
+
:model_name, :model_data, :json_data_is_arr, :readme
|
42
43
|
|
43
44
|
# 将 erb 文件的一个实例化,绑定到当前的一个对象的上下文
|
44
45
|
def config_file
|
@@ -52,6 +53,10 @@ import GWUserCenterBase
|
|
52
53
|
import SwiftyUserDefaults
|
53
54
|
import HandyJSON
|
54
55
|
|
56
|
+
/** 参考代码
|
57
|
+
<%="#{@readme}"%>
|
58
|
+
*/
|
59
|
+
|
55
60
|
public extension <%="#{@api_name}"%> {
|
56
61
|
<%="#{@model_data}\n"%>
|
57
62
|
}
|
@@ -92,7 +97,8 @@ public extension <%="#{@api_name}"%> {
|
|
92
97
|
ERB.new(template, nil, '>').result(binding)
|
93
98
|
end
|
94
99
|
|
95
|
-
def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath,
|
100
|
+
def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath,
|
101
|
+
model_name, model_data, json_data_is_arr, readme)
|
96
102
|
@api_name = api_name
|
97
103
|
@httpmethod = httpmethod
|
98
104
|
@prefixPath = prefixPath
|
@@ -101,6 +107,7 @@ public extension <%="#{@api_name}"%> {
|
|
101
107
|
@model_name = model_name
|
102
108
|
@model_data = model_data
|
103
109
|
@json_data_is_arr = json_data_is_arr
|
110
|
+
@readme = readme
|
104
111
|
|
105
112
|
File.open(file_path, 'w+') do |f|
|
106
113
|
f.puts(config_file)
|
@@ -115,16 +122,21 @@ public extension <%="#{@api_name}"%> {
|
|
115
122
|
return prefix_path.gsub(/^\/|\/$/, "") if dir_path.start_with?(prefix_path)
|
116
123
|
end
|
117
124
|
|
125
|
+
puts "PrefixPath不在预设数组中, 请检查请求路径中的PrefixPath, 预设 PrefixPath(代码生成终止) \n#{dir_path}\n#{prefixPaths} "
|
126
|
+
|
118
127
|
return ''
|
119
128
|
end
|
120
129
|
|
121
130
|
def self.getModulePath(dir_path, prefixPath)
|
122
131
|
modulePaths = %w[content community]
|
123
132
|
dir_path = dir_path.gsub(/\A#{prefixPath}/, "").gsub(/^\/|\/$/, "")
|
133
|
+
|
124
134
|
modulePaths.each do |modulePath|
|
125
|
-
return modulePath if dir_path.
|
135
|
+
return modulePath if dir_path.split("/").first == modulePath
|
126
136
|
end
|
127
137
|
|
138
|
+
puts "ModulePath不在预设数组中, 请检查请求路径中的ModulePath, 预设 ModulePath \n#{dir_path}\n#{modulePaths} "
|
139
|
+
|
128
140
|
return ''
|
129
141
|
end
|
130
142
|
|
@@ -134,31 +146,84 @@ public extension <%="#{@api_name}"%> {
|
|
134
146
|
.gsub(/^\/|\/$/, "")
|
135
147
|
end
|
136
148
|
|
137
|
-
def self.createPathFile(dir_path, dir, dirs, index,
|
149
|
+
def self.createPathFile(dir_path, dir, dirs, index,
|
150
|
+
url_path,
|
151
|
+
url,
|
152
|
+
header,
|
153
|
+
httpMethod,
|
154
|
+
parameters,
|
155
|
+
body,
|
156
|
+
responseJson)
|
138
157
|
app_name = "AppAPI.#{dirs[0..index-1].join('.')}"
|
139
158
|
if index == 0
|
140
159
|
app_name = "AppAPI"
|
141
160
|
end
|
142
161
|
if dir == 'restful'
|
143
|
-
swift_model = generate_model("#{
|
162
|
+
swift_model = generate_model("#{httpMethod.capitalize.chomp}Model", responseJson)
|
144
163
|
prefixPath = getPrefixPath(url_path)
|
164
|
+
|
165
|
+
return unless !prefixPath.empty?
|
166
|
+
|
145
167
|
modulePath = getModulePath(url_path, prefixPath)
|
146
168
|
detailPath = getDetailPath(url_path, prefixPath, modulePath)
|
169
|
+
|
170
|
+
readme = """
|
171
|
+
请求参数
|
172
|
+
URL = #{url.chomp}
|
173
|
+
HTTPMETHOD = #{httpMethod.chomp}
|
174
|
+
|
175
|
+
ViewModel
|
176
|
+
class <##SomeViewModel##> {
|
177
|
+
let disposeBag = DisposeBag()
|
178
|
+
|
179
|
+
public struct Input {
|
180
|
+
let <##AnyObservable##>: Observable<<##AnyObservableType##>>
|
181
|
+
}
|
182
|
+
|
183
|
+
public struct Output {
|
184
|
+
let data: BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>
|
185
|
+
}
|
186
|
+
|
187
|
+
public func transform(input: Input) -> Output {
|
188
|
+
let outDatas = BehaviorRelay<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}>(value: [])
|
189
|
+
input.<##AnyObservable##>.flatMapLatest({ [weak self] () -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> in
|
190
|
+
guard let self = self else { return Observable.just([]) }
|
191
|
+
return self.reqData()
|
192
|
+
}).subscribe(onNext: { (items) in
|
193
|
+
outDatas.accept(outDatas.value + items)
|
194
|
+
}, onError: { error in
|
195
|
+
outDatas.accept([])
|
196
|
+
}).disposed(by: disposeBag)
|
197
|
+
|
198
|
+
return Output(data: outDatas)
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
extension <##SomeViewModel##> {
|
203
|
+
func reqData() -> Observable<#{@json_data_is_arr ? "[" : ""}#{app_name}.#{httpMethod.capitalize.chomp}Model#{@json_data_is_arr ? "]" : ""}> {
|
204
|
+
#{app_name}.#{httpMethod.capitalize.chomp}()
|
205
|
+
.compactMap { $0 } // 解析出需要的类型
|
206
|
+
.observeOn(MainScheduler.instance)
|
207
|
+
.catchErrorJustReturn([])
|
208
|
+
}
|
209
|
+
}
|
210
|
+
"""
|
211
|
+
|
147
212
|
ModelReqGenerateConfig.new(
|
148
213
|
"#{dir_path}.swift",
|
149
214
|
app_name,
|
150
|
-
|
215
|
+
httpMethod.capitalize.chomp,
|
151
216
|
prefixPath,
|
152
217
|
modulePath,
|
153
218
|
detailPath,
|
154
|
-
"#{
|
219
|
+
"#{httpMethod.capitalize.chomp}Model",
|
155
220
|
swift_model,
|
156
|
-
@json_data_is_arr
|
221
|
+
@json_data_is_arr,
|
222
|
+
readme
|
157
223
|
)
|
158
224
|
|
159
225
|
`swiftformat --swiftversion 5.0 #{dir_path}.swift`
|
160
226
|
else
|
161
|
-
|
162
227
|
PathGenerateConfig.new(
|
163
228
|
"#{dir_path}.swift",
|
164
229
|
app_name,
|
@@ -168,6 +233,10 @@ public extension <%="#{@api_name}"%> {
|
|
168
233
|
|
169
234
|
end
|
170
235
|
|
236
|
+
def self.capitalize_first_letter(str)
|
237
|
+
str[0].upcase + str[1..-1]
|
238
|
+
end
|
239
|
+
|
171
240
|
def self.generate_model(name, json_data, level = 0)
|
172
241
|
|
173
242
|
if json_data == nil
|
@@ -206,15 +275,16 @@ public extension <%="#{@api_name}"%> {
|
|
206
275
|
when String
|
207
276
|
swift_model += " var #{key}: String?\n"
|
208
277
|
when Array
|
278
|
+
|
209
279
|
if !value.empty?
|
210
|
-
swift_model += " var #{key}: [#{key
|
211
|
-
swift_model += generate_model("#{key
|
280
|
+
swift_model += " var #{key}: [#{capitalize_first_letter(key)}] = []\n"
|
281
|
+
swift_model += generate_model("#{capitalize_first_letter(key)}", value.first, level+1)
|
212
282
|
else
|
213
283
|
swift_model += " var #{key}: [Any] = []\n"
|
214
284
|
end
|
215
285
|
when Hash
|
216
|
-
swift_model += " var #{key}: #{key
|
217
|
-
swift_model += generate_model("#{key
|
286
|
+
swift_model += " var #{key}: #{capitalize_first_letter(key)}?\n"
|
287
|
+
swift_model += generate_model("#{capitalize_first_letter(key)}", value, level+1)
|
218
288
|
else
|
219
289
|
swift_model += " var #{key}: #{value.class == String ? 'String' : 'Int'}\n"
|
220
290
|
end
|
data/lib/gwtools/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gwtools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chenglq
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Write a longer description or delete this line.
|
14
14
|
email:
|