From c696842e110b6645bebf006f95e2da50d5d16ac3 Mon Sep 17 00:00:00 2001 From: Russell Wheatley Date: Wed, 3 Jul 2024 09:34:22 +0100 Subject: [PATCH] ci: update dependencies with script (#369) --- melos.yaml | 2 + scripts/update_dependencies.dart | 119 +++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 scripts/update_dependencies.dart diff --git a/melos.yaml b/melos.yaml index b9d09cc8..cb2c960f 100644 --- a/melos.yaml +++ b/melos.yaml @@ -148,3 +148,5 @@ scripts: emulator:start: run: firebase emulators:start --only firestore,auth,functions,storage,database --import=./emulators-data --export-on-exit=./emulators-data + update-dependencies: + run: dart scripts/update_dependencies.dart \ No newline at end of file diff --git a/scripts/update_dependencies.dart b/scripts/update_dependencies.dart new file mode 100644 index 00000000..9bf0b116 --- /dev/null +++ b/scripts/update_dependencies.dart @@ -0,0 +1,119 @@ +// Copyright 2024, the Chromium project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:convert'; +import 'dart:io'; +import 'package:path/path.dart' as p; +import 'package:yaml/yaml.dart'; +import 'package:yaml_edit/yaml_edit.dart'; + +Future main(List args) async { + const bomPath = + 'https://raw.githubusercontent.com/firebase/flutterfire/master/scripts/versions.json'; + final http = HttpClient(); + final request = await http.getUrl(Uri.parse(bomPath)); + final response = await request.close(); // sends the request + final jsonString = await response.transform(utf8.decoder).join(); + final json = jsonDecode(jsonString) as Map; + + final latestVersions = + json[json.keys.first]["packages"] as Map; + + final iosSdkVersion = json[json.keys.first]["firebase_sdk"]["ios"] as String; + + final pubspecFilePaths = findPubspecFiles(Directory.current); + + final listOfFutures = []; + for (final filePath in pubspecFilePaths) { + final future = updatePubspecFile(filePath, latestVersions); + listOfFutures.add(future); + } + + listOfFutures.add( + updatePodfileVersion( + iosSdkVersion, + './tests/ios/Podfile', + ), + ); + listOfFutures.add( + updatePodfileVersion( + iosSdkVersion, + './tests/macos/Podfile', + ), + ); + + await Future.wait(listOfFutures); + + print( + 'All dependencies updated, please double check they are the latest, commit and push the changes.', + ); +} + +Future updatePubspecFile( + String filePath, + Map latestVersions, +) async { + final content = await File(filePath).readAsString(); + final yamlEditor = YamlEditor(content); + final pubspec = loadYaml(content) as YamlMap; + + final dependencies = pubspec['dependencies'] as YamlMap?; + + if (dependencies != null) { + dependencies.forEach((key, value) { + if (latestVersions.containsKey(key)) { + yamlEditor.update(['dependencies', key], '^${latestVersions[key]}'); + } + }); + } + + await File(filePath).writeAsString(yamlEditor.toString()); +} + +List findPubspecFiles(Directory root) { + final pubspecFiles = []; + final directories = [ + Directory(p.join(root.path, 'packages')), + Directory(p.join(root.path, 'tests')) + ]; + + for (final dir in directories) { + if (dir.existsSync()) { + dir.listSync(recursive: true).forEach((entity) { + if (entity is File && p.basename(entity.path) == 'pubspec.yaml') { + if (!entity.path.contains('.symlinks') && + !entity.path.contains('.plugin_symlinks')) { + pubspecFiles.add(entity.path); + } + } + }); + } + } + + return pubspecFiles; +} + +Future updatePodfileVersion( + String iosSdkVersion, + String podfilePath, +) async { + final podfile = File(podfilePath); + if (!await podfile.exists()) { + throw Exception('Podfile not found at $podfilePath'); + } + + String content = await podfile.readAsString(); + + final updatedContent = content.replaceAllMapped( + RegExp( + r"pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '\d+\.\d+\.\d+'"), + (match) => + "pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '$iosSdkVersion'", + ); + + await podfile.writeAsString(updatedContent); + + print( + 'Updated Podfile Firestore framework on path: $podfilePath to version: $iosSdkVersion'); +}