|
| 1 | +// Copyright 2024, the Chromium project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | +import 'package:path/path.dart' as p; |
| 8 | +import 'package:yaml/yaml.dart'; |
| 9 | +import 'package:yaml_edit/yaml_edit.dart'; |
| 10 | + |
| 11 | +Future<void> main(List<String> args) async { |
| 12 | + const bomPath = |
| 13 | + 'https://raw.githubusercontent.com/firebase/flutterfire/master/scripts/versions.json'; |
| 14 | + final http = HttpClient(); |
| 15 | + final request = await http.getUrl(Uri.parse(bomPath)); |
| 16 | + final response = await request.close(); // sends the request |
| 17 | + final jsonString = await response.transform(utf8.decoder).join(); |
| 18 | + final json = jsonDecode(jsonString) as Map<String, dynamic>; |
| 19 | + |
| 20 | + final latestVersions = |
| 21 | + json[json.keys.first]["packages"] as Map<String, dynamic>; |
| 22 | + |
| 23 | + final iosSdkVersion = json[json.keys.first]["firebase_sdk"]["ios"] as String; |
| 24 | + |
| 25 | + final pubspecFilePaths = findPubspecFiles(Directory.current); |
| 26 | + |
| 27 | + final listOfFutures = <Future>[]; |
| 28 | + for (final filePath in pubspecFilePaths) { |
| 29 | + final future = updatePubspecFile(filePath, latestVersions); |
| 30 | + listOfFutures.add(future); |
| 31 | + } |
| 32 | + |
| 33 | + listOfFutures.add( |
| 34 | + updatePodfileVersion( |
| 35 | + iosSdkVersion, |
| 36 | + './tests/ios/Podfile', |
| 37 | + ), |
| 38 | + ); |
| 39 | + listOfFutures.add( |
| 40 | + updatePodfileVersion( |
| 41 | + iosSdkVersion, |
| 42 | + './tests/macos/Podfile', |
| 43 | + ), |
| 44 | + ); |
| 45 | + |
| 46 | + await Future.wait(listOfFutures); |
| 47 | + |
| 48 | + print( |
| 49 | + 'All dependencies updated, please double check they are the latest, commit and push the changes.', |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +Future<void> updatePubspecFile( |
| 54 | + String filePath, |
| 55 | + Map<String, dynamic> latestVersions, |
| 56 | +) async { |
| 57 | + final content = await File(filePath).readAsString(); |
| 58 | + final yamlEditor = YamlEditor(content); |
| 59 | + final pubspec = loadYaml(content) as YamlMap; |
| 60 | + |
| 61 | + final dependencies = pubspec['dependencies'] as YamlMap?; |
| 62 | + |
| 63 | + if (dependencies != null) { |
| 64 | + dependencies.forEach((key, value) { |
| 65 | + if (latestVersions.containsKey(key)) { |
| 66 | + yamlEditor.update(['dependencies', key], '^${latestVersions[key]}'); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + await File(filePath).writeAsString(yamlEditor.toString()); |
| 72 | +} |
| 73 | + |
| 74 | +List<String> findPubspecFiles(Directory root) { |
| 75 | + final pubspecFiles = <String>[]; |
| 76 | + final directories = [ |
| 77 | + Directory(p.join(root.path, 'packages')), |
| 78 | + Directory(p.join(root.path, 'tests')) |
| 79 | + ]; |
| 80 | + |
| 81 | + for (final dir in directories) { |
| 82 | + if (dir.existsSync()) { |
| 83 | + dir.listSync(recursive: true).forEach((entity) { |
| 84 | + if (entity is File && p.basename(entity.path) == 'pubspec.yaml') { |
| 85 | + if (!entity.path.contains('.symlinks') && |
| 86 | + !entity.path.contains('.plugin_symlinks')) { |
| 87 | + pubspecFiles.add(entity.path); |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return pubspecFiles; |
| 95 | +} |
| 96 | + |
| 97 | +Future<void> updatePodfileVersion( |
| 98 | + String iosSdkVersion, |
| 99 | + String podfilePath, |
| 100 | +) async { |
| 101 | + final podfile = File(podfilePath); |
| 102 | + if (!await podfile.exists()) { |
| 103 | + throw Exception('Podfile not found at $podfilePath'); |
| 104 | + } |
| 105 | + |
| 106 | + String content = await podfile.readAsString(); |
| 107 | + |
| 108 | + final updatedContent = content.replaceAllMapped( |
| 109 | + RegExp( |
| 110 | + r"pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '\d+\.\d+\.\d+'"), |
| 111 | + (match) => |
| 112 | + "pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '$iosSdkVersion'", |
| 113 | + ); |
| 114 | + |
| 115 | + await podfile.writeAsString(updatedContent); |
| 116 | + |
| 117 | + print( |
| 118 | + 'Updated Podfile Firestore framework on path: $podfilePath to version: $iosSdkVersion'); |
| 119 | +} |
0 commit comments