Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(gradle-plugin): do not add logsender dependency if it is disabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Sep 9, 2023
1 parent a1c6b53 commit 42ac697
Show file tree
Hide file tree
Showing 15 changed files with 241 additions and 64 deletions.
3 changes: 3 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ dependencies {
implementation(projects.eventbus)
implementation(projects.eventbusAndroid)
implementation(projects.eventbusEvents)
implementation(projects.gradlePluginConfig)
implementation(projects.idestats)
implementation(projects.subprojects.aaptcompiler)
implementation(projects.subprojects.javacServices)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.itsaky.androidide.preferences.internal.isOfflineEnabled
import com.itsaky.androidide.preferences.internal.isScanEnabled
import com.itsaky.androidide.preferences.internal.isStacktraceEnabled
import com.itsaky.androidide.preferences.internal.isWarningModeAllEnabled
import com.itsaky.androidide.preferences.logsenderEnabled
import com.itsaky.androidide.projects.ProjectManagerImpl
import com.itsaky.androidide.projects.builder.BuildService
import com.itsaky.androidide.resources.R
Expand All @@ -48,6 +49,7 @@ import com.itsaky.androidide.tooling.api.ForwardingToolingApiClient
import com.itsaky.androidide.tooling.api.IProject
import com.itsaky.androidide.tooling.api.IToolingApiClient
import com.itsaky.androidide.tooling.api.IToolingApiServer
import com.itsaky.androidide.tooling.api.LogSenderConfig.PROPERTY_LOGSENDER_ENABLED
import com.itsaky.androidide.tooling.api.messages.InitializeProjectParams
import com.itsaky.androidide.tooling.api.messages.TaskExecutionMessage
import com.itsaky.androidide.tooling.api.messages.result.BuildCancellationRequestResult
Expand Down Expand Up @@ -261,6 +263,7 @@ class GradleBuildService : Service(), BuildService, IToolingApiClient,
// Override AAPT2 binary
// The one downloaded from Maven is not built for Android
extraArgs.add("-Pandroid.aapt2FromMavenOverride=" + Environment.AAPT2.absolutePath)
extraArgs.add("-P${PROPERTY_LOGSENDER_ENABLED}=${logsenderEnabled}")
if (isStacktraceEnabled) {
extraArgs.add("--stacktrace")
}
Expand Down
1 change: 1 addition & 0 deletions gradle-plugin-config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
24 changes: 24 additions & 0 deletions gradle-plugin-config/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

plugins {
//noinspection JavaPluginLanguageLevel
id("java-library")
id("com.vanniktech.maven.publish.base")
}

description = "Configuration options for the Tooling API and Gradle Plugin."
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.tooling.api;

/**
* Configuration options for LogSender. Properties defined in this class whose name start with an
* <code>_</code> are internal.
*
* @author Akash Yadav
*/
public final class LogSenderConfig {

/**
* Property to enable or disable <code>LogSender</code> in the project. Value can be
* <code>true</code> or <code>false</code>.
*/
public static final String PROPERTY_LOGSENDER_ENABLED = "androidide.logsender.isEnabled";

/**
* Property that is set in tests to indicate that the plugin is being applied in a test
* environment.
* <p>
* <b>This is an internal property and should not be manually set by users.</b>
*/
public static final String _PROPERTY_IS_TEST_ENV = "androidide.plugins.internal.isTestEnv";

/**
* Property that is set in tests to provide path to the local maven repository. If this property
* is empty, `null` or not set at all, the default maven local repository is used.
*
* <b>This is an internal property and should not be manually set by users.</b>
*/
public static final String _PROPERTY_MAVEN_LOCAL_REPOSITORY = "androidide.plugins.internal.mavenLocalRepositories";

private LogSenderConfig() {
throw new UnsupportedOperationException("This class cannot be instantiated.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.tooling.api;

/**
* Configuration options for the Tooling API.
*
* @author Akash Yadav
*/
public final class ToolingConfig {

private ToolingConfig() {
throw new UnsupportedOperationException("This class cannot be instantiated.");
}
}
1 change: 1 addition & 0 deletions gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ configurations {
}

dependencies {
implementation(projects.gradlePluginConfig)
implementation(projects.buildInfo)

// use the AGP APIs from the minimum supported AGP version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.itsaky.androidide.gradle

import com.itsaky.androidide.tooling.api.LogSenderConfig.PROPERTY_LOGSENDER_ENABLED
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.logging.Logging
Expand All @@ -39,9 +40,21 @@ class AndroidIDEGradlePlugin : Plugin<Project> {

target.run {

val isLogSenderEnabled = if (hasProperty(PROPERTY_LOGSENDER_ENABLED)) {
property(PROPERTY_LOGSENDER_ENABLED).toString().toBoolean()
} else {
// enabled by default
true
}

if (plugins.hasPlugin(APP_PLUGIN)) {
logger.info("Trying to apply LogSender plugin to project '${project.path}'")
pluginManager.apply(LogSenderPlugin::class.java)

if (isLogSenderEnabled) {
logger.info("Trying to apply LogSender plugin to project '${project.path}'")
pluginManager.apply(LogSenderPlugin::class.java)
} else {
logger.warn("LogSender is disabled. Dependency will not be added to project '${project.path}'.")
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package com.itsaky.androidide.gradle

import com.itsaky.androidide.buildinfo.BuildInfo
import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_IS_TEST_ENV
import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_MAVEN_LOCAL_REPOSITORY
import org.gradle.api.Plugin
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.artifacts.repositories.ArtifactRepository
import org.gradle.api.initialization.Settings
import org.gradle.api.invocation.Gradle
import org.gradle.api.logging.Logging
Expand All @@ -38,29 +39,14 @@ class AndroidIDEInitScriptPlugin : Plugin<Gradle> {
companion object {

private val logger = Logging.getLogger(AndroidIDEInitScriptPlugin::class.java)

/**
* Property that is set in tests to indicate that the plugin is being applied in a test environment.
*
* **This is an internal property and should not be manually set by users.**
*/
internal const val PROPERTY_IS_TEST_ENV = "androidide.plugins.internal.isTestEnv"

/**
* Property that is set in tests to provide path to the local maven repository.
* If this property is empty, `null` or not set at all, the default maven local repository is used.
*
* **This is an internal property and should not be manually set by users.**
*/
internal const val PROPERTY_MAVEN_LOCAL_REPOSITORY = "androidide.plugins.internal.mavenLocalRepositories"
}

override fun apply(target: Gradle) {
target.settingsEvaluated { settings ->
val (isTestEnv, mavenLocalRepos) = settings.startParameter.run {
val isTestEnv = projectProperties.containsKey(PROPERTY_IS_TEST_ENV)
&& projectProperties[PROPERTY_IS_TEST_ENV].toString().toBoolean()
val mavenLocalRepos = projectProperties.getOrDefault(PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
val isTestEnv = projectProperties.containsKey(_PROPERTY_IS_TEST_ENV)
&& projectProperties[_PROPERTY_IS_TEST_ENV].toString().toBoolean()
val mavenLocalRepos = projectProperties.getOrDefault(_PROPERTY_MAVEN_LOCAL_REPOSITORY, "")
isTestEnv to mavenLocalRepos
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package com.itsaky.androidide.gradle

import com.itsaky.androidide.buildinfo.BuildInfo
import com.itsaky.androidide.gradle.AndroidIDEInitScriptPlugin.Companion.PROPERTY_IS_TEST_ENV
import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_IS_TEST_ENV
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.dsl.DependencyHandler
Expand All @@ -31,10 +31,10 @@ const val APP_PLUGIN = "com.android.application"
const val LIBRARY_PLUGIN = "com.android.library"

internal val Project.isTestEnv: Boolean
get() = hasProperty(PROPERTY_IS_TEST_ENV) && property(
PROPERTY_IS_TEST_ENV).toString().toBoolean()
get() = hasProperty(_PROPERTY_IS_TEST_ENV) && property(
_PROPERTY_IS_TEST_ENV).toString().toBoolean()

internal fun depVersion(testEnv: Boolean) : String {
internal fun depVersion(testEnv: Boolean): String {
return if (testEnv && !System.getenv("CI").toBoolean()) {
BuildInfo.VERSION_NAME_SIMPLE
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,45 +45,6 @@ class AndroidIDEInitScriptPluginTest {
assertBasics(result)
}

private fun buildProject(
agpVersion: String = BuildInfo.AGP_VERSION_LATEST,
gradleVersion: String = "8.2",
vararg plugins: String
): BuildResult {
val projectRoot = openProject(agpVersion, *plugins)
val initScript = FileProvider.testHomeDir().resolve(".androidide/init/androidide.init.gradle")
val mavenLocal = FileProvider.projectRoot().resolve("gradle-plugin/build/maven-local/repos.txt").toFile()

if (!(mavenLocal.exists() && mavenLocal.isFile)) {
throw FileNotFoundException("repos.txt file not found")
}

val repositories = mavenLocal.readText()

for (repo in repositories.split(':')) {
val file = File(repo)
if (!(file.exists() && file.isDirectory)) {
throw FileNotFoundException("Maven local repository does not exist : $repo")
}
}

val runner = GradleRunner.create()
.withProjectDir(projectRoot.toFile())
.withGradleVersion(gradleVersion)
.withArguments(
":app:tasks", // run any task, as long as it applies the plugins
"--init-script", initScript.pathString,
"-Pandroidide.plugins.internal.isTestEnv=true", // plugins should be published to maven local first
"-Pandroidide.plugins.internal.mavenLocalRepositories=$repositories",
"--stacktrace"
)

writeInitScript(initScript.toFile(),
PluginUnderTestMetadataReading.readImplementationClasspath())

return runner.build()
}

private fun assertBasics(result: BuildResult) {
// These plugins must be applied to the
for ((project, plugins) in mapOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/

package com.itsaky.androidide.gradle

import com.google.common.truth.Truth.assertThat
import com.itsaky.androidide.tooling.api.LogSenderConfig.PROPERTY_LOGSENDER_ENABLED
import org.junit.jupiter.api.Test

/**
* @author Akash Yadav
*/
class AndroidIDEPluginTest {

@Test
fun `test logsender must be enabled by default`() {
val result = buildProject()
assertThat(result.output).doesNotContain("LogSender is disabled")
}

@Test
fun `test logsender must be enabled if specified explicitly`() {
val result = buildProject(configureArgs = {
it.add("-P$PROPERTY_LOGSENDER_ENABLED=true")
})
assertThat(result.output).doesNotContain("LogSender is disabled")
}

@Test
fun `test logsender must be disabled if specified explicitly`() {
val result = buildProject(configureArgs = {
it.add("-P$PROPERTY_LOGSENDER_ENABLED=false")
})
assertThat(result.output).contains("LogSender is disabled")
}
}
Loading

0 comments on commit 42ac697

Please sign in to comment.