-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
Support a primary entry point for bundles. #666
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/functionalTest/groovy/com/autonomousapps/jvm/BundleSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.autonomousapps.jvm | ||
|
||
import com.autonomousapps.jvm.projects.BundleProject | ||
|
||
import static com.autonomousapps.utils.Runner.build | ||
import static com.google.common.truth.Truth.assertThat | ||
|
||
final class BundleSpec extends AbstractJvmSpec { | ||
|
||
def "can define entry point to bundle (#gradleVersion)"() { | ||
given: | ||
def project = new BundleProject() | ||
gradleProject = project.gradleProject | ||
|
||
when: | ||
build(gradleVersion, gradleProject.rootDir, 'buildHealth') | ||
|
||
then: | ||
assertThat(project.actualProjectAdvice()).containsExactlyElementsIn(project.expectedProjectAdvice) | ||
|
||
where: | ||
gradleVersion << gradleVersions() | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
src/functionalTest/groovy/com/autonomousapps/jvm/projects/BundleProject.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package com.autonomousapps.jvm.projects | ||
|
||
import com.autonomousapps.AbstractProject | ||
import com.autonomousapps.kit.GradleProject | ||
import com.autonomousapps.kit.Plugin | ||
import com.autonomousapps.kit.Source | ||
import com.autonomousapps.kit.SourceType | ||
import com.autonomousapps.model.Advice | ||
import com.autonomousapps.model.ProjectAdvice | ||
|
||
import static com.autonomousapps.AdviceHelper.* | ||
import static com.autonomousapps.kit.Dependency.project | ||
|
||
final class BundleProject extends AbstractProject { | ||
|
||
final GradleProject gradleProject | ||
|
||
BundleProject() { | ||
this.gradleProject = build() | ||
} | ||
|
||
private GradleProject build() { | ||
def builder = newGradleProjectBuilder() | ||
builder.withRootProject { | ||
it.withBuildScript { bs -> | ||
bs.additions = """ | ||
dependencyAnalysis { | ||
dependencies { | ||
bundle('facade') { | ||
primary(':entry-point') | ||
includeDependency(':entry-point') | ||
includeDependency(':used') | ||
} | ||
} | ||
} | ||
""".stripIndent() | ||
} | ||
} | ||
// consumer -> unused -> entry-point -> used | ||
// consumer only uses :used. | ||
// :used and :entry-point are in a bundle | ||
// plugin should advise to add :entry-point and remove :unused. | ||
// it should _not_ advise to add :used. | ||
builder.withSubproject('consumer') { c -> | ||
c.sources = sourcesConsumer | ||
c.withBuildScript { bs -> | ||
bs.plugins = [Plugin.javaLibraryPlugin] | ||
bs.dependencies = [ | ||
project('implementation', ':unused') | ||
] | ||
} | ||
|
||
} | ||
builder.withSubproject('unused') { s -> | ||
s.sources = sourcesUnused | ||
s.withBuildScript { bs -> | ||
bs.plugins = [Plugin.javaLibraryPlugin] | ||
bs.dependencies = [ | ||
project('api', ':entry-point') | ||
] | ||
} | ||
} | ||
builder.withSubproject('entry-point') { s -> | ||
s.sources = sourcesEntryPoint | ||
s.withBuildScript { bs -> | ||
bs.plugins = [Plugin.javaLibraryPlugin] | ||
bs.dependencies = [ | ||
project('api', ':used') | ||
] | ||
} | ||
} | ||
builder.withSubproject('used') { s -> | ||
s.sources = sourcesUsed | ||
s.withBuildScript { bs -> | ||
bs.plugins = [Plugin.javaLibraryPlugin] | ||
} | ||
} | ||
|
||
def project = builder.build() | ||
project.writer().write() | ||
return project | ||
} | ||
|
||
private sourcesConsumer = [ | ||
new Source( | ||
SourceType.JAVA, "Consumer", "com/example/consumer", | ||
"""\ | ||
package com.example.consumer; | ||
|
||
import com.example.used.Used; | ||
|
||
public class Consumer { | ||
private Used used; | ||
} | ||
""".stripIndent() | ||
) | ||
] | ||
|
||
private sourcesUnused = [ | ||
new Source( | ||
SourceType.JAVA, "Unused", "com/example/unused", | ||
"""\ | ||
package com.example.unused; | ||
|
||
import com.example.entry.EntryPoint; | ||
|
||
public abstract class Unused { | ||
public abstract EntryPoint getEntryPoint(); | ||
} | ||
""".stripIndent() | ||
) | ||
] | ||
|
||
private sourcesEntryPoint = [ | ||
new Source( | ||
SourceType.JAVA, "EntryPoint", "com/example/entry", | ||
"""\ | ||
package com.example.entry; | ||
|
||
import com.example.used.Used; | ||
|
||
public abstract class EntryPoint { | ||
public abstract Used getUsed(); | ||
} | ||
""".stripIndent() | ||
) | ||
] | ||
|
||
private sourcesUsed = [ | ||
new Source( | ||
SourceType.JAVA, "Used", "com/example/used", | ||
"""\ | ||
package com.example.used; | ||
|
||
public class Used {} | ||
""".stripIndent() | ||
) | ||
] | ||
|
||
Set<ProjectAdvice> actualProjectAdvice() { | ||
return actualProjectAdvice(gradleProject) | ||
} | ||
|
||
private final Set<Advice> consumerAdvice = [ | ||
Advice.ofAdd(projectCoordinates(':entry-point'), 'implementation'), | ||
Advice.ofRemove(projectCoordinates(':unused'), 'implementation') | ||
] | ||
|
||
final Set<ProjectAdvice> expectedProjectAdvice = [ | ||
projectAdviceForDependencies(':consumer', consumerAdvice), | ||
emptyProjectAdviceFor(':unused'), | ||
emptyProjectAdviceFor(':entry-point'), | ||
emptyProjectAdviceFor(':used') | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.autonomousapps.internal | ||
|
||
import com.autonomousapps.extension.DependenciesHandler.SerializableBundles | ||
import com.autonomousapps.graph.Graphs.children | ||
import com.autonomousapps.graph.Graphs.reachableNodes | ||
import com.autonomousapps.model.Advice | ||
import com.autonomousapps.model.Coordinates | ||
import com.autonomousapps.model.DependencyGraphView | ||
import com.autonomousapps.model.ProjectCoordinates | ||
import com.autonomousapps.model.declaration.Bucket | ||
import com.autonomousapps.model.intermediates.Usage | ||
|
||
/** | ||
* :proj | ||
* | | ||
* B -> unused, not declared, but top of graph (added by plugin) | ||
* | | ||
* C -> used as API, part of bundle with B. Should not be declared! | ||
*/ | ||
internal class Bundles(private val dependencyUsages: Map<Coordinates, Set<Usage>>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved from elsewhere for easier testing. |
||
|
||
// a sort of adjacency-list structure | ||
private val parentKeyedBundle = mutableMapOf<Coordinates, MutableSet<Coordinates>>() | ||
|
||
// link child/transitive node to parent node (which is directly adjacent to root project node) | ||
private val parentPointers = mutableMapOf<Coordinates, Coordinates>() | ||
|
||
// if a set of rules has a primary identifier, use this to map advice to it | ||
private val primaryPointers = mutableMapOf<Coordinates, Coordinates>() | ||
|
||
operator fun set(parentNode: Coordinates, childNode: Coordinates) { | ||
// nb: parents point to themselves as well. This is what lets DoubleDeclarationsSpec pass. | ||
parentKeyedBundle.merge(parentNode, mutableSetOf(parentNode, childNode)) { acc, inc -> | ||
acc.apply { addAll(inc) } | ||
} | ||
parentPointers.putIfAbsent(parentNode, parentNode) | ||
parentPointers.putIfAbsent(childNode, parentNode) | ||
} | ||
|
||
fun setPrimary(primary: Coordinates, subordinate: Coordinates) { | ||
primaryPointers.putIfAbsent(subordinate, primary) | ||
} | ||
|
||
fun hasParentInBundle(coordinates: Coordinates): Boolean = parentPointers[coordinates] != null | ||
|
||
fun primary(advice: Advice): Advice { | ||
check(advice.isAdd()) { "Must be add-advice" } | ||
|
||
return primaryPointers[advice.coordinates]?.let { primary -> | ||
advice.copy(coordinates = primary) | ||
} ?: advice | ||
} | ||
|
||
fun hasUsedChild(coordinates: Coordinates): Boolean { | ||
val children = parentKeyedBundle[coordinates] ?: return false | ||
return children.any { child -> | ||
dependencyUsages[child].orEmpty().any { it.bucket != Bucket.NONE } | ||
} | ||
} | ||
|
||
companion object { | ||
fun of( | ||
projectNode: ProjectCoordinates, | ||
dependencyGraph: Map<String, DependencyGraphView>, | ||
bundleRules: SerializableBundles, | ||
dependencyUsages: Map<Coordinates, Set<Usage>>, | ||
ignoreKtx: Boolean | ||
): Bundles { | ||
val bundles = Bundles(dependencyUsages) | ||
|
||
// Handle bundles with primary entry points | ||
bundleRules.primaries.forEach { (name, primaryId) -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some ugly duplication here, but it works ™️ |
||
val regexes = bundleRules.rules[name]!! | ||
dependencyGraph.forEach { (_, view) -> | ||
view.graph.reachableNodes(projectNode) | ||
.find { it.identifier == primaryId } | ||
?.let { primaryNode -> | ||
val reachableNodes = view.graph.reachableNodes(primaryNode) | ||
reachableNodes.filter { subordinateNode -> | ||
regexes.any { it.matches(subordinateNode.identifier) } | ||
}.forEach { subordinateNode -> | ||
bundles.setPrimary(primaryNode, subordinateNode) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Handle bundles that don't have a primary entry point | ||
dependencyGraph.forEach { (_, view) -> | ||
view.graph.children(projectNode).forEach { parentNode -> | ||
val rules = bundleRules.matchingBundles(parentNode) | ||
|
||
// handle user-supplied bundles | ||
if (rules.isNotEmpty()) { | ||
val reachableNodes = view.graph.reachableNodes(parentNode) | ||
rules.forEach { (_, regexes) -> | ||
reachableNodes.filter { childNode -> | ||
regexes.any { it.matches(childNode.identifier) } | ||
}.forEach { childNode -> | ||
bundles[parentNode] = childNode | ||
} | ||
} | ||
} | ||
|
||
// handle dynamic ktx bundles | ||
if (ignoreKtx) { | ||
if (parentNode.identifier.endsWith("-ktx")) { | ||
val baseId = parentNode.identifier.substringBeforeLast("-ktx") | ||
view.graph.children(parentNode).find { child -> | ||
child.identifier == baseId | ||
}?.let { bundles[parentNode] = it } | ||
} | ||
} | ||
} | ||
} | ||
|
||
return bundles | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what the new API looks like in practice. Note that I haven't added an override to the
bundle()
function, primarily due to limitations in Gradle that make it annoying to create domain objects with custom constructors.