Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.itsaky.androidide.projects.api.GradleProject
import com.itsaky.androidide.projects.api.JavaModule
import com.itsaky.androidide.projects.api.ModuleProject
import com.itsaky.androidide.projects.api.Workspace
import com.itsaky.androidide.projects.models.ArtifactScope
import io.mockk.every
import io.mockk.spyk
import org.junit.rules.TemporaryFolder
Expand Down Expand Up @@ -90,7 +91,8 @@ internal class ModuleFixtures(
)
}

variantDeps.setMainArtifact(
variantDeps.putArtifacts(
ArtifactScope.Main.key,
AndroidModels.ArtifactDependencies
.newBuilder()
.setCompileGraph(graph.build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.itsaky.androidide.project.Common
import com.itsaky.androidide.project.GradleModels
import com.itsaky.androidide.project.JavaModels
import com.itsaky.androidide.projects.api.JavaModule
import com.itsaky.androidide.projects.models.ArtifactScope
import java.nio.file.Path
import kotlin.io.path.pathString

Expand Down Expand Up @@ -86,6 +87,11 @@ private fun projectDependencies(projectPaths: List<String>): AndroidModels.Varia
)
}
return variantDeps
.setMainArtifact(AndroidModels.ArtifactDependencies.newBuilder().setCompileGraph(graph))
.build()
.putArtifacts(
ArtifactScope.Main.key,
AndroidModels.ArtifactDependencies
.newBuilder()
.setCompileGraph(graph)
.build(),
).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.itsaky.androidide.projects.models

/**
* A component of a module's build that compiles its own sources against its own dependencies: main
* code, test fixtures, or one kind of host (JVM) or device test.
*
* Each scope has exactly one value, so it can key maps and caches. [key] is how the scope is stored
* in the project model, by the tooling server that writes it and the IDE that reads it; [fromKey]
* reads it back. Host and device test keys carry a prefix so one AGP test type name can never name
* two scopes.
*/
sealed class ArtifactScope {
/** The key this scope is stored under in the project model. */
abstract val key: String

/** The suffix of this scope's Kotlin analysis module path; empty for [Main]. */
abstract val moduleSuffix: String

/**
* Whether this scope's sources may reference [other]'s. Main sees only itself, test fixtures see
* main, and a test sees main and test fixtures; test scopes never see each other.
*/
fun sees(other: ArtifactScope): Boolean =
when (this) {
Main -> other == Main
TestFixtures -> other == Main || other == TestFixtures
is HostTest, is DeviceTest -> other == Main || other == TestFixtures || other == this
}

/** Main sources: `src/main` and the selected variant's layers over it. */
data object Main : ArtifactScope() {
override val key = "main"
override val moduleSuffix = ""
}

/** Test fixtures: `src/testFixtures`, visible to the module's tests. */
data object TestFixtures : ArtifactScope() {
override val key = "testFixtures"
override val moduleSuffix = "testFixtures"
}

/** Tests that run on the host JVM, named by the key AGP's model gives them (e.g. `_unit_test_`). */
data class HostTest(
val type: String,
) : ArtifactScope() {
override val key = "$HOST_PREFIX$type"
override val moduleSuffix = camelCase(type)
}

/** Tests that run on a device, named by the key AGP's model gives them (e.g. `_android_test_`). */
data class DeviceTest(
val type: String,
) : ArtifactScope() {
override val key = "$DEVICE_PREFIX$type"
override val moduleSuffix = camelCase(type)
}

companion object {
private const val HOST_PREFIX = "host:"
private const val DEVICE_PREFIX = "device:"

// The type names are AGP's ComponentTypeImpl artifact names, the keys of its model's test maps.

/** Unit tests, `src/test`. */
@JvmField
val UNIT_TEST = HostTest("_unit_test_")

/** Screenshot tests, `src/screenshotTest`. */
@JvmField
val SCREENSHOT_TEST = HostTest("_screenshot_test_")

/** Instrumented tests, `src/androidTest`. */
@JvmField
val ANDROID_TEST = DeviceTest("_android_test_")

/**
* Main, test fixtures, unit tests, android tests, then other host and device tests by key: the
* order a file shared by several scopes is attributed in.
*/
@JvmField
val ORDER: Comparator<ArtifactScope> =
compareBy<ArtifactScope> { scope ->
when (scope) {
Main -> 0
TestFixtures -> 1
UNIT_TEST -> 2
ANDROID_TEST -> 3
is HostTest -> 4
is DeviceTest -> 5
}
}.thenBy { it.key }

/** `_unit_test_` -> `unitTest`, so Kotlin module paths stay readable. */
private fun camelCase(type: String): String =
type
.split('_')
.filter { it.isNotEmpty() }
.mapIndexed { index, part ->
if (index ==
0
) {
part.replaceFirstChar(Char::lowercaseChar)
} else {
part.replaceFirstChar(Char::uppercaseChar)
}
}.joinToString("")

/** The scope stored under [key], or null when [key] names no scope. */
@JvmStatic
fun fromKey(key: String): ArtifactScope? =
when {
key == Main.key -> Main
key == TestFixtures.key -> TestFixtures
key.startsWith(HOST_PREFIX) -> HostTest(key.removePrefix(HOST_PREFIX))
key.startsWith(DEVICE_PREFIX) -> DeviceTest(key.removePrefix(DEVICE_PREFIX))
else -> null
}
}
}
29 changes: 20 additions & 9 deletions subprojects/project-models/src/main/proto/android.proto
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,15 @@ message VariantDependencies {
// The name of the variant. It is made up of the build type and flavors (if applicable)
string name = 1;

// The dependencies for the variant's main artifact.
ArtifactDependencies mainArtifact = 2;

// The list of external libraries used by all the variants in the module.
//
// The key for the map entries is the keys found via [com.android.builder.model.v2.ide.GraphItem.key]
// and [Library.key]
map<string, Library> libraries = 3;

// The dependencies of each of the variant's components (main, test fixtures, each host and
// device test type), keyed by ArtifactScope.key. A component the variant lacks has no entry.
map<string, ArtifactDependencies> artifacts = 2;
}

// A custom source directory.
Expand Down Expand Up @@ -220,8 +221,9 @@ message SourceProvider {
// Android source set container.
message SourceSetContainer {

// The default source provider.
SourceProvider sourceProvider = 1;
// The source provider of each component (src/main, src/test, src/androidTest, ...), keyed by
// ArtifactScope.key. A component the module lacks has no entry.
map<string, SourceProvider> sourceProviders = 1;
}

// View binding options for Android projects.
Expand Down Expand Up @@ -275,6 +277,17 @@ message AndroidArtifact {
int32 targetSdkVersionOverride = 13;
}

// The sources one component of a variant adds over that component's default source set.
message VariantArtifactSources {

// The source providers AGP layers over the component's default source set for this variant: its
// build type, each product flavor, the multi-flavor provider and the variant's own provider.
repeated SourceProvider variantSourceProviders = 1;

// The component's generated source folders (kapt, KSP, BuildConfig and the like).
repeated string generatedSourceFolderPaths = 2;
}

// An Android project variant.
message AndroidVariant {

Expand All @@ -284,9 +297,8 @@ message AndroidVariant {
// The 'main' artifact of the variant.
AndroidArtifact mainArtifact = 2;

// The source providers AGP layers over the main source set for this variant: its build
// type, each product flavor, the multi-flavor provider and the variant's own provider.
repeated SourceProvider variantSourceProviders = 3;
// The sources each of the variant's components adds, keyed by ArtifactScope.key.
map<string, VariantArtifactSources> artifactSources = 3;
}

// The Android project model.
Expand Down Expand Up @@ -324,5 +336,4 @@ message AndroidProject {

// The compiler settings for Kotlin sources.
optional KotlinCompilerSettings kotlinCompilerSettings = 11;

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.itsaky.androidide.projects.api.JavaModule
import com.itsaky.androidide.projects.api.ModuleProject
import com.itsaky.androidide.projects.api.Workspace
import com.itsaky.androidide.projects.builder.BuildService
import com.itsaky.androidide.projects.models.ArtifactScope
import com.itsaky.androidide.projects.models.resDirs
import com.itsaky.androidide.tasks.executeAsync
import com.itsaky.androidide.tooling.api.IAndroidProject
Expand Down Expand Up @@ -430,7 +431,7 @@ class ProjectManagerImpl :
}

val isResource =
module.mainSourceSet?.sourceProvider?.resDirs?.any {
module.defaultSources(ArtifactScope.Main)?.resDirs?.any {
this.pathString.contains(it.path)
} ?: false

Expand All @@ -446,11 +447,11 @@ class ProjectManagerImpl :
generateSourcesIfNecessary(event)

if (DocumentUtils.isJavaFile(event.file.toPath())) {
IProjectManager.getInstance().findModuleForFile(event.file, false)?.let {
val sourceRoot = it.findSourceRoot(event.file) ?: return@let

// add the source node entry
it.compileJavaSourceClasses.append(event.file.toPath(), sourceRoot)
IProjectManager.getInstance().findModuleForFile(event.file, false)?.let { module ->
// add the source node entry to main's trie and/or the test scopes' that see this file
module.javaSourceClassesFor(event.file).forEach { (trie, sourceRoot) ->
trie.append(event.file.toPath(), sourceRoot)
}
}
}
}
Expand All @@ -464,17 +465,19 @@ class ProjectManagerImpl :
// Do not check for Java file DocumentUtils.isJavaFile(...) as it checks for file existence as
// well. As the file is already deleted, it will always return false
if (event.file.extension == "java") {
IProjectManager
.getInstance()
.findModuleForFile(
event.file,
false,
)?.compileJavaSourceClasses
?.findSource(event.file.toPath())
?.let { it.parent?.removeChild(it) }
removeJavaSource(event.file)
}
}

/** Removes [file] from every Java source trie of its module. */
private fun removeJavaSource(file: File) {
IProjectManager
.getInstance()
.findModuleForFile(file, false)
?.allJavaSourceClasses()
?.forEach { trie -> trie.findSource(file.toPath())?.let { it.parent?.removeChild(it) } }
}

@Suppress("unused")
@Subscribe(threadMode = ThreadMode.BACKGROUND)
fun onFileRenamed(event: FileRenameEvent) {
Expand All @@ -483,22 +486,14 @@ class ProjectManagerImpl :
// Do not check for Java file DocumentUtils.isJavaFile(...) as it checks for file existence as
// well. As the file is already renamed to another filename, it will always return false
if (event.file.extension == "java") {
// remove the source node entry
IProjectManager
.getInstance()
.findModuleForFile(
event.file,
false,
)?.compileJavaSourceClasses
?.findSource(event.file.toPath())
?.let { it.parent?.removeChild(it) }
removeJavaSource(event.file)
}

if (DocumentUtils.isJavaFile(event.newFile.toPath())) {
IProjectManager.getInstance().findModuleForFile(event.newFile, false)?.let {
val sourceRoot = it.findSourceRoot(event.newFile) ?: return@let
// add the new source node entry
it.compileJavaSourceClasses.append(event.newFile.toPath(), sourceRoot)
IProjectManager.getInstance().findModuleForFile(event.newFile, false)?.let { module ->
module.javaSourceClassesFor(event.newFile).forEach { (trie, sourceRoot) ->
trie.append(event.newFile.toPath(), sourceRoot)
}
}
}
}
Expand Down
Loading
Loading