Skip to content

Commit

Permalink
fix: support both gradle and gradle.kts file formats #305
Browse files Browse the repository at this point in the history
  • Loading branch information
yyyng2 committed Dec 29, 2024
1 parent eb5c413 commit f163973
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
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

0 comments on commit f163973

Please sign in to comment.