1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Refactor PomGenerator.

This commit is contained in:
Cedric Beust 2016-07-25 00:39:06 -08:00
parent 095a3918f1
commit 5df0d01a21
8 changed files with 29 additions and 20 deletions

View file

@ -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 =

View file

@ -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

View file

@ -46,7 +46,7 @@ open class Project(
val testConfigs = arrayListOf<TestConfig>()
// 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()

View file

@ -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<BuildConfig>) : String

View file

@ -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()
}
}

View file

@ -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()

View file

@ -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()
}

View file

@ -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<String>, project: Project) {
val variantNames = HashSet(Variant.allVariants(project).map { it.toTask("compile") })
Assert.assertEquals(variantNames, expected)