Skip to content

Commit

Permalink
Readability change to flutter.groovy, align on null assignment, red…
Browse files Browse the repository at this point in the history
…uce unused scope for some methods, apply static where possible (flutter#157471)

removed public modifier from this methods :`getVersionCode` , `getVersionName`.
add static to :`pluginSupportsAndroidPlatform` ,`buildGradleFile`,`settingsGradleFile`
`getCompileSdkFromProject` ,`getAssembleTask` 
refactor `==null` usage  to `:?` to unify the usage 

see flutter#147122 for context
  • Loading branch information
AbdeMohlbi authored Nov 5, 2024
1 parent 509cb43 commit ecc2437
Showing 1 changed file with 16 additions and 35 deletions.
51 changes: 16 additions & 35 deletions packages/flutter_tools/gradle/src/main/groovy/flutter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FlutterExtension {
public String flutterVersionName = null

/** Returns flutterVersionCode as an integer with error handling. */
public Integer getVersionCode() {
Integer getVersionCode() {
if (flutterVersionCode == null) {
throw new GradleException("flutterVersionCode must not be null.")
}
Expand All @@ -92,7 +92,7 @@ class FlutterExtension {
}

/** Returns flutterVersionName with error handling. */
public String getVersionName() {
String getVersionName() {
if (flutterVersionName == null) {
throw new GradleException("flutterVersionName must not be null.")
}
Expand Down Expand Up @@ -271,17 +271,11 @@ class FlutterPlugin implements Plugin<Project> {
}
}

Object flutterVersionCode = localProperties.getProperty("flutter.versionCode")
if (flutterVersionCode == null) {
flutterVersionCode = "1"
}
extension.flutterVersionCode = flutterVersionCode
String flutterVersionCode = localProperties.getProperty("flutter.versionCode")
extension.flutterVersionCode = flutterVersionCode ?: "1"

Object flutterVersionName = localProperties.getProperty("flutter.versionName")
if (flutterVersionName == null) {
flutterVersionName = "1.0"
}
extension.flutterVersionName = flutterVersionName
String flutterVersionName = localProperties.getProperty("flutter.versionName")
extension.flutterVersionName = flutterVersionName ?: "1.0"

this.addFlutterTasks(project)

Expand Down Expand Up @@ -717,7 +711,7 @@ class FlutterPlugin implements Plugin<Project> {
* Returns `true` if the given project is a plugin project having an `android` directory
* containing a `build.gradle` or `build.gradle.kts` file.
*/
private Boolean pluginSupportsAndroidPlatform(Project project) {
private static Boolean pluginSupportsAndroidPlatform(Project project) {
File buildGradle = new File(project.projectDir.parentFile, "android" + File.separator + "build.gradle")
File buildGradleKts = new File(project.projectDir.parentFile, "android" + File.separator + "build.gradle.kts")
return buildGradle.exists() || buildGradleKts.exists()
Expand All @@ -728,7 +722,7 @@ class FlutterPlugin implements Plugin<Project> {
* Kotlin variants exist, then Groovy (build.gradle) is preferred over
* Kotlin (build.gradle.kts). This is the same behavior as Gradle 8.5.
*/
private File buildGradleFile(Project project) {
private static File buildGradleFile(Project project) {
File buildGradle = new File(project.projectDir.parentFile, "app" + File.separator + "build.gradle")
File buildGradleKts = new File(project.projectDir.parentFile, "app" + File.separator + "build.gradle.kts")
if (buildGradle.exists() && buildGradleKts.exists()) {
Expand All @@ -746,7 +740,7 @@ class FlutterPlugin implements Plugin<Project> {
* Kotlin variants exist, then Groovy (settings.gradle) is preferred over
* Kotlin (settings.gradle.kts). This is the same behavior as Gradle 8.5.
*/
private File settingsGradleFile(Project project) {
private static File settingsGradleFile(Project project) {
File settingsGradle = new File(project.projectDir.parentFile, "settings.gradle")
File settingsGradleKts = new File(project.projectDir.parentFile, "settings.gradle.kts")
if (settingsGradle.exists() && settingsGradleKts.exists()) {
Expand Down Expand Up @@ -929,7 +923,7 @@ class FlutterPlugin implements Plugin<Project> {
* Returns the portion of the compileSdkVersion string that corresponds to either the numeric
* or string version.
*/
private String getCompileSdkFromProject(Project gradleProject) {
private static String getCompileSdkFromProject(Project gradleProject) {
return gradleProject.android.compileSdkVersion.substring(8)
}

Expand Down Expand Up @@ -997,17 +991,9 @@ class FlutterPlugin implements Plugin<Project> {
if (localProperties == null) {
localProperties = readPropertiesIfExist(new File(project.projectDir.parentFile, "local.properties"))
}
String result
if (project.hasProperty(name)) {
result = project.property(name)
}
if (result == null) {
result = localProperties.getProperty(name)
}
if (result == null) {
result = defaultValue
}
return result
String result = project.hasProperty(name) ? project.property(name) : null
result = result ?: localProperties?.getProperty(name)
return result ?: defaultValue
}

private List<String> getTargetPlatforms() {
Expand Down Expand Up @@ -1071,10 +1057,7 @@ class FlutterPlugin implements Plugin<Project> {
* Gets the target file. This is typically `lib/main.dart`.
*/
private String getFlutterTarget() {
String target = project.flutter.target
if (target == null) {
target = "lib/main.dart"
}
String target = project.flutter.target ?: "lib/main.dart"
final String propTarget = "target"
if (project.hasProperty(propTarget)) {
target = project.property(propTarget)
Expand Down Expand Up @@ -1135,7 +1118,7 @@ class FlutterPlugin implements Plugin<Project> {
return false
}

private Task getAssembleTask(variant) {
private static Task getAssembleTask(variant) {
// `assemble` became `assembleProvider` in AGP 3.3.0.
return variant.hasProperty("assembleProvider") ? variant.assembleProvider.get() : variant.assemble
}
Expand Down Expand Up @@ -1476,9 +1459,7 @@ class FlutterPlugin implements Plugin<Project> {
if (buildModeFor(appProjectVariant.buildType) != variantBuildMode) {
return
}
if (copyFlutterAssetsTask == null) {
copyFlutterAssetsTask = addFlutterDeps(libraryVariant)
}
copyFlutterAssetsTask = copyFlutterAssetsTask ?: addFlutterDeps(libraryVariant)
Task mergeAssets = project
.tasks
.findByPath(":${hostAppProjectName}:merge${appProjectVariant.name.capitalize()}Assets")
Expand Down

0 comments on commit ecc2437

Please sign in to comment.