downloadDartPlugin function

Future<void> downloadDartPlugin(
  1. String version
)

Downloads the Dart protoc plugin for version.

Fetches packages and compiles the plugin for faster execution.

Implementation

Future<void> downloadDartPlugin(String version) async {
  final dartExeName =
      Platform.isWindows ? 'protoc-gen-dart.bat' : 'protoc-gen-dart';
  final localPluginPath = path.join(
    pluginDirectory.path,
    'protobuf.dart-protoc_plugin-v$version',
    'protoc_plugin',
  );

  dartPlugin = path.join(localPluginPath, 'bin', dartExeName);
  if (await File(dartPlugin).exists()) {
    return;
  }

  final downloadUri =
      Uri.parse('https://github.com/google/protobuf.dart/archive/'
          'refs/tags/protoc_plugin-v$version.zip');

  stdout.write('${progressBar(0, colorCompleted: penDone)} '
      'downloading dart protoc plugin');
  final bytes = await _getWithProgress(downloadUri, (progress) {
    final bar = progressBar(progress, colorCompleted: penDone);
    stdout
      ..moveFarLeft()
      ..write(bar);
  });
  stdout
    ..moveFarLeft()
    ..write(progressBar(1.0, colorCompleted: penDone));
  stdout.writeln();

  stdout.write('${progressBar(0, colorCompleted: penDone)} '
      'uncompressing dart protoc plugin');
  await unzip(
    bytes,
    pluginDirectory.path,
    where: _pluginWhere,
    progress: (progress) {
      stdout
        ..moveFarLeft()
        ..write(progressBar(progress, colorCompleted: penDone));
    },
  );
  stdout.writeln();

  // Fetch build deps from thirdparty package.
  await waitForTask('fetch plugin dart packages',
      Process.run('dart', ['pub', 'get'], workingDirectory: localPluginPath));

  // Compile program for higher performance.
  await waitForTask(
      'compiling protoc_plugin.dart for speed',
      Process.run('dart', ['compile', 'exe', 'bin/protoc_plugin.dart'],
          workingDirectory: localPluginPath));

  // Replace the "script" with the compiled excutible. Dart always outputs.exe
  final protocBin = path.join(localPluginPath, 'bin', 'protoc_plugin.exe');
  await File(protocBin).rename(dartPlugin);

  // Mark as executible when needed.
  if (!Platform.isWindows) {
    await Process.run('chmod', ['+x', '$localPluginPath/bin/protoc-gen-dart']);
  }
}