Skip to content

Commit 6c782b9

Browse files
Introduce utbot-android-studio module and fix dependencies installation (#1180)
* Introduce utbot-android-studio module * Remove unused method * Path corrected for non Android Studio users * Missed file * Add documentation to the gradle script
1 parent b3ec3da commit 6c782b9

File tree

6 files changed

+172
-1
lines changed

6 files changed

+172
-1
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ideType=IC
66

77
# In order to run Android Studion instead of Intellij Community,
88
# specify the path to your Android Studio installation
9-
//androidStudioPath=D:/AS2021
9+
//androidStudioPath=your_path_to_android_studio
1010

1111
pythonCommunityPluginVersion=222.4167.37
1212
#Version numbers: https://plugins.jetbrains.com/plugin/631-python/versions

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ include 'utbot-maven'
3030
include 'utbot-summary-tests'
3131
include 'utbot-framework-test'
3232
include 'utbot-rd'
33+
include 'utbot-android-studio'
3334

utbot-android-studio/build.gradle.kts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
plugins {
2+
id("org.jetbrains.intellij") version "1.7.0"
3+
}
4+
5+
intellij {
6+
/*
7+
The list of Android Studio releases can be found here https://plugins.jetbrains.com/docs/intellij/android-studio-releases-list.html
8+
For each release a compatible Intellij Idea version can be found in the right column. Specify it in "version.set("...")
9+
10+
NOTE!!!
11+
We use Android Studio Chipmunk (2021.2.1), although Android Studio Dolphin (2021.3.1) has been released.
12+
The reason is that a version of Kotlin plugin compatible with Android Studio is required.
13+
The list of Kotlin plugin releases can be found here https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable
14+
The last compatible with AS plugin version on 19 Oct 2022 is Kotlin 212-1.7.10-release-333-AS5457.46,
15+
it is not compatible with Dolphin release (https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable/193255).
16+
*/
17+
18+
val androidPlugins = listOf("org.jetbrains.android")
19+
20+
val jvmPlugins = listOf(
21+
"java",
22+
"org.jetbrains.kotlin:212-1.7.10-release-333-AS5457.46"
23+
)
24+
25+
plugins.set(jvmPlugins + androidPlugins)
26+
27+
version.set("212.5712.43")
28+
type.set("IC")
29+
}
30+
31+
project.tasks.asMap["runIde"]?.enabled = false
32+
33+
tasks {
34+
compileKotlin {
35+
kotlinOptions {
36+
jvmTarget = "11"
37+
freeCompilerArgs = freeCompilerArgs + listOf("-Xallow-result-return-type", "-Xsam-conversions=class")
38+
allWarningsAsErrors = false
39+
}
40+
}
41+
42+
java {
43+
sourceCompatibility = JavaVersion.VERSION_11
44+
targetCompatibility = JavaVersion.VERSION_11
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package org.androidstudio.plugin.util
2+
3+
import com.android.tools.idea.gradle.AndroidGradleJavaProjectModelModifier
4+
import com.android.tools.idea.gradle.dsl.api.GradleBuildModel
5+
import com.android.tools.idea.gradle.dsl.api.dependencies.ArtifactDependencySpec
6+
import com.android.tools.idea.gradle.dsl.api.dependencies.CommonConfigurationNames
7+
import com.android.tools.idea.gradle.project.sync.GradleSyncInvoker
8+
import com.android.tools.idea.gradle.project.sync.GradleSyncListener
9+
import com.android.tools.idea.gradle.project.sync.idea.GradleSyncExecutor
10+
import com.android.tools.idea.gradle.util.GradleUtil
11+
import com.android.tools.idea.project.AndroidProjectInfo
12+
import com.android.tools.idea.projectsystem.TestArtifactSearchScopes
13+
import com.google.wireless.android.sdk.stats.GradleSyncStats
14+
import com.intellij.openapi.command.WriteCommandAction
15+
import com.intellij.openapi.command.undo.BasicUndoableAction
16+
import com.intellij.openapi.command.undo.UndoManager
17+
import com.intellij.openapi.externalSystem.ExternalSystemModulePropertyManager
18+
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx
19+
import com.intellij.openapi.module.Module
20+
import com.intellij.openapi.project.Project
21+
import com.intellij.openapi.roots.DependencyScope
22+
import com.intellij.openapi.roots.ExternalLibraryDescriptor
23+
import com.intellij.openapi.vfs.VirtualFile
24+
import com.intellij.util.containers.ContainerUtil
25+
import org.jetbrains.concurrency.AsyncPromise
26+
import org.jetbrains.concurrency.Promise
27+
28+
class UtAndroidGradleJavaProjectModelModifier : AndroidGradleJavaProjectModelModifier() {
29+
override fun addExternalLibraryDependency(
30+
modules: Collection<Module?>,
31+
descriptor: ExternalLibraryDescriptor,
32+
scope: DependencyScope
33+
): Promise<Void?>? {
34+
val module = ContainerUtil.getFirstItem(modules) ?: return null
35+
36+
if (!isAndroidGradleProject(module.project)) {
37+
return null
38+
}
39+
40+
val dependencySpec = ArtifactDependencySpec.create(descriptor.libraryArtifactId, descriptor.libraryGroupId, descriptor.preferredVersion)
41+
return addExternalLibraryDependency(module, dependencySpec, scope)
42+
43+
}
44+
45+
private fun addExternalLibraryDependency(
46+
module: Module,
47+
dependencySpec: ArtifactDependencySpec,
48+
scope: DependencyScope,
49+
): Promise<Void?>? {
50+
val project = module.project
51+
val openedFile = FileEditorManagerEx.getInstanceEx(project).currentFile
52+
val buildModelsToUpdate: MutableList<GradleBuildModel> = ArrayList()
53+
54+
val buildModel = GradleBuildModel.get(module) ?: return null
55+
val configurationName = getConfigurationName(module, scope, openedFile)
56+
val dependencies = buildModel.dependencies()
57+
dependencies.addArtifact(configurationName, dependencySpec)
58+
buildModelsToUpdate.add(buildModel)
59+
60+
WriteCommandAction.writeCommandAction(project).withName("Add Gradle Library Dependency").run<RuntimeException> {
61+
buildModelsToUpdate.forEach { buildModel -> buildModel.applyChanges() }
62+
registerUndoAction(project)
63+
}
64+
65+
return doAndroidGradleSync(project, GradleSyncStats.Trigger.TRIGGER_MODIFIER_ADD_LIBRARY_DEPENDENCY)
66+
}
67+
68+
private fun getConfigurationName(module: Module, scope: DependencyScope, openedFile: VirtualFile?): String =
69+
GradleUtil.mapConfigurationName(
70+
getLegacyConfigurationName(module, scope, openedFile),
71+
GradleUtil.getAndroidGradleModelVersionInUse(module),
72+
false
73+
)
74+
75+
private fun getLegacyConfigurationName(
76+
module: Module,
77+
scope: DependencyScope,
78+
openedFile: VirtualFile?
79+
): String {
80+
if (!scope.isForProductionCompile) {
81+
val testScopes = TestArtifactSearchScopes.getInstance(module)
82+
if (testScopes != null && openedFile != null) {
83+
return if (testScopes.isAndroidTestSource(openedFile)) CommonConfigurationNames.ANDROID_TEST_COMPILE else CommonConfigurationNames.TEST_COMPILE
84+
}
85+
}
86+
return CommonConfigurationNames.COMPILE
87+
}
88+
89+
private fun registerUndoAction(project: Project) {
90+
UndoManager.getInstance(project).undoableActionPerformed(object : BasicUndoableAction() {
91+
92+
override fun undo() {
93+
doAndroidGradleSync(project, GradleSyncStats.Trigger.TRIGGER_MODIFIER_ACTION_UNDONE)
94+
95+
}
96+
97+
override fun redo() {
98+
doAndroidGradleSync(project, GradleSyncStats.Trigger.TRIGGER_MODIFIER_ACTION_REDONE)
99+
}
100+
})
101+
}
102+
103+
private fun isAndroidGradleProject(project: Project): Boolean = AndroidProjectInfo.getInstance(project).requiresAndroidModel()
104+
105+
private fun doAndroidGradleSync(project: Project, trigger: GradleSyncStats.Trigger): AsyncPromise<Void?> {
106+
val promise = AsyncPromise<Void?>()
107+
val request = GradleSyncInvoker.Request(trigger)
108+
val listener = object : GradleSyncListener {
109+
override fun syncSucceeded(project: Project) {
110+
promise.setResult(null)
111+
}
112+
113+
override fun syncFailed(project: Project, errorMessage: String) {
114+
promise.setError(errorMessage)
115+
}
116+
}
117+
GradleSyncExecutor(project).sync(request, listener)
118+
119+
return promise
120+
}
121+
}

utbot-intellij/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,6 @@ dependencies {
9696
//api(project(":utbot-analytics"))
9797
testImplementation("org.mock-server:mockserver-netty:5.4.1")
9898
testApi(project(":utbot-framework"))
99+
100+
implementation(project(":utbot-android-studio"))
99101
}

utbot-intellij/src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<registryKey defaultValue="false" description="Enable editing Kotlin test files" key="kotlin.ultra.light.classes.empty.text.range"/>
3333
<postStartupActivity implementation="org.utbot.intellij.plugin.ui.GotItTooltipActivity"/>
3434
<projectModelModifier implementation="org.utbot.intellij.plugin.util.UtProjectModelModifier"/>
35+
<projectModelModifier implementation="org.androidstudio.plugin.util.UtAndroidGradleJavaProjectModelModifier" order="first"/>
3536
<!--Documentation-->
3637
<customJavadocTagProvider implementation="org.utbot.intellij.plugin.javadoc.UtCustomJavaDocTagProvider"/>
3738
<lang.documentationProvider language="JAVA" order="first" implementationClass="org.utbot.intellij.plugin.javadoc.UtDocumentationProvider"/>

0 commit comments

Comments
 (0)