Skip to content

Commit

Permalink
BuildSrcVersions{Task,Plugin} & clean output Splitties#26
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard committed Dec 14, 2018
1 parent 13a5913 commit 55b3f0b
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 159 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ plugins {
}

group = "de.fayard"
version = "0.3.0"
version = "0.3"

gradlePlugin {
plugins {
create("buildSrcVersions") {
id = "de.fayard.buildSrcVersions"
displayName = "buildSrcVersions"
description = "Painless dependencies management"
implementationClass = "de.fayard.GradleKotlinDslLibsPlugin"
implementationClass = "de.fayard.BuildSrcVersionsPlugin"
}
}
}
Expand All @@ -44,7 +44,7 @@ dependencies {

implementation("com.squareup.okio:okio:2.1.0")
implementation( "com.squareup.moshi:moshi:1.7.0")
implementation("com.squareup:kotlinpoet:1.0.0-RC1")
implementation("com.squareup:kotlinpoet:1.0.0")

}

Expand Down
11 changes: 4 additions & 7 deletions doc/Libs.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import kotlin.String

/**
* Generated by [gradle-kotlin-dsl-libs](https://github.com/jmfayard/gradle-kotlin-dsl-libs)
* Generated by https://github.com/jmfayard/buildSrcVersions
*
* Run again
* `$ ./gradlew syncLibs`
* to update this file */
* Update this file with
* `$ ./gradlew buildSrcVersions`
**/
object Libs {
/**
* [gradle website](https://developer.android.com/studio) */
Expand All @@ -28,9 +28,6 @@ object Libs {
const val io_fabric_tools_gradle: String =
"io.fabric.tools:gradle:" + Versions.io_fabric_tools_gradle

const val jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin: String =
"jmfayard.github.io.gradle-kotlin-dsl-libs:jmfayard.github.io.gradle-kotlin-dsl-libs.gradle.plugin:" + Versions.jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin

/**
* [kotlin-scripting-compiler-embeddable website](https://kotlinlang.org/) */
const val kotlin_scripting_compiler_embeddable: String =
Expand Down
109 changes: 109 additions & 0 deletions doc/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
include::doc/includes.adoc[]

:toc: right


= buildSrcVersions

{badge_mit} {badge_version} {badge_issues} {badge_pr} {badge_build}

This Gradle plugin adds a **:buildSrcVersions** task to your build that

- Extract automatically all your Gradle dependencies to {root}/buildSrc/src/main/kotlin/Versions.kt[buildSrc/src/main/kotlin/Versions.kt].
- Inform you about available updates inside {root}/buildSrc/src/main/kotlin/Versions.kt[buildSrc/src/main/kotlin/Versions.kt].






== Extract your dependencies


```bash
$ ./gradlew buildSrcVersions
> Task :dependencyUpdates
> Task :buildSrcVersions
new file: buildSrc/build.gradle.kts
new file: buildSrc/.gitignore
new file: buildSrc/settings.gradle.kts
new file: buildSrc/src/main/kotlin/Libs.kt
new file: buildSrc/src/main/kotlin/Versions.kt
```


== IDE integration

TODO: screencast



```kotlin
dependencies {
implementation(Libs.okio)
implementation(Libs.com_squareup_moshi_moshi)
implementation(Libs.retrofit)
}
```



== Update your dependencies

```bash
$ ./gradlew buildSrcVersions
> Task :dependencyUpdates
> Task :buildSrcVersions
modified: buildSrc/src/main/kotlin/Libs.kt
modified: buildSrc/src/main/kotlin/Versions.kt

```

TODO: screencast

== Prerequisites

- Kotlin, JVM and Android projects.
- Gradle 4.1+ and Gradle 5.0+
- The IDE integration works:
- for both `build.gradle` (Groovy) and `build.gradle.kts` (Kotlin) files.
- in IntelliJ >= 2008.03+ and Android Studio 3.2+.

== Installation

Edit your root `build.gradle(.kts)` file

[source,kotlin,subs=attributes]
----
buildscript {
//...
}
plugins {
id("jmfayard.github.io.gradle-kotlin-dsl-libs") version "{plugin_version}"
}
// Don't put any code before the buildscripts {} and plugins {} block
----


== Changelog

See https://github.com/jmfayard/gradle-kotlin-dsl-libs/blob/master/CHANGELOG.md[CHANGELOG.md]

//== Contributors
//
//include::doc/contributors.adoc[]

== Contributing

- This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details
- Explain your use case and start the discussion before your submit a pull-request
- Run the automated tests with
- Format your code

Please read [CONTRIBUTING.md](https://gist.github.com/PurpleBooth/b24679402957c63ec426) for details on our code of conduct, and the process for submitting pull requests to us.


== Acknowledgments

This project sits on the shoulder of giants:

24 changes: 7 additions & 17 deletions doc/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,32 @@ import kotlin.String

/**
* Find which updates are available by running
* `$ ./gradlew syncLibs`
* `$ ./gradlew buildSrcVersions`
* This will only update the comments.
*
* YOU are responsible for updating manually the dependency version. */
object Versions {
/**
* [gradle website](https://developer.android.com/studio) */
const val com_android_tools_build_gradle = "3.2.1"
/**
* [firebase-database website](https://github.com/b3er/RxFirebase2) */
const val com_github_b3er_rxfirebase_firebase_database = "11.2.0"
/**
* [compiler website](https://github.com/bumptech/glide) */
const val com_github_bumptech_glide_compiler = "4.8.0"
const val com_android_tools_build_gradle = "3.2.1"

const val com_github_b3er_rxfirebase_firebase_database = "11.2.0"

const val com_github_bumptech_glide_compiler = "4.8.0"

const val com_google_firebase_firebase_database = "12.0.0" // available: "16.0.3"

const val io_fabric_tools_gradle: String = "1.26.1"

const val jmfayard_github_io_gradle_kotlin_dsl_libs_gradle_plugin = "0.2.6"

/**
* [kotlin-scripting-compiler-embeddable website](https://kotlinlang.org/) */
const val kotlin_scripting_compiler_embeddable: String = "1.2.71"

const val krangl: String = "0.10.3"

/**
* [okio website](https://github.com/square/okio/) */
const val okio = "2.0.0" // available: "2.1.0"

const val org_jetbrains_kotlin_jvm_gradle_plugin = "1.2.71"

/**
* [rxjava website](https://github.com/ReactiveX/RxJava) */
const val rxjava: String = "2.2.0" // available: "2.2.2"
const val rxjava: String = "2.2.0" // available: "2.2.2"

/**
*
Expand Down
26 changes: 26 additions & 0 deletions doc/includes.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
https://plugins.gradle.org/plugin/de.fayard.buildSrcVersions

:plugin_version: 0.3
:repo: jmfayard/gradle-kotlin-dsl-libs
:github: https://github.com/{repo}
:plugin_issues: https://github.com/gradle/kotlin-dsl/issues?utf8=%E2%9C%93&q=author%3Ajmfayard+
:plugin_gradle_portal: https://plugins.gradle.org/plugin/jmfayard.github.io.gradle-kotlin-dsl-libs
:gradle_kotlin_dsl: https://github.com/gradle/kotlin-dsl
:gradle_guide_new: https://guides.gradle.org/creating-new-gradle-builds
:gradle_guide_buidllogic: https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/
:medium_puzzle: https://blog.kotlin-academy.com/gradle-kotlin-the-missing-piece-of-the-puzzle-7528a85f0d2c
:medium_protips: https://proandroiddev.com/android-studio-pro-tips-for-working-with-gradle-8a7aa61a8cc4
:medium_mvp: https://dev.to/jmfayard/the-minimum-viable-pull-request-5e7p
:devto_readme: https://dev.to/jmfayard/how-to-write-a-good-readme-discuss-4hkl
:root: file:https:///Users/jmfayard/Dev/mautinoa/gradle-kotlin-dsl-libs

// todo
:badge_mit: image:https://img.shields.io/github/license/mashape/apistatus.svg["GitHub",link="{github}/blob/master/LICENSE.txt"]

:badge_version: image:https://img.shields.io/maven-metadata/v/https/plugins.gradle.org/m2/jmfayard/github/io/gradle-kotlin-dsl-libs/maven-metadata.xml.svg?label=gradlePluginPortal["Maven Central",link="https://plugins.gradle.org/plugin/jmfayard.github.io.gradle-kotlin-dsl-libs"]

:badge_issues: image:https://img.shields.io/github/issues/{repo}.svg["GitHub issues",link="{github}/issues"]

:badge_pr: image:https://img.shields.io/github/issues-pr/{repo}.svg["GitHub pull requests",link="{github}/pulls?utf8=%E2%9C%93&q=is%3Apr+"]

:badge_build: image:https://img.shields.io/travis/com/{repo}/documentation.svg["Travis (.org)",link="https://travis-ci.com/{repo}"]
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create

open class GradleKotlinDslLibsPlugin : Plugin<Project> {
open class BuildSrcVersionsPlugin : Plugin<Project> {

override fun apply(project: Project) {

Expand All @@ -29,7 +29,7 @@ open class GradleKotlinDslLibsPlugin : Plugin<Project> {
}


project.tasks.create("syncLibs", SyncLibsTask::class) {
project.tasks.create("buildSrcVersions", BuildSrcVersionsTask::class) {
dependsOn(":dependencyUpdates")
jsonInputPath = benManesVersions.outputDir + "/" + benManesVersions.reportfileName + ".json"
}
Expand Down
98 changes: 98 additions & 0 deletions src/main/kotlin/de/fayard/BuildSrcVersionsTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package de.fayard

import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import okio.buffer
import okio.source
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.tasks.TaskAction
import java.io.File

open class BuildSrcVersionsTask : DefaultTask() {

init {
description = "Update buildSrc/src/main/kotlin/Libs.kt"
group = "build"
}

companion object {
val moshiAdapter: JsonAdapter<DependencyGraph> by lazy {
Moshi.Builder().build().adapter(DependencyGraph::class.java)
}

fun readGraphFromJsonFile(jsonInput: File): DependencyGraph {
return moshiAdapter.fromJson(jsonInput.source().buffer())!!
}

}


var jsonInputPath = "build/dependencyUpdates/report.json"


@TaskAction
fun taskAction() {
val jsonInput = project.file(jsonInputPath)
val outputDir = project.file(OutputFile.OUTPUTDIR.path).also {
if (!it.isDirectory) it.mkdirs()
}

checkIfFilesExistInitially(project)

val initializationMap = mapOf(
OutputFile.BUILD to INITIAL_BUILD_GRADLE_KTS,
OutputFile.GIT_IGNORE to INITIAL_GITIGNORE,
OutputFile.SETTINGS to INITIAL_SETTINGS)

for ((outputFile, initialContent) in initializationMap) {
if (outputFile.existed.not()) {
project.file(outputFile.path).writeText(initialContent)
outputFile.logFileWasModified()
}
}

val dependencyGraph = readGraphFromJsonFile(jsonInput)

val dependencies: List<Dependency> = parseGraph(dependencyGraph)

val kotlinPoetry: KotlinPoetry = kotlinpoet(dependencies, dependencyGraph.gradle)

kotlinPoetry.Libs.writeTo(outputDir)
OutputFile.LIBS.logFileWasModified()

kotlinPoetry.Versions.writeTo(outputDir)
OutputFile.VERSIONS.logFileWasModified()
}

fun checkIfFilesExistInitially(project: Project) {
for (output in OutputFile.values()) {
output.existed = project.file(output.path).exists()
}
}



}

internal enum class OutputFile(val path: String, var existed: Boolean = false) {
OUTPUTDIR("buildSrc/src/main/kotlin"),
BUILD("buildSrc/build.gradle.kts"),
SETTINGS("buildSrc/settings.gradle.kts"),
GIT_IGNORE("buildSrc/.gitignore"),
LIBS("buildSrc/src/main/kotlin/Libs.kt"),
VERSIONS("buildSrc/src/main/kotlin/Versions.kt");

fun logFileWasModified() {
val ANSI_RESET = "\u001B[0m"
val ANSI_GREEN = "\u001B[32m"

val status = if (existed) {
" modified: "
} else {
" new file: "
}
println("$ANSI_GREEN$status$path$ANSI_RESET")
}
}

Loading

0 comments on commit 55b3f0b

Please sign in to comment.