downloadProtoc function

Future<String> downloadProtoc(
  1. String protocVersion
)

Implementation

Future<String> downloadProtoc(String protocVersion) async {
  final Directory protocTmpDir =
      Directory(path.join(_builderTmpDir, "protoc", "v$protocVersion"));

  final protoc = File(
    path.join(
        protocTmpDir.path, "bin", Platform.isWindows ? "protoc.exe" : "protoc"),
  );

  if (protoc.existsSync()) {
    return protoc.path;
  } else {
    if (!protocTmpDir.existsSync()) {
      await protocTmpDir.create(recursive: true);
    }

    Uri url = Uri.parse(
        "https://github.com/protocolbuffers/protobuf/releases/download/v$protocVersion/protoc-$protocVersion-${_getPlatform(true)}.zip");
    final archive = ZipDecoder().decodeBytes(await http.readBytes(url));
    for (final file in archive) {
      if (file.isFile) {
        final outFile = File(path.join(protocTmpDir.path, file.name));
        await outFile.create(recursive: true);
        await outFile.writeAsBytes(file.content);
      }
    }

    if (protoc.existsSync()) {
      if (!Platform.isWindows) {
        await ProcessUtil.runCommand("chmod", ["+x", protoc.path]);
      }
      return protoc.path;
    } else {
      throw AssertionError('protoc is not exist.');
    }
  }
}