override fun execute()

in src/main/kotlin/org/jetbrains/intellij/platform/gradle/artifacts/LocalIvyArtifactPathComponentMetadataRule.kt [91:152]


    override fun execute(context: ComponentMetadataContext) {
        val id = context.details.id
        // Since we also need to fix transitive dependencies, we have to intercept everything and filter.
        if (id.group !in replacementGroups) {
            return
        }

        /**
         * Unfortunately, Gradle here doesn't expose anything from Ivy metadata, all we know is: group, name and version.
         * Much more is visible in debug, but all that is private.
         * So we have to read the Ivy XML again.
         */
        val ivyXmlFile = File("$absNormalizedIvyPath/${id.version}/${id.group}-${id.name}-${id.version}.xml")
        val ivyModule = XML.decodeFromString<IvyModule>(ivyXmlFile.readText())
        val (moduleType, moduleVersion) = id.version.parseIdeNotation()
        val productInfo = Path.of(absNormalizedPlatformPath).productInfo()

        if (moduleType != productInfo.type || moduleVersion != productInfo.buildNumber) {
            return
        }

        context.details.allVariants {
            withFiles {
                // Remove all existing artifacts because they have relative paths and won't be found.
                removeAllFiles()

                // Add new files (i.e., artifacts) with the correct absolute path.
                ivyModule.publications.forEach { artifact ->
                    val fileName = "${artifact.name}.${artifact.ext}"
                    val absPathString = "$absNormalizedPlatformPath/${artifact.url}/$fileName"

                    if (Path.of(absPathString).notExists()) {
                        log.error("The following artifact of the $id module ${artifact.name} is not found: $absPathString")
                        return@forEach
                    }

                    /**
                     * It is important to pass in the name and absolute path as the second arg, instead of just `addFile(absPathString)`,
                     * because when only the path is given, Gradle thinks it downloads a file from a URL and copied all artifacts into
                     * `~/.gradle/caches/modules-2/files-2.1/`.
                     */

                    if (OperatingSystem.current().isWindows) {
                        /**
                         * On Windows we should add a leading slash because there absolute paths start from a drive letter, but if Gradle sees such path
                         * (without a leading slash), it will treat it relative to the build dir and absPathString will become malformed like:
                         * `C:/Users/user-name/AppData/Local/Temp/tmp2087252038786353695/D:/project/.gradle/caches/8.10.2/transforms/137db90ba7a52eac7de798d9291575dd/transformed/ideaIC-2022.3.3-win/plugins/copyright/lib/copyright.jar`
                         *
                         * But this option works well:
                         * `/D:/project/.gradle/caches/8.10.2/transforms/137db90ba7a52eac7de798d9291575dd/transformed/ideaIC-2022.3.3-win/plugins/copyright/lib/copyright.jar`
                         */
                        addFile(fileName, "/$absPathString")
                    } else {
                        /**
                         * On Linux and OSX absolute paths start from slash naturally.
                         */
                        addFile(fileName, absPathString)
                    }
                }
            }
        }
    }