|
| 1 | +// Copyright 2024 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | +// ignore_for_file: avoid_print |
| 5 | + |
| 6 | +import 'dart:convert'; |
| 7 | +import 'dart:io'; |
| 8 | + |
| 9 | +import 'package:cli_util/cli_logging.dart' as logging; |
| 10 | +import 'package:glob/glob.dart'; |
| 11 | +import 'package:melos/melos.dart' as melos; |
| 12 | +import 'package:pub_semver/pub_semver.dart' as melos; |
| 13 | + |
| 14 | +import 'generate_bom.dart'; |
| 15 | + |
| 16 | +void main(List<String> arguments) async { |
| 17 | + final newBoMVersion = await getBoMNextVersion(shouldLog: true); |
| 18 | + if (newBoMVersion == null) { |
| 19 | + print('No changes detected'); |
| 20 | + } else { |
| 21 | + print('New BoM Version: $newBoMVersion'); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +Future<melos.MelosWorkspace> getMelosWorkspace() async { |
| 26 | + final packageFilters = melos.PackageFilters( |
| 27 | + includePrivatePackages: false, |
| 28 | + ignore: [ |
| 29 | + Glob('*web*'), |
| 30 | + Glob('*platform*'), |
| 31 | + Glob('*internals*'), |
| 32 | + ], |
| 33 | + ); |
| 34 | + final workspace = await melos.MelosWorkspace.fromConfig( |
| 35 | + await melos.MelosWorkspaceConfig.fromWorkspaceRoot(Directory.current), |
| 36 | + logger: melos.MelosLogger(logging.Logger.standard()), |
| 37 | + packageFilters: packageFilters, |
| 38 | + ); |
| 39 | + |
| 40 | + return workspace; |
| 41 | +} |
| 42 | + |
| 43 | +Future<String?> getBoMNextVersion({bool shouldLog = false}) async { |
| 44 | + File currentVersionsJson = File(versionsJsonFile); |
| 45 | + Map<String, dynamic> currentVersions = |
| 46 | + jsonDecode(currentVersionsJson.readAsStringSync()); |
| 47 | + // We always append the latest version to the top of the file |
| 48 | + // and it's preserved during parsing |
| 49 | + final currentVersionNumber = currentVersions.keys.first; |
| 50 | + |
| 51 | + final currentBoMVersion = melos.Version.parse(currentVersionNumber); |
| 52 | + final previousBoMPackageNameAndVersions = |
| 53 | + currentVersions[currentVersionNumber]['packages'] |
| 54 | + as Map<String, dynamic>? ?? |
| 55 | + {}; |
| 56 | + |
| 57 | + final currentPackageNameAndVersionsMap = await getPackagesUsingMelos(); |
| 58 | + final changes = <String, int>{}; |
| 59 | + final changedPackages = <String, List<String>>{}; |
| 60 | + for (final entry in currentPackageNameAndVersionsMap.entries) { |
| 61 | + final previousVersion = previousBoMPackageNameAndVersions[entry.key]; |
| 62 | + if (previousVersion == null) { |
| 63 | + changes['new'] = (changes['new'] ?? 0) + 1; |
| 64 | + if (shouldLog) { |
| 65 | + print('New package: ${entry.key} ${entry.value}'); |
| 66 | + } |
| 67 | + } else { |
| 68 | + final previous = melos.Version.parse(previousVersion); |
| 69 | + final current = entry.value.version; |
| 70 | + if (current.major > previous.major) { |
| 71 | + changes['major'] = (changes['major'] ?? 0) + 1; |
| 72 | + changedPackages[entry.key] = [previousVersion, current.toString()]; |
| 73 | + } else if (current.minor > previous.minor) { |
| 74 | + changes['minor'] = (changes['minor'] ?? 0) + 1; |
| 75 | + changedPackages[entry.key] = [previousVersion, current.toString()]; |
| 76 | + } else if (current.patch > previous.patch) { |
| 77 | + changes['patch'] = (changes['patch'] ?? 0) + 1; |
| 78 | + changedPackages[entry.key] = [previousVersion, current.toString()]; |
| 79 | + } else if (current.build.isNotEmpty) { |
| 80 | + final previousBuild = |
| 81 | + previous.build.isEmpty ? 0 : previous.build.first as int; |
| 82 | + final currentBuild = current.build.first as int; |
| 83 | + if (currentBuild > previousBuild) { |
| 84 | + changes['patch'] = (changes['patch'] ?? 0) + 1; |
| 85 | + changedPackages[entry.key] = [previousVersion, current.toString()]; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (shouldLog) { |
| 92 | + print( |
| 93 | + 'Previous BoM Package Name and Versions: $previousBoMPackageNameAndVersions', |
| 94 | + ); |
| 95 | + print( |
| 96 | + 'Current Package Name and Versions: $currentPackageNameAndVersionsMap', |
| 97 | + ); |
| 98 | + print('-' * 80); |
| 99 | + print('Changes: $changes'); |
| 100 | + print('Changed Packages: $changedPackages'); |
| 101 | + print('-' * 80); |
| 102 | + print('Current BoM Version: $currentBoMVersion'); |
| 103 | + } |
| 104 | + |
| 105 | + var newBoMVersion = currentBoMVersion; |
| 106 | + |
| 107 | + if (changes.isNotEmpty) { |
| 108 | + if (changes['major'] != null) { |
| 109 | + newBoMVersion = newBoMVersion.nextMajor; |
| 110 | + } else if (changes['minor'] != null) { |
| 111 | + newBoMVersion = newBoMVersion.nextMinor; |
| 112 | + } else if (changes['patch'] != null) { |
| 113 | + newBoMVersion = newBoMVersion.nextPatch; |
| 114 | + } else if (changes['new'] != null) { |
| 115 | + newBoMVersion = newBoMVersion.nextMinor; |
| 116 | + } |
| 117 | + return newBoMVersion.toString(); |
| 118 | + } else if (shouldLog) { |
| 119 | + print('No changes detected'); |
| 120 | + } |
| 121 | + return null; |
| 122 | +} |
0 commit comments