gwtools 0.0.1 → 0.0.5
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/bin/gwtools +5 -4
- data/lib/gwtools/analysis/file_handler.rb +132 -0
- data/lib/gwtools/command/generate/req_info.rb +30 -0
- data/lib/gwtools/command/generate.rb +26 -0
- data/lib/gwtools/command/network/model.rb +35 -0
- data/lib/gwtools/command/network/url.rb +36 -0
- data/lib/gwtools/command/network.rb +21 -0
- data/lib/gwtools/command/pod.rb +23 -0
- data/lib/gwtools/command.rb +22 -0
- data/lib/gwtools/generate/swift.rb +113 -0
- data/lib/gwtools/version.rb +1 -1
- data/lib/gwtools.rb +4 -2
- metadata +11 -3
- data/lib/network_tools.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43e5aaf57e5a1887d0fd602c416c4727a8163296e5cc0cb23c2a3cd6226bcee7
|
4
|
+
data.tar.gz: 72c5d96b01cf73ace17487f392213b6c7e9c186b5c9ecfb82d7884cd11c7d4bf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5ed11c0b2efadafa7c0564431fc8191a70451b1601ef4603a13078e10b570326c0802a44044f15e80596c12c75765c29c94712c58974fd492756275843307890
|
7
|
+
data.tar.gz: f5eda98e671f387149f20d879efa1aaf5698b7c0544b1224c07f52d50200388daff4fb1171755b7947ce700826929a973d5057163e9efdb1d238919770f5c156
|
data/bin/gwtools
CHANGED
@@ -2,9 +2,10 @@
|
|
2
2
|
|
3
3
|
require 'gwtools'
|
4
4
|
require 'pp'
|
5
|
+
require 'cocoapods'
|
6
|
+
require 'claide'
|
5
7
|
|
6
|
-
puts 'gwtools 是一个内部工具,用于做基本的代码生成,项目管理工作 !!!'
|
7
|
-
|
8
|
-
pp ARGV
|
8
|
+
# puts 'gwtools 是一个内部工具,用于做基本的代码生成,项目管理工作 !!!'
|
9
|
+
require 'gwtools'
|
9
10
|
|
10
|
-
|
11
|
+
Gwtools::Command.run(ARGV)
|
@@ -0,0 +1,132 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
module Gwtools
|
8
|
+
class FileHandler
|
9
|
+
def initialize(file_path, target_path = '')
|
10
|
+
@file_path = file_path || raise(ArgumentError, 'Missing required argument `file_path`')
|
11
|
+
@target_path = target_path
|
12
|
+
|
13
|
+
analyDataStr
|
14
|
+
end
|
15
|
+
|
16
|
+
# 解析内容
|
17
|
+
def analyDataStr
|
18
|
+
@data_str = File.read(@file_path)
|
19
|
+
getURL
|
20
|
+
getHeaders
|
21
|
+
getHttpMethod
|
22
|
+
getParameters
|
23
|
+
getBody
|
24
|
+
getResponse
|
25
|
+
|
26
|
+
# puts """
|
27
|
+
# URL: #{@url}
|
28
|
+
# HEADERS: #{@header}
|
29
|
+
# HTTPMETHOD: #{@httpMethod}
|
30
|
+
# parameters: #{@parameters}
|
31
|
+
# BODY: #{@body}
|
32
|
+
# """
|
33
|
+
|
34
|
+
makeDirAndFile(@url)
|
35
|
+
# 生成对应Model文件
|
36
|
+
Generate::Swift.createModelAndRequest(@target_path, @responseJson)
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
def makeDirAndFile(url)
|
41
|
+
uri = URI(url)
|
42
|
+
path = uri.path
|
43
|
+
|
44
|
+
# remove leading '/'
|
45
|
+
path = path[1..-1] if path.start_with? '/'
|
46
|
+
|
47
|
+
# split path by '/' and create directories
|
48
|
+
dirs = path.split('/').map { |part| part.gsub(/[.-]/, "_") } + ['restful']
|
49
|
+
FileUtils.mkdir_p(@target_path) unless File.directory?(@target_path)
|
50
|
+
dirs.each_with_index do |dir, index|
|
51
|
+
# construct directory path
|
52
|
+
dir_path = @target_path + '/' + dirs[0..index].join('/')
|
53
|
+
|
54
|
+
# create directory if it doesn't exist, with out restful
|
55
|
+
if dir != 'restful'
|
56
|
+
FileUtils.mkdir_p(dir_path) unless File.directory?(dir_path)
|
57
|
+
end
|
58
|
+
|
59
|
+
# create file
|
60
|
+
Generate::Swift.createPathFile(dir_path, dir, dirs, index)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# def createSwiftFile(dir_path)
|
65
|
+
# File.open("#{dir_path}.swift", "w") do |file|
|
66
|
+
# # write swift file content
|
67
|
+
# if index == 0
|
68
|
+
# file.write("extension AppAPI { \n")
|
69
|
+
# else
|
70
|
+
# file.write("extension AppAPI.#{dirs[0..index-1].join('.')} { \n")
|
71
|
+
# end
|
72
|
+
# file.write(" public struct #{dir} { } \n")
|
73
|
+
# file.write("}")
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
#
|
77
|
+
# # File.open("#{dir_path}.swift", "w") do |file|
|
78
|
+
# #
|
79
|
+
# # end
|
80
|
+
# end
|
81
|
+
|
82
|
+
def getURL
|
83
|
+
match = /^URL = (.*)$/.match(@data_str)
|
84
|
+
if match
|
85
|
+
@url = match[1]
|
86
|
+
else
|
87
|
+
@url = nil
|
88
|
+
puts "URL No match found"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def getHeaders
|
93
|
+
match = /^HEADERS = (.*)$/.match(@data_str)
|
94
|
+
if match
|
95
|
+
@header = match[1]
|
96
|
+
else
|
97
|
+
@header = nil
|
98
|
+
puts "HEADERS No match found"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def getHttpMethod
|
103
|
+
match = /^HTTPMETHOD = (.*)$/.match(@data_str)
|
104
|
+
if match
|
105
|
+
@httpMethod = match[1]
|
106
|
+
else
|
107
|
+
@httpMethod = nil
|
108
|
+
puts "HTTPMETHOD No match found"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def getParameters
|
113
|
+
@parameters = @data_str.split("\nparameters = ")[1].split("\nBODY = ")[0]
|
114
|
+
if !@parameters.strip.empty?
|
115
|
+
@parametersJson = JSON.parse(@parameters) || raise(ArgumentError, 'json parse error `parameters`')
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def getBody
|
120
|
+
@body = @data_str.split("\nBODY = ")[1].split("\nstatusCode = ")[0]
|
121
|
+
if !@body.strip.empty?
|
122
|
+
@bodyJson = JSON.parse(@body) || raise(ArgumentError, 'json parse error `BODY`')
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def getResponse
|
127
|
+
@response = @data_str.split("\nRESPONSE =")[1]
|
128
|
+
@responseJson = JSON.parse(@response) || raise(ArgumentError, 'json parse error `RESPONSE`')
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gwtools
|
4
|
+
class Command
|
5
|
+
class Generate < Command
|
6
|
+
class ReqInfo < Generate
|
7
|
+
self.summary = '代码转换工具'
|
8
|
+
self.command = 'req'
|
9
|
+
|
10
|
+
self.description = <<-DESC
|
11
|
+
请求内容解析工具.
|
12
|
+
DESC
|
13
|
+
|
14
|
+
def initialize(argv)
|
15
|
+
@file_path = argv.shift_argument
|
16
|
+
@target_path = argv.shift_argument
|
17
|
+
end
|
18
|
+
|
19
|
+
def validate!
|
20
|
+
# super TODO: 这里还没检查参数
|
21
|
+
end
|
22
|
+
|
23
|
+
def run
|
24
|
+
FileHandler.new(@file_path, @target_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'gwtools/command/generate/req_info'
|
4
|
+
|
5
|
+
module Gwtools
|
6
|
+
class Command
|
7
|
+
class Generate < Command
|
8
|
+
self.abstract_command = true
|
9
|
+
self.summary = '代码转换工具'
|
10
|
+
self.command = 'g'
|
11
|
+
|
12
|
+
self.description = <<-DESC
|
13
|
+
代码转换工具.
|
14
|
+
DESC
|
15
|
+
|
16
|
+
def self.options
|
17
|
+
options = []
|
18
|
+
options.concat(super.reject { |option, _| option == '--silent' })
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(argv)
|
22
|
+
super
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gwtools
|
4
|
+
class Command
|
5
|
+
class Network < Command
|
6
|
+
class Model < Network
|
7
|
+
self.summary = 'model xxxxxxx'
|
8
|
+
|
9
|
+
self.description = <<-DESC
|
10
|
+
modelmodelmodelmodel xxxxxxxurl xxxxxxxurl xxxxxxxurl xxxxxxxurl xxxxxxx.
|
11
|
+
DESC
|
12
|
+
|
13
|
+
self.arguments = [
|
14
|
+
CLAide::Argument.new('NAME', false),
|
15
|
+
CLAide::Argument.new('NAME', false),
|
16
|
+
]
|
17
|
+
|
18
|
+
def self.options
|
19
|
+
[[
|
20
|
+
'--short', 'Only print the path relative to the cache root'
|
21
|
+
]].concat(super)
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(argv)
|
25
|
+
super
|
26
|
+
end
|
27
|
+
|
28
|
+
def run
|
29
|
+
puts 'Gwtools.Command.Network.model run ...'
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gwtools
|
4
|
+
class Command
|
5
|
+
class Network < Command
|
6
|
+
class URL < Network
|
7
|
+
self.summary = 'url xxxxxxx'
|
8
|
+
|
9
|
+
self.description = <<-DESC
|
10
|
+
url xxxxxxxurl xxxxxxxurl xxxxxxxurl xxxxxxxurl xxxxxxx.
|
11
|
+
DESC
|
12
|
+
|
13
|
+
self.arguments = [
|
14
|
+
CLAide::Argument.new('URL', false),
|
15
|
+
]
|
16
|
+
|
17
|
+
def self.options
|
18
|
+
[[
|
19
|
+
'--short', 'Only print the path relative to the cache root'
|
20
|
+
]].concat(super)
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(argv)
|
24
|
+
@pod_name = argv.shift_argument
|
25
|
+
@short_output = argv.flag?('short')
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def run
|
30
|
+
puts 'Gwtools.Command.Network.URL run ... ' + "#{@pod_name}" + " | #{@short_output}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'gwtools/command/network/url'
|
4
|
+
require 'gwtools/command/network/model'
|
5
|
+
|
6
|
+
module Gwtools
|
7
|
+
class Command
|
8
|
+
class Network < Command
|
9
|
+
self.abstract_command = true
|
10
|
+
self.summary = 'Command.Network'
|
11
|
+
|
12
|
+
self.description = <<-DESC
|
13
|
+
Command.NetworkCommand.NetworkCommand.NetworkCommand.NetworkCommand.NetworkCommand.Network.
|
14
|
+
DESC
|
15
|
+
|
16
|
+
def initialize(argv)
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Gwtools
|
4
|
+
class Command
|
5
|
+
class Pod < Command
|
6
|
+
self.abstract_command = true
|
7
|
+
self.summary = 'Gwtools pod utils'
|
8
|
+
|
9
|
+
self.description = <<-DESC
|
10
|
+
Gwtools pod utils.
|
11
|
+
DESC
|
12
|
+
|
13
|
+
def self.options
|
14
|
+
options = []
|
15
|
+
options.concat(super.reject { |option, _| option == '--silent' })
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(argv)
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'claide'
|
3
|
+
|
4
|
+
module Gwtools
|
5
|
+
class Command < CLAide::Command
|
6
|
+
|
7
|
+
# require 'gwtools/command/network'
|
8
|
+
# require 'gwtools/command/pod'
|
9
|
+
require 'gwtools/command/generate'
|
10
|
+
|
11
|
+
self.abstract_command = true
|
12
|
+
self.command = 'gwtools'
|
13
|
+
self.version = Gwtools::VERSION
|
14
|
+
self.description = 'gwtools 是一个内部工具,用于做基本的代码生成,项目管理工作 !!!'
|
15
|
+
self.plugin_prefixes = %w(claide)
|
16
|
+
|
17
|
+
def initialize(argv)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Gwtools
|
7
|
+
class Generate
|
8
|
+
class Swift
|
9
|
+
|
10
|
+
class PathGenerateConfig
|
11
|
+
include(ERB::Util)
|
12
|
+
attr_accessor :api_name, :this_path
|
13
|
+
|
14
|
+
def config_file
|
15
|
+
# ERB.new(File.read('../lib/erb/path_url.swift.erb'), nil, '>').result(binding)
|
16
|
+
template = <<-ERB
|
17
|
+
// 此文件由代码生成,不要任何修
|
18
|
+
|
19
|
+
extension <%="#{@api_name}"%> {
|
20
|
+
// <%="#{@this_path}\n"%>
|
21
|
+
public struct <%="#{@this_path}"%> { }
|
22
|
+
}
|
23
|
+
ERB
|
24
|
+
ERB.new(template, nil, '>').result(binding)
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(file_path, api_name, this_path)
|
28
|
+
@api_name = api_name
|
29
|
+
@this_path = this_path
|
30
|
+
|
31
|
+
File.open(file_path, 'w+') do |f|
|
32
|
+
f.puts(config_file)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class ModelReqGenerateConfig
|
39
|
+
include(ERB::Util)
|
40
|
+
attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data
|
41
|
+
|
42
|
+
def config_file
|
43
|
+
ERB.new(File.read('../lib/erb/model_req.swift.erb'), nil, '>').result(binding)
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data)
|
47
|
+
@api_name = api_name
|
48
|
+
@httpmethod = httpmethod
|
49
|
+
@prefixPath = prefixPath
|
50
|
+
@modulePath = modulePath
|
51
|
+
@detailPath = detailPath
|
52
|
+
@model_name = model_name
|
53
|
+
@model_data = model_data
|
54
|
+
|
55
|
+
File.open(file_path, 'w+') do |f|
|
56
|
+
f.puts(config_file)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.createPathFile(dir_path, dir, dirs, index)
|
62
|
+
if index <= 0
|
63
|
+
return
|
64
|
+
end
|
65
|
+
PathGenerateConfig.new(
|
66
|
+
"#{dir_path}.swift",
|
67
|
+
"AppAPI.#{dirs[0..index-1].join('.')}",
|
68
|
+
dir
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.generate_model(name, json_data)
|
73
|
+
swift_model = "struct #{name}: HandyJson {\n"
|
74
|
+
if json_data == nil
|
75
|
+
return ''
|
76
|
+
end
|
77
|
+
json_data.each do |key, value|
|
78
|
+
case value
|
79
|
+
when NilClass # nil 对象默认给个Stirng?类型
|
80
|
+
swift_model += " var #{key}: String?\n"
|
81
|
+
when FalseClass
|
82
|
+
swift_model += " var #{key}: Bool = false\n"
|
83
|
+
when TrueClass
|
84
|
+
swift_model += " var #{key}: Bool = false\n"
|
85
|
+
when Float
|
86
|
+
swift_model += " var #{key}: Float = 0.0\n"
|
87
|
+
when Integer
|
88
|
+
swift_model += " var #{key}: Int = 0\n"
|
89
|
+
when String
|
90
|
+
swift_model += " var #{key}: String?\n"
|
91
|
+
when Array
|
92
|
+
swift_model += " var #{key}: [#{key.capitalize}] = []\n"
|
93
|
+
swift_model += generate_model("#{key.capitalize}", value.first)
|
94
|
+
when Hash
|
95
|
+
swift_model += " var #{key}: #{key.capitalize}?\n"
|
96
|
+
swift_model += generate_model("#{key.capitalize}", value)
|
97
|
+
else
|
98
|
+
swift_model += " var #{key}: #{value.class == String ? 'String' : 'Int'}\n"
|
99
|
+
end
|
100
|
+
end
|
101
|
+
swift_model + "}\n"
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.createModelAndRequest(target_path, responseJson)
|
105
|
+
swift_model = generate_model("Model", responseJson)
|
106
|
+
File.write(target_path + 'Model.swift', swift_model)
|
107
|
+
end
|
108
|
+
|
109
|
+
# 生成 Swift 模型
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/lib/gwtools/version.rb
CHANGED
data/lib/gwtools.rb
CHANGED
@@ -5,6 +5,8 @@ require_relative "gwtools/version"
|
|
5
5
|
module Gwtools
|
6
6
|
class Error < StandardError; end
|
7
7
|
# Your code goes here...
|
8
|
-
require_relative 'network_tools'
|
9
|
-
|
8
|
+
# require_relative 'network_tools'
|
9
|
+
autoload :Command, 'gwtools/command'
|
10
|
+
autoload :FileHandler, 'gwtools/analysis/file_handler'
|
11
|
+
autoload :Generate, 'gwtools/generate/swift'
|
10
12
|
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.
|
4
|
+
version: 0.0.5
|
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-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Write a longer description or delete this line.
|
14
14
|
email:
|
@@ -23,8 +23,16 @@ files:
|
|
23
23
|
- README.md
|
24
24
|
- bin/gwtools
|
25
25
|
- lib/gwtools.rb
|
26
|
+
- lib/gwtools/analysis/file_handler.rb
|
27
|
+
- lib/gwtools/command.rb
|
28
|
+
- lib/gwtools/command/generate.rb
|
29
|
+
- lib/gwtools/command/generate/req_info.rb
|
30
|
+
- lib/gwtools/command/network.rb
|
31
|
+
- lib/gwtools/command/network/model.rb
|
32
|
+
- lib/gwtools/command/network/url.rb
|
33
|
+
- lib/gwtools/command/pod.rb
|
34
|
+
- lib/gwtools/generate/swift.rb
|
26
35
|
- lib/gwtools/version.rb
|
27
|
-
- lib/network_tools.rb
|
28
36
|
homepage: https://gitlab.btpoc.com/chenglq/gwtools.git.
|
29
37
|
licenses:
|
30
38
|
- MIT
|
data/lib/network_tools.rb
DELETED