Skip to content

Commit 9f4e6a4

Browse files
authored
Helper to resolve dart version for clients of analytics (#233)
* Helper method added to parse dart version * Additional documentation * Export helper + update changelog * Prepare to publish
1 parent 8323b21 commit 9f4e6a4

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

pkgs/unified_analytics/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
## 5.8.2-wip
1+
## 5.8.2
22

33
- Added new event `Event.analyticsException` to track internal errors for this package
44
- Redirecting the `Analytics.test` factory to return an instance of `FakeAnalytics`
5+
- Exposing new helper function that can be used to parse the Dart SDK version
56

67
## 5.8.1
78

pkgs/unified_analytics/lib/src/constants.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
8282
const String kLogFileName = 'dart-flutter-telemetry.log';
8383

8484
/// The current version of the package, should be in line with pubspec version.
85-
const String kPackageVersion = '5.8.2-wip';
85+
const String kPackageVersion = '5.8.2';
8686

8787
/// The minimum length for a session.
8888
const int kSessionDurationMinutes = 30;

pkgs/unified_analytics/lib/src/utils.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,34 @@ bool legacyOptOut({required FileSystem fs, required Directory home}) {
215215
return false;
216216
}
217217

218+
/// Helper method that can be used to resolve the Dart SDK version for clients
219+
/// of package:unified_analytics.
220+
///
221+
/// Input [versionString] for this method should be the returned string from
222+
/// [io.Platform.version].
223+
///
224+
/// For tools that don't already have a method for resolving the Dart
225+
/// SDK version in semver notation, this helper can be used. This uses
226+
/// the [io.Platform.version] to parse the semver.
227+
///
228+
/// Example for stable version:
229+
/// `3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64"` into
230+
/// `3.3.0`.
231+
///
232+
/// Example for non-stable version:
233+
/// `2.1.0-dev.8.0.flutter-312ae32` into `2.1.0 (build 2.1.0-dev.8.0 312ae32)`.
234+
String parseDartSDKVersion(String versionString) {
235+
versionString = versionString.trim();
236+
final justVersion = versionString.split(' ')[0];
237+
238+
// For non-stable versions, this regex will include build information
239+
return justVersion.replaceFirstMapped(RegExp(r'(\d+\.\d+\.\d+)(.+)'),
240+
(Match match) {
241+
final noFlutter = match[2]!.replaceAll('.flutter-', ' ');
242+
return '${match[1]} (build ${match[1]}$noFlutter)'.trim();
243+
});
244+
}
245+
218246
/// Will use two strings to produce a double for applying a sampling
219247
/// rate for [Survey] to be returned to the user.
220248
double sampleRate(String string1, String string2) =>

pkgs/unified_analytics/lib/unified_analytics.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export 'src/enums.dart' show DashTool;
88
export 'src/event.dart' show Event;
99
export 'src/log_handler.dart' show LogFileStats;
1010
export 'src/survey_handler.dart' show Survey, SurveyButton, SurveyHandler;
11+
export 'src/utils.dart' show parseDartSDKVersion;

pkgs/unified_analytics/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: >-
44
to Google Analytics.
55
# When updating this, keep the version consistent with the changelog and the
66
# value in lib/src/constants.dart.
7-
version: 5.8.2-wip
7+
version: 5.8.2
88
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics
99

1010
environment:

pkgs/unified_analytics/test/unified_analytics_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,4 +1276,21 @@ Privacy Policy (https://policies.google.com/privacy).
12761276
expect(eventList.contains(eventToMatch), true);
12771277
expect(eventList.where((element) => element == eventToMatch).length, 1);
12781278
});
1279+
1280+
group('Unit tests for util dartSDKVersion', () {
1281+
test('parses correctly for non-stable version', () {
1282+
final originalVersion =
1283+
'3.4.0-148.0.dev (dev) (Thu Feb 15 12:05:45 2024 -0800) on "macos_arm64"';
1284+
1285+
expect(parseDartSDKVersion(originalVersion),
1286+
'3.4.0 (build 3.4.0-148.0.dev)');
1287+
});
1288+
1289+
test('parses correctly for stable version', () {
1290+
final originalVersion =
1291+
'3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64"';
1292+
1293+
expect(parseDartSDKVersion(originalVersion), '3.3.0');
1294+
});
1295+
});
12791296
}

0 commit comments

Comments
 (0)