-
Notifications
You must be signed in to change notification settings - Fork 10
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b8a08bc
fix: do not overwrite repositories in Gradle Plugin
itsaky-adfa 2908c96
Merge branch 'stage' into fix/ADFA-2849
itsaky-adfa d11bfb2
fix: our local maven repo is not added to Gradle
itsaky-adfa 999750a
Merge branch 'stage' into fix/ADFA-2849
itsaky-adfa 8d87843
fix: add COTG maven repos using a separate Settings plugin
itsaky-adfa 39fd9be
Merge branch 'stage' into fix/ADFA-2849
itsaky-adfa 88a549b
fix: apply COTGSettingsPlugin to root Gradle builds only
itsaky-adfa 651bd53
fix: use pathSeparatorChar and avoid re-adding repos
itsaky-adfa 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
83 changes: 83 additions & 0 deletions
83
gradle-plugin/src/main/java/com/itsaky/androidide/gradle/COTGSettingsPlugin.kt
This file contains hidden or 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,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() } | ||
| } | ||
| } | ||
|
|
||
| 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.
Show resolved
Hide resolved
|
||
|
|
||
| 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 } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.