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

Convert BaseFlutterTask From Groovy to Kotlin #163148

Merged
merged 33 commits into from
Mar 14, 2025

Conversation

jesswrd
Copy link
Contributor

@jesswrd jesswrd commented Feb 12, 2025

Converted BaseFlutterTask from Groovy to Kotlin.

Note: Since it looks like Gradle prevents us from instantiating and using an object that extends abstract class BaseFlutterTask, we must use principles of Test Driven Development (TDD) in order to unit test BaseFlutterTask. BaseFlutterTaskHelper was created solely for unit testing to simulate the behavior of all functions in the original BaseFlutterTask.

Fixes #162111

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels. labels Feb 12, 2025
@jesswrd jesswrd force-pushed the i162111-convert-BaseFlutterTask branch from 6d5600d to 8eb0bbf Compare February 14, 2025 19:35
@jesswrd jesswrd force-pushed the i162111-convert-BaseFlutterTask branch from 6c76b81 to 4902ed0 Compare February 28, 2025 00:05
@jesswrd jesswrd force-pushed the i162111-convert-BaseFlutterTask branch from 4902ed0 to 79b6933 Compare February 28, 2025 22:29
@jesswrd jesswrd changed the title [WIP] Convert BaseFlutterTask From Groovy to Kotlin Convert BaseFlutterTask From Groovy to Kotlin Mar 4, 2025
@jesswrd jesswrd marked this pull request as ready for review March 4, 2025 19:53
fun buildBundle() {
val helper = BaseFlutterTaskHelper(baseFlutterTask = this)
helper.checkPreConditions()
intermediateDir!!.mkdirs()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont like having a nullable intermediateDir then using !!. I thought maybe checkPreConditions would ensure that intermediateDir was not null but that does not look like the case.

I think the right thing to do here is to see if we can make intermediateDir non null and if we cant then handle null on this line. In BaseFlutterTaskHelper.getDependenciesFiles we treat the value as non null.

Copy link
Member

@gmackall gmackall Mar 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was done because the original code behaved this way i.e.,

  1. intermediateDir took a Java File, and so was nullable and
  2. we would throw a NPE on the old line 1642 of flutter.groovy, if intermediateDir was null.

I know we had initially spoken about not making behavior changes in the conversion. Do you think we should amend that for the case of null pointer exceptions? I think that would be reasonable, but we should be explicit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmackall talked with some members of the flutter android team and the decision was made to file an issue to track fixing this issue and keeping the code as it was before.
There is a balance between fixing issues as we see them and introducing new behavior and the goal of this project (language conversion + unit testing) is to minimize (to the point of zero) the introduction of new behavior if possible.

Keeping an NPE we know about hurts but we are following our own principals in this project. After the conversion is done we do intend to fix many of the issues we have found.

private val extraGenSnapshotOptionsTest = "--debugger"
private val splitDebugInfoTest = "/path/to/build/debug_info_directory"
private val codeSizeDirectoryTest = "/path/to/build/code_size_directory"
private val minSDKVersionTest = 21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After @ash-google lands his min sdk check pr can this test use that value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 86 to 115
baseFlutterTask.splitDebugInfo?.let {
args("-dSplitDebugInfo=${baseFlutterTask.splitDebugInfo}")
}
if (baseFlutterTask.treeShakeIcons == true) {
args("-dTreeShakeIcons=true")
}
if (baseFlutterTask.dartObfuscation == true) {
args("-dDartObfuscation=true")
}
baseFlutterTask.dartDefines?.let {
args("--DartDefines=${baseFlutterTask.dartDefines}")
}
baseFlutterTask.bundleSkSLPath?.let {
args("-dBundleSkSLPath=${baseFlutterTask.bundleSkSLPath}")
}
baseFlutterTask.codeSizeDirectory?.let {
args("-dCodeSizeDirectory=${baseFlutterTask.codeSizeDirectory}")
}
baseFlutterTask.flavor?.let {
args("-dFlavor=${baseFlutterTask.flavor}")
}
baseFlutterTask.extraGenSnapshotOptions?.let {
args("--ExtraGenSnapshotOptions=${baseFlutterTask.extraGenSnapshotOptions}")
}
baseFlutterTask.frontendServerStarterPath?.let {
args("-dFrontendServerStarterPath=${baseFlutterTask.frontendServerStarterPath}")
}
baseFlutterTask.extraFrontEndOptions?.let {
args("--ExtraFrontEndOptions=${baseFlutterTask.extraFrontEndOptions}")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're not using the parameter from .let, let's just replace these with if null checks instead so it's a little more readable.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead why not use the parameter from let?

baseFlutterTask.extraFrontEndOptions?.let {
    args("--ExtraFrontEndOptions=$it")
}

vs

if (baseFlutterTask.extraFrontEndOptions != null)  {
    args("--ExtraFrontEndOptions=${baseFlutterTask.extraFrontEndOptions}")
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. Used the parameter from let.

import kotlin.test.assertTrue

class BaseFlutterTaskHelperTest {
object BaseFlutterTaskPropertiesTest {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best practice for Kotlin when sharing constants, values, and/or variables across files.

var flavor: String? = null

/**
* Gets the dependency file(s) based on the path from the intermediate directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you know can you expand on which dependency files or what they are a dependency of.
This dart doc can also just point to the helper if tha tis where you want to keep the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

every { mockExecSpec.args("--local-engine-src-path", localEngineSrcPath) } returns mockExecSpec

val localEngineHost = baseFlutterTask.localEngineHost
every { mockExecSpec.args("--local-engine-host", localEngineHost) } returns mockExecSpec
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can collapse many of these args mock calls to a single

every { mockExecSpec.args(any<String>(), any()) } returns execSpec

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@reidbaker
Copy link
Contributor

This is looking good. Super close to being able to land.

Something I had a comment for but removed was making a utility class for mocking that let you pass in values. I think because you have really a test for null values and a test for non null it is probably not worth creating a utility class.

jesswrd added 2 commits March 11, 2025 16:13
addressed comments

changed back to list

debugging windows integration test

ktlint formatting matches CI

delete commas and added platform-specific path separators

update comment

updated comment and removed debugging commands

cut down the mocked function calls
@jesswrd jesswrd force-pushed the i162111-convert-BaseFlutterTask branch from a41a362 to 1b9b454 Compare March 11, 2025 23:26
@jesswrd jesswrd requested a review from reidbaker March 12, 2025 20:34
@jesswrd jesswrd added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 14, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Mar 14, 2025
Merged via the queue into flutter:master with commit 58a2116 Mar 14, 2025
133 of 134 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Mar 14, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 15, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 16, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 17, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FGP Kotlin conversion] Convert BaseFlutterTask
4 participants