Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 55
ADFA-2849: do not overwrite repositories in Gradle Plugin#995
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
b8a08bc2908c96d11bfb2999750a8d8784339fd9be88a549b651bd53File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package com.itsaky.androidide.gradle | ||
| import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_IS_TEST_ENV | ||
| import com.itsaky.androidide.tooling.api.LogSenderConfig._PROPERTY_MAVEN_LOCAL_REPOSITORY | ||
| import org.adfa.constants.MAVEN_LOCAL_REPOSITORY | ||
| import org.gradle.StartParameter | ||
| import org.gradle.api.Plugin | ||
| import org.gradle.api.artifacts.dsl.RepositoryHandler | ||
| import org.gradle.api.artifacts.repositories.MavenArtifactRepository | ||
| import org.gradle.api.initialization.Settings | ||
| import org.gradle.api.logging.Logger | ||
| import org.gradle.api.logging.Logging | ||
| import java.io.File | ||
| import java.net.URI | ||
| class COTGSettingsPlugin : Plugin<Settings> { | ||
| private val logger = Logging.getLogger(COTGSettingsPlugin::class.java) | ||
| override fun apply(target: Settings) { | ||
| if (target.gradle.parent != null) { | ||
| // only apply the settings plugin to the root Gradle build | ||
| return | ||
| } | ||
| logger.info("Plugin instance: ${System.identityHashCode(this)}") | ||
| // Add our local maven repo, always. | ||
| val allLocalRepos = mutableListOf(MAVEN_LOCAL_REPOSITORY) | ||
| // Then check if we need to add additional repos, based on whether | ||
| // we're in a test environment | ||
| val (isTestEnv, mavenLocalRepos) = getTestEnvProps(target.startParameter) | ||
| if (isTestEnv) { | ||
| allLocalRepos += mavenLocalRepos | ||
| } | ||
| target.addLocalRepos(allLocalRepos) | ||
| } | ||
| private fun RepositoryHandler.addLocalRepos(repos: List<String>) { | ||
| repos.forEach { repo -> | ||
| addLocalMavenRepoIfMissing(logger, repo) | ||
| } | ||
| } | ||
| @Suppress("UnstableApiUsage") | ||
| private fun Settings.addLocalRepos(mavenLocalRepos: List<String>) { | ||
| dependencyResolutionManagement.repositories.addLocalRepos(mavenLocalRepos) | ||
| pluginManagement.repositories.addLocalRepos(mavenLocalRepos) | ||
| } | ||
| private fun getTestEnvProps(startParameter: StartParameter): Pair<Boolean, List<String>> = | ||
| 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, "") | ||
| isTestEnv to mavenLocalRepos.split(File.pathSeparatorChar).toList().filter { it.isNotBlank() } | ||
| } | ||
itsaky-adfa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| private fun RepositoryHandler.addLocalMavenRepoIfMissing( | ||
| logger: Logger, | ||
| path: String, | ||
| ) { | ||
| val dir = File(path) | ||
| require(dir.isDirectory) { "Repo not found: $path" } | ||
itsaky-adfa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| val uri = dir.toURI() | ||
| addMavenRepoIfMissing(logger, uri) | ||
| } | ||
| private fun RepositoryHandler.addMavenRepoIfMissing( | ||
| logger: Logger, | ||
| uri: URI, | ||
| ) { | ||
| if (none { it is MavenArtifactRepository && it.url == uri }) { | ||
| logger.info("Adding maven repository: $uri") | ||
| maven { it.url = uri } | ||
| } | ||
| } | ||
itsaky-adfa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
Uh oh!
There was an error while loading. Please reload this page.