-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Read package.json into extension #232
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/github/gradle/node/PackageJsonExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.github.gradle.node | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.property | ||
|
||
|
||
open class PackageJsonExtension(project: Project) { | ||
val node = project.objects.property<JsonNode>() | ||
|
||
init { | ||
node.finalizeValueOnRead() | ||
node.set(project.provider { project.file("package.json").let(ObjectMapper()::readTree) }) | ||
} | ||
|
||
val name = project.provider { node.get().get("name").asText() } | ||
|
||
val version = project.provider { node.get().get("version").asText() } | ||
|
||
val description = project.provider { node.get().get("description").asText() } | ||
|
||
val homepage = project.provider { node.get().get("homepage").asText() } | ||
|
||
val license = project.provider { node.get().get("license").asText() } | ||
|
||
val private = project.provider { node.get().get("private").asBoolean() } | ||
|
||
fun get(name: String): String { | ||
return node.get().get(name).asText() | ||
} | ||
|
||
fun getBoolean(name: String): Boolean { | ||
return node.get().get(name).asBoolean() | ||
} | ||
|
||
fun get(vararg name: String): String { | ||
return name.fold(node.get()) { acc, next -> acc.get(next) }.asText() | ||
} | ||
|
||
companion object { | ||
/** | ||
* Extension name in Gradle | ||
*/ | ||
const val NAME = "package.json" | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/groovy/com/github/gradle/node/PackageJsonExtensionTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//file:noinspection GroovyPointlessBoolean | ||
package com.github.gradle.node | ||
|
||
import com.github.gradle.AbstractProjectTest | ||
|
||
class PackageJsonExtensionTest extends AbstractProjectTest { | ||
def 'check standard attributes'() { | ||
when: | ||
temporaryFolder.newFile("package.json") << """ | ||
{ "name": "test", "version": "1.10.2", "private": false } | ||
""" | ||
project.apply plugin: 'com.github.node-gradle.node' | ||
project.evaluate() | ||
|
||
then: | ||
def ext = project.extensions.getByName('package.json') as PackageJsonExtension | ||
ext.name.get() == "test" | ||
ext.version.get() == "1.10.2" | ||
ext.private.get() == false | ||
} | ||
|
||
def 'get raw attributes'() { | ||
when: | ||
temporaryFolder.newFile("package.json") << """ | ||
{ "name": "test", "version": "1.10.2", "private": false } | ||
""" | ||
project.apply plugin: 'com.github.node-gradle.node' | ||
project.evaluate() | ||
|
||
then: | ||
def ext = project.extensions.getByName('package.json') as PackageJsonExtension | ||
ext.get("name") == "test" | ||
ext.getBoolean("private") == false | ||
} | ||
|
||
def 'get nested attributes'() { | ||
when: | ||
temporaryFolder.newFile("package.json") << """ | ||
{ | ||
"upper": { "lower": { "end": "done" } } | ||
} | ||
""" | ||
project.apply plugin: 'com.github.node-gradle.node' | ||
project.evaluate() | ||
|
||
then: | ||
def ext = project.extensions.getByName('package.json') as PackageJsonExtension | ||
ext.get("upper", "lower", "end") == "done" | ||
} | ||
} |