From 5df0d01a212115ef4493048e3c3c436255e431a5 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 25 Jul 2016 00:39:06 -0800 Subject: [PATCH] Refactor PomGenerator. --- .../kotlin/com/beust/kobalt/JarGenerator.kt | 2 +- .../main/kotlin/com/beust/kobalt/Variant.kt | 6 ++-- .../kotlin/com/beust/kobalt/api/Project.kt | 2 +- .../com/beust/kobalt/internal/ProjectInfo.kt | 2 +- .../com/beust/kobalt/maven/PomGenerator.kt | 31 ++++++++++++------- .../plugin/packaging/PackagingPlugin.kt | 2 +- .../kobalt/plugin/publish/PublishPlugin.kt | 2 +- .../kotlin/com/beust/kobalt/VariantTest.kt | 2 +- 8 files changed, 29 insertions(+), 20 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt index 8a2f5733..1146fd3b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/JarGenerator.kt @@ -125,7 +125,7 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) // // Generate the manifest - // If manifest attributes were specified in the build file, use those to generate the manifest. Otherwise, + // If manifest attributes were specified in the build file, use those to generateAndSave the manifest. Otherwise, // try to find a META-INF/MANIFEST.MF and use that one if we find any. Otherwise, use the default manifest. // val manifest = diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt index ea3ea5f3..2bafde37 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt @@ -154,7 +154,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, /** * Generate BuildConfig.java if requested. Also look up if any BuildConfig is defined on the current build type, - * product flavor or main project, and use them to generate any additional field (in that order to + * product flavor or main project, and use them to generateAndSave any additional field (in that order to * respect the priorities). Return the generated file if it was generated, null otherwise. */ fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) : File? { @@ -163,7 +163,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, if (buildConfigs.size > 0) { val pkg = project.packageName ?: project.group ?: throw KobaltException( - "packageName needs to be defined on the project in order to generate BuildConfig") + "packageName needs to be defined on the project in order to generateAndSave BuildConfig") val contributor = ActorUtils.selectAffinityActor(context.pluginInfo.buildConfigContributors, project) if (contributor != null) { @@ -178,7 +178,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, log(2, "Generated ${outputDir.path}") return result } else { - throw KobaltException("Couldn't find a contributor to generate BuildConfig") + throw KobaltException("Couldn't find a contributor to generateAndSave BuildConfig") } } else { return null diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt index 7034fe25..374fb90d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -46,7 +46,7 @@ open class Project( val testConfigs = arrayListOf() - // If one is specified by default, we only generate a BuildConfig, find a way to fix that + // If one is specified by default, we only generateAndSave a BuildConfig, find a way to fix that override var buildConfig : BuildConfig? = null // BuildConfig() val projectProperties = ProjectProperties() diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ProjectInfo.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ProjectInfo.kt index dccf9290..df865cec 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ProjectInfo.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/ProjectInfo.kt @@ -12,7 +12,7 @@ import com.beust.kobalt.api.Project interface IBuildConfig { /** * If at least one build config was found either on the project or the variant, this function - * will be used to generate the BuildConfig file with the correct language. + * will be used to generateAndSave the BuildConfig file with the correct language. */ fun generateBuildConfig(project: Project, context: KobaltContext, packageName: String, variant: Variant, buildConfigs: List) : String diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt index a7c11320..8f349c99 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/PomGenerator.kt @@ -18,7 +18,25 @@ class PomGenerator @Inject constructor(@Assisted val project: Project) { fun create(project: Project) : PomGenerator } - fun generate() { + /** + * Generate the POM file and save it. + */ + fun generateAndSave() { + val buildDir = KFiles.makeDir(project.directory, project.buildDirectory) + val outputDir = KFiles.makeDir(buildDir.path, "libs") + val NO_CLASSIFIER = null + val mavenId = MavenId.create(project.group!!, project.artifactId!!, project.packaging, NO_CLASSIFIER, + project.version!!) + val pomFile = SimpleDep(mavenId).toPomFileName() + val outputFile = File(outputDir, pomFile) + outputFile.writeText(generate(), Charset.defaultCharset()) + log(1, " Created $outputFile") + } + + /** + * @return the text content of the POM file. + */ + fun generate() : String { requireNotNull(project.version, { "version mandatory on project ${project.name}" }) requireNotNull(project.group, { "group mandatory on project ${project.name}" }) requireNotNull(project.artifactId, { "artifactId mandatory on project ${project.name}" }) @@ -60,15 +78,6 @@ class PomGenerator @Inject constructor(@Assisted val project: Project) { val s = StringWriter() MavenXpp3Writer().write(s, pom) - - val buildDir = KFiles.makeDir(project.directory, project.buildDirectory) - val outputDir = KFiles.makeDir(buildDir.path, "libs") - val NO_CLASSIFIER = null - val mavenId = MavenId.create(project.group!!, project.artifactId!!, project.packaging, NO_CLASSIFIER, - project.version!!) - val pomFile = SimpleDep(mavenId).toPomFileName() - val outputFile = File(outputDir, pomFile) - outputFile.writeText(s.toString(), Charset.defaultCharset()) - log(1, " Created $outputFile") + return s.toString() } } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 9d33bb2e..82490802 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -77,7 +77,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana packageConfig.wars.forEach { warGenerator.generateWar(packageConfig.project, context, it) } packageConfig.zips.forEach { zipGenerator.generateZip(packageConfig.project, context, it) } if (packageConfig.generatePom) { - pomFactory.create(project).generate() + pomFactory.create(project).generateAndSave() } } TaskResult() diff --git a/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt index 036dba5e..9032b056 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/publish/PublishPlugin.kt @@ -41,7 +41,7 @@ public class PublishPlugin @Inject constructor(val files: KFiles, val factory: P @Suppress("UNUSED_FUNCTION_LITERAL") @Task(name = TASK_GENERATE_POM, description = "Generate the .pom file", dependsOn = arrayOf("assemble")) fun taskGeneratePom(project: Project): TaskResult { - factory.create(project).generate() + factory.create(project).generateAndSave() return TaskResult() } diff --git a/src/test/kotlin/com/beust/kobalt/VariantTest.kt b/src/test/kotlin/com/beust/kobalt/VariantTest.kt index 44ec202c..054541b5 100644 --- a/src/test/kotlin/com/beust/kobalt/VariantTest.kt +++ b/src/test/kotlin/com/beust/kobalt/VariantTest.kt @@ -35,7 +35,7 @@ class VariantTest : KobaltTest() { ) @Test(dataProvider = "projectVariants", description = - "Make sure we generate the correct dynamic tasks based on the product flavor and build types.") + "Make sure we generateAndSave the correct dynamic tasks based on the product flavor and build types.") fun taskNamesShouldWork(expected: Set, project: Project) { val variantNames = HashSet(Variant.allVariants(project).map { it.toTask("compile") }) Assert.assertEquals(variantNames, expected)