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

Use domain object container to avoid afterEvaluate. #9

Merged
merged 3 commits into from
Apr 25, 2022

Conversation

autonomousapps
Copy link
Contributor

This draft PR is based on the test PR I made earlier (so you can ignore the first commit). I'm sharing this in its draft state to get your feedback on the general idea. In the end, the ultimate idea would be to remove the mutable list entirely from the extension.

One thing worth noting is that there's a subtle bug in the current implementation of the plugin. You use this function in your task configuration:

requirePluginConfig(target, extension)

That will fail if extension.configurations is empty. You are implicitly assuming that the dependencyGuard DSL block will be evaluated before tasks get configured. I can imagine a scenario that triggers task configuration early, causing a failure even though the build script is correctly configured. It would be a complex scenario, but people do crazy things in their builds and plugins. I think you'd be better off delaying that check to task execution.

/**
* Configuration for [DependencyGuardPlugin]
*/
public class DependencyGuardConfiguration(
public open class DependencyGuardConfiguration @Inject constructor(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changes here were necessary to

  1. Let me use ObjectFactory to instantiate this class
  2. Let me add instances of this class to a domain object container.

baselineTask: TaskProvider<DependencyGuardListTask>,
guardTask: TaskProvider<DependencyGuardListTask>
) {
extension.container.all {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The all callback is the magic that makes it work. It triggers the action on each item in the container, and added to the container.

val c = objects.newInstance(DependencyGuardConfiguration::class.java, name).apply {
config.execute(this)
}
container.add(c)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't use container.create(name, config) for reasons I indicated in slack.

@autonomousapps autonomousapps force-pushed the trobalik.domainobjects branch from 2121f83 to 0dc6296 Compare April 25, 2022 17:27
/**
* Whether to allow a dependency.
*
* TODO: not sure how to model this as a task input. May not matter since the task that uses it
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note this TODO.

@@ -3,7 +3,7 @@ package com.dropbox.gradle.plugins.dependencyguard.internal
import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPlugin
import org.gradle.api.Project

internal fun Project.isRootProject(): Boolean = (path == rootProject.path)
internal fun Project.isRootProject(): Boolean = this == rootProject
Copy link
Contributor Author

Choose a reason for hiding this comment

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

simpler check


internal open class DependencyGuardListTask : DefaultTask() {
public abstract class DependencyGuardListTask : DefaultTask() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All tasks should be abstract by default (preferable to open, as it allows abstract/managed property inputs, see below). And I don't see a point in marking a task as internal since Gradle extends it at runtime anyway, ignoring the visibility marker entirely.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the apiCheck binary compatibility validator might care?

@@ -56,9 +56,28 @@ internal open class DependencyGuardListTask : DefaultTask() {
)
}

@get:Input
public abstract val shouldBaseline: Property<Boolean>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are called "managed properties" and Gradle instantiates them for us.

@Suppress("NestedBlockDepth")
@TaskAction
internal fun execute() {
requirePluginConfig(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This check now happens at the start of task execution instead of in configuration. This is the main real behavior change in the PR. The rest is basically refactoring.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sounds good to me.

@autonomousapps autonomousapps force-pushed the trobalik.domainobjects branch from 3d4a19f to 118ce9d Compare April 25, 2022 18:59
@autonomousapps autonomousapps changed the title WIP. Use domain object container to avoid afterEvaluate. Use domain object container to avoid afterEvaluate. Apr 25, 2022
@autonomousapps
Copy link
Contributor Author

It's failing the API check again (which is expected).

@autonomousapps autonomousapps marked this pull request as ready for review April 25, 2022 19:28
Copy link
Collaborator

@handstandsam handstandsam left a comment

Choose a reason for hiding this comment

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

Thanks!

@autonomousapps autonomousapps force-pushed the trobalik.domainobjects branch from 118ce9d to a4ade1f Compare April 25, 2022 19:40
@handstandsam handstandsam merged commit ce24348 into dropbox:main Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants