gwtools 0.0.10 → 0.0.12

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: cd2b37aeec182ef52a494b197f0eb4abfe9405e440efa2e2ab3b79b9c810e044
4
- data.tar.gz: 3734ca2671df99c5a879ec5a5a5a336fab43f6e96028900f9ecf68baaffa9081
3
+ metadata.gz: 670dd9b4a0d32ab1e294c84b78c12ee087ccaf95968656746aeb14083ed2d714
4
+ data.tar.gz: 05e0d47a0d97bcf2c5d61b238bc84997082fb1c843b363931da452a24be91afa
5
5
  SHA512:
6
- metadata.gz: 259ec36cc9e7cb1ea91f86ac6c646a69375103ccd005e5c9d86629a91ac442cd642804b0c04807c81d5ac0878a541b7eedbafd70417ee2a68910ae0aa914aeab
7
- data.tar.gz: ed034226ac174115a66b2e16173b33afb0057c1797453c292010759fa190e4804b55d6f174c1743d35b069a9adcb4cdac303dbaba3091e0f3b6964c22e442b23
6
+ metadata.gz: 2c158030a8039e03495ad4629e2ab14702fbcd65b392325c73a961af601addea3300f3527c61edae47158a506d3df49472c2405e42fff42d1ae9c4e9d2c0734f
7
+ data.tar.gz: 6051e050e2b3baf2802f23f1a78b3a3877da7e99def5747299998ddda908d82801de0c108d36ef6b8e1eacc67716634144a31e0abf262ae7d7400f4a439e9afd
@@ -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
- # 先判断路径下是否存在restful.swift
49
- puts "文件已存在: #{@target_path + '/' + dirs.join('/') + '.swift'}" unless !File.exist?(@target_path + '/' + dirs.join('/') + '.swift')
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
- dirs.each_with_index do |dir, index|
53
- # construct directory path
54
- dir_path = @target_path + '/' + dirs[0..index].join('/')
55
-
56
- # create directory if it doesn't exist, with out file_name
57
- if dir != file_name
58
- FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
59
- end
60
- # create file
61
- Generate::Swift.createPathFile(dir_path, dir, dirs, index,
62
- path,
63
- @url,
64
- @header,
65
- @httpMethod,
66
- @parameters,
67
- @body,
68
- @responseJson
69
- )
70
- end
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
@@ -116,7 +116,8 @@ public extension <%="#{@api_name}"%> {
116
116
  end
117
117
 
118
118
  def self.getPrefixPath(dir_path)
119
- prefixPaths = %w[app-api/api/v1.0 app-api/api/v2.0 community-app-api/app-api/api/v1.0]
119
+ prefixPaths = %w[app-api/api/v1.0 app-api/api/v2.0 community-app-api/app-api/api/v1.0 app-api/api/haval/v2.0
120
+ bt-cpsp-api/cpsp-api/v1]
120
121
 
121
122
  prefixPaths.each do |prefix_path|
122
123
  return prefix_path.gsub(/^\/|\/$/, "") if dir_path.start_with?(prefix_path)
@@ -146,6 +147,31 @@ public extension <%="#{@api_name}"%> {
146
147
  .gsub(/^\/|\/$/, "")
147
148
  end
148
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
+ # 从 .swift 文件读取 enum
157
+ Parser.extract_from(target_swift_file)
158
+ json1Hash = JSON.parse(Parser.get_clear_enums.to_json)
159
+
160
+ # 从 url 解析 enum
161
+ url_enum = Parser.enum_from_url(url)
162
+ json2Hash = {url_enum.enum_name.to_sym => url_enum}
163
+
164
+ # puts json1Hash.to_json
165
+ # puts json2Hash.to_json
166
+ puts "-"*50
167
+ puts url
168
+ merged_json = Parser.merge_hashes(JSON.parse(json1Hash.to_json), JSON.parse(json2Hash.to_json))
169
+ enum = Enum.from_hash(merged_json[url_enum.enum_name])
170
+ enum.write_to_file(target_swift_file)
171
+
172
+ `swiftformat --swiftversion 5.0 #{target_swift_file}`
173
+ end
174
+
149
175
  def self.createPathFile(dir_path, dir, dirs, index,
150
176
  url_path,
151
177
  url,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.10"
4
+ VERSION = "0.0.12"
5
5
  end
data/lib/gwtools.rb CHANGED
@@ -9,4 +9,5 @@ module Gwtools
9
9
  autoload :Command, 'gwtools/command'
10
10
  autoload :FileHandler, 'gwtools/analysis/file_handler'
11
11
  autoload :Generate, 'gwtools/generate/swift'
12
+ autoload :Parser, 'gwtools/analysis/swift_parser'
12
13
  end
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.10
4
+ version: 0.0.12
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-31 00:00:00.000000000 Z
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