io.github.kelvindev15.gradle.GeneratePackageFileTask.kt Maven / Gradle / Ivy
package io.github.kelvindev15.gradle
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.uchuhimo.konf.Config
import com.uchuhimo.konf.OptionalItem
import io.github.kelvindev15.npm.NpmPackageFile
import io.github.kelvindev15.npm.PackageJsonSpec
import io.github.kelvindev15.npm.Repository
import org.gradle.api.DefaultTask
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.property
import java.io.File
/**
* The task to generate the package.json file.
*/
open class GeneratePackageFileTask : DefaultTask() {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
/**
* The object representing the package.json file.
*/
@Input
val packageJson: Property = project.objects.property()
/**
* Generates the package.json file.
*/
@TaskAction
fun generatePackageFile() {
val packageJsonOptions = packageJson.get()
File(project.projectDir, "package.json").also { file ->
if (file.exists()) {
val configuration = Config {
addSpec(PackageJsonSpec)
}.from.json.file(file)
configuration[PackageJsonSpec.name] = packageJsonOptions.name
configuration[PackageJsonSpec.version] = packageJsonOptions.version
configuration[PackageJsonSpec.description] = packageJsonOptions.description
configuration[PackageJsonSpec.author] = packageJsonOptions.author
configuration[PackageJsonSpec.license] = packageJsonOptions.license
configuration[PackageJsonSpec.main] = packageJsonOptions.main
configuration[PackageJsonSpec.homepage] = packageJsonOptions.homepage
configuration[PackageJsonSpec.type] = packageJsonOptions.type
fun addToCurrent(key: OptionalItem