Skip to content

Gradle Plugin to add utils for build.gradle.kts and settings.gradle.kts

Notifications You must be signed in to change notification settings

wuseal/GradleKotlinUtils

Repository files navigation

GradleKotlinUtils

This is a plugin for gradle settings and projects, that's means that you can apply it in settings.gradle.kts and build.gradle.kts

It can let you use Gson in build.gradle.kts and settings.gradle.kts and provided a easy function to run shell commmand line

Usage

Apply in settings.gradle.kts

plugins {
    id("io.github.wuseal.utils") version "1.0.7"
}

Apply in build.gradle.kts

plugins {
    id("io.github.wuseal.utils") version "1.0.7"
}

Ability

After apply this plugin, you can

  • Use Gson library to do json serialize and deserialize by Gson library in settings.gradle.kts and build.gradle.kts
  • Use runCommand and evalBash funtion to execute eternal shell command in settings.gradle.kts and and build.gradle.kts
  • Use pre functions in any *.gradle.kts that applied in settings.gradle.kts and build.gradle.kts

Demos

Gson demo in buld.gradle.kts or settings.gradle.kts

Gson().toJson(Pair(123, 456))

prettyJsonGson.fromJson<Pair<Int, Int>>("{\"first\":1,\"second\":2}")

Command Run demo in build.gradle.kts or settings.gradle.kts

"ls".runCommand()

println("cat /Users/user/Scripts/TempGradleProject/build.gradle.kts".evalBash())

In other *.gradle.kts that applied in settings.gradle.kts or build.gradle.kts

Firstly, declare the functions in *.gradle.kts

val fromJson: String.(Class<*>) -> Any by extensions
val toJson: Any.() -> String by extensions
val runCommand: String.() -> Result<Unit> by extensions
val evalBash: String.() -> Result<String> by extensions

And then use them in *.gradle.kts

val fromJsonObj = "{\"first\":1,\"second\":2}".fromJson(typeOf<Pair<String, String>>().concreteClass)
println(fromJsonObj.toString())
(fromJsonObj as Pair<*, *>).run {
    assert(first is String)
    assert(second is String)
    println("print form typeof" + this.toString())
}
"cd ~ && ls".runCommand()
println("echo \$GITHUB_USER_NAME".evalBash().getOrThrow())

About

Gradle Plugin to add utils for build.gradle.kts and settings.gradle.kts

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages