Protocol Buffer is copyright with Google Inc. https://developers.google.com/protocol-buffers/
Protocol Buffer to TypeScript Plugin is the extension of the Protocol Compiler to compile the proto files into corresponding TypeScript code.
Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. You can find protobuf's documentation on the Google Developers site.
This repository contains the TypeScript extension that implements Protocol Buffers functionality in TypeScript.
Consider file test.proto,
syntax = "proto3";
package test;
message Msg{
string name = 1;
}
Compiling the file into TypeScript code,
import { Observable } from 'rxjs';
export interface Msg {
name: string;
}
-
Make sure you have Python 2.x. If in doubt, run:
$ python -V
-
If you don't have the protocol compiler,
protoc
installed, install a binary distribution ofprotoc
, and the simplest way to install it is to download a pre-built binary.$ protoc --version
-
Download or clone the Python Protocol Buffers runtime library
-
Build and run the tests:
$ python setup.py build $ python setup.py test
-
And Install:
$ python setup.py install
-
Now, clone our plugin and you are all done.
For Windows
-
Set the interpreter to the plugin by adding your Python 2.x path 'C:\Python27\python.exe' in the begining of the
tsPlugin.py
file#!C:\Python27\python.exe
-
Now, edit the
runPlugin.bat
file to make the python file executable. -
Now, generate your TypeScript code using,
$ protoc -I <Project_Path> -I <Proto_File_Directory_Path> <Proto_Files_Path> --plugin=protoc-gen-custom=<Executable_File_Path> --custom_out=<Output_Directory_Path>
Like:
$ pwd ..\protobuf-to-typescript $ protoc -I . -I examples\test examples\test\test.proto --plugin=protoc-gen-custom=runPlugin.bat --custom_out=examples\output`
Fow Linux
-
Set the interpreter to the plugin by adding your Python 2.x path '/usr/bin/env python' in the begining of the
tsPlugin.py
file#!/usr/bin/env python
-
Now, generate your TypeScript code using,
$ protoc -I <Project_Path> -I <Proto_File_Directory_Path> <Proto_Files_Path> --plugin=protoc-gen-custom=<Plugin_Path> --custom_out=<Output_Directory_Path>
Like:
$ protoc -I . -I examples\test examples/test/test.proto --plugin=protoc-gen-custom=tsPlugin.py --custom_out=examples/output
or
Use the
generator.sh
script to compile all the proto files in a directory
Changes and improvements are more than welcome! Feel free to fork and open a pull request. And Please make your changes in a specific branch and request to pull into master! If you can, please make sure the game fully works before sending the PR, as that will help speed up the process.
Protocol Buffer to TypeScript Plugin is licensed under the MIT license.