Skip to content
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

Support generate declared repositories #67

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fun generateGradmGeneratedSources(
generateVersionsSourceFile()
generatePluginSourceFile()
generateDependenciesSourceFiles()
generateRepositoriesSourceFile()
generateSelfSourceFile()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package me.omico.gradm.internal.codegen

import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import me.omico.elucidator.FunctionScope
import me.omico.elucidator.addClass
Expand All @@ -37,9 +36,7 @@ import me.omico.gradm.GradmExperimentalConfiguration
import me.omico.gradm.GradmGeneratedPluginType
import me.omico.gradm.VersionsMeta
import me.omico.gradm.internal.YamlDocument
import me.omico.gradm.internal.config.gradleBuildInRepositories
import me.omico.gradm.internal.config.plugins
import me.omico.gradm.internal.config.repositories
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.initialization.Settings
Expand Down Expand Up @@ -162,13 +159,4 @@ private fun FunctionScope.addExtensionsIfNeeds(
): Unit =
addStatement(format = "$extensionsPath.findByName(\"${name}\") ?: $extensionsPath.add(\"${name}\", %T)", className)

private fun FunctionScope.addDeclaredRepositoryStatements(gradmConfigDocument: YamlDocument): Unit =
gradmConfigDocument.repositories.forEach { repository ->
when {
repository in gradleBuildInRepositories -> addStatement("${repository.id}()")
!repository.noUpdates -> addStatement("%M(url = %S)", mavenMemberName, repository.url)
}
}

private val extensionsPathRegex = """^(\w+\.)*\w+$""".toRegex()
private val mavenMemberName: MemberName = MemberName("org.gradle.kotlin.dsl", "maven")
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2023 Omico
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.omico.gradm.internal.codegen

import com.squareup.kotlinpoet.MemberName
import me.omico.elucidator.FunctionScope
import me.omico.elucidator.KtFileScope
import me.omico.elucidator.addFunction
import me.omico.elucidator.addLambdaStatement
import me.omico.elucidator.addStatement
import me.omico.elucidator.ktFile
import me.omico.elucidator.receiver
import me.omico.elucidator.writeTo
import me.omico.gradm.GRADM_PACKAGE_NAME
import me.omico.gradm.internal.YamlDocument
import me.omico.gradm.internal.config.gradleBuildInRepositories
import me.omico.gradm.internal.config.repositories
import org.gradle.api.Project
import org.gradle.api.initialization.Settings

internal fun CodeGenerator.generateRepositoriesSourceFile() {
ktFile(GRADM_PACKAGE_NAME, "Repositories") {
addSuppressWarningTypes()
addGradmComment()
addAddDeclaredRepositoriesForSettingsScope(gradmConfigDocument)
addAddDeclaredRepositoriesForProjectScope(gradmConfigDocument)
writeTo(generatedSourcesDirectory)
}
}

internal fun FunctionScope.addDeclaredRepositoryStatements(gradmConfigDocument: YamlDocument): Unit =
gradmConfigDocument.repositories.forEach { repository ->
when {
repository in gradleBuildInRepositories -> addStatement("${repository.id}()")
!repository.noUpdates -> addStatement("%M(url = %S)", mavenMemberName, repository.url)
}
}

private fun KtFileScope.addAddDeclaredRepositoriesForSettingsScope(gradmConfigDocument: YamlDocument): Unit =
addDeclaredRepositoryStatements<Settings> {
addLambdaStatement("pluginManagement.repositories") {
addDeclaredRepositoryStatements(gradmConfigDocument)
}
}

private fun KtFileScope.addAddDeclaredRepositoriesForProjectScope(gradmConfigDocument: YamlDocument): Unit =
addDeclaredRepositoryStatements<Project> {
addLambdaStatement("%M", repositoriesMemberName) {
addDeclaredRepositoryStatements(gradmConfigDocument)
}
}

private inline fun <reified T> KtFileScope.addDeclaredRepositoryStatements(crossinline block: FunctionScope.() -> Unit): Unit =
addFunction("addDeclaredRepositories") {
receiver<T>()
block()
}

private val mavenMemberName: MemberName = MemberName("org.gradle.kotlin.dsl", "maven")
private val repositoriesMemberName: MemberName = MemberName("org.gradle.kotlin.dsl", "repositories")