proto-convert 0.3.0 → 0.4.0

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 +74 -74
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5538e9d2c6050048c066b5694afe9bced0bdf8f3a8bf383aee963a1a0cc39034
4
- data.tar.gz: 1e0bf071c4b5b6e493a7bf966cb8a795cfe04d3323d66982195cdb1cb0bd42e7
3
+ metadata.gz: 50391f8c109fde552acc487acc6eac0683753ab4151d69bbc34acbe88368edd5
4
+ data.tar.gz: cf799c6473fbd87e3ae97f6381fa64742864ac3a1b14291e828ae9b5e3d4602a
5
5
  SHA512:
6
- metadata.gz: 462d652162fc52893b8b7799afceba2f595b0b9aba813d4c38abc0c4f37b48f10b13ba9b03c4ce644905ed1da179f5845506728b6f2b019f5dd976f260a32af7
7
- data.tar.gz: c7e225689e68e81e9fceab2c8a861eab48c134a65204a7113856b19e1c95583ce24fcd1f4387099f26496ae2ae7458bf57499097200f0c4b5a960941bd416349
6
+ metadata.gz: 6d129c25316819ae34f2238267bf92113967f144bfbc9e80c74182a1b851cf92188766cabdadb19805db141e287df4a81355989457be15247d7f6b2b36a3d034
7
+ data.tar.gz: 9836ade35781300dc5b3b42d88b8e88a22e8dc1ec162edff70cde18618b58bd7cc74585f65d9531391d81804976074f8b329e2ca8e2b162bdd6f54d572f778e0
data/bin/proto-convert CHANGED
@@ -32,24 +32,24 @@
32
32
  require 'optparse'
33
33
  require 'English'
34
34
 
35
- VERSION = '0.3.0'
35
+ VERSION = '0.4.0'
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
55
  protoc_cmd =
@@ -58,27 +58,25 @@ def compile_proto(filename)
58
58
  " --proto_path=#{file_dir} \\\n" \
59
59
  " #{file_path}"
60
60
 
61
- puts "\n#{protoc_cmd}" if $debug
61
+ puts "\n#{protoc_cmd}" if $verbose
62
62
 
63
63
  `#{protoc_cmd}`
64
- raise StandardError, "Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success?
65
-
66
- puts "\n Compiled [#{file_path}] " if $debug
64
+ abort "ERROR: Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success?
67
65
 
68
66
  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)
67
+ abort "ERROR: Compiled schema not found! [#{compiled_proto}]" unless File.file?(compiled_proto)
71
68
 
72
- if $debug
73
- puts " Validatd [#{compiled_proto}]"
74
- puts "<< Compilion and validation complete! [#{file_path} => #{compiled_proto}]"
75
- end
69
+ puts "\n generated file : #{compiled_proto}" if $verbose
76
70
 
77
71
  compiled_proto
78
72
  end
79
73
 
80
74
  def valid_msgtype?(compiled_proto, msg_type)
81
- puts "\n>> Validating msgtype [#{msg_type}] in [#{compiled_proto}]" if $debug
75
+ if $verbose
76
+ puts "\n>> Validate msgtype"
77
+ puts " msgtype : #{msg_type}"
78
+ puts " pb file : #{compiled_proto}"
79
+ end
82
80
 
83
81
  msg_types = []
84
82
  File.foreach(compiled_proto) do |line|
@@ -89,17 +87,11 @@ def valid_msgtype?(compiled_proto, msg_type)
89
87
  end
90
88
 
91
89
  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
90
+ unless is_valid
91
+ puts "ERROR: Invalid msgtype! [#{msg_type}]"
92
+ puts " Available types: #{msg_types}"
99
93
  end
100
94
 
101
- puts "<< Validation of msgtype [#{msg_type}] complete!" if $debug
102
-
103
95
  is_valid
104
96
  end
105
97
 
@@ -109,11 +101,20 @@ def msg_class(compiled_proto, msg_type)
109
101
  msg.msgclass
110
102
  end
111
103
 
112
- def convert(compiled_proto, msg_type, conversion_mode, input_file, output_file)
113
- puts "\n>> Converting [#{input_file}], mode: #{conversion_mode}" if $debug
104
+ def convert(compiled_proto, args)
105
+ msg_type = args[:msgtype]
106
+ conversion_mode = args[:mode]
107
+ input_file = args[:input]
108
+ output_file = args[:output]
109
+
110
+ if $verbose
111
+ puts "\n>> Convert"
112
+ puts " file : #{input_file}"
113
+ puts " mode : #{conversion_mode}"
114
+ end
114
115
 
115
116
  pb_msg_class = msg_class(compiled_proto, msg_type)
116
- raise StandardError, "Message type ['#{msg_type}'] not registered!'" if pb_msg_class.nil?
117
+ abort "ERROR: Message type ['#{msg_type}'] not registered!'" if pb_msg_class.nil?
117
118
 
118
119
  begin
119
120
  case conversion_mode
@@ -137,27 +138,30 @@ def convert(compiled_proto, msg_type, conversion_mode, input_file, output_file)
137
138
  puts "<< [B] #{output_file} (#{output_msg.length} bytes)"
138
139
  end
139
140
  rescue Google::Protobuf::ParseError
140
- raise StandardError, "Incompatible input message! [msgtype: #{msg_type}] #{$ERROR_INFO}"
141
+ abort "ERROR: Incompatible input message! [msgtype: #{msg_type}] #{$ERROR_INFO}"
141
142
  rescue StandardError
142
- raise StandardError, "Conversion failed! #{$ERROR_INFO}"
143
+ abort "ERROR: Conversion failed! #{$ERROR_INFO}"
143
144
  end
144
145
 
145
- puts ">> Converion complete! [#{input_file}] => [#{output_file}]" if $debug
146
+ puts ">> Converted! [#{input_file}] => [#{output_file}]" if $verbose
146
147
  end
147
148
 
148
149
  def start
149
150
  mandatory_args = %i[mode proto msgtype input output].freeze
150
151
 
151
- options = {}
152
- mandatory_args.each { |arg| options[arg] = nil }
152
+ args = {}
153
+ mandatory_args.each { |arg| args[arg] = nil }
153
154
 
154
155
  parser = OptionParser.new do |opts|
155
156
  opts.banner = "Usage: #{$PROGRAM_NAME} -m [mode] -p [proto] -t [msgtype] -i [input] -o [output]"
156
- opts.separator ''
157
+ opts.separator "\nOPTIONS:\n\n"
157
158
 
158
159
  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)
160
+ opts.on('-m', '--mode [MODE]', String, "conversion mode #{modes.map(&:to_s)}") do |mode|
161
+ abort 'ERROR: Missing mode!' if mode.nil?
162
+
163
+ mode = mode.to_sym
164
+ abort "ERROR: Invalid mode! [#{mode}]" unless modes.include?(mode)
161
165
 
162
166
  case mode
163
167
  when :b2j
@@ -166,35 +170,40 @@ def start
166
170
  mode = :json2binary
167
171
  end
168
172
 
169
- options[:mode] = mode
173
+ args[:mode] = mode
170
174
  end
171
175
 
172
- opts.on('-p', '--proto [FILENAME]', 'protobuf schema (.proto)') do |filename|
173
- raise StandardError, "Protobuf schema not found! [#{filename}]" unless File.file?(filename)
176
+ opts.on('-p', '--proto [FILENAME]', String, 'protobuf schema (.proto)') do |filename|
177
+ abort 'ERROR: Missing schema filename!' if filename.nil?
178
+ abort "ERROR: Protobuf schema not found! [#{filename}]" unless File.file?(filename)
174
179
 
175
- options[:proto] = filename
180
+ args[:proto] = filename
176
181
  end
177
182
 
178
- opts.on('-t', '--msgtype [TYPE]', 'fully-qualified message type') do |msgtype|
179
- options[:msgtype] = msgtype
183
+ opts.on('-t', '--msgtype [TYPE]', String, 'fully-qualified message type') do |msgtype|
184
+ abort 'ERROR: Missing msgtype!' if msgtype.nil?
185
+
186
+ args[:msgtype] = msgtype
180
187
  end
181
188
 
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)
189
+ opts.on('-i', '--input [FILENAME]', String, 'source file (JSON/binary)') do |filename|
190
+ abort 'ERROR: Missing input filename!' if filename.nil?
191
+ abort "ERROR: Input file not found! [#{filename}]" unless File.file?(filename)
184
192
 
185
- options[:input] = filename
193
+ args[:input] = filename
186
194
  end
187
195
 
188
- opts.on('-o', '--output [FILENAME]', 'destination file (binary/JSON)') do |filename|
189
- options[:output] = filename
196
+ opts.on('-o', '--output [FILENAME]', String, 'destination file (binary/JSON)') do |filename|
197
+ abort 'ERROR: Missing output filename!' if filename.nil?
198
+
199
+ args[:output] = filename
190
200
  end
191
201
 
192
- opts.on('-d', '--debug', 'prints debugging information') do
193
- options[:debug] = true
194
- $debug = true
202
+ opts.on('-v', '--verbose', 'print verbose information') do
203
+ $verbose = true
195
204
  end
196
205
 
197
- opts.on('-h', '--help', 'prints help') do
206
+ opts.on('-h', '--help', 'print help') do
198
207
  puts "#{$PROGRAM_NAME} #{VERSION}\n\n#{opts}\n#{AUTHOR_INFO}\n#{AUTHOR_GITHUB}"
199
208
  exit
200
209
  end
@@ -203,40 +212,31 @@ def start
203
212
  begin
204
213
  parser.parse!
205
214
 
206
- puts ">> #{$PROGRAM_NAME} #{VERSION} [debug mode]" if $debug
215
+ puts ">> #{$PROGRAM_NAME} #{VERSION} [verbose mode]" if $verbose
207
216
 
208
217
  # 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?
218
+ missing_args = mandatory_args.select { |arg| args[arg].nil? }
219
+ abort "ERROR: Missing required arguments! --#{missing_args.join(', --')}" unless missing_args.empty?
212
220
 
213
- if $debug
214
- puts "\n>> Arguments:"
215
- options.each do |arg, val|
221
+ if $verbose
222
+ puts "\n>> Arguments"
223
+ args.each do |arg, val|
216
224
  puts format(' %<arg>8s : %<val>s', arg: arg, val: val)
217
225
  end
218
226
  end
219
227
 
220
228
  # 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)
229
+ compiled_proto = compile_proto(args[:proto])
230
+ abort unless valid_msgtype?(compiled_proto, args[:msgtype])
231
+
232
+ convert(compiled_proto, args)
230
233
  rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument
231
234
  puts $ERROR_INFO
232
- puts "\n#{$PROGRAM_NAME} #{VERSION}\n\n#{parser}\n#{AUTHOR_INFO}"
233
- exit 1
235
+ abort "\n#{$PROGRAM_NAME} #{VERSION}\n\n#{parser}\n#{AUTHOR_INFO}\n#{AUTHOR_GITHUB}"
234
236
  rescue LoadError
235
- puts "ERROR: Possible 'import' issue! #{$ERROR_INFO}"
236
- exit 1
237
+ abort "ERROR: Possible 'import' issue! #{$ERROR_INFO}"
237
238
  rescue StandardError
238
- puts "ERROR: #{$ERROR_INFO}"
239
- exit 1
239
+ abort "ERROR: #{$ERROR_INFO}"
240
240
  ensure
241
241
  File.delete(compiled_proto) if !compiled_proto.nil? && File.file?(compiled_proto)
242
242
  end
metadata CHANGED
@@ -1,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Azeem Sajid