gwtools 0.0.11 → 0.0.13
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 +25 -21
- data/lib/gwtools/analysis/swift_parser.rb +221 -0
- data/lib/gwtools/generate/swift.rb +28 -0
- data/lib/gwtools/version.rb +1 -1
- data/lib/gwtools.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d9ac7103f49aa7472021c8a7b66d2937718a43c69753eb679427ec1a211bff7
|
4
|
+
data.tar.gz: 739815ba75420526ea48db985d73fc30f769aa30e8d153ed1b811a373ffa51c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: def094dbfbf093ed45bbbff27000e1686b5f54f8a5a95e2ba217e61f5224bc60ba8559a246146388ad151114cc8df524f33c3d9d5941d214096e85daa3e9a1be
|
7
|
+
data.tar.gz: a7209c21c28ebc0b5be46bba41134758a5c26edc33cd2ce400c1e1f6cb53e3f221204e44e8089379d3f56d3b0536bc089a4fe792c1313e463427817016b7252e
|
@@ -45,29 +45,33 @@ module Gwtools
|
|
45
45
|
# split path by '/' and create directories
|
46
46
|
dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") } + [file_name]
|
47
47
|
|
48
|
-
#
|
49
|
-
|
48
|
+
# puts @target_path
|
49
|
+
# puts Parser.enum_from_url(url).to_json
|
50
50
|
|
51
|
+
# # 先判断路径下是否存在restful.swift
|
52
|
+
# puts "文件已存在: #{@target_path + '/' + dirs.join('/') + '.swift'}" unless !File.exist?(@target_path + '/' + dirs.join('/') + '.swift')
|
53
|
+
#
|
51
54
|
FileUtils.mkdir_p(@target_path) unless File.directory?(@target_path)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
+
# )
|
74
|
+
# end
|
71
75
|
end
|
72
76
|
|
73
77
|
# def createSwiftFile(dir_path)
|
@@ -0,0 +1,221 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# 解析指定路径 swift 文件
|
8
|
+
# module SwiftParser
|
9
|
+
# # 解析的所有 enum 类型
|
10
|
+
# ENUMS = {}
|
11
|
+
#
|
12
|
+
# end
|
13
|
+
|
14
|
+
module Gwtools
|
15
|
+
class Parser
|
16
|
+
# 解析的所有 enum 类型
|
17
|
+
@@ENUMS = {}
|
18
|
+
|
19
|
+
# def initialize(enums = {})
|
20
|
+
# @ENUMS = enums
|
21
|
+
# end
|
22
|
+
|
23
|
+
def self.read_file(file_path)
|
24
|
+
swift_file = File.open(file_path, "r")
|
25
|
+
file_content = swift_file.read.force_encoding("UTF-8")
|
26
|
+
swift_file.close
|
27
|
+
|
28
|
+
return file_content
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.enum_from_url(url)
|
32
|
+
uri = URI(url)
|
33
|
+
path = uri.path
|
34
|
+
file_name = 'restful'
|
35
|
+
|
36
|
+
path = path[1..-1] if path.start_with? '/'
|
37
|
+
|
38
|
+
dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") }# + [file_name]
|
39
|
+
parent_enum = nil
|
40
|
+
sub_enum = nil
|
41
|
+
dirs.each_with_index do |dir, index|
|
42
|
+
a_enum = Enum.new('', index+1)
|
43
|
+
a_enum.enum_name = dir
|
44
|
+
a_enum.path = dirs[0..index].join('.')
|
45
|
+
a_enum.sub_enums = {}
|
46
|
+
if !sub_enum.nil?
|
47
|
+
sub_enum.sub_enums.merge!(a_enum.enum_name.to_sym => a_enum)
|
48
|
+
end
|
49
|
+
parent_enum = a_enum unless index != 0
|
50
|
+
sub_enum = a_enum
|
51
|
+
end
|
52
|
+
|
53
|
+
return parent_enum
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.merge_hashes(hash1, hash2)
|
57
|
+
hash1.each do |key, value|
|
58
|
+
if hash2.has_key?(key)
|
59
|
+
if value.is_a?(Hash) && hash2[key].is_a?(Hash)
|
60
|
+
merge_hashes(value, hash2[key])
|
61
|
+
else
|
62
|
+
hash2[key] = value
|
63
|
+
end
|
64
|
+
else
|
65
|
+
hash2[key] = value
|
66
|
+
end
|
67
|
+
end
|
68
|
+
hash2
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.add_to_enums(enum)
|
72
|
+
return if enum.nil?
|
73
|
+
return if enum.path.nil? || enum.path.empty?
|
74
|
+
# return if enum.path.split('.').length > 1
|
75
|
+
|
76
|
+
# 找是否有 name 对应的 Enum 对象
|
77
|
+
if @@ENUMS.key?(enum.path.to_sym)
|
78
|
+
# 合并 sub_enums
|
79
|
+
@@ENUMS[enum.path.to_sym].sub_enums.merge!(enum.sub_enums)
|
80
|
+
else
|
81
|
+
@@ENUMS[enum.path.to_sym] = enum
|
82
|
+
end
|
83
|
+
|
84
|
+
# 强制去除一下顶层name包含.的enum
|
85
|
+
# SwiftParser::ENUMS.delete_if { |key, value| key.to_s.split('.').length > 1 }
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.get_clear_enums
|
89
|
+
return @@ENUMS.delete_if { |key, value| key.to_s.split('.').length > 1 }
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.extract_from(file_path)
|
93
|
+
Parser.extract_enum(Parser.read_file(file_path))
|
94
|
+
return get_clear_enums
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.extract_enum_name(content)
|
98
|
+
# return content.scan(/^\w+\s+extension\s+(\w+)\s+\{/).flatten + content.scan(/^\w+\s+enum\s+(\w+)\s+\{/).flatten
|
99
|
+
return (content.scan(/enum\s+(\w+)\s+\{/).flatten + content.scan(/extension\s+(\w+)\s+\{/).flatten).first
|
100
|
+
end
|
101
|
+
|
102
|
+
# 根据括号匹配enums内容
|
103
|
+
def self.extract_enums_content(content, stack=[], enums=[], start=0)
|
104
|
+
return [] if content.nil?
|
105
|
+
while start < content.length
|
106
|
+
i = content.index("{", start)
|
107
|
+
j = content.index("}", start)
|
108
|
+
|
109
|
+
if i && (!j || i < j)
|
110
|
+
stack.push("{")
|
111
|
+
start = i + 1
|
112
|
+
elsif j
|
113
|
+
stack.pop
|
114
|
+
if stack.empty?
|
115
|
+
enums << content[0...j+1]
|
116
|
+
content = content[j+1..-1]
|
117
|
+
start = 0
|
118
|
+
else
|
119
|
+
start = j + 1
|
120
|
+
end
|
121
|
+
else
|
122
|
+
break
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
enums
|
127
|
+
end
|
128
|
+
|
129
|
+
def self.extract_enum(content, parent_enum = nil)
|
130
|
+
level = parent_enum.nil? ? 0 : parent_enum.level
|
131
|
+
pre_name = parent_enum.nil? ? '' : "#{parent_enum.path}."
|
132
|
+
enum_codes = extract_enums_content(content)
|
133
|
+
# puts enum_codes
|
134
|
+
if enum_codes.length == 1 && !enum_codes.first.empty?
|
135
|
+
code_lines = enum_codes.first.strip.split("\n")
|
136
|
+
this_enum_name = extract_enum_name(code_lines.first)
|
137
|
+
|
138
|
+
a_enum = Enum.new(code_lines[1..-2].join("\n"), level+1)
|
139
|
+
a_enum.enum_name = this_enum_name
|
140
|
+
a_enum.path = "#{pre_name}#{this_enum_name}"
|
141
|
+
a_enum.sub_enums = {}
|
142
|
+
|
143
|
+
if !parent_enum.nil?
|
144
|
+
parent_enum.sub_enums.merge!(this_enum_name.to_sym => a_enum)
|
145
|
+
end
|
146
|
+
|
147
|
+
add_to_enums(a_enum)
|
148
|
+
extract_enum(code_lines[1..-2].join("\n"), @@ENUMS[a_enum.path.to_sym])
|
149
|
+
else
|
150
|
+
if content.empty?
|
151
|
+
|
152
|
+
else
|
153
|
+
enum_codes.each do |enum_code|
|
154
|
+
extract_enum(enum_code, parent_enum)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
class Enum
|
164
|
+
attr_accessor :content, :level, :path
|
165
|
+
attr_accessor :access_level, :enum_name, :sub_enums
|
166
|
+
# :enum_cases, :enum_cases_with_value, :enum_cases_without_value
|
167
|
+
|
168
|
+
def initialize(content = '', level = 0)
|
169
|
+
@content = content.strip
|
170
|
+
@level = level
|
171
|
+
@sub_enums = []
|
172
|
+
# puts @content
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
def self.from_json(json)
|
177
|
+
json = JSON.parse(json)
|
178
|
+
from_hash(json)
|
179
|
+
end
|
180
|
+
|
181
|
+
def self.from_hash(hash)
|
182
|
+
enum = Enum.new
|
183
|
+
enum.level = hash['level']
|
184
|
+
enum.path = hash['path']
|
185
|
+
enum.enum_name = hash['enum_name']
|
186
|
+
|
187
|
+
if hash['sub_enums']
|
188
|
+
enum.sub_enums = {}
|
189
|
+
hash['sub_enums'].each do |k, v|
|
190
|
+
enum.sub_enums[k] = from_hash(v)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
enum
|
195
|
+
end
|
196
|
+
|
197
|
+
def to_swift()
|
198
|
+
content_str = "public enum #{@enum_name} {\n"
|
199
|
+
@sub_enums.each do |key, value|
|
200
|
+
content_str += value.to_swift()
|
201
|
+
end
|
202
|
+
content_str += "}\n"
|
203
|
+
return content_str
|
204
|
+
end
|
205
|
+
|
206
|
+
def write_to_file(file_path)
|
207
|
+
File.open(file_path, "w") do |file|
|
208
|
+
file.write(to_swift)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_json(*a)
|
213
|
+
{
|
214
|
+
:level => self.level,
|
215
|
+
:path => self.path,
|
216
|
+
:enum_name => self.enum_name,
|
217
|
+
:sub_enums => self.sub_enums
|
218
|
+
}.to_json(*a)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
@@ -147,6 +147,34 @@ public extension <%="#{@api_name}"%> {
|
|
147
147
|
.gsub(/^\/|\/$/, "")
|
148
148
|
end
|
149
149
|
|
150
|
+
|
151
|
+
def self.createFile(target_path, url)
|
152
|
+
pre_name = 'pre_path'
|
153
|
+
target_swift_file = "#{target_path + "/#{pre_name}"}/#{url.split('/').first}.swift"
|
154
|
+
FileUtils.mkdir_p(target_path + "/#{pre_name}") unless File.directory?(target_path + "/#{pre_name}")
|
155
|
+
|
156
|
+
File.open(target_swift_file, "w") do |file|
|
157
|
+
file.write("{}")
|
158
|
+
end
|
159
|
+
# 从 .swift 文件读取 enum
|
160
|
+
Parser.extract_from(target_swift_file)
|
161
|
+
json1Hash = JSON.parse(Parser.get_clear_enums.to_json)
|
162
|
+
|
163
|
+
# 从 url 解析 enum
|
164
|
+
url_enum = Parser.enum_from_url(url)
|
165
|
+
json2Hash = {url_enum.enum_name.to_sym => url_enum}
|
166
|
+
|
167
|
+
# puts json1Hash.to_json
|
168
|
+
# puts json2Hash.to_json
|
169
|
+
puts "-"*50
|
170
|
+
puts url
|
171
|
+
merged_json = Parser.merge_hashes(JSON.parse(json1Hash.to_json), JSON.parse(json2Hash.to_json))
|
172
|
+
enum = Enum.from_hash(merged_json[url_enum.enum_name])
|
173
|
+
enum.write_to_file(target_swift_file)
|
174
|
+
|
175
|
+
`swiftformat --swiftversion 5.0 #{target_swift_file}`
|
176
|
+
end
|
177
|
+
|
150
178
|
def self.createPathFile(dir_path, dir, dirs, index,
|
151
179
|
url_path,
|
152
180
|
url,
|
data/lib/gwtools/version.rb
CHANGED
data/lib/gwtools.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.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- chenglq
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Write a longer description or delete this line.
|
14
14
|
email:
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- bin/gwtools
|
25
25
|
- lib/gwtools.rb
|
26
26
|
- lib/gwtools/analysis/file_handler.rb
|
27
|
+
- lib/gwtools/analysis/swift_parser.rb
|
27
28
|
- lib/gwtools/command.rb
|
28
29
|
- lib/gwtools/command/generate.rb
|
29
30
|
- lib/gwtools/command/generate/req_info.rb
|