Skip to content

Commit

Permalink
Helper to resolve dart version for clients of analytics (#233)
Browse files Browse the repository at this point in the history
* Helper method added to parse dart version

* Additional documentation

* Export helper + update changelog

* Prepare to publish
  • Loading branch information
eliasyishak authored Feb 16, 2024
1 parent 8323b21 commit 9f4e6a4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkgs/unified_analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## 5.8.2-wip
## 5.8.2

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

## 5.8.1

Expand Down
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/lib/src/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const int kLogFileLength = 2500;
const String kLogFileName = 'dart-flutter-telemetry.log';

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

/// The minimum length for a session.
const int kSessionDurationMinutes = 30;
Expand Down
28 changes: 28 additions & 0 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@ bool legacyOptOut({required FileSystem fs, required Directory home}) {
return false;
}

/// Helper method that can be used to resolve the Dart SDK version for clients
/// of package:unified_analytics.
///
/// Input [versionString] for this method should be the returned string from
/// [io.Platform.version].
///
/// For tools that don't already have a method for resolving the Dart
/// SDK version in semver notation, this helper can be used. This uses
/// the [io.Platform.version] to parse the semver.
///
/// Example for stable version:
/// `3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64"` into
/// `3.3.0`.
///
/// Example for non-stable version:
/// `2.1.0-dev.8.0.flutter-312ae32` into `2.1.0 (build 2.1.0-dev.8.0 312ae32)`.
String parseDartSDKVersion(String versionString) {
versionString = versionString.trim();
final justVersion = versionString.split(' ')[0];

// For non-stable versions, this regex will include build information
return justVersion.replaceFirstMapped(RegExp(r'(\d+\.\d+\.\d+)(.+)'),
(Match match) {
final noFlutter = match[2]!.replaceAll('.flutter-', ' ');
return '${match[1]} (build ${match[1]}$noFlutter)'.trim();
});
}

/// Will use two strings to produce a double for applying a sampling
/// rate for [Survey] to be returned to the user.
double sampleRate(String string1, String string2) =>
Expand Down
1 change: 1 addition & 0 deletions pkgs/unified_analytics/lib/unified_analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export 'src/enums.dart' show DashTool;
export 'src/event.dart' show Event;
export 'src/log_handler.dart' show LogFileStats;
export 'src/survey_handler.dart' show Survey, SurveyButton, SurveyHandler;
export 'src/utils.dart' show parseDartSDKVersion;
2 changes: 1 addition & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: >-
to Google Analytics.
# When updating this, keep the version consistent with the changelog and the
# value in lib/src/constants.dart.
version: 5.8.2-wip
version: 5.8.2
repository: https://github.com/dart-lang/tools/tree/main/pkgs/unified_analytics

environment:
Expand Down
17 changes: 17 additions & 0 deletions pkgs/unified_analytics/test/unified_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1276,4 +1276,21 @@ Privacy Policy (https://policies.google.com/privacy).
expect(eventList.contains(eventToMatch), true);
expect(eventList.where((element) => element == eventToMatch).length, 1);
});

group('Unit tests for util dartSDKVersion', () {
test('parses correctly for non-stable version', () {
final originalVersion =
'3.4.0-148.0.dev (dev) (Thu Feb 15 12:05:45 2024 -0800) on "macos_arm64"';

expect(parseDartSDKVersion(originalVersion),
'3.4.0 (build 3.4.0-148.0.dev)');
});

test('parses correctly for stable version', () {
final originalVersion =
'3.3.0 (stable) (Tue Feb 13 10:25:19 2024 +0000) on "macos_arm64"';

expect(parseDartSDKVersion(originalVersion), '3.3.0');
});
});
}

0 comments on commit 9f4e6a4

Please sign in to comment.