Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support both gradle and gradle.kts file formats #305 #349

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ import '../common/utils.dart';
import '../flutter_app.dart';
import 'firebase_options.dart';

// https://regex101.com/r/Lj93lx/1
// https://regex101.com/r/Lj93lx/2
final _androidBuildGradleRegex = RegExp(
r'dependencies\s*\{',
r'dependencies\s*[\{]',
multiLine: true,
);
// https://regex101.com/r/OZnO1j/1
// https://regex101.com/r/OZnO1j/2
final _androidAppBuildGradleRegex = RegExp(
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.android\.application['"]{1})|(^[\s]*?id[\s]+["']com\.android\.application["']))''',
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.android\.application['"]{1})|(^[\s]*?id[\s]+["']com\.android\.application["'])|plugins\s*\{\s*id\(['"]{1}com\.android\.application['"]{1}\))''',
multiLine: true,
);
// https://regex101.com/r/ndlYVL/1
// https://regex101.com/r/ndlYVL/2
final _androidBuildGradleGoogleServicesRegex = RegExp(
r'''((?<indentation>^[\s]*?)classpath\s?['"]{1}com\.google\.gms:google-services:.*?['"]{1}\s*?$)''',
r'''((?<indentation>^[\s]*?)(?:classpath\s?['"]{1}com\.google\.gms:google-services:.*?['"]{1}\s*?$|id\(['"]{1}com\.google\.gms\.google-services['"]{1}\)))''',
multiLine: true,
);
// https://regex101.com/r/pP1k6i/1
// https://regex101.com/r/pP1k6i/2
final _androidAppBuildGradleGoogleServicesRegex = RegExp(
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.google\.gms\.google-services['"]{1})|(^[\s]*?id[\s]+['"]com\.google\.gms\.google-services['"]))''',
r'''(?:(^[\s]*?apply[\s]+plugin\:[\s]+['"]{1}com\.google\.gms\.google-services['"]{1})|(^[\s]*?id[\s]+['"]com\.google\.gms\.google-services['"])|plugins\s*\{\s*id\(['"]{1}com\.google\.gms\.google-services['"]{1}\))''',
multiLine: true,
);

Expand Down Expand Up @@ -57,10 +57,11 @@ String _applyGradleSettingsDependency(
String version, {
bool flutterfireComments = false,
}) {
if (flutterfireComments) {
return '\n $_flutterFireConfigCommentStart\n id "$dependency" version "$version" apply false\n $_flutterFireConfigCommentEnd';
}
return '\n id "$dependency" version "$version" apply false';
final kotlinDslContent = flutterfireComments
? '\n $_flutterFireConfigCommentStart\n id("$dependency") version "$version" apply false\n $_flutterFireConfigCommentEnd'
: '\n id("$dependency") version "$version" apply false';

return kotlinDslContent;
}

enum BuildGradleConfiguration {
Expand Down Expand Up @@ -182,28 +183,36 @@ Future<void> gradleContentUpdates(
final androidBuildGradleFile = File(
path.join(
flutterApp.androidDirectory.path,
'build.gradle',
'build.gradle.kts',
),
);
).existsSync()
? File(path.join(flutterApp.androidDirectory.path, 'build.gradle.kts'))
: File(path.join(flutterApp.androidDirectory.path, 'build.gradle'));

final androidBuildGradleFileContents =
androidBuildGradleFile.readAsStringSync();

final androidAppBuildGradleFile = File(
path.join(
flutterApp.androidDirectory.path,
'app',
'build.gradle',
'build.gradle.kts',
),
);
).existsSync()
? File(path.join(flutterApp.androidDirectory.path, 'app', 'build.gradle.kts'))
: File(path.join(flutterApp.androidDirectory.path, 'app', 'build.gradle'));

final androidAppBuildGradleFileContents =
androidAppBuildGradleFile.readAsStringSync();

final androidGradleSettingsFile = File(
path.join(
flutterApp.androidDirectory.path,
'settings.gradle',
'settings.gradle.kts',
),
);
).existsSync()
? File(path.join(flutterApp.androidDirectory.path, 'settings.gradle.kts'))
: File(path.join(flutterApp.androidDirectory.path, 'settings.gradle'));

final androidGradleSettingsFileContents =
androidGradleSettingsFile.readAsStringSync();
Expand Down
19 changes: 16 additions & 3 deletions packages/flutterfire_cli/lib/src/flutter_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,22 @@ class FlutterApp {
Directory(package.path),
),
);
if (appGradleFile.existsSync()) {
final fileContents = appGradleFile.readAsStringSync();
// Captures old and new method for setting applicationId in app/build.gradle file:

final appGradleKtsFile = File(
'${androidAppBuildGradlePathForAppDirectory(
Directory(package.path),
)}.kts',
);

String? fileContents;
if (appGradleKtsFile.existsSync()) {
fileContents = appGradleKtsFile.readAsStringSync();
} else if (appGradleFile.existsSync()) {
fileContents = appGradleFile.readAsStringSync();
}

if (fileContents != null) {
// Captures old and new method for setting applicationId in both app/build.gradle and app/build.gradle.kt file:
// https://regex101.com/r/d9i4G6/1
final appIdRegex = RegExp(
r'''applicationId\s*(?:=)?\s*['"](?<applicationId>([A-Za-z]{1}[A-Za-z\d_]*\.)+[A-Za-z][A-Za-z\d_]*)['"]''',
Expand Down
Loading