Skip to content

Commit

Permalink
Added gem specs.
Browse files Browse the repository at this point in the history
Signed-off-by: Azeem Sajid <<[email protected]>>
  • Loading branch information
iamazeem committed Jul 7, 2020
1 parent 098a191 commit f8cfb26
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,84 @@
# proto-convert

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square)](https://github.com/iamAzeem/proto-convert/blob/master/LICENSE)
[![RubyGems Downloads](https://img.shields.io/gem/dt/proto-convert?color=blue&style=flat-square&label=Downloads)](https://rubygems.org/gems/proto-convert)

A command line tool to convert protobuf messages from binary to JSON and vice versa.

## Installation

Please make sure that the Protocol Buffers Compiler `protoc` is installed.

### RubyGems

```
$ gem install proto-convert
```

### Bundler

Add following line to your Gemfile:
```ruby
gem "proto-convert"
```

And then execute:
```
$ bundle
```

## Usage

```shell
Usage: proto-convert -m [mode] -p [proto] -t [msgtype] -i [input] -o [output]

-m, --mode [MODE] conversion mode ["binary2json", "b2j", "json2binary", "j2b"]
-p, --proto [FILENAME] protobuf schema (.proto)
-t, --msgtype [TYPE] fully-qualified message type
-i, --input [FILENAME] source file (JSON/binary)
-o, --output [FILENAME] destination file (binary/JSON)
-v, --version prints version information
-h, --help prints help
```

### Test Run

Consider this simple .proto file ([`test.proto`](test/test.proto)):

```
syntax = "proto3";
package test;
message Message {
int32 id = 1;
string body = 2;
}
```

These test files (`test.proto`, `test.json`, and `test.bin`) are located in [`test`](test) folder.

**JSON to Binary Conversion**
```
$ proto-convert -m json2binary -p test.proto -t test.Message -i test.json -o test.bin
>> [J] test.json (24 bytes)
<< [B] test.bin (8 bytes)
```

**Binary to JSON Conversion**
```
$ proto-convert -m binary2json -p test.proto -t test.Message -i test.bin -o test.json
>> [B] test.bin (8 bytes)
<< [J] test.json (24 bytes)
```

and,

```
$ cat test.json
{"id":123,"body":"test"}
```

## Contact

Email: [email protected]
Expand Down
28 changes: 25 additions & 3 deletions proto-convert → bin/proto-convert
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

# MIT License
#
# Copyright (c) 2020 Azeem Sajid
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# proto-convert
# A command-line tool to convert protobuf messages from binary to JSON and vice versa
# GitHub: https://github.com/iamAzeem/proto-convert
Expand All @@ -10,7 +32,7 @@
require 'optparse'
require 'English'

VERSION = "#{$PROGRAM_NAME} 1.0.0"
VERSION = "#{$PROGRAM_NAME} 0.1.0"
AUTHOR_NAME = 'Azeem Sajid'
AUTHOR_EMAIL = '[email protected]'
AUTHOR_INFO = "Developed by #{AUTHOR_NAME} <#{AUTHOR_EMAIL}>"
Expand All @@ -20,8 +42,8 @@ def compile_proto(filename)
file_path = File.expand_path(filename)
file_dir = File.dirname(file_path)

`protoc --ruby_out=#{file_dir} --proto_path=#{file_dir} #{file_path}`
raise StandardError, "Invalid schema! [#{filename}] Resolve the error(s)." unless $CHILD_STATUS.success?
`protoc --ruby_out.=#{file_dir} --proto_path=#{file_dir} #{file_path}`
raise StandardError, "Invalid schema! [#{filename}] Resolve error(s)." unless $CHILD_STATUS.success?

compiled_proto = file_dir + '/' + File.basename(file_path, '.proto') + '_pb.rb'
raise StandardError, "Compiled schema not found! [#{compiled_proto}]" unless File.file?(compiled_proto)
Expand Down
22 changes: 22 additions & 0 deletions proto-convert.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

lib = File.expand_path('../lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = 'proto-convert'
spec.version = '0.1.0'
spec.date = '2020-07-07'
spec.authors = ['Azeem Sajid']
spec.email = ['[email protected]']

spec.summary = 'Protobuf Message Converter [Binary <-> JSON]'
spec.description = 'A command line tool to convert protobuf messages from binary to JSON and vice versa'
spec.homepage = 'https://github.com/iamAzeem/proto-convert'
spec.license = 'MIT'

spec.executables = ['proto-convert']

spec.add_development_dependency 'bundler', '~> 1.14'
spec.add_runtime_dependency 'google-protobuf', '~> 3.12', '>= 3.12.2'
end

0 comments on commit f8cfb26

Please sign in to comment.