@accility/protoc-tools 中文文档教程

发布于 3年前 浏览 23 项目主页 更新于 3年前

Protocol Buffer Tools

:warning: 这个包还处于早期阶段,接口可能会来回变化。 当足够稳定时,它将作为 v1.0 发布。

protoc-tools 是 Google 的 .proto 编译器 protoc 可执行文件的包装器包。 该软件包管理从命令行和以编程方式从节点安装和调用 protoc。

What problems do we solve?

  • No need to pre-install the protoc executable file globally on your machine.
  • On a build server, the correct architecture for the executable is automatically used.
  • Simplify calling protoc. You do not need to know the location of the exectuable or the core proto-files. You only need to reference your own protobuffer files.
  • Better conformance to the "npm-way" of doing things.

Installing

npm install -D @accility/protoc-tools

或者全局安装

npm install -g @accility/protoc-tools

Usage

Calling protoc on the command line

(假设您位于可以到达 node_modules 包的位置。)

protoc --version

Calling protoc programmatically

const tools = require('@accility/protoc-tools');
const path = require('path');

// Generate client code for all supported languages
tools.protoc({
    includeDirs: [
        path.resolve('./test/protos')
    ],
    files: ['simple.proto'],
    outDir: path.resolve(__dirname, 'generated'),
    outOptions: [
        tools.generators.cpp({ outPath: path.resolve(__dirname, 'generated/cpp') }), // Override output folder for the C++ files
        tools.generators.csharp({ outOptions: 'file_extension=.g.cs' }), // Use generator specific options
        tools.generators.java(),
        tools.generators.js(),
        tools.generators.kotlin(),
        tools.generators.objc(),
        tools.generators.php(),
        tools.generators.python(),
        tools.generators.ruby()
    ]
});

:information_source:目录 include/google/protobuf 包含所有核心 .proto 文件,并且是 自动添加到包含目录。

有关更多 API 示例,请参阅测试套件

Code Generators

protoc 可以输出 C++、C#、Java、Javascript、Kotlin、Objective-C、PHP、Python 和 默认情况下的 Ruby 序列化代码。

:信息来源: 请参阅生成器选项 有关每个内置生成器的可用选项的信息。

在插件的帮助下,我们可以生成代码 额外的情况。 另见:

@accility/protoc-swagger-plugin

Protocol Buffer Tools

:warning: This package is still in early days and the interfaces might change back and forth. When stable enough it will be released as v1.0.

protoc-tools is a wrapper package for Google's .proto compiler protoc executable. The package manages installing and calling protoc both from the command line and programmatically from node.

What problems do we solve?

  • No need to pre-install the protoc executable file globally on your machine.
  • On a build server, the correct architecture for the executable is automatically used.
  • Simplify calling protoc. You do not need to know the location of the exectuable or the core proto-files. You only need to reference your own protobuffer files.
  • Better conformance to the "npm-way" of doing things.

Installing

npm install -D @accility/protoc-tools

Alternatively Install globally

npm install -g @accility/protoc-tools

Usage

Calling protoc on the command line

(Assuming you are located where the node_modules package can be reached.)

protoc --version

Calling protoc programmatically

const tools = require('@accility/protoc-tools');
const path = require('path');

// Generate client code for all supported languages
tools.protoc({
    includeDirs: [
        path.resolve('./test/protos')
    ],
    files: ['simple.proto'],
    outDir: path.resolve(__dirname, 'generated'),
    outOptions: [
        tools.generators.cpp({ outPath: path.resolve(__dirname, 'generated/cpp') }), // Override output folder for the C++ files
        tools.generators.csharp({ outOptions: 'file_extension=.g.cs' }), // Use generator specific options
        tools.generators.java(),
        tools.generators.js(),
        tools.generators.kotlin(),
        tools.generators.objc(),
        tools.generators.php(),
        tools.generators.python(),
        tools.generators.ruby()
    ]
});

:information_source: The directory include/google/protobuf includes all core .proto-files, and is automatically added to the include directories.

For more API examples, see the test suite.

Code Generators

protoc can output C++, C#, Java, Javascript, Kotlin, Objective-C, PHP, Python and Ruby serialization code by default.

:information_source: See Generator Options for information about available options for each of the built-in generators.

With the help of plugins we can generate code for additional situations. See also:

@accility/protoc-swagger-plugin

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文