proto-convert 0.3.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/proto-convert +79 -82
  3. metadata +12 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5538e9d2c6050048c066b5694afe9bced0bdf8f3a8bf383aee963a1a0cc39034
4
- data.tar.gz: 1e0bf071c4b5b6e493a7bf966cb8a795cfe04d3323d66982195cdb1cb0bd42e7
3
+ metadata.gz: f5838325797314e221a75028304dad6529875a0313bc0c718b8a85fd6a39c000
4
+ data.tar.gz: 59ca7099f09e630d907bd61557c3ede9149d2a183b55a8c819949d02be35e1f1
5
5
  SHA512:
6
- metadata.gz: 462d652162fc52893b8b7799afceba2f595b0b9aba813d4c38abc0c4f37b48f10b13ba9b03c4ce644905ed1da179f5845506728b6f2b019f5dd976f260a32af7
7
- data.tar.gz: c7e225689e68e81e9fceab2c8a861eab48c134a65204a7113856b19e1c95583ce24fcd1f4387099f26496ae2ae7458bf57499097200f0c4b5a960941bd416349
6
+ metadata.gz: f36c7eaac6931d92299889d86ce4fc9858cc2461e5f05a0f4e0a9ebfc3805ea46a2ec3b95368c2a21b3722e4a6ca90cc5c51deef1519a29882bc0a3b46099957
7
+ data.tar.gz: ab63c7fc6ee12cac7e7f6b837c4c672ba41551bc985780052c2a3d42ce3820ea594d4e9913621be55a3a61afb8f4b33697a0940055a690902c99556d39cdd501
data/bin/proto-convert CHANGED
@@ -32,73 +32,62 @@
32
32
  require 'optparse'
33
33
  require 'English'
34
34
 
35
- VERSION = '0.3.0'
35
+ VERSION = '0.4.1'
36
36
  AUTHOR_NAME = 'Azeem Sajid'
37
37
  AUTHOR_EMAIL = '<azeem.sajid@gmail.com>'
38
38
  AUTHOR_INFO = "Author: #{AUTHOR_NAME} #{AUTHOR_EMAIL}"
39
39
  AUTHOR_GITHUB = 'GitHub: https://github.com/iamAzeem/proto-convert'
40
40
 
41
- $debug = false
41
+ $verbose = false
42
42
 
43
43
  def compile_proto(filename)
44
- puts "\n>> Compiling [#{filename}]" if $debug
44
+ puts "\n>> Compile .proto file" if $verbose
45
45
 
46
46
  file_path = File.expand_path(filename)
47
47
  file_dir = File.dirname(file_path)
48
48
 
49
- if $debug
50
- puts " File path: #{file_path}"
49
+ if $verbose
51
50
  protoc_version = `protoc --version`.chomp.split(' ')[1]
52
- puts " Running protoc #{protoc_version}:"
51
+ puts " protoc version : #{protoc_version}"
52
+ puts " proto file path : #{file_path}"
53
53
  end
54
54
 
55
- protoc_cmd =
56
- " protoc \\\n" \
57
- " --ruby_out=#{file_dir} \\\n" \
58
- " --proto_path=#{file_dir} \\\n" \
59
- " #{file_path}"
55
+ protoc_cmd = "protoc --ruby_out=\"#{file_dir}\" --proto_path=\"#{file_dir}\" \"#{file_path}\""
60
56
 
61
- puts "\n#{protoc_cmd}" if $debug
57
+ puts "\n #{protoc_cmd}" if $verbose
62
58
 
63
59
  `#{protoc_cmd}`
64
- raise StandardError, "Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success?
65
-
66
- puts "\n Compiled [#{file_path}] " if $debug
60
+ abort "ERROR: Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success?
67
61
 
68
62
  compiled_proto = "#{file_dir}/#{File.basename(file_path, '.proto')}_pb.rb"
69
- puts " Validating [#{compiled_proto}]" if $debug
70
- raise StandardError, "Compiled schema not found! [#{compiled_proto}]" unless File.file?(compiled_proto)
63
+ abort "ERROR: Compiled schema not found! [#{compiled_proto}]" unless File.file?(compiled_proto)
71
64
 
72
- if $debug
73
- puts " Validatd [#{compiled_proto}]"
74
- puts "<< Compilion and validation complete! [#{file_path} => #{compiled_proto}]"
75
- end
65
+ puts "\n generated file : #{compiled_proto}" if $verbose
76
66
 
77
67
  compiled_proto
78
68
  end
79
69
 
80
70
  def valid_msgtype?(compiled_proto, msg_type)
81
- puts "\n>> Validating msgtype [#{msg_type}] in [#{compiled_proto}]" if $debug
82
-
83
71
  msg_types = []
84
72
  File.foreach(compiled_proto) do |line|
85
- if line.lstrip.start_with?('add_message')
86
- extracted_msg_type = line[/"([^"]*)"/, 1].freeze # regex: <add_message> 'msg_type' <do>
73
+ if line.include?('::Google::Protobuf::DescriptorPool.generated_pool.lookup')
74
+ extracted_msg_type = line[/"([^"]*)"/, 1].freeze
87
75
  msg_types.push(extracted_msg_type) unless extracted_msg_type.nil?
88
76
  end
89
77
  end
90
78
 
91
- is_valid = msg_types.include?(msg_type)
92
-
93
- if $debug
94
- puts " msgtype [#{msg_type}] available? #{is_valid ? 'yes' : 'no'}"
95
- puts ' Available types:'
96
- msg_types.each do |t|
97
- puts " - #{t}"
98
- end
79
+ if $verbose
80
+ puts "\n>> Validate msgtype"
81
+ puts " msgtype : #{msg_type}"
82
+ puts " pb file : #{compiled_proto}"
83
+ puts " pb types: #{msg_types}"
99
84
  end
100
85
 
101
- puts "<< Validation of msgtype [#{msg_type}] complete!" if $debug
86
+ is_valid = msg_types.include?(msg_type)
87
+ unless is_valid
88
+ puts "ERROR: Invalid msgtype! [#{msg_type}]"
89
+ puts " Available types: #{msg_types}"
90
+ end
102
91
 
103
92
  is_valid
104
93
  end
@@ -109,11 +98,20 @@ def msg_class(compiled_proto, msg_type)
109
98
  msg.msgclass
110
99
  end
111
100
 
112
- def convert(compiled_proto, msg_type, conversion_mode, input_file, output_file)
113
- puts "\n>> Converting [#{input_file}], mode: #{conversion_mode}" if $debug
101
+ def convert(compiled_proto, args)
102
+ msg_type = args[:msgtype]
103
+ conversion_mode = args[:mode]
104
+ input_file = args[:input]
105
+ output_file = args[:output]
106
+
107
+ if $verbose
108
+ puts "\n>> Convert"
109
+ puts " file : #{input_file}"
110
+ puts " mode : #{conversion_mode}"
111
+ end
114
112
 
115
113
  pb_msg_class = msg_class(compiled_proto, msg_type)
116
- raise StandardError, "Message type ['#{msg_type}'] not registered!'" if pb_msg_class.nil?
114
+ abort "ERROR: Message type ['#{msg_type}'] not registered!'" if pb_msg_class.nil?
117
115
 
118
116
  begin
119
117
  case conversion_mode
@@ -137,27 +135,30 @@ def convert(compiled_proto, msg_type, conversion_mode, input_file, output_file)
137
135
  puts "<< [B] #{output_file} (#{output_msg.length} bytes)"
138
136
  end
139
137
  rescue Google::Protobuf::ParseError
140
- raise StandardError, "Incompatible input message! [msgtype: #{msg_type}] #{$ERROR_INFO}"
138
+ abort "ERROR: Incompatible input message! [msgtype: #{msg_type}] #{$ERROR_INFO}"
141
139
  rescue StandardError
142
- raise StandardError, "Conversion failed! #{$ERROR_INFO}"
140
+ abort "ERROR: Conversion failed! #{$ERROR_INFO}"
143
141
  end
144
142
 
145
- puts ">> Converion complete! [#{input_file}] => [#{output_file}]" if $debug
143
+ puts ">> Converted! [#{input_file}] => [#{output_file}]" if $verbose
146
144
  end
147
145
 
148
146
  def start
149
147
  mandatory_args = %i[mode proto msgtype input output].freeze
150
148
 
151
- options = {}
152
- mandatory_args.each { |arg| options[arg] = nil }
149
+ args = {}
150
+ mandatory_args.each { |arg| args[arg] = nil }
153
151
 
154
152
  parser = OptionParser.new do |opts|
155
153
  opts.banner = "Usage: #{$PROGRAM_NAME} -m [mode] -p [proto] -t [msgtype] -i [input] -o [output]"
156
- opts.separator ''
154
+ opts.separator "\nOPTIONS:\n\n"
157
155
 
158
156
  modes = %i[binary2json b2j json2binary j2b].freeze
159
- opts.on('-m', '--mode [MODE]', modes, "conversion mode #{modes.map(&:to_s)}") do |mode|
160
- raise OptionParser::InvalidArgument unless modes.include?(mode)
157
+ opts.on('-m', '--mode [MODE]', String, "conversion mode #{modes.map(&:to_s)}") do |mode|
158
+ abort 'ERROR: Missing mode!' if mode.nil?
159
+
160
+ mode = mode.to_sym
161
+ abort "ERROR: Invalid mode! [#{mode}]" unless modes.include?(mode)
161
162
 
162
163
  case mode
163
164
  when :b2j
@@ -166,35 +167,40 @@ def start
166
167
  mode = :json2binary
167
168
  end
168
169
 
169
- options[:mode] = mode
170
+ args[:mode] = mode
170
171
  end
171
172
 
172
- opts.on('-p', '--proto [FILENAME]', 'protobuf schema (.proto)') do |filename|
173
- raise StandardError, "Protobuf schema not found! [#{filename}]" unless File.file?(filename)
173
+ opts.on('-p', '--proto [FILENAME]', String, 'protobuf schema (.proto file)') do |filename|
174
+ abort 'ERROR: Missing schema filename!' if filename.nil?
175
+ abort "ERROR: Protobuf schema not found! [#{filename}]" unless File.file?(filename)
174
176
 
175
- options[:proto] = filename
177
+ args[:proto] = filename
176
178
  end
177
179
 
178
- opts.on('-t', '--msgtype [TYPE]', 'fully-qualified message type') do |msgtype|
179
- options[:msgtype] = msgtype
180
+ opts.on('-t', '--msgtype [TYPE]', String, 'fully-qualified message type') do |msgtype|
181
+ abort 'ERROR: Missing msgtype!' if msgtype.nil?
182
+
183
+ args[:msgtype] = msgtype
180
184
  end
181
185
 
182
- opts.on('-i', '--input [FILENAME]', 'source file (JSON/binary)') do |filename|
183
- raise StandardError, "Source/input file not found! [#{filename}]" unless File.file?(filename)
186
+ opts.on('-i', '--input [FILENAME]', String, 'source file (JSON/binary)') do |filename|
187
+ abort 'ERROR: Missing input filename!' if filename.nil?
188
+ abort "ERROR: Input file not found! [#{filename}]" unless File.file?(filename)
184
189
 
185
- options[:input] = filename
190
+ args[:input] = filename
186
191
  end
187
192
 
188
- opts.on('-o', '--output [FILENAME]', 'destination file (binary/JSON)') do |filename|
189
- options[:output] = filename
193
+ opts.on('-o', '--output [FILENAME]', String, 'destination file (binary/JSON)') do |filename|
194
+ abort 'ERROR: Missing output filename!' if filename.nil?
195
+
196
+ args[:output] = filename
190
197
  end
191
198
 
192
- opts.on('-d', '--debug', 'prints debugging information') do
193
- options[:debug] = true
194
- $debug = true
199
+ opts.on('-v', '--verbose', 'print verbose information') do
200
+ $verbose = true
195
201
  end
196
202
 
197
- opts.on('-h', '--help', 'prints help') do
203
+ opts.on('-h', '--help', 'print help') do
198
204
  puts "#{$PROGRAM_NAME} #{VERSION}\n\n#{opts}\n#{AUTHOR_INFO}\n#{AUTHOR_GITHUB}"
199
205
  exit
200
206
  end
@@ -203,40 +209,31 @@ def start
203
209
  begin
204
210
  parser.parse!
205
211
 
206
- puts ">> #{$PROGRAM_NAME} #{VERSION} [debug mode]" if $debug
212
+ puts ">> #{$PROGRAM_NAME} #{VERSION} [verbose mode]" if $verbose
207
213
 
208
214
  # Validate missing mandatory arguments
209
- missing_args = mandatory_args.select { |arg| options[arg].nil? }
210
- raise OptionParser::MissingArgument, 'No arguments provided!' if missing_args.length == mandatory_args.length
211
- raise OptionParser::MissingArgument, "--#{missing_args.join(', --')}" unless missing_args.empty?
215
+ missing_args = mandatory_args.select { |arg| args[arg].nil? }
216
+ abort "ERROR: Missing required arguments! --#{missing_args.join(', --')}" unless missing_args.empty?
212
217
 
213
- if $debug
214
- puts "\n>> Arguments:"
215
- options.each do |arg, val|
218
+ if $verbose
219
+ puts "\n>> Arguments"
220
+ args.each do |arg, val|
216
221
  puts format(' %<arg>8s : %<val>s', arg: arg, val: val)
217
222
  end
218
223
  end
219
224
 
220
225
  # Compile and validate proto and msgtype
221
- compiled_proto = compile_proto(options[:proto])
222
- msg_type = options[:msgtype]
223
- raise OptionParser::InvalidArgument, "--msgtype #{msg_type}" unless valid_msgtype?(compiled_proto, msg_type)
224
-
225
- # Convert and write to output file
226
- conversion_mode = options[:mode]
227
- input_file = options[:input]
228
- output_file = options[:output]
229
- convert(compiled_proto, msg_type, conversion_mode, input_file, output_file)
226
+ compiled_proto = compile_proto(args[:proto])
227
+ abort unless valid_msgtype?(compiled_proto, args[:msgtype])
228
+
229
+ convert(compiled_proto, args)
230
230
  rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument
231
231
  puts $ERROR_INFO
232
- puts "\n#{$PROGRAM_NAME} #{VERSION}\n\n#{parser}\n#{AUTHOR_INFO}"
233
- exit 1
232
+ abort "\n#{$PROGRAM_NAME} #{VERSION}\n\n#{parser}\n#{AUTHOR_INFO}\n#{AUTHOR_GITHUB}"
234
233
  rescue LoadError
235
- puts "ERROR: Possible 'import' issue! #{$ERROR_INFO}"
236
- exit 1
234
+ abort "ERROR: Possible 'import' issue! #{$ERROR_INFO}"
237
235
  rescue StandardError
238
- puts "ERROR: #{$ERROR_INFO}"
239
- exit 1
236
+ abort "ERROR: #{$ERROR_INFO}"
240
237
  ensure
241
238
  File.delete(compiled_proto) if !compiled_proto.nil? && File.file?(compiled_proto)
242
239
  end
metadata CHANGED
@@ -1,35 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proto-convert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Azeem Sajid
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-14 00:00:00.000000000 Z
11
+ date: 2023-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2.1'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
19
  version: 2.1.0
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.1'
23
23
  type: :development
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '2.1'
30
27
  - - ">="
31
28
  - !ruby/object:Gem::Version
32
29
  version: 2.1.0
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: google-protobuf
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -37,9 +37,6 @@ dependencies:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
39
  version: '3.12'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 3.12.2
43
40
  type: :runtime
44
41
  prerelease: false
45
42
  version_requirements: !ruby/object:Gem::Requirement
@@ -47,11 +44,8 @@ dependencies:
47
44
  - - "~>"
48
45
  - !ruby/object:Gem::Version
49
46
  version: '3.12'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 3.12.2
53
- description: A command-line tool to convert protobuf messages from binary to JSON
54
- and vice versa
47
+ description: a CLI tool to convert protobuf messages from binary to JSON and vice
48
+ versa
55
49
  email:
56
50
  - azeem.sajid@gmail.com
57
51
  executables:
@@ -64,7 +58,7 @@ homepage: https://github.com/iamAzeem/proto-convert
64
58
  licenses:
65
59
  - MIT
66
60
  metadata: {}
67
- post_install_message: Thanks for installing! :)
61
+ post_install_message:
68
62
  rdoc_options: []
69
63
  require_paths:
70
64
  - lib
@@ -79,8 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
79
73
  - !ruby/object:Gem::Version
80
74
  version: '0'
81
75
  requirements: []
82
- rubyforge_project:
83
- rubygems_version: 2.7.6
76
+ rubygems_version: 3.0.9
84
77
  signing_key:
85
78
  specification_version: 4
86
79
  summary: Protobuf Message Converter [Binary <-> JSON]