This repository was archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathproject.dart
226 lines (195 loc) · 6.59 KB
/
project.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:path/path.dart' as path;
/// Sets of project template directory paths.
class ProjectTemplates {
ProjectTemplates._({
required this.dartPath,
required this.flutterPath,
required this.firebasePath,
required this.firebaseDeprecatedPath,
required this.summaryFilePath,
});
factory ProjectTemplates({required bool nullSafety}) {
final basePath = _baseTemplateProject(nullSafety: nullSafety);
final summaryFilePath = path.join(
'artifacts',
nullSafety ? 'null-safe' : 'null-unsafe',
'flutter_web.dill',
);
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.
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;
static ProjectTemplates nullUnsafe = ProjectTemplates(nullSafety: false);
static ProjectTemplates nullSafe = ProjectTemplates(nullSafety: true);
static String _baseTemplateProject({required bool nullSafety}) => path.join(
Directory.current.path,
'project_templates',
nullSafety ? 'null-safe' : 'null-unsafe',
);
}
/// 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 deprecated Firebase packages.
const Set<String> deprecatedFirebasePackages = {
'firebase',
};
/// 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',
'cloud_functions',
'firebase_auth',
'firebase_database',
'firebase_storage',
};
/// The set of Firebase packages which indicate that Firebase is being used.
const Set<String> firebasePackages = {
'firebase_analytics',
'firebase_messaging',
...coreFirebasePackages,
...registerableFirebasePackages,
...deprecatedFirebasePackages,
};
/// The set of packages which indicate that Flutter Web is being used.
const Set<String> supportedFlutterPackages = {
'flutter_bloc',
'flutter_hooks',
'flutter_lints',
'flutter_riverpod',
'hooks_riverpod',
'url_launcher',
};
/// The set of packages which indicate that Flutter Web is being used.
const Set<String> _packagesIndicatingFlutter = {
'flutter',
'flutter_test',
...supportedFlutterPackages,
...firebasePackages,
};
/// The set of basic Dart (non-Flutter) packages which can be directly imported
/// into a script.
const Set<String> supportedBasicDartPackages = {
'bloc',
'characters',
'collection',
'google_fonts',
'http',
'intl',
'js',
'lints',
'meta',
'path',
'pedantic',
'provider',
'riverpod',
'vector_math',
};
/// A set of all allowed `dart:` imports. Currently includes non-VM libraries
/// listed as the [doc](https://api.dart.dev/stable/index.html) categories.
const Set<String> _allowedDartImports = {
'dart:async',
'dart:collection',
'dart:convert',
'dart:core',
'dart:developer',
'dart:math',
'dart:typed_data',
'dart:html',
'dart:indexed_db',
'dart:js',
'dart:js_util',
'dart:svg',
'dart:web_audio',
'dart:web_gl',
'dart:web_sql',
'dart:ui',
};
/// Returns whether [imports] denote use of Flutter Web.
bool usesFlutterWeb(Iterable<ImportDirective> imports) {
return imports.any((import) {
final uriString = import.uri.stringValue;
if (uriString == null) return false;
if (uriString == 'dart:ui') return true;
final packageName = _packageNameFromPackageUri(uriString);
return packageName != null &&
_packagesIndicatingFlutter.contains(packageName);
});
}
/// Returns whether [imports] denote use of deprecated Firebase.
bool usesDeprecatedFirebase(Iterable<ImportDirective> imports) {
return imports.any((import) {
final uriString = import.uri.stringValue;
if (uriString == null) return false;
final packageName = _packageNameFromPackageUri(uriString);
return packageName != null &&
deprecatedFirebasePackages.contains(packageName);
});
}
/// Returns whether [imports] denote use of Firebase.
bool usesFirebase(Iterable<ImportDirective> imports) {
return imports.any((import) {
final uriString = import.uri.stringValue;
if (uriString == null) return false;
final packageName = _packageNameFromPackageUri(uriString);
return packageName != null && firebasePackages.contains(packageName);
});
}
/// If [uriString] represents a 'package:' URI, then returns the package name;
/// otherwise `null`.
String? _packageNameFromPackageUri(String uriString) {
final uri = Uri.tryParse(uriString);
if (uri == null) return null;
if (uri.scheme != 'package') return null;
if (uri.pathSegments.isEmpty) return null;
return uri.pathSegments.first;
}
List<ImportDirective> getUnsupportedImports(List<ImportDirective> imports) {
return imports.where((import) {
final uriString = import.uri.stringValue;
if (uriString == null) {
return false;
}
// All non-VM 'dart:' imports are ok.
if (uriString.startsWith('dart:')) {
return !_allowedDartImports.contains(uriString);
}
final uri = Uri.tryParse(uriString);
if (uri == null) return false;
// We allow a specific set of package imports.
if (uri.scheme == 'package') {
if (uri.pathSegments.isEmpty) return true;
final package = uri.pathSegments.first;
return !isSupportedPackage(package);
}
// Don't allow file imports.
return true;
}).toList();
}
bool isSupportedPackage(String package) =>
_packagesIndicatingFlutter.contains(package) ||
supportedBasicDartPackages.contains(package);