gwtools 0.0.1 → 0.0.2

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: b3b354978eaffbf394cb6774e2765315ffb9005a890b8601174728e99ee99a15
4
- data.tar.gz: ee66564cd0f5db2347d4480e25ce0246d24e150690ca688c08df1ed8910027ce
3
+ metadata.gz: e30a16fee67b211db364b6405196e97cff2ad8267de76c51b2d2f738e2dc4d17
4
+ data.tar.gz: 707f9f8edcf5215c8d170c9b9f35950e1953e2b2306fff15e4cfe5974ea06449
5
5
  SHA512:
6
- metadata.gz: f53c6023f2cbf418c0ae12acdf6859bda57d2d3a993f5541b63efcb95fc857fc43ad838f4df4e1cd06d28294be2c04333a47cce867084ecddae781d5929fa47d
7
- data.tar.gz: e145bcc141e6ac03552a87c265177d2bd0a2752a758c6c9ecf01e14a49f78a12ebc4a2a94d3f6a38851ab44c3433daa94dd814287d1815645093ac27e8632c07
6
+ metadata.gz: bcc442197200a01b6166aa5f45c1debcc32f9858b5ab8aa7cf7e73dc226fb1bdd0b641bcf66367b990c351696b35130518376fdb9e3e322aba944641b1b55f8a
7
+ data.tar.gz: da4f80305680631b2c3ccacdad38c4a8d42217ddc48fc1b5fa14309357c68df056d4d10351c5b9140887a4d0faaef4017218a8f4227c5551f0ceafa4c30265fb
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
- # Gwtools::Network.new("this is request_info")
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,104 @@
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
+ end
17
+
18
+ def initialize(file_path, api_name, this_path)
19
+ @api_name = api_name
20
+ @this_path = this_path
21
+
22
+ File.open(file_path, 'w+') do |f|
23
+ f.puts(config_file)
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ class ModelReqGenerateConfig
30
+ include(ERB::Util)
31
+ attr_accessor :api_name, :httpmethod, :prefixPath, :modulePath, :detailPath, :model_name, :model_data
32
+
33
+ def config_file
34
+ ERB.new(File.read('lib/erb/model_req.swift.erb'), nil, '>').result(binding)
35
+ end
36
+
37
+ def initialize(file_path, api_name, httpmethod, prefixPath, modulePath, detailPath, model_name, model_data)
38
+ @api_name = api_name
39
+ @httpmethod = httpmethod
40
+ @prefixPath = prefixPath
41
+ @modulePath = modulePath
42
+ @detailPath = detailPath
43
+ @model_name = model_name
44
+ @model_data = model_data
45
+
46
+ File.open(file_path, 'w+') do |f|
47
+ f.puts(config_file)
48
+ end
49
+ end
50
+ end
51
+
52
+ def self.createPathFile(dir_path, dir, dirs, index)
53
+ if index <= 0
54
+ return
55
+ end
56
+ PathGenerateConfig.new(
57
+ "#{dir_path}.swift",
58
+ "AppAPI.#{dirs[0..index-1].join('.')}",
59
+ dir
60
+ )
61
+ end
62
+
63
+ def self.generate_model(name, json_data)
64
+ swift_model = "struct #{name}: HandyJson {\n"
65
+ if json_data == nil
66
+ return ''
67
+ end
68
+ json_data.each do |key, value|
69
+ case value
70
+ when NilClass # nil 对象默认给个Stirng?类型
71
+ swift_model += " var #{key}: String?\n"
72
+ when FalseClass
73
+ swift_model += " var #{key}: Bool = false\n"
74
+ when TrueClass
75
+ swift_model += " var #{key}: Bool = false\n"
76
+ when Float
77
+ swift_model += " var #{key}: Float = 0.0\n"
78
+ when Integer
79
+ swift_model += " var #{key}: Int = 0\n"
80
+ when String
81
+ swift_model += " var #{key}: String?\n"
82
+ when Array
83
+ swift_model += " var #{key}: [#{key.capitalize}] = []\n"
84
+ swift_model += generate_model("#{key.capitalize}", value.first)
85
+ when Hash
86
+ swift_model += " var #{key}: #{key.capitalize}?\n"
87
+ swift_model += generate_model("#{key.capitalize}", value)
88
+ else
89
+ swift_model += " var #{key}: #{value.class == String ? 'String' : 'Int'}\n"
90
+ end
91
+ end
92
+ swift_model + "}\n"
93
+ end
94
+
95
+ def self.createModelAndRequest(target_path, responseJson)
96
+ swift_model = generate_model("Model", responseJson)
97
+ File.write(target_path + 'Model.swift', swift_model)
98
+ end
99
+
100
+ # 生成 Swift 模型
101
+
102
+ end
103
+ end
104
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gwtools
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
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.1
4
+ version: 0.0.2
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-29 00:00:00.000000000 Z
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
@@ -1,10 +0,0 @@
1
- module Gwtools
2
- class Network
3
- attr_reader :request_info
4
-
5
- def initialize(request_info)
6
- request_info = request_info || raise(ArgumentError, 'Missing required argument `request_info`')
7
- puts request_info
8
- end
9
- end
10
- end