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