Skip to content

Commit

Permalink
[unified_analytics] Drop direct dependency on package:path (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
parlough authored Sep 28, 2024
1 parent 8ebc763 commit 0b41bd2
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 119 deletions.
1 change: 1 addition & 0 deletions pkgs/unified_analytics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 6.1.4-wip

- Fix formatting and remove dependency on `package:intl`.
- Remove direct usage of `package:path` in favor of `package:file`.

## 6.1.3

Expand Down
9 changes: 3 additions & 6 deletions pkgs/unified_analytics/example/serving_surveys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:path/path.dart' as p;

import 'package:unified_analytics/src/constants.dart';
import 'package:unified_analytics/src/enums.dart';
Expand Down Expand Up @@ -54,11 +53,9 @@ void main() async {
fs: fs,
platform: DevicePlatform.macos,
surveyHandler: FakeSurveyHandler.fromList(
dismissedSurveyFile: fs.file(p.join(
home.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)),
dismissedSurveyFile: home
.childDirectory(kDartToolDirectoryName)
.childFile(kDismissedSurveyFileName),
initializedSurveys: [
Survey(
uniqueId: 'uniqueId',
Expand Down
64 changes: 24 additions & 40 deletions pkgs/unified_analytics/lib/src/analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:file/local.dart';
import 'package:file/memory.dart';
import 'package:http/http.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;

import 'asserts.dart';
import 'config_handler.dart';
Expand Down Expand Up @@ -78,7 +77,7 @@ abstract class Analytics {
apiSecret: kGoogleAnalyticsApiSecret,
);

final firstRun = runInitialization(homeDirectory: homeDirectory, fs: fs);
final firstRun = runInitialization(homeDirectory: homeDirectory);

return AnalyticsImpl(
tool: tool,
Expand All @@ -91,11 +90,9 @@ abstract class Analytics {
fs: fs,
gaClient: gaClient,
surveyHandler: SurveyHandler(
dismissedSurveyFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)),
dismissedSurveyFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kDismissedSurveyFileName),
),
enableAsserts: enableAsserts,
clientIde: clientIde,
Expand Down Expand Up @@ -157,7 +154,7 @@ abstract class Analytics {
apiSecret: kTestApiSecret,
);

final firstRun = runInitialization(homeDirectory: homeDirectory, fs: fs);
final firstRun = runInitialization(homeDirectory: homeDirectory);

return AnalyticsImpl(
tool: tool,
Expand All @@ -170,11 +167,9 @@ abstract class Analytics {
fs: fs,
gaClient: gaClient,
surveyHandler: SurveyHandler(
dismissedSurveyFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)),
dismissedSurveyFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kDismissedSurveyFileName),
),
enableAsserts: enableAsserts,
clientIde: clientIde,
Expand Down Expand Up @@ -307,7 +302,7 @@ abstract class Analytics {
String toolsMessage = kToolsMessage,
bool enableAsserts = true,
}) {
final firstRun = runInitialization(homeDirectory: homeDirectory, fs: fs);
final firstRun = runInitialization(homeDirectory: homeDirectory);

return FakeAnalytics._(
tool: tool,
Expand All @@ -320,11 +315,9 @@ abstract class Analytics {
fs: fs,
surveyHandler: surveyHandler ??
FakeSurveyHandler.fromList(
dismissedSurveyFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
)),
dismissedSurveyFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kDismissedSurveyFileName),
initializedSurveys: [],
),
gaClient: gaClient ?? const FakeGAClient(),
Expand Down Expand Up @@ -395,17 +388,13 @@ class AnalyticsImpl implements Analytics {
}) : _gaClient = gaClient,
_surveyHandler = surveyHandler,
_enableAsserts = enableAsserts,
_clientIdFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kClientIdFileName,
)),
_clientIdFile = homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kClientIdFileName),
_userProperty = UserProperty(
sessionFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kSessionFileName,
)),
sessionFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kSessionFileName),
flutterChannel: flutterChannel,
host: platform.label,
flutterVersion: flutterVersion,
Expand All @@ -420,20 +409,15 @@ class AnalyticsImpl implements Analytics {
enabledFeatures: enabledFeatures,
),
_configHandler = ConfigHandler(
fs: fs,
homeDirectory: homeDirectory,
configFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kConfigFileName,
)),
configFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kConfigFileName),
),
_logHandler = LogHandler(
logFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kLogFileName,
)),
logFile: homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kLogFileName),
) {
// This initializer class will let the instance know
// if it was the first run; if it is, nothing will be sent
Expand Down
11 changes: 1 addition & 10 deletions pkgs/unified_analytics/lib/src/config_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ import 'dart:convert';
import 'package:clock/clock.dart';
import 'package:convert/convert.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as p;

import 'constants.dart';
import 'initializer.dart';
import 'utils.dart';

Expand All @@ -33,7 +31,6 @@ class ConfigHandler {
RegExp(telemetryFlagPattern, multiLine: true);
static RegExp toolRegex = RegExp(toolPattern, multiLine: true);

final FileSystem fs;
final Directory homeDirectory;
final File configFile;

Expand All @@ -45,7 +42,6 @@ class ConfigHandler {
bool _telemetryEnabled = true;

ConfigHandler({
required this.fs,
required this.homeDirectory,
required this.configFile,
}) : configFileLastModified = configFile.lastModifiedSync() {
Expand Down Expand Up @@ -176,12 +172,7 @@ class ConfigHandler {
/// [parsedTools] map and trigger parsing the config again.
void resetConfig() {
createConfigFile(
configFile: fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kConfigFileName,
)),
fs: fs,
configFile: configFile,
homeDirectory: homeDirectory,
);
parsedTools.clear();
Expand Down
34 changes: 14 additions & 20 deletions pkgs/unified_analytics/lib/src/initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as p;

import 'constants.dart';
import 'utils.dart';
Expand All @@ -22,14 +21,13 @@ void createClientIdFile({required File clientIdFile}) {
void createConfigFile({
required File configFile,
required Directory homeDirectory,
required FileSystem fs,
}) {
configFile.createSync(recursive: true);

// If the user was previously opted out, then we will
// replace the line that assumes automatic opt in with
// an opt out from the start
if (legacyOptOut(fs: fs, home: homeDirectory)) {
if (legacyOptOut(homeDirectory: homeDirectory)) {
configFile.writeAsStringSync(
kConfigString.replaceAll('reporting=1', 'reporting=0'));
} else {
Expand Down Expand Up @@ -75,47 +73,43 @@ DateTime createSessionFile({required File sessionFile}) {
/// - Dismissed survey JSON file
bool runInitialization({
required Directory homeDirectory,
required FileSystem fs,
}) {
var firstRun = false;
final dartToolDirectory =
homeDirectory.childDirectory(kDartToolDirectoryName);

// When the config file doesn't exist, initialize it with the default tools
// and the current date
final configFile = fs.file(
p.join(homeDirectory.path, kDartToolDirectoryName, kConfigFileName));
// and the current date.
final configFile = dartToolDirectory.childFile(kConfigFileName);
if (!configFile.existsSync()) {
firstRun = true;
createConfigFile(
configFile: configFile,
fs: fs,
homeDirectory: homeDirectory,
);
}

// Begin initialization checks for the client id
final clientFile = fs.file(
p.join(homeDirectory.path, kDartToolDirectoryName, kClientIdFileName));
// Begin initialization checks for the client id.
final clientFile = dartToolDirectory.childFile(kClientIdFileName);
if (!clientFile.existsSync()) {
createClientIdFile(clientIdFile: clientFile);
}

// Begin initialization checks for the session file
final sessionFile = fs.file(
p.join(homeDirectory.path, kDartToolDirectoryName, kSessionFileName));
// Begin initialization checks for the session file.
final sessionFile = dartToolDirectory.childFile(kSessionFileName);
if (!sessionFile.existsSync()) {
createSessionFile(sessionFile: sessionFile);
}

// Begin initialization checks for the log file to persist events locally
final logFile =
fs.file(p.join(homeDirectory.path, kDartToolDirectoryName, kLogFileName));
// Begin initialization checks for the log file to persist events locally.
final logFile = dartToolDirectory.childFile(kLogFileName);
if (!logFile.existsSync()) {
createLogFile(logFile: logFile);
}

// Begin initialization checks for the dismissed survey file
final dismissedSurveyFile = fs.file(p.join(
homeDirectory.path, kDartToolDirectoryName, kDismissedSurveyFileName));
// Begin initialization checks for the dismissed survey file.
final dismissedSurveyFile =
dartToolDirectory.childFile(kDismissedSurveyFileName);
if (!dismissedSurveyFile.existsSync()) {
createDismissedSurveyFile(dismissedSurveyFile: dismissedSurveyFile);
}
Expand Down
39 changes: 20 additions & 19 deletions pkgs/unified_analytics/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'dart:math' show Random;
import 'package:clock/clock.dart';
import 'package:convert/convert.dart';
import 'package:file/file.dart';
import 'package:path/path.dart' as p;

import 'enums.dart';
import 'event.dart';
Expand Down Expand Up @@ -146,30 +145,32 @@ Directory? getHomeDirectory(FileSystem fs) {
/// }
/// }
/// ```
bool legacyOptOut({required FileSystem fs, required Directory home}) {
bool legacyOptOut({required Directory homeDirectory}) {
// List of Maps for each of the config file, `key` refers to the
// key in the json file that indicates if the user has been opted
// out or not
final legacyConfigFiles = [
{
'tool': DashTool.dartTool,
'file': fs.file(p.join(home.path, '.dart', 'dartdev.json')),
'key': 'enabled',
},
{
'tool': DashTool.flutterTool,
'file': fs.file(p.join(home.path, '.flutter')),
'key': 'enabled',
},
{
'tool': DashTool.devtools,
'file': fs.file(p.join(home.path, '.flutter-devtools', '.devtools')),
'key': 'analyticsEnabled',
},
(
tool: DashTool.dartTool,
file: homeDirectory.childDirectory('.dart').childFile('dartdev.json'),
key: 'enabled',
),
(
tool: DashTool.flutterTool,
file: homeDirectory.childFile('.flutter'),
key: 'enabled',
),
(
tool: DashTool.devtools,
file: homeDirectory
.childDirectory('.flutter-devtools')
.childFile('.devtools'),
key: 'analyticsEnabled',
),
];
for (final legacyConfigObj in legacyConfigFiles) {
final legacyFile = legacyConfigObj['file']! as File;
final lookupKey = legacyConfigObj['key']! as String;
final legacyFile = legacyConfigObj.file;
final lookupKey = legacyConfigObj.key;

if (legacyFile.existsSync()) {
try {
Expand Down
1 change: 0 additions & 1 deletion pkgs/unified_analytics/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ dependencies:
file: '>=6.1.4 <8.0.0'
http: '>=0.13.5 <2.0.0'
meta: ^1.9.0
path: ^1.8.0

dev_dependencies:
dart_flutter_team_lints: ^3.0.0
Expand Down
9 changes: 3 additions & 6 deletions pkgs/unified_analytics/test/events_with_fake_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import 'package:clock/clock.dart';
import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import 'package:unified_analytics/src/constants.dart';
Expand Down Expand Up @@ -52,11 +51,9 @@ void main() {
setUp(() async {
fs = MemoryFileSystem.test(style: FileSystemStyle.posix);
homeDirectory = fs.directory('home');
dismissedSurveyFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kDismissedSurveyFileName,
));
dismissedSurveyFile = homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kDismissedSurveyFileName);

final initialAnalytics = Analytics.fake(
tool: DashTool.flutterTool,
Expand Down
9 changes: 3 additions & 6 deletions pkgs/unified_analytics/test/log_handler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'dart:convert';

import 'package:file/file.dart';
import 'package:file/memory.dart';
import 'package:path/path.dart' as p;
import 'package:test/fake.dart';
import 'package:test/test.dart';

Expand All @@ -27,11 +26,9 @@ void main() {
setUp(() {
fs = MemoryFileSystem.test(style: FileSystemStyle.posix);
homeDirectory = fs.directory('home');
logFile = fs.file(p.join(
homeDirectory.path,
kDartToolDirectoryName,
kLogFileName,
));
logFile = homeDirectory
.childDirectory(kDartToolDirectoryName)
.childFile(kLogFileName);

// Create the initialization analytics instance to onboard the tool
final initializationAnalytics = Analytics.fake(
Expand Down
Loading

0 comments on commit 0b41bd2

Please sign in to comment.