A Dart build script that downloads the Protobuf compiler and Dart plugin to streamline .proto to .dart compilation.

Overview

A Dart build package to compile Protocol Buffer files to Dart source code using build_runner (i.e. the Dart build pipline) without needing to manually install the protoc compiler or the Dart Protobuf plugin protoc_plugin.

The protoc_builder package downloads the necessary Protobuf dependencies for your platform to a temporary local directory, thereby streamlining the development process.

Installation

Add the necessary dependencies to your pubspec.yaml file:

dev_dependencies:
  build_runner: 
   
  protoc_builder: 
   

Configuration

You must add your .proto files to a build.yaml file next to the pubspec.yaml:

targets:
  $default:
    sources:
      - $package$
      - lib/$lib$
      - proto/** # Your .proto directory

This will use the default configuration for the protoc_builder.

You may also configure custom options:

targets:
  $default:
    sources:
      - $package$
      - lib/$lib$
      - proto/**
    builders:
      protoc_builder:
        options:
          # The version of the Protobuf compiler to use.
          protobuf_version: "3.19.1" # Make sure to use quotation marks.
          # The version of the Dart protoc_plugin package to use.
          protoc_plugin_version: "20.0.0" # Make sure to use quotation marks.
          # Include paths given to the Protobuf compiler during compilation.
          proto_paths:
            - "proto/"
          # The root directory for generated Dart output files.
          out_dir: "lib/src/generated"

Running

Once everything is set up, you may simply run the build_runner package:

pub run build_runner build

The build_runner sometimes caches results longer than it should, so in some cases, it may be necessary to delete the .dart_tool/build directory.

Comments
  • Broken on Windows with Dart 2.17.3?

    Broken on Windows with Dart 2.17.3?

    I checked out your repo and tried the example and it doesn't seem to work with the latest Dart?

    Overall, love the idea behind this package. :)

    PS C:\Users\micro\Documents\GitHub\dart-protoc-builder\example> dart run build_runner build
    [INFO] Generating build script completed, took 532ms
    [INFO] Precompiling build script... completed, took 8.2s
    [INFO] Building new asset graph completed, took 1.2s
    [INFO] Checking for unexpected pre-existing outputs. completed, took 3ms[INFO] Running build completed, took 8ms
    [INFO] Caching finalized dependency graph completed, took 44ms
    [INFO] Succeeded after 72ms with 0 outputs (0 actions)
    PS C:\Users\micro\Documents\GitHub\dart-protoc-builder\example> dart --version
    Dart SDK version: 2.17.3 (stable) (Wed Jun 1 11:06:41 2022 +0200) on "windows_x64"
    PS C:\Users\micro\Documents\GitHub\dart-protoc-builder\example> flutter 
    --version
    Flutter 3.0.2 • channel stable • https://github.com/flutter/flutter.git
    Framework • revision cd41fdd495 (11 days ago) • 2022-06-08 09:52:13     
    -0700
    Engine • revision f15f824b57
    Tools • Dart 2.17.3 • DevTools 2.12.2
    
    # Generated by pub
    # See https://dart.dev/tools/pub/glossary#lockfile
    packages:
      _fe_analyzer_shared:
        dependency: transitive
        description:
          name: _fe_analyzer_shared
          url: "https://pub.dartlang.org"
        source: hosted
        version: "40.0.0"
      analyzer:
        dependency: transitive
        description:
          name: analyzer
          url: "https://pub.dartlang.org"
        source: hosted
        version: "4.1.0"
      archive:
        dependency: transitive
        description:
          name: archive
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.3.0"
      args:
        dependency: transitive
        description:
          name: args
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.3.1"
      async:
        dependency: transitive
        description:
          name: async
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.9.0"
      build:
        dependency: transitive
        description:
          name: build
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.3.0"
      build_config:
        dependency: transitive
        description:
          name: build_config
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.0"
      build_daemon:
        dependency: transitive
        description:
          name: build_daemon
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.1.0"
      build_resolvers:
        dependency: transitive
        description:
          name: build_resolvers
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.0.9"
      build_runner:
        dependency: "direct dev"
        description:
          name: build_runner
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.11"
      build_runner_core:
        dependency: transitive
        description:
          name: build_runner_core
          url: "https://pub.dartlang.org"
        source: hosted
        version: "7.2.3"
      built_collection:
        dependency: transitive
        description:
          name: built_collection
          url: "https://pub.dartlang.org"
        source: hosted
        version: "5.1.1"
      built_value:
        dependency: transitive
        description:
          name: built_value
          url: "https://pub.dartlang.org"
        source: hosted
        version: "8.3.3"
      checked_yaml:
        dependency: transitive
        description:
          name: checked_yaml
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.0.1"
      code_builder:
        dependency: transitive
        description:
          name: code_builder
          url: "https://pub.dartlang.org"
        source: hosted
        version: "4.1.0"
      collection:
        dependency: transitive
        description:
          name: collection
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.16.0"
      convert:
        dependency: transitive
        description:
          name: convert
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.0.2"
      crypto:
        dependency: transitive
        description:
          name: crypto
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.0.2"
      dart_style:
        dependency: transitive
        description:
          name: dart_style
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.2.3"
      file:
        dependency: transitive
        description:
          name: file
          url: "https://pub.dartlang.org"
        source: hosted
        version: "6.1.2"
      fixnum:
        dependency: transitive
        description:
          name: fixnum
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.1"
      frontend_server_client:
        dependency: transitive
        description:
          name: frontend_server_client
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.3"
      glob:
        dependency: transitive
        description:
          name: glob
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.0"
      googleapis_auth:
        dependency: transitive
        description:
          name: googleapis_auth
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.3.1"
      graphs:
        dependency: transitive
        description:
          name: graphs
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.0"
      grpc:
        dependency: "direct main"
        description:
          name: grpc
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.0.2"
      http:
        dependency: transitive
        description:
          name: http
          url: "https://pub.dartlang.org"
        source: hosted
        version: "0.13.4"
      http2:
        dependency: transitive
        description:
          name: http2
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.0.0"
      http_multi_server:
        dependency: transitive
        description:
          name: http_multi_server
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.2.1"
      http_parser:
        dependency: transitive
        description:
          name: http_parser
          url: "https://pub.dartlang.org"
        source: hosted
        version: "4.0.1"
      io:
        dependency: transitive
        description:
          name: io
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.3"
      js:
        dependency: transitive
        description:
          name: js
          url: "https://pub.dartlang.org"
        source: hosted
        version: "0.6.4"
      json_annotation:
        dependency: transitive
        description:
          name: json_annotation
          url: "https://pub.dartlang.org"
        source: hosted
        version: "4.5.0"
      lints:
        dependency: "direct dev"
        description:
          name: lints
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.1"
      logging:
        dependency: transitive
        description:
          name: logging
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.2"
      matcher:
        dependency: transitive
        description:
          name: matcher
          url: "https://pub.dartlang.org"
        source: hosted
        version: "0.12.12"
      meta:
        dependency: transitive
        description:
          name: meta
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.8.0"
      mime:
        dependency: transitive
        description:
          name: mime
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.2"
      package_config:
        dependency: transitive
        description:
          name: package_config
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.0"
      path:
        dependency: transitive
        description:
          name: path
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.8.2"
      pool:
        dependency: transitive
        description:
          name: pool
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.5.1"
      protobuf:
        dependency: "direct main"
        description:
          name: protobuf
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.0"
      protoc_builder:
        dependency: "direct dev"
        description:
          path: ".."
          relative: true
        source: path
        version: "0.2.1+1"
      pub_semver:
        dependency: transitive
        description:
          name: pub_semver
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.1"
      pubspec_parse:
        dependency: transitive
        description:
          name: pubspec_parse
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.2.0"
      shelf:
        dependency: transitive
        description:
          name: shelf
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.3.1"
      shelf_web_socket:
        dependency: transitive
        description:
          name: shelf_web_socket
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.2"
      source_span:
        dependency: transitive
        description:
          name: source_span
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.9.0"
      stack_trace:
        dependency: transitive
        description:
          name: stack_trace
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.10.0"
      stream_channel:
        dependency: transitive
        description:
          name: stream_channel
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.1.0"
      stream_transform:
        dependency: transitive
        description:
          name: stream_transform
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.0.0"
      string_scanner:
        dependency: transitive
        description:
          name: string_scanner
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.1.1"
      term_glyph:
        dependency: transitive
        description:
          name: term_glyph
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.2.1"
      timing:
        dependency: transitive
        description:
          name: timing
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.0"
      typed_data:
        dependency: transitive
        description:
          name: typed_data
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.3.1"
      watcher:
        dependency: transitive
        description:
          name: watcher
          url: "https://pub.dartlang.org"
        source: hosted
        version: "1.0.1"
      web_socket_channel:
        dependency: transitive
        description:
          name: web_socket_channel
          url: "https://pub.dartlang.org"
        source: hosted
        version: "2.2.0"
      yaml:
        dependency: transitive
        description:
          name: yaml
          url: "https://pub.dartlang.org"
        source: hosted
        version: "3.1.1"
    sdks:
      dart: ">=2.16.0 <3.0.0"
    
    opened by eseidel 4
  • Ensure that changes to input .proto files are watched

    Ensure that changes to input .proto files are watched

    Without this, the build system won't notice that a .proto file has been changed and will not invoke the builder again.

    Tested this in the example/ directory with dart run build_runner watch and making changes to the proto/my_proto.proto file and watching the resulting .dart files be regenerated.

    Fixes #1

    opened by ewaters 2
  • Fixed bug on Windows that wouldn't generate anything

    Fixed bug on Windows that wouldn't generate anything

    Believe it or not, being safe and using path.join to account for Window's backslash instead of a forward slash was actually breaking Windows builds. The reason is that these aren't actual paths processed by the system, they're more like templates passed to package:build, which expects forward slashes and didn't recognize the back-slash.

    This PR replaces the use of path.join with a hard-coded / instead.

    Fixes #5

    opened by Levi-Lesches 1
  • Support protoc_plugin v20.0.1

    Support protoc_plugin v20.0.1

    Summary

    Bumped the default protoc_plugin version to the latest v20.0.1 (which fixes mono_repo configuration after a breaking change)

    Notes

    protoc_plugin v20.0.1 adds a local relative dependency override to the protobuf package so I needed to also unpack and pub get it

    opened by stasgora 1
  • Broken at latest plugin release

    Broken at latest plugin release

    The latest dart protoc plugin version uses relative paths in the protoc-plugin folder (../protobuf) - this causes the dart pub get to fail as the filter removes any other folders.

    Suggested: update the test to include protobuf when decompressing.

    opened by jtmcdole 1
  • Ensure that changes to input .proto files are watched

    Ensure that changes to input .proto files are watched

    Without this, the build system won't notice that a .proto file has been changed and will not invoke the builder again.

    I updated this with another commit that ensures that output files will be recreated if they are deleted. However, due to a minor bug, it seems that it only watches the first output file in the list of buildExtensions, so if one removes the '{{}}.pb.dart' file, it will trigger a regenerate, but removing the other files won't.

    Tested this in the example/ directory with dart run build_runner watch and making changes to the proto/my_proto.proto file and watching the resulting .dart files be regenerated.

    Fixes #1

    opened by ewaters 1
  • added support of the well known types

    added support of the well known types

    Add support of the base google's types:

    • google/protobuf/any.proto
    • google/protobuf/api.proto
    • google/protobuf/descriptor.proto
    • google/protobuf/duration.proto
    • google/protobuf/empty.proto
    • google/protobuf/field_mask.proto
    • google/protobuf/source_context.proto
    • google/protobuf/struct.proto
    • google/protobuf/timestamp.proto
    • google/protobuf/type.proto
    • google/protobuf/wrappers.proto
    opened by EnvOut 0
  • Output is not regenerated upon changes to the input files

    Output is not regenerated upon changes to the input files

    Currently, a change to the underlying .proto file doesn't invalidate the build asset cache for some reason, and will not call ProtocBuilder. It seems even that one can delete the generated files, and further calls to build_runner build will not rebuild the output.

    opened by ewaters 0
  • Files are being generated however cannot find the generated proto

    Files are being generated however cannot find the generated proto

    I run the builder, it generates succesfully however I get the following error:

    [SEVERE] protoc_builder on lib/proto/service.proto:
    
    FileSystemException: Cannot open file, path = 'lib/lib/config/protos/service.pb.dart' (OS Error: The system cannot find the path specified.
    , errno = 3)
    [INFO] 28.5s elapsed, 301/317 actions completed.
    [INFO] Running build completed, took 29.1s
    

    looking at the hiearchy I can see the generated files exist. image

    This is my build.yaml:

    targets:
      $default:
        builders:
          drift_dev:
            options:
              generate_connect_constructor: false
    # generate_values_in_copy_with (defaults to true): Generates a Value<T?> instead of T?
    # for nullable columns in copyWith. This allows to set columns back to null (by using Value(null)).
    # Passing null was ignored before, making it impossible to set columns to null.
              generate_values_in_copy_with : false
          protoc_builder:
            options:
              # The version of the Protobuf compiler to use.
              protobuf_version: "3.19.1" # Make sure to use quotation marks.
              # The version of the Dart protoc_plugin package to use.
              protoc_plugin_version: "20.0.0" # Make sure to use quotation marks.
              # Include paths given to the Protobuf compiler during compilation.
              proto_paths:
                - "lib/proto/"
              # The root directory for generated Dart output files.
              out_dir: "lib/config/protos"
    

    If I change my build.yaml where out_dir: "./config/protos" then no files get generated.

    opened by Leylan24 0
  • Run faster! Compile and move the proto plugin

    Run faster! Compile and move the proto plugin

    reference: https://github.com/pikaju/dart-protoc-builder/blob/90966ef0f1fd38ec56dcec9b42710d11d3943c43/lib/src/protoc_plugin_download.dart#L49-L51

    The plugin's "executable" is just running dart (the vm) - loading and parsing each and every execution. I've noticed this to be a significant slowdown.

        // Compile the program for faster startup.
        await Process.run('dart', ['compile', 'exe', 'bin/protoc_plugin.dart'],
            workingDirectory: protocPluginPackageDirectory.path);
    
        // Move the program so no one can tell any better....
        // dart compile always writes ".exe" file on every system.
        final protocBin = '${protocPluginPackageDirectory.path/bin/protoc_plugin.exe';
        await File(protocBin).rename(protocPlugin .path);
    
    opened by jtmcdole 2
Owner
Julien Scholz
Software engineer and wannabe entrepreneur
Julien Scholz
Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC.

grpc_protobuf_convert Provides simple conversion between Dart classes and Protobuf / Fixnum classes used in gRPC. Using the library Add the repo to yo

null 2 Nov 1, 2022
Parser tool is a real-time compiler and runtime engine for strongly typed PEG parsers

parser_tool Version 0.1.1 (BETA) Parser tool is a real-time compiler and runtime engine for strongly typed PEG parsers. Parser tool contains libraries

null 6 Jun 28, 2021
A Dart testing utility for asserting that some code emits a compilation error.

A Dart testing utility for asserting that some code emits a compilation error.

Remi Rousselet 32 Dec 11, 2022
Hot restart for Dart console application with fast incremental compilation.

Hot restart for Dart console application with fast incremental compilation. Why do I need this? If your console application compiles too long before r

Olzhas Suleimen 1 Oct 7, 2022
Library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script

To-Do telegram client dart ✅️ support multi token ( bot / userbot ) ✅️ support bot and userbot ✅️ support telegram-bot-api local server ✅️ support tel

Azka Full Snack Developer:) 73 Jan 7, 2023
A script for automating the creation and uploading a flutter project to github

Flutter Automation Script A script for automating the tasks of creating and uploading a flutter project to github. Made with Batch Scripts, Shell Scri

Tushar Patil 18 Dec 15, 2022
null 2 Apr 17, 2022
Provides Dart Build System builder for creating Injection pattern using annotations.

Provides Dart Build System builder for creating Injection pattern using annotations. Gate generator The core package providing generators using annoat

Apparence.io 17 Dec 20, 2022
Mustang: A framework to build Flutter applications

Mustang A framework to build Flutter applications. Following features are available out of the box. State Management Persistence Cache File layout and

null 10 Oct 26, 2022
null 9 Dec 1, 2022
A flutter plugin that provides external storage path and external public storage path

ext_storage ext_storage is minimal flutter plugin that provides external storage path and external public storage path

null 0 Nov 16, 2021
A flutter plugin for execute dart code in background.

flutter_background_service A flutter plugin for execute dart code in background. Android No additional setting required. To change notification icon,

Eka Setiawan Saputra 160 Dec 27, 2022
Morphological analysis of Japanese sentences. This Flutter plugin helps you to analyze Japanese sentences.

ringo ringo is japanese word separator. Usage final ringo = await Ringo.init(); final tokenized = ringo.tokenize('吾輩はRingoである'); print('tokenized: $to

ryo 8 Oct 31, 2022
A flutter plugin that implements google's standalone ml kit

A flutter plugin that implements google's standalone ml kit

Bharat Biradar 424 Dec 30, 2022
A Flutter plugin that exposes Monet (Material You, Material 3) system colors on Android 12.

Monet Colors A Flutter plugin that exposes Monet (Material You, Material 3) system colors on Android 12. Returns null on unsupported platforms and lea

İhsan Işık 3 Aug 26, 2022
Flutter WebRTC plugin Demo

Flutter WebRTC plugin Demo

null 0 Nov 19, 2021
Flutter Map plugin for ArcGIS Esri. Currently support feature layer (point, polygon)

Flutter Map plugin for ArcGIS Esri Currently support feature layer(point, polygon, polyline coming soon) We are working on more features A Dart implem

Munkh-Altai 17 Nov 9, 2022
Flutter plugin to help experiment with different Color Schemes without pain.

Random Color Scheme Making a coherent Material Design theme is hard. This is a Flutter plugin that generates a random color pallet based on Material D

Bernardo Ferrari 77 Dec 6, 2022
A Flutter plugin for XUpdate -- Android Update Library

flutter_xupdate A Flutter plugin for XUpdate -- Android Update Library。See the use Chinese Document for details。 About me WeChat public number juejin

薛翔 233 Dec 25, 2022