Skip to content

Commit

Permalink
remove firebase support from dartpad (#2752)
Browse files Browse the repository at this point in the history
* remove firebase support from dartpad

* update analysis issue url

* dartfmt
  • Loading branch information
devoncarew committed Jan 25, 2024
1 parent 3dba38c commit c60b360
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 347 deletions.
17 changes: 12 additions & 5 deletions pkgs/dart_services/lib/src/analysis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ class AnalysisServerWrapper {
required this.sdkPath,
String? projectPath,
}) :
// During analysis, we use the Firebase project template. The Firebase
// template is separate from the Flutter template only to keep Firebase
// references out of app initialization code at runtime.
// During analysis, we use the Flutter project template.
projectPath =
projectPath ?? ProjectTemplates.projectTemplates.firebasePath;
projectPath ?? ProjectTemplates.projectTemplates.flutterPath;

String get mainPath => _getPathFromName(kMainDart);

Expand Down Expand Up @@ -327,7 +325,16 @@ class AnalysisServerWrapper {
}
} else if (import.packageImport) {
final packageName = import.packageName;
if (!isSupportedPackage(packageName)) {

if (isFirebasePackage(packageName)) {
importIssues.add(api.AnalysisIssue(
kind: 'warning',
message: 'Firebase is no longer supported by DartPad.',
url:
'https://github.com/dart-lang/dart-pad/wiki/Package-and-plugin-support#deprecated-firebase-packages',
location: import.getLocation(source),
));
} else if (!isSupportedPackage(packageName)) {
importIssues.add(api.AnalysisIssue(
kind: 'warning',
message: "Unsupported package: 'package:$packageName'.",
Expand Down
4 changes: 1 addition & 3 deletions pkgs/dart_services/lib/src/compiling.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ class Compiler {

try {
final usingFlutter = usesFlutterWeb(imports);
if (usesFirebase(imports)) {
_copyPath(_projectTemplates.firebasePath, temp.path);
} else if (usingFlutter) {
if (usingFlutter) {
_copyPath(_projectTemplates.flutterPath, temp.path);
} else {
_copyPath(_projectTemplates.dartPath, temp.path);
Expand Down
78 changes: 6 additions & 72 deletions pkgs/dart_services/lib/src/project_creator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,17 @@ ${_sdk.experiments.map((experiment) => ' - $experiment').join('\n')}

/// Builds a Flutter project template directory, complete with `pubspec.yaml`,
/// `analysis_options.yaml`, and `web/index.html`.
///
/// Depending on [firebaseStyle], Firebase packages are included in
/// `pubspec.yaml` which affects how `flutter packages get` will register
/// plugins.
Future<void> buildFlutterProjectTemplate({
required FirebaseStyle firebaseStyle,
}) async {
final projectDirName = firebaseStyle == FirebaseStyle.none
? 'flutter_project'
: 'firebase_project';
final projectPath = path.join(
_templatesPath,
projectDirName,
);
Future<void> buildFlutterProjectTemplate() async {
final projectPath = path.join(_templatesPath, 'flutter_project');
await Directory(projectPath).create(recursive: true);
await Directory(path.join(projectPath, 'lib')).create();
await Directory(path.join(projectPath, 'web')).create();
await File(path.join(projectPath, 'web', 'index.html')).create();
var packages = {

final dependencies = _dependencyVersions({
...supportedBasicDartPackages,
...supportedFlutterPackages,
if (firebaseStyle != FirebaseStyle.none) ...coreFirebasePackages,
if (firebaseStyle == FirebaseStyle.flutterFire)
...registerableFirebasePackages,
};
final dependencies = _dependencyVersions(packages);
});
File(path.join(projectPath, 'pubspec.yaml'))
.writeAsStringSync(createPubspec(
includeFlutterWeb: true,
Expand All @@ -121,30 +106,6 @@ ${_sdk.experiments.map((experiment) => ' - $experiment').join('\n')}
path.join(projectPath, 'lib', 'generated_plugin_registrant.dart'));
}

if (firebaseStyle != FirebaseStyle.none) {
// `flutter packages get` has been run with a _subset_ of all supported
// Firebase packages, the ones that don't require a Firebase app to be
// configured in JavaScript, before executing Dart. Now add the full set of
// supported Firebase packages. This workaround is a very fragile hack.
packages = {
...supportedBasicDartPackages,
...supportedFlutterPackages,
...firebasePackages,
};
final dependencies = _dependencyVersions(packages);
File(path.join(projectPath, 'pubspec.yaml'))
.writeAsStringSync(createPubspec(
includeFlutterWeb: true,
dartLanguageVersion: _dartLanguageVersion,
dependencies: dependencies,
));

final exitCode = await runFlutterPubGet(_sdk, projectPath, log: _log);
if (exitCode != 0) {
throw StateError('flutter pub get failed ($exitCode)');
}
}

var contents = '''
include: package:flutter_lints/flutter.yaml
Expand Down Expand Up @@ -180,31 +141,12 @@ ${_sdk.experiments.map((experiment) => ' - $experiment').join('\n')}
Map<String, String> _dependencyVersions(Iterable<String> packages) {
final allVersions =
parsePubDependenciesFile(dependenciesFile: _dependenciesFile);
final result = {
return {
for (final package in packages) package: allVersions[package] ?? 'any',
};

// Overwrite with important constraints.
for (final entry in overrideVersionConstraints().entries) {
if (result.containsKey(entry.key)) {
result[entry.key] = entry.value;
}
}

return result;
}
}

/// A mapping of version constraints for certain packages.
Map<String, String> overrideVersionConstraints() {
// Ensure that pub version solving keeps these at sane minimum versions.
return {
'firebase_auth': '^4.2.0',
'firebase_auth_web': '^5.2.0',
'cloud_firestore_platform_interface': '^5.10.0',
};
}

/// Parses [dependenciesFile] as a JSON Map of Strings.
Map<String, String> parsePubDependenciesFile({required File dependenciesFile}) {
final packageVersions =
Expand Down Expand Up @@ -261,11 +203,3 @@ String get _pubCachePath {
Directory(pubCachePath).createSync();
return pubCachePath;
}

enum FirebaseStyle {
/// Indicates that no Firebase is used.
none,

/// Indicates that the "pure Dart" Flutterfire packages are used.
flutterFire,
}
71 changes: 16 additions & 55 deletions pkgs/dart_services/lib/src/project_templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ class ProjectTemplates {
ProjectTemplates._({
required this.dartPath,
required this.flutterPath,
required this.firebasePath,
required this.firebaseDeprecatedPath,
required this.summaryFilePath,
});

Expand All @@ -27,25 +25,16 @@ class ProjectTemplates {
return ProjectTemplates._(
dartPath: path.join(basePath, 'dart_project'),
flutterPath: path.join(basePath, 'flutter_project'),
firebasePath: path.join(basePath, 'firebase_project'),
firebaseDeprecatedPath:
path.join(basePath, 'firebase_deprecated_project'),
summaryFilePath: summaryFilePath,
);
}

/// The path to the plain Dart project template path.
final String dartPath;

/// The path to the Flutter (without Firebase) project template path.
/// The path to the Flutter project template path.
final String flutterPath;

/// The path to the Firebase (with Flutter) project template path.
final String firebasePath;

/// The path to the deprecated Firebase (with Flutter) project template path.
final String firebaseDeprecatedPath;

/// The path to summary files.
final String summaryFilePath;

Expand All @@ -55,40 +44,10 @@ class ProjectTemplates {
path.join(Directory.current.path, 'project_templates');
}

/// The set of Firebase packages which are used in both deprecated Firebase
/// projects and "pure Dart" Flutterfire projects.
const Set<String> coreFirebasePackages = {
'firebase_core',
};

/// The set of Firebase packages which can be registered in the generated
/// registrant file. Theoretically this should be _all_ plugins, but there
/// are bugs. See https://github.com/dart-lang/dart-pad/issues/2033 and
/// https://github.com/FirebaseExtended/flutterfire/issues/3962.
const Set<String> registerableFirebasePackages = {
'cloud_firestore',
'firebase_auth',
};

/// The set of Firebase packages which indicate that Firebase is being used.
const Set<String> firebasePackages = {
...coreFirebasePackages,
...registerableFirebasePackages,
};

/// The set of supported Flutter-oriented packages.
const Set<String> supportedFlutterPackages = {
'animations',
'creator',
'firebase_analytics',
'firebase_database',
'firebase_messaging',
'firebase_storage',
'flame',
'flame_fire_atlas',
'flame_forge2d',
'flame_splash_screen',
'flame_tiled',
'flutter_adaptive_scaffold',
'flutter_bloc',
'flutter_hooks',
Expand All @@ -111,7 +70,6 @@ const Set<String> _packagesIndicatingFlutter = {
'flutter',
'flutter_test',
...supportedFlutterPackages,
...firebasePackages,
};

/// The set of basic Dart (non-Flutter) packages which can be directly imported
Expand Down Expand Up @@ -192,8 +150,8 @@ bool isDeprecatedCoreWebLibrary(String libraryName) =>
bool usesFlutterWeb(Iterable<ImportDirective> imports) =>
imports.any((import) => isFlutterWebImport(import.uri.stringValue));

/// Whether the [importString] represents an import
/// that denotes use of Flutter Web.
/// Whether the [importString] represents an import that denotes use of Flutter
/// Web.
@visibleForTesting
bool isFlutterWebImport(String? importString) {
if (importString == null) return false;
Expand All @@ -204,18 +162,21 @@ bool isFlutterWebImport(String? importString) {
_packagesIndicatingFlutter.contains(packageName);
}

/// Returns whether [imports] denote use of Firebase.
bool usesFirebase(Iterable<ImportDirective> imports) =>
imports.any((import) => isFirebaseImport(import.uri.stringValue));
/// The core set of Firebase packages.
const Set<String> firebasePackages = {
'cloud_firestore',
'firebase_auth',
'firebase_core',
'flame',
};

/// Whether the [importString] represents an import
/// that denotes use of a Firebase package.
@visibleForTesting
bool isFirebaseImport(String? importString) {
if (importString == null) return false;
bool isFirebasePackage(String packageName) {
if (firebasePackages.contains(packageName)) return true;

final packageName = _packageNameFromPackageUri(importString);
return packageName != null && firebasePackages.contains(packageName);
if (packageName.startsWith('firebase_')) return true;
if (packageName.startsWith('flame_')) return true;

return false;
}

/// If [uriString] represents a 'package:' URI, then returns the package name;
Expand Down
2 changes: 1 addition & 1 deletion pkgs/dart_services/lib/src/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const _flutterPackages = [
/// This is expensive to calculate; they require reading from disk.
/// None of them changes during execution.
final Map<String, String> _packageVersions = packageVersionsFromPubspecLock(
project.ProjectTemplates.projectTemplates.firebasePath);
project.ProjectTemplates.projectTemplates.flutterPath);

/// Returns a mapping of Pub package name to package version.
Map<String, String> getPackageVersions() => _packageVersions;
Expand Down
Loading

0 comments on commit c60b360

Please sign in to comment.